Skip to content

Commit

Permalink
[CodeQuality] Skip Generator on SimplifyForeachToCoalescingRector (#…
Browse files Browse the repository at this point in the history
…6221)

* [CodeQuality] Skip Generator on SimplifyForeachToCoalescingRector

* [CodeQuality] Skip Generator on SimplifyForeachToCoalescingRector

* ensure array type

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Aug 6, 2024
1 parent 6a4d717 commit 69a3e31
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

function simplifyForeachToCoalescing2()
function simplifyForeachToCoalescing2($currentFunction)
{
foreach ($this->oldToNewFunctions as $oldFunction => $newFunction) {
$oldToNewFunctions = ['a' => 'b'];
foreach ($oldToNewFunctions as $oldFunction => $newFunction) {
if ($oldFunction === $currentFunction) {
return $newFunction;
}
Expand All @@ -15,9 +16,10 @@ function simplifyForeachToCoalescing2()
-----
<?php

function simplifyForeachToCoalescing2()
function simplifyForeachToCoalescing2($currentFunction)
{
return $this->oldToNewFunctions[$currentFunction] ?? 45;
$oldToNewFunctions = ['a' => 'b'];
return $oldToNewFunctions[$currentFunction] ?? 45;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ForeachKeyValue
/**
* @var mixed[]
*/
private $oldToNewOption = [];
private array $oldToNewOption = [];

public function run()
{
Expand All @@ -33,7 +33,7 @@ class ForeachKeyValue
/**
* @var mixed[]
*/
private $oldToNewOption = [];
private array $oldToNewOption = [];

public function run()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector\Fixture;

final class SkipGenerator
{
public function load(): \Generator
{
foreach (['a' => 1, 'b' => 2] as $key => $value) {
yield $key => $value;
}
}

public function loadByKey(string $name): ?int
{
foreach ($this->load() as $key => $value) {
if ($key === $name) {
return $value;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ private function matchForeachReturnOrAssign(Foreach_ $foreach): Expression|Retur
return null;
}

$foreachExprType = $this->nodeTypeResolver->getNativeType($foreach->expr);
if (! $foreachExprType->isArray()->yes()) {
return null;
}

$innerStmt = $if->stmts[0];
if ($innerStmt instanceof Return_) {
return $innerStmt;
Expand Down

0 comments on commit 69a3e31

Please sign in to comment.