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

RemoveUnusedNonEmptyArrayBeforeForeachRector: skip array dim fetch #5166

Merged
merged 6 commits into from
Oct 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture;

class KnownOffset
{
public const DEFAULT_GROUP = 'default';

/**
* @var array<string, list<self>>
*/
private static $groups = [];

public static function knownOffset(): array
{
$group = 'default';

self::$groups[$group] = ["foo"];

if (! empty(self::$groups[$group])) {
foreach (self::$groups[$group] as $group) {
echo "hello";
}
}

return [];
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture;

class KnownOffset
{
public const DEFAULT_GROUP = 'default';

/**
* @var array<string, list<self>>
*/
private static $groups = [];

public static function knownOffset(): array
{
$group = 'default';

self::$groups[$group] = ["foo"];

foreach (self::$groups[$group] as $group) {
echo "hello";
}

return [];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture;

class SkipPhpdocOffset
{
/**
* @param array{default: foo} $array
*/
public static function knownOffset($array): array
{
$group = 'default';

if (! empty($array[$group])) {
foreach ($array[$group] as $groupValue) {
echo "hello";
}
}

return [];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture;

class SkipPropertyDimFetch
{
public const DEFAULT_GROUP = 'default';

/**
* @var array<string, list<self>>
*/
private static $groups = [];

public static function getFootnotes($group = self::DEFAULT_GROUP): array
{
if (! empty(self::$groups[$group])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we already skip "regular" dim fetch, I think it makes sense to also skip it for this example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to to check native ArrayDimFetch type for this case?

if ($foreachExpr instanceof Expr\ArrayDimFetch) {
   $nativeType = $this->nodeTypeResolver-getNativeType(...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a test case in mind which fails atm?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like assign in prev stmt:

self::$groups[$group] = ["foo"];

if (! empty(self::$groups[$group]))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed with 0edaf2d

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you fix phpstan notice? thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch :-). fixed.

foreach (self::$groups[$group] as $note) {
echo "hello";
}
}

return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,8 @@ private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool
$foreach = $if->stmts[0];
$foreachExpr = $foreach->expr;

if ($foreachExpr instanceof Variable) {
$variableName = $this->nodeNameResolver->getName($foreachExpr);
if (is_string($variableName) && $this->reservedKeywordAnalyzer->isNativeVariable($variableName)) {
return false;
}

$ifType = $scope->getNativeType($foreachExpr);
if (!$ifType->isArray()->yes()) {
return false;
}
if ($this->shouldSkipForeachExpr($foreachExpr, $scope)) {
return false;
}

$ifCond = $if->cond;
Expand Down Expand Up @@ -208,4 +200,29 @@ private function refactorIf(If_ $if, Scope $scope): ?Foreach_

return $stmt;
}

private function shouldSkipForeachExpr(Expr $foreachExpr, Scope $scope): bool
{
if ($foreachExpr instanceof Expr\ArrayDimFetch && $foreachExpr->dim !== null) {
$exprType = $this->nodeTypeResolver->getNativeType($foreachExpr->var);
$dimType = $this->nodeTypeResolver->getNativeType($foreachExpr->dim);
if (!$exprType->hasOffsetValueType($dimType)->yes()) {
return true;
}
}

if ($foreachExpr instanceof Variable) {
$variableName = $this->nodeNameResolver->getName($foreachExpr);
if (is_string($variableName) && $this->reservedKeywordAnalyzer->isNativeVariable($variableName)) {
return true;
}

$ifType = $scope->getNativeType($foreachExpr);
if (!$ifType->isArray()->yes()) {
return true;
}
}

return false;
}
}