rector icon indicating copy to clipboard operation
rector copied to clipboard

Use of alias cause a fatal error

Open arnaud-fr opened this issue 6 years ago • 14 comments

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

arnaud-fr avatar Jul 03 '20 09:07 arnaud-fr

Could you run a demo https://getrector.org/demo and copy past a url from the url bar after submitting demo with Process! button?

ghost avatar Jul 03 '20 10:07 ghost

https://getrector.org/demo/20c203b7-b6ab-4086-b555-18db06ffd152

arnaud-fr avatar Jul 03 '20 13:07 arnaud-fr

Demo runs fine. Please try again locally and if the problem remains please write down more information.

ghost avatar Jul 03 '20 13:07 ghost

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 ?

arnaud-fr avatar Jul 03 '20 16:07 arnaud-fr

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.

arnaud-fr avatar Jul 06 '20 10:07 arnaud-fr

What operating system do you use?

Try option --debug for more error output

TomasVotruba avatar Jul 06 '20 10:07 TomasVotruba

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

arnaud-fr avatar Jul 06 '20 10:07 arnaud-fr

The command "composer show doctrine/annotations" executed from my rector directory gives version 1.10.3

arnaud-fr avatar Jul 06 '20 10:07 arnaud-fr

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

TomasVotruba avatar Jul 06 '20 10:07 TomasVotruba

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

arnaud-fr avatar Jul 06 '20 11:07 arnaud-fr

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)

TomasVotruba avatar Jul 06 '20 11:07 TomasVotruba

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

arnaud-fr avatar Jul 06 '20 12:07 arnaud-fr

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.

TomasVotruba avatar Jul 06 '20 12:07 TomasVotruba

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.

TomasVotruba avatar Jul 06 '20 12:07 TomasVotruba