vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.54k stars 659 forks source link

Uncaught Psalm\Exception\IncorrectDocblockException: Misplaced variable #9809

Open crtl opened 1 year ago

crtl commented 1 year ago

After installing psalm and trying to init it throws an IncorrectDocblocException:

user@6de99abff372:/var/www/html$ ./vendor/bin/psalm --init
Calculating best config level based on project files
Warning: "findUnusedCode" will be defaulted to "true" in Psalm 6. You should explicitly enable or disable this setting.
Calculating best config level based on project files
Warning: "findUnusedCode" will be defaulted to "true" in Psalm 6. You should explicitly enable or disable this setting.
Target PHP version: 8.1 (inferred from composer.json) Enabled extensions: simplexml (unsupported extensions: fileinfo, pcre).
Scanning files...
Uncaught Psalm\Exception\IncorrectDocblockException: Misplaced variable in /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/CommentAnalyzer.php:129
Stack trace:
#0 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/CommentAnalyzer.php(64): Psalm\Internal\Analyzer\CommentAnalyzer::arrayToDocblocks(Object(PhpParser\Comment\Doc), Object(Psalm\Internal\Scanner\ParsedDocblock), Object(Psalm\Internal\Scanner\FileScanner), Object(Psalm\Aliases), Array, Array)
#1 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php(605): Psalm\Internal\Analyzer\CommentAnalyzer::getTypeFromComment(Object(PhpParser\Comment\Doc), Object(Psalm\Internal\Scanner\FileScanner), Object(Psalm\Aliases), Array, Array)
#2 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php(208): Psalm\Internal\PhpVisitor\Reflector\FunctionLikeNodeScanner->start(Object(PhpParser\Node\Stmt\ClassMethod))
#3 /var/www/html/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(200): Psalm\Internal\PhpVisitor\ReflectorVisitor->enterNode(Object(PhpParser\Node\Stmt\ClassMethod))
#4 /var/www/html/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(114): PhpParser\NodeTraverser->traverseArray(Array)
#5 /var/www/html/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(223): PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Stmt\Class_))
#6 /var/www/html/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(114): PhpParser\NodeTraverser->traverseArray(Array)
#7 /var/www/html/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(223): PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Stmt\Namespace_))
#8 /var/www/html/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(91): PhpParser\NodeTraverser->traverseArray(Array)
#9 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Scanner/FileScanner.php(79): PhpParser\NodeTraverser->traverse(Array)
#10 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Codebase/Scanner.php(558): Psalm\Internal\Scanner\FileScanner->scan(Object(Psalm\Codebase), Object(Psalm\Storage\FileStorage), false, Object(Psalm\Progress\DefaultProgress))
#11 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Codebase/Scanner.php(785): Psalm\Internal\Codebase\Scanner->scanFile('/var/www/html/s...', Array, true)
#12 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Codebase/Scanner.php(428): Psalm\Internal\Codebase\Scanner->scanAPath(534, '/var/www/html/s...')
#13 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Codebase/Scanner.php(280): Psalm\Internal\Codebase\Scanner->scanFilePaths(1)
#14 /var/www/html/vendor/vimeo/psalm/src/Psalm/Codebase.php(505): Psalm\Internal\Codebase\Scanner->scanFiles(Object(Psalm\Internal\Codebase\ClassLikes), 1)
#15 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php(518): Psalm\Codebase->scanFiles(1)
#16 /var/www/html/vendor/vimeo/psalm/src/Psalm/Internal/Cli/Psalm.php(370): Psalm\Internal\Analyzer\ProjectAnalyzer->check('/var/www/html/', true)
#17 /var/www/html/vendor/vimeo/psalm/psalm(9): Psalm\Internal\Cli\Psalm::run(Array)
#18 /var/www/html/vendor/bin/psalm(120): include('/var/www/html/v...')
#19 {main}
(Psalm 5.12.0@f90118cdeacd0088e7215e64c0c99ceca819e176 crashed due to an uncaught Throwable)
psalm-github-bot[bot] commented 1 year ago

Hey @crtl, can you reproduce the issue on https://psalm.dev ?

ygottschalk commented 1 year ago

In the stacktrace # 11 / 12 gives the file in which you have an incorrect/maleformed docblock comment (seems like it is a class method docblock). Fix that and the issue should go away. If not, provide us with the code in question and we'll try to help you from there.

crtl commented 1 year ago

@ygottschalk Thanks for the reply. I have never used Psalm before. Shouldnt it show me which docblock is incorrect?

Anyways ive resolved the issue by running init with the --debug flag which shows what file is causing the issue. The docbloc in question had the following format:

public function __construct(
/**
 * @var $varName Type[]
 */
 protected array $varName,
) {}

but should be

public function __construct(
/**
 * @var Type[]
 */
 protected array $varName,
) {}
orklah commented 1 year ago

Psalm should catch those Exceptions, if nothing more to say "Run with --debug-by-line" to know where Psalm failed and create an issue if needed on github" or something like that

ygottschalk commented 1 year ago

@ygottschalk Thanks for the reply. I have never used Psalm before. Shouldnt it show me which docblock is incorrect?

Yes it should and in "usual / normal" mode it does (see: https://psalm.dev/r/88965cbfc5). Seems to be an issue with the --init flag / mode.

The syntax used for the @var annotation used by psalm is the other way around like you used it (see example above) and is the "standard" phpdoc syntax (see also: https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc-tags.md#518-var)

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/88965cbfc5 ```php