-
-
Notifications
You must be signed in to change notification settings - Fork 429
Directly check type of substr() on RecastingRemovalRector #7446
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,14 @@ | |
| use PhpParser\Node\Expr\Cast\Int_; | ||
| use PhpParser\Node\Expr\Cast\Object_; | ||
| use PhpParser\Node\Expr\Cast\String_; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PHPStan\Reflection\Php\PhpPropertyReflection; | ||
| use PHPStan\Type\ArrayType; | ||
| use PHPStan\Type\BooleanType; | ||
| use PHPStan\Type\Constant\ConstantArrayType; | ||
| use PHPStan\Type\Constant\ConstantBooleanType; | ||
| use PHPStan\Type\FloatType; | ||
| use PHPStan\Type\IntegerType; | ||
| use PHPStan\Type\MixedType; | ||
|
|
@@ -103,6 +105,13 @@ public function refactor(Node $node): ?Node | |
| return null; | ||
| } | ||
|
|
||
| // substr can return false on php 7.x | ||
| if ($node->expr instanceof FuncCall | ||
| && $this->isName($node->expr, 'substr') | ||
| && ! $node->expr->isFirstClassCallable()) { | ||
| $nodeType = new UnionType([new StringType(), new ConstantBooleanType(false)]); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be checked by version in Rector. If on PHP 8.0+, this should be skipped.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The downgrade rule need to exists first then, as without this, the cast string will be removed at and add back the regression bug. After downgrade rule created, then we can safely remove this check.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this first and add PHP version check, so we don't create regressions in existing rule.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need php version check, as phpstan already check it when run on php 7.x, just need add skip config in // on php 7.x, substr() result can return false, so force (string) is needed
RecastingRemovalRector::class => [
__DIR__ . '/rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/ClassLikeNameClassNameImportSkipVoter.php',
],
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| if ($nodeType instanceof ConstantArrayType && $nodeClass === Array_::class) { | ||
| if ($this->shouldSkip($node->expr)) { | ||
| return null; | ||
|
|
||
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.
Instead of changing existing fixture, always make a new one. To make clear former one works.
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.
it is the old fixture behaviour, see previous PR revert:
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.
This is original fixture before the changes train, see PR order changes:
rector.php, and add test for cast removal for(string) substr()for RecastingRemovalRector)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.
Same as before, do not change fixture, but rather add new one. Renaming a fuction that we talk about is confusing to read.
Uh oh!
There was an error while loading. Please reload this page.
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.
@samsonasik Exactly my point. Instead of renaming fixture content back and forth and having to read 3 PRs to understand, they should be always related only to current PR. These PRs should be independent on each other, standalone units.