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

Process error in just one file of 756 files: Name cannot be empty while analysing file /path/to/file/file.php #10867

Open
gabebruno opened this issue Apr 11, 2024 · 5 comments · May be fixed by phpstan/phpstan-src#3013

Comments

@gabebruno
Copy link

Bug report

My application has 756 files, but only one of them is generating this error, excluding the file from analysis, PHPStan runs normally.

Follow the error and image:

 -- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     Error
 -- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     Internal error: Internal error: Name cannot be empty while analysing file /usr/local/apache2/htdocs/search/file.php

     Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml:
     ## phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/nikic/php-parser/lib/PhpParser/Node/Name.php(220)
     #0 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/nikic/php-parser/lib/PhpParser/Node/Name.php(24):
     PhpParser\Node\Name::prepareName('')
     #1 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Type/Php/ConstantHelper.php(27): PhpParser\Node\Name->__construct('')
     #2 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Type/Php/ConstantFunctionReturnTypeExtension.php(35):
     PHPStan\Type\Php\ConstantHelper->createExprFromConstantName('')
     #3 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1566):
     PHPStan\Type\Php\ConstantFunctionReturnTypeExtension->getTypeFromFunctionCall(Object(PHPStan\Reflection\Native\NativeFunctionReflection), Object(PhpParser\Node\Expr\FuncCall),
     Object(PHPStan\Analyser\MutatingScope))
     #4 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(588):
     PHPStan\Analyser\MutatingScope->resolveType('\\constant($sysE...', Object(PhpParser\Node\Expr\FuncCall))
     #5 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Rules/RuleLevelHelper.php(223):
     PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\FuncCall))
     #6 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Cast/EchoRule.php(38):
     PHPStan\Rules\RuleLevelHelper->findTypeToCheck(Object(PHPStan\Analyser\MutatingScope), Object(PhpParser\Node\Expr\FuncCall), '', Object(Closure))
     #7 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(107):
     PHPStan\Rules\Cast\EchoRule->processNode(Object(PhpParser\Node\Stmt\Echo_), Object(PHPStan\Analyser\MutatingScope))
     #8 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(477):
     PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}(Object(PhpParser\Node\Stmt\Echo_), Object(PHPStan\Analyser\MutatingScope))
     #9 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(420):
     PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Echo_), Object(PHPStan\Analyser\MutatingScope), Object(Closure),
     Object(PHPStan\Analyser\StatementContext))
     #10 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(772):
     PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Else_), Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure),
     Object(PHPStan\Analyser\StatementContext))
     #11 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(389):
     PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\If_), Object(PHPStan\Analyser\MutatingScope), Object(Closure),
     Object(PHPStan\Analyser\StatementContext))
     #12 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(166):
     PHPStan\Analyser\NodeScopeResolver->processNodes(Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
     #13 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(132):
     PHPStan\Analyser\FileAnalyser->analyseFile('/usr/local/apac...', Array, Object(PHPStan\Rules\LazyRegistry), Object(PHPStan\Collectors\Registry), NULL)
     #14 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97):
     PHPStan\Command\WorkerCommand->PHPStan\Command\{closure}(Array)
     #15 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/clue/ndjson-react/src/Decoder.php(117):
     _PHPStan_6b522806f\Evenement\EventEmitter->emit('data', Array)
     #16 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97):
     _PHPStan_6b522806f\Clue\React\NDJson\Decoder->handleData(Array)
     #17 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Util.php(62):
     _PHPStan_6b522806f\Evenement\EventEmitter->emit('data', Array)
     #18 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97):
     _PHPStan_6b522806f\React\Stream\Util::_PHPStan_6b522806f\React\Stream\{closure}('{"action":"anal...')
     #19 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/DuplexResourceStream.php(154):
     _PHPStan_6b522806f\Evenement\EventEmitter->emit('data', Array)
     #20 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(201):
     _PHPStan_6b522806f\React\Stream\DuplexResourceStream->handleData(Resource id #8009)
     #21 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(173):
     _PHPStan_6b522806f\React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)
     #22 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(98):
     _PHPStan_6b522806f\React\EventLoop\StreamSelectLoop->run()
     #23 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(259):
     PHPStan\Command\WorkerCommand->execute(Object(_PHPStan_6b522806f\Symfony\Component\Console\Input\ArgvInput),
     Object(_PHPStan_6b522806f\Symfony\Component\Console\Output\ConsoleOutput))
     #24 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(870):
     _PHPStan_6b522806f\Symfony\Component\Console\Command\Command->run(Object(_PHPStan_6b522806f\Symfony\Component\Console\Input\ArgvInput),
     Object(_PHPStan_6b522806f\Symfony\Component\Console\Output\ConsoleOutput))
     #25 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(261):
     _PHPStan_6b522806f\Symfony\Component\Console\Application->doRunCommand(Object(PHPStan\Command\WorkerCommand),
     Object(_PHPStan_6b522806f\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_6b522806f\Symfony\Component\Console\Output\ConsoleOutput))
     #26 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(157):
     _PHPStan_6b522806f\Symfony\Component\Console\Application->doRun(Object(_PHPStan_6b522806f\Symfony\Component\Console\Input\ArgvInput),
     Object(_PHPStan_6b522806f\Symfony\Component\Console\Output\ConsoleOutput))
     #27 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(124): _PHPStan_6b522806f\Symfony\Component\Console\Application->run()
     #28 phar:///usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(125): _PHPStan_6b522806f\{closure}()
     #29 /usr/local/apache2/htdocs/.../lib/vendor/phpstan/phpstan/phpstan(8): require('phar:///usr/loc...')
     #30 /usr/local/apache2/htdocs/.../lib/vendor/bin/phpstan(119): include('/usr/local/apac...')
     #31 {main}
     Child process error (exit code 1):

The image is running just the file with PHPStan!

image

Code snippet that reproduces the problem

No response

Expected output

The file should be analyzed and errors printed on the terminal!

Did PHPStan help you today? Did it make you happy in any way?

No response

Copy link

mergeable bot commented Apr 11, 2024

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Please reproduce the issue with a minimal code snippet.

@gabebruno
Copy link
Author

gabebruno commented Apr 12, 2024

My file has PHP and HTML in the same file, trying to reduce the code and testing, I managed to get to this minimum piece of code to analyze.

https://phpstan.org/r/1b2b6ea3-65d6-4ba1-8a6e-bf82a013b137

Apparently the problem is in the constant() method, because removing the method, the file will run succesfully.

Edit: Doing some tests here, I found another information about the problem, the method only causes problems if used with a variable.

Edit2: another file having the constant($variable) method used don't show any errors, it's just with this specific code snippet.

@ondrejmirtes
Copy link
Member

So there's a case where your code runs with constant('')? What's the purpose?

@gabebruno
Copy link
Author

gabebruno commented Apr 12, 2024

It's not exactly on purpose, it's legacy code with a lot of old and incorrect implementations, I figured PHPStan would mention the problem to fix it, but this is breaking PHPStan itself.
If it's not a valid issue, that's fine, but I think it will be a good source of answers for others facing the same problem.

The code was much larger, this is just a small part of the code to reproduce the error. This $sysERR variable can be changed in the middle of the code, so it is not exactly empty, the empty is just the first initialization of the variable and even then PHPStan is breaking.

@staabm staabm linked a pull request Apr 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants