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

SetList::DEAD_CODE with SetList::SAFE_07 causes PHP Fatal error #6420

Closed
lbausch opened this issue May 15, 2021 · 3 comments · Fixed by rectorphp/rector-src#65
Closed

SetList::DEAD_CODE with SetList::SAFE_07 causes PHP Fatal error #6420

lbausch opened this issue May 15, 2021 · 3 comments · Fixed by rectorphp/rector-src#65
Labels

Comments

@lbausch
Copy link

lbausch commented May 15, 2021

Bug Report

Subject Details
Rector version 0.11.2
Installed as composer dependency

SetList::DEAD_CODE with SetList::SAFE_07 causes PHP Fatal error:

terminal output
$ ./vendor/bin/rector -v --dry-run process fail.php
[parsing] fail.php
[refactoring] fail.php
[post rectors] fail.php
[printing] fail.php
PHP Notice:  Undefined offset: -1 in /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php on line 205
Notice: Undefined offset: -1 in /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php on line 205
PHP Fatal error:  Uncaught TypeError: Return value of PhpParser\Internal\TokenStream::getIndentationBefore() must be of the type int, null returned in /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php:205
Stack trace:
#0 /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php(608): PhpParser\Internal\TokenStream->getIndentationBefore(-1)
#1 /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php(616): PhpParser\PrettyPrinterAbstract->p(Object(PhpParser\Node\Expr\FuncCall), true)
#2 /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php(799): PhpParser\PrettyPrinterAbstract->p(Object(PhpParser\Node\Stmt\Expression), true)
#3 /home/user/project/vendor/rector/rector/src/PhpParser/Printer/BetterStandardPrinter.php(174): PhpParser\PrettyPrinterAbstract->pArray(Array, Array, 0, 0, 'File', 'stmts', NULL)
#4 /home/user in /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php on line 205
Fatal error: Uncaught TypeError: Return value of PhpParser\Internal\TokenStream::getIndentationBefore() must be of the type int, null returned in /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php:205
Stack trace:
#0 /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php(608): PhpParser\Internal\TokenStream->getIndentationBefore(-1)
#1 /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php(616): PhpParser\PrettyPrinterAbstract->p(Object(PhpParser\Node\Expr\FuncCall), true)
#2 /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php(799): PhpParser\PrettyPrinterAbstract->p(Object(PhpParser\Node\Stmt\Expression), true)
#3 /home/user/project/vendor/rector/rector/src/PhpParser/Printer/BetterStandardPrinter.php(174): PhpParser\PrettyPrinterAbstract->pArray(Array, Array, 0, 0, 'File', 'stmts', NULL)
#4 /home/user in /home/user/project/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php on line 205

Minimal PHP Code Causing Issue

https://getrector.org/demo/65c1e5e4-a8e7-4fa5-872d-f6adc0457817

Expected Behaviour

Rector should display a diff which replaces preg_replace() with \Safe\preg_replace()

@lbausch lbausch added the bug label May 15, 2021
@sabbelasichon
Copy link
Contributor

@TomasVotruba I took a look into it yesterday. Seems a tough one. The culprit is the RemoveDeadStmtRector which holds context of the nodes. So if a node has changed during the run, PhpParser fails because nodes are not the same anymore.

@TomasVotruba
Copy link
Member

I see. Could you send a failing test case? I'll see if I can tackle it

@lbausch
Copy link
Author

lbausch commented May 18, 2021

Thank you 👍

TomasVotruba added a commit that referenced this issue Nov 12, 2024
rectorphp/rector-src@bcfb598 Add ClassNameFromObjectTypeResolver to cover TypeWithClassName instance checks (#6420)
carlos-granados added a commit to carlos-granados/rector that referenced this issue Nov 13, 2024
carlos-granados/rector-src@f7ea3db [PHP8.3] add new rectors for get_class()/get_parent_class() without arguments (rectorphp#6405)
carlos-granados/rector-src@1afc647 Rectify (rectorphp#6408)
carlos-granados/rector-src@afe7c46 [DeadCode] Skip nullable @ template on RemoveUselessReturnTagRector (rectorphp#6409)
carlos-granados/rector-src@293eb97 [Php81] Skip Doctrine Embeddable on ReadOnlyPropertyRector (rectorphp#6411)
carlos-granados/rector-src@1b1807b [Privatization] Skip with parameter on PrivatizeLocalGetterToPropertyRector (rectorphp#6412)
carlos-granados/rector-src@2c67908 Fix ClassDependencyManipulator to add dependency on right position (rectorphp#6413)
carlos-granados/rector-src@29e8932 [automated] Apply Coding Standard (rectorphp#6414)
carlos-granados/rector-src@3949fc2 Move instanceof PHPStan Type to ->is*() take 1 (rectorphp#6416)
carlos-granados/rector-src@0cac71a [CodeQuality] Add fixture test for skip nullable array from property with default value (rectorphp#6417)
carlos-granados/rector-src@d2db35e [Php80] Returns null on no change on ClassPropertyAssignToConstructorPromotionRector (rectorphp#6418)
carlos-granados/rector-src@06adce3 use ->isInteger()->yes() on ReturnTypeFromStrictNewArrayRector (rectorphp#6419)
carlos-granados/rector-src@bcfb598 Add ClassNameFromObjectTypeResolver to cover TypeWithClassName instance checks (rectorphp#6420)
carlos-granados/rector-src@84bb596 [StaticTypeMapper] Reduce instanceof TypeWithClassName usage via ClassNameFromObjectTypeResolver (rectorphp#6421)
carlos-granados/rector-src@26b8b54 [Naming] Reduce instanceof ObjectType on ExpectedNameResolver (take 5) (rectorphp#6423)
carlos-granados/rector-src@5baf487 Require-dev phpstan/phpstan-deprecation-rules to show list of deprecated (rectorphp#6415)
carlos-granados/rector-src@c23ba82 [deprecation] Remove deprecated PublicConstantVisibilityRector, cleanup deprecated SetListInterface (rectorphp#6424)
carlos-granados/rector-src@c2053c7 [deprecation] Deprecate AbstractScopeAwareRector in favor of single AbstractRector (rectorphp#6425)
carlos-granados/rector-src@2c5cd97 [deprecated] Remove deprecated AbstractTestCase (rectorphp#6426)
carlos-granados/rector-src@fb3c33a Merge commit '2c5cd97a3a98ab5af9efb846f6f78738e4a195a6'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants