-
-
Notifications
You must be signed in to change notification settings - Fork 338
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
87963a4
RemoveUnusedNonEmptyArrayBeforeForeachRector: skip array dim fetch
staabm 0edaf2d
fix with known offset
staabm 8ce4f23
test phpdoc array shape
staabm 9d58fc5
Merge branch 'main' into dim-fetch
staabm af07835
refactor
staabm 79af99b
simplify test
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
.../If_/RemoveUnusedNonEmptyArrayBeforeForeachRector/Fixture/offset_exists_dim_fetch.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} | ||
|
||
?> |
24 changes: 24 additions & 0 deletions
24
...RemoveUnusedNonEmptyArrayBeforeForeachRector/Fixture/skip_phpdoc_offset_dim_fetch.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} | ||
|
||
?> |
24 changes: 24 additions & 0 deletions
24
.../If_/RemoveUnusedNonEmptyArrayBeforeForeachRector/Fixture/skip_property_dim_fetch.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) { | ||
foreach (self::$groups[$group] as $note) { | ||
echo "hello"; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed with 0edaf2d
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch :-). fixed.