Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ForeachItemsAssignToEmptyArrayToAssignRector] apply on code it should not #2587

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ private function matchAssignItemsOnlyForeachArrayVariable(Foreach_ $foreach): ?E
return null;
}

if ($onlyStatement->var->dim !== null) {
return null;
}

if (! $this->areNodesEqual($foreach->valueVar, $onlyStatement->expr)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

class SkipNonEmptyDimKey1
{
public function indexBy(array $array, callable $key): array
{
$indexed = [];
foreach ($array as $item) {
$indexed[$key($item)] = $item;
}

return $indexed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

class SkipNonEmptyDimKey2
{
public function mapWithKeys(array $array, callable $fn): array
{
$result = [];
foreach ($array as $key => $value) {
foreach (self::assertIterable($fn($value, $key)) as $mapKey => $mapValue) {
$result[$mapKey] = $mapValue;
}
}

return $result;
}

public static function assertIterable($traversable)
{
if (is_iterable($traversable)) {
return $traversable;
}

throw new \UnexpectedValueException('Traversable expected');
}
}