Use of alias cause a fatal error
Bug Report
| Subject | Details |
|---|---|
| Rector version | v0.7.41 |
| Installed as | composer dependency |
The parsing process does not seem to take in consideration the use of alias.
Minimal PHP Code Causing Issue
namespace Sample\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Sample class to illustrate the use of alias for annotations.
*
* @ORM\Entity
* @ORM\Table
*/
class SampleEntity
{
}
=> ERROR] Could not process "/src/Sample/Entity/SampleEntity.php" file, due to: "[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class Sample\Entity\SampleEntity was never imported. Did you maybe forget to add a "use" statement for this annotation?".
Expected Behaviour
Could you run a demo https://getrector.org/demo and copy past a url from the url bar after submitting demo with Process! button?
https://getrector.org/demo/20c203b7-b6ab-4086-b555-18db06ffd152
Demo runs fine. Please try again locally and if the problem remains please write down more information.
I agree with you demo works fine.
I runed again the same sample code locally and encountered the same fatal error.
My environnement is based on
- OpenSuse Leap 15.1.
- PHP 7.2.5
In the same environment, I runed also phpstan (version 0.12.32) / level : max on my sample class. No error was detected on the code.
Is there any debug parameter that I can set to identify on rectorphp 0.7.41 or on my environment what's going wrong ?
I cleaned my environment and remade an install ("composer require rector/rector --dev" => rector 0.7.43). I encountered the same error.
I updated my environment to be on the latest dev-master. I encountered also the same error.
What operating system do you use?
Try option --debug for more error output
I use OpenSuse Leap 15.1 / PHP 7.2.5 The option --debug produce this kind of output :
PHP Fatal error: Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class Sample\Entity\SampleEntity was never imported. Did you maybe forget to add a "use" statement for this annotation? in /dev/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:54
Stack trace:
#0 /home/arnaud/vendor/rector/rector/packages/doctrine-annotation-generated/src/ConstantPreservingDocParser.php(536): Doctrine\Common\Annotations\AnnotationException::semanticalError('The annotation ...')
#1 /home/arnaud/vendor/rector/rector/packages/doctrine-annotation-generated/src/ConstantPreservingDocParser.php(480): Rector\DoctrineAnnotationGenerated\ConstantPreservingDocParser->Annotation()
#2 /home/arnaud/vendor/rector/rector/packages/doctrine-annotation-generated/src/ConstantPreservingDocParser.php(245): Rector\DoctrineAnnotationGenerated\ConstantPreservingDocParser->Annotations()
#3 /home/arnaud/vendo in /dev/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 54
The command "composer show doctrine/annotations" executed from my rector directory gives version 1.10.3
my rector directory
Do you have Rector and app in the same directory?
If not, Rector probably can't find its autoload. See README how to add it
Rector and app are not in the same directory but rector.yaml points to the app autoloader
parameters:
php_version_features: '7.2'
paths:
- '/dev/src/Sample/Entity/SampleEntity.php'
autoload_paths:
- '/dev/vendor/autoload.php'
sets:
- php52
- php53
- php54
- php55
- php56
- php70
- php71
- php72
Ah, this is weird:
'/dev/vendor/autoload.php'
It normaly is:
'vendor/autoload.php'
Same for /dev/src → /src
Why is this way?
Also, avoid aboluste paths, starting with /vendor vs vendor (correct)
I've got two mouting point on my machine : one dedicated to rector (ie. /home/arnaud) and one for the project (ie. /dev). That's why, maybe I'm wrong, I'm using abolute path from my rector home directory to the project directory
I never used this approach to any tool, so I can't help here.
I recommend using single directory, you'll avoid many issues that will come eventually.
Rector is designed to be used as
- composer dependency
- prefixed PHAR https://github.com/rectorphp/rector-prefixed
- both in Docker
Anything else is not guaranteed and depends on skill of the user with that environment.
If you find a way to resolve it and it's something in Rector that can be changed, let me know. Closing as out of Rector scope.