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

System error: "leaveNode() returned invalid value of type integer" #8436

Closed
stein197 opened this issue Jan 29, 2024 · 3 comments
Closed

System error: "leaveNode() returned invalid value of type integer" #8436

stein197 opened this issue Jan 29, 2024 · 3 comments
Labels

Comments

@stein197
Copy link

Bug Report

Subject Details
Rector version 1608ae8c9497bbfaf78ce54130ec304963ac6f2a
PHP version 8.2.11 TS
System Windows 11 x64

Cannot remove includes and requires from files. I wrote a rule that removes any includes and requires. When I run, I get the next error:

#0
C:\Projects\trunk\vendor\rector\rector\vendor\nikic\php-parser\lib\PhpParser\NodeTraverser.php(196):
PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Stmt\Expression))
#1
C:\Projects\trunk\vendor\rector\rector\vendor\nikic\php-parser\lib\PhpParser\NodeTraverser.php(105):
PhpParser\NodeTraverser->traverseArray(Array)
#2
C:\Projects\trunk\vendor\rector\rector\vendor\nikic\php-parser\lib\PhpParser\NodeTraverser.php(196):
PhpParser\NodeTraverser->traverseNode(Object(Rector\PhpParser\Node\CustomNode\FileWithoutNamespace))
#3
C:\Projects\trunk\vendor\rector\rector\vendor\nikic\php-parser\lib\PhpParser\NodeTraverser.php(85):
PhpParser\NodeTraverser->traverseArray(Array)
#4
C:\Projects\trunk\vendor\rector\rector\src\PhpParser\NodeTraverser\RectorNodeTraverser.php(41):
PhpParser\NodeTraverser->traverse(Array)
#5
C:\Projects\trunk\vendor\rector\rector\src\Application\FileProcessor.php(105):
Rector\PhpParser\NodeTraverser\RectorNodeTraverser->traverse(Array)
#6
C:\Projects\trunk\vendor\rector\rector\src\Application\ApplicationFileProcessor.php(176):
Rector\Application\FileProcessor->processFile(Object(Rector\ValueObject\Application\File), Object(Rector\ValueObject\Configuration))
#7
C:\Projects\trunk\vendor\rector\rector\src\Application\ApplicationFileProcessor.php(153):
Rector\Application\ApplicationFileProcessor->processFile(Object(Rector\ValueObject\Application\File), Object(Rector\ValueObject\Configuration))
#8
C:\Projects\trunk\vendor\rector\rector\src\Application\ApplicationFileProcessor.php(128):
Rector\Application\ApplicationFileProcessor->processFiles(Array, Object(Rector\ValueObject\Configuration), Object(Closure), Object(Closure))
#9
C:\Projects\trunk\vendor\rector\rector\src\Console\Command\ProcessCommand.php(117):
Rector\Application\ApplicationFileProcessor->run(Object(Rector\ValueObject\Configuration), Object(RectorPrefix202401\Symfony\Component\Console\Input\ArgvInput))
#10
C:\Projects\trunk\vendor\rector\rector\vendor\symfony\console\Command\Command.php(327):
Rector\Console\Command\ProcessCommand->execute(Object(RectorPrefix202401\Symfony\Component\Console\Input\ArgvInput), Object(RectorPrefix202401\Symfony\Component\Console\Output\ConsoleOutput))
#11
C:\Projects\trunk\vendor\rector\rector\vendor\symfony\console\Application.php(960):
RectorPrefix202401\Symfony\Component\Console\Command\Command->run(Object(RectorPrefix202401\Symfony\Component\Console\Input\ArgvInput), Object(RectorPrefix202401\Symfony\Component\Console\Output\ConsoleOutput))
#12
C:\Projects\trunk\vendor\rector\rector\vendor\symfony\console\Application.php(333):
RectorPrefix202401\Symfony\Component\Console\Application->doRunCommand(Object(Rector\Console\Command\ProcessCommand), Object(RectorPrefix202401\Symfony\Component\Console\Input\ArgvInput), Object(RectorPrefix202401\Symfony\Component\Console\Output\ConsoleOutput))
#13
C:\Projects\trunk\vendor\rector\rector\src\Console\ConsoleApplication.php(53):
RectorPrefix202401\Symfony\Component\Console\Application->doRun(Object(RectorPrefix202401\Symfony\Component\Console\Input\ArgvInput), Object(RectorPrefix202401\Symfony\Component\Console\Output\ConsoleOutput))
#14
C:\Projects\trunk\vendor\rector\rector\vendor\symfony\console\Application.php(216):
Rector\Console\ConsoleApplication->doRun(Object(RectorPrefix202401\Symfony\Component\Console\Input\ArgvInput), Object(RectorPrefix202401\Symfony\Component\Console\Output\ConsoleOutput))
#15 C:\Projects\trunk\vendor\rector\rector\bin\rector.php(130):
RectorPrefix202401\Symfony\Component\Console\Application->run()                  #16 C:\Projects\trunk\vendor\rector\rector\bin\rector(5):
require_once('C:\\Projects\\tru...')                                             #17 C:\Projects\trunk\vendor\bin\rector(119):
include('C:\\Projects\\tru...')                                                  #18 {main}". On line: 150

There are next issues already:

But they don't seem to help. I've tried to make workarounds like overriding supermethods to access parent nodes

Minimal PHP Code Causing Issue

https://getrector.com/demo/d14e8e14-3157-43da-8d9c-c3aa3c9ee4e8

Expected Behaviour

Rector should remove node when NodeTraverser::REMOVE_NODE is returned from the refactor(Node $node) method

@stein197 stein197 added the bug label Jan 29, 2024
@samsonasik
Copy link
Member

samsonasik commented Jan 29, 2024

That's expected, you can't use NodeTraverser::REMOVE_NODE directly under refactor() on Expr, you need to remove the whole Expression stmt, something like:

	public function getNodeTypes(): array {
		return [
			Node\Stmt\Expression::class
		];
	}

	public function refactor(Node $node) {
            if ($node->expr instanceof Node\Expr\ErrorSuppress && $node->expr->expr instanceof Include_) {
    		return NodeTraverser::REMOVE_NODE;
            }

            if ($node->expr instanceof Include_) {
    		return NodeTraverser::REMOVE_NODE;
            }
        
            return null;
	}

that already answered at #8172

@stein197
Copy link
Author

Thank you! Is it defined somewhere in the documentation? I cannot find this

@TomasVotruba
Copy link
Member

It's inherited treat of AST tree, so I'd look for it somewhere in:
https://github.com/nikic/PHP-Parser/tree/4.x/doc

Still, we might add a "AST Gotchas" page to our docs, as this is something hard to find.
Where would you expect this to find?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants