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

NotImplementedException due to: "Rector\NodeTypeResolver\StaticTypeMapper::mapPHPStanTypeToPhpParserNode for PHPStan\Type\IntersectionType" #2614

Closed
UFTimmy opened this issue Jan 8, 2020 · 4 comments · Fixed by #2660
Labels

Comments

@UFTimmy
Copy link
Contributor

UFTimmy commented Jan 8, 2020

Subject Details
Rector version Rector dev-master@870e6e7
PHP version 7.2.12
Full Command ..\vendor\bin\rector.bat process --dry-run --rule=Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector

Current Behaviour

    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector
PHP Fatal error:  Uncaught Rector\Exception\NotImplementedException: Rector\NodeTypeResolver\StaticTypeMapper::mapPHPStanTypeToPhpParserNode for PHPStan\Type\IntersectionType in vendor\rector\rector\packages\NodeTypeResolver\src\StaticTypeMapper.php:288
Stack trace:
#0 vendor\rector\rector\packages\TypeDeclaration\src\Rector\FunctionLike\ReturnTypeDeclarationRector.php(189): Rector\NodeTypeResolver\StaticTypeMapper->mapPHPStanTypeToPhpParserNode(Object(PHPStan\Type\IntersectionType))
#1 vendor\rector\rector\packages\TypeDeclaration\src\Rector\FunctionLike\ReturnTypeDeclarationRector.php(120): Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector->isReturnTypeAlreadyAdded(Object(PhpParser\Node\Stmt\ClassMethod), Object(PHPStan\Type\IntersectionType))
#2 vendor\rector\rector\src\Rector\AbstractRector.php(116): Rector\TypeDeclaration\Rector in vendor\rector\rector\packages\NodeTypeResolver\src\StaticTypeMapper.php on line 288
Fatal error: Uncaught Rector\Exception\NotImplementedException: Rector\NodeTypeResolver\StaticTypeMapper::mapPHPStanTypeToPhpParserNode for PHPStan\Type\IntersectionType in vendor\rector\rector\packages\NodeTypeResolver\src\StaticTypeMapper.php:288
Stack trace:
#0 vendor\rector\rector\packages\TypeDeclaration\src\Rector\FunctionLike\ReturnTypeDeclarationRector.php(189): Rector\NodeTypeResolver\StaticTypeMapper->mapPHPStanTypeToPhpParserNode(Object(PHPStan\Type\IntersectionType))
#1 vendor\rector\rector\packages\TypeDeclaration\src\Rector\FunctionLike\ReturnTypeDeclarationRector.php(120): Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector->isReturnTypeAlreadyAdded(Object(PhpParser\Node\Stmt\ClassMethod), Object(PHPStan\Type\IntersectionType))
#2 vendor\rector\rector\src\Rector\AbstractRector.php(116): Rector\TypeDeclaration\Rector in vendor\rector\rector\packages\NodeTypeResolver\src\StaticTypeMapper.php on line 288

Minimal PHP Code Causing Issue

<?php 

use Doctrine\Common\Collections\Collection;

class Topic
{
    /**
     * @var Collection|Lesson[]
     */
    protected $lessons;

    /**
     * @return Lesson[]
     */
    public function getLessons() : Collection
    {
        return $this->lessons;
    }
}

Basically, this is a simplified Doctrine Entity with a Collection. The getter says it returns the Collection, and the parameter type hint is an Intersection of the Collection type, and the array syntax of what the Collection contains.

Removing either the getter or the @var annotation makes the error not appear.

Expected Behaviour

No error? 😃

@TomasVotruba
Copy link
Member

TomasVotruba commented Jan 8, 2020

What about Rector 0.6.9?

What in your rector.yaml?

@UFTimmy
Copy link
Contributor Author

UFTimmy commented Jan 9, 2020

Sorry, I was under the impression that the rector.yaml file was not processed if I passed in a rule name in the command line. That was my mistake. I updated to 0.6.9, and the error still persists.

Here's the smallest rector.yaml file that produces the error with the PHP code from the original post:

parameters:
    autoload_paths:
      - 'C:\path\to\project\vendor\autoload.php'

    sets:
      - 'type-declaration'

Removing the autoload_paths prevents the error from happening.

Here is the full command and the output:

..\vendor\bin\rector.bat process C:\path\to\project\src\EntityBundle\Entity\Content\Topic.php --
dry-run
Rector v0.6.9
Config file: rector.yaml

 3/3 [============================] 100%


 [ERROR] Could not process
         "C:\path\to\project\src\EntityBundle\Entity\Content\Topic.php" file, due
         to:
         "Rector\NodeTypeResolver\StaticTypeMapper::mapPHPStanTypeToPhpParserNode for PHPStan\Type\IntersectionType".

@TomasVotruba
Copy link
Member

Thanks for more details.

Removing the autoload_paths prevents the error from happening.

Why do you need to add it? It should be loaded by default by Rector.

How is the Rector installed?


I'm having troubles to replicate the issue. When I run:

vendor/bin/rector process theFileYouProvided.php --set type-declaration -n

No code is changed and Rector is ended with OK.

Could add failing test case here?

https://github.com/rectorphp/rector/tree/master/packages/TypeDeclaration/tests/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture

@UFTimmy
Copy link
Contributor Author

UFTimmy commented Jan 13, 2020

Why do you need to add it? It should be loaded by default by Rector.
How is the Rector installed?

Both of these are probably related -- I have rector installed globally. It resides in a separate project, as we develop dozens of smaller apps. My hope was to have one rector installation to work on all of the projects. I wrote a small script that auto generates the rector.yaml file based on the Symfony and Twig versions of each of the applications.

Sorry I cannot be more help, I don't have the experience with Rector or Unit tests that allows me to add a failing one. I understand that this means the bug can't be duplicated, and apologize for taking up your time.

TomasVotruba added a commit that referenced this issue Jul 3, 2022
rectorphp/rector-src@f608461 Remove RemoveOverriddenValuesRector as flow of control is not reliable and could cause invalid removal (#2614)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants