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

NodeAnnotationReader::readPropertyAnnotation crashes on trait property with annotation #6023

Closed
jnugh opened this issue Mar 31, 2021 · 4 comments
Labels

Comments

@jnugh
Copy link

jnugh commented Mar 31, 2021

Bug Report

Subject Details
Rector version 0.10.3/dev-main
Installed as composer dependency

This looked similar to #5980 so I tested dev-main too just to make sure it has not been fixed yet.

I have a file containing a trait that has a property which has an annotation. (@ORM\Column imported trough use Doctrine\ORM\Mapping as ORM;). If I run rector on that file I get the below output (I used SetList::DEAD_CODE only).
This does not seem to happen if I don't alias the namespace in my file (use Doctrine\ORM\Mapping; and @Mapping\Column).

vendor/bin/rector process test.php -vvv     
[parsing] test.php
[refactoring] test.php
    [applying] Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector
    [applying] Rector\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector

In NodeAnnotationReader.php line 101:
                                                                                                                   
  [Rector\Core\Exception\ShouldNotHappenException]                                                                 
  Look at "Rector\BetterPhpDocParser\AnnotationReader\NodeAnnotationReader::readPropertyAnnotation()" on line 101  
                                                                                                                   

Exception trace:
  at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/AnnotationReader/NodeAnnotationReader.php:101
 Rector\BetterPhpDocParser\AnnotationReader\NodeAnnotationReader->readPropertyAnnotation() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/AnnotationReader/NodeAnnotationReader.php:66
 Rector\BetterPhpDocParser\AnnotationReader\NodeAnnotationReader->readAnnotation() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector-doctrine/src/PhpDoc/NodeFactory/MultiPhpDocNodeFactory.php:118
 Rector\Doctrine\PhpDoc\NodeFactory\MultiPhpDocNodeFactory->createFromNodeAndTokens() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php:173
 Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser->parseTagValue() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php:150
 Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser->parseTag() at /Users/jonas/Documents/Dev/rector-bug/vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php:67
 PHPStan\PhpDocParser\Parser\PhpDocParser->parseChild() at n/a:n/a
 ReflectionMethod->invokeArgs() at /Users/jonas/Documents/Dev/rector-bug/vendor/symplify/package-builder/src/Reflection/PrivatesCaller.php:32
 Symplify\PackageBuilder\Reflection\PrivatesCaller->callPrivateMethod() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php:211
 Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser->parseChildAndStoreItsPositions() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php:119
 Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser->parse() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php:156
 Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory->parseTokensToPhpDocNode() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php:126
 Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory->createFromNode() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php:93
 Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory->createFromNodeOrEmpty() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Exclusion/ExclusionManager.php:61
 Rector\Core\Exclusion\ExclusionManager->hasNoRectorPhpDocTagMatch() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Exclusion/ExclusionManager.php:44
 Rector\Core\Exclusion\ExclusionManager->isNodeSkippedByRector() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Rector/AbstractRector.php:519
 Rector\Core\Rector\AbstractRector->shouldSkipCurrentNode() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Rector/AbstractRector.php:291
 Rector\Core\Rector\AbstractRector->enterNode() at /Users/jonas/Documents/Dev/rector-bug/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:200
 PhpParser\NodeTraverser->traverseArray() at /Users/jonas/Documents/Dev/rector-bug/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
 PhpParser\NodeTraverser->traverseNode() at /Users/jonas/Documents/Dev/rector-bug/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
 PhpParser\NodeTraverser->traverseArray() at /Users/jonas/Documents/Dev/rector-bug/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
 PhpParser\NodeTraverser->traverseNode() at /Users/jonas/Documents/Dev/rector-bug/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
 PhpParser\NodeTraverser->traverseArray() at /Users/jonas/Documents/Dev/rector-bug/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:91
 PhpParser\NodeTraverser->traverse() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/PhpParser/NodeTraverser/RectorNodeTraverser.php:92
 Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser->traverse() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Application/FileProcessor.php:134
 Rector\Core\Application\FileProcessor->refactor() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Application/RectorApplication.php:238
 Rector\Core\Application\RectorApplication->Rector\Core\Application\{closure}() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Application/RectorApplication.php:253
 Rector\Core\Application\RectorApplication->tryCatchWrapper() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Application/RectorApplication.php:239
 Rector\Core\Application\RectorApplication->refactorNodesWithRectors() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Application/RectorApplication.php:158
 Rector\Core\Application\RectorApplication->runOnPaths() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Console/Command/ProcessCommand.php:216
 Rector\Core\Console\Command\ProcessCommand->execute() at /Users/jonas/Documents/Dev/rector-bug/vendor/symfony/console/Command/Command.php:256
 Symfony\Component\Console\Command\Command->run() at /Users/jonas/Documents/Dev/rector-bug/vendor/symfony/console/Application.php:971
 Symfony\Component\Console\Application->doRunCommand() at /Users/jonas/Documents/Dev/rector-bug/vendor/symfony/console/Application.php:290
 Symfony\Component\Console\Application->doRun() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/src/Console/ConsoleApplication.php:81
 Rector\Core\Console\ConsoleApplication->doRun() at /Users/jonas/Documents/Dev/rector-bug/vendor/symfony/console/Application.php:166
 Symfony\Component\Console\Application->run() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/bin/rector.php:61
 require_once() at /Users/jonas/Documents/Dev/rector-bug/vendor/rector/rector/bin/rector:4

Minimal PHP Code Causing Issue

I was not able to do this on https://getrector.org/demo/ not sure if it does not display such errors? I created a little repo:
https://github.com/jnugh/php-rector-bug-example

But the code is actually quite short:

<?php

use Doctrine\ORM\Mapping as ORM;

trait Sortable
{
    /**
     * @ORM\Column(type="integer")
     */
    private $sort = 0;
}

Expected Behaviour

It should not crash

@jnugh jnugh added the bug label Mar 31, 2021
@hyptos
Copy link

hyptos commented Apr 2, 2021

👍
I have the same issue.

@samsonasik
Copy link
Member

samsonasik commented Apr 5, 2021

@jnugh I cloned your repo and try follow the step to reproduce and it seems working fine now with latest dev-main:

Screen Shot 2021-04-05 at 22 56 05

please composer update with try latest:

"rector/rector": "dev-main"

@TomasVotruba
Copy link
Member

The NodeAnnotationReader was removed in #5974 and replaced with static annotation parser. It should solve this and similar issues and help with analysis of old code.

@samsonasik Thanks for confirmation 👍

@jnugh
Copy link
Author

jnugh commented Apr 5, 2021

I can confirm it's working with the current dev-main version. perfect and thanks for this very quick response / fix 😄 👍🏽

TomasVotruba added a commit that referenced this issue Jun 24, 2024
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

4 participants