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

"PHPStan\Reflection\ClassReflection::getProperty(): Argument #2 ($scope) must be of type PHPStan\Reflection\ClassMemberAccessAnswerer, null given #5980

Closed
alies-dev opened this issue Mar 24, 2021 · 5 comments · Fixed by #5998
Labels

Comments

@alies-dev
Copy link

alies-dev commented Mar 24, 2021

Bug Report

Subject Details
Rector version 0.10.3
Installed as composer dependency

fatal error

"PHPStan\Reflection\ClassReflection::getProperty(): Argument #2     ($scope) must be of type PHPStan\Reflection\ClassMemberAccessAnswerer,  null given, called in vendor/rector/rector/packages/NodeTypeResolver/NodeTypeResolver/PropertyFetchTypeResolver.php:135". 

Minimal PHP Code Causing Issue

Sorry, I couldn't find a code to reproduce the issue on https://getrector.org/demo, does it use the latest 0.10.3 version? (I got this fatal after updating from 0.10.1).

It failed on a trait that used Eloquent properties (but not only), example:

trait UserHasAddress
{
    public function getAddressLine1(): ?string
    {
        return $this->address_line_1;
    }
}

But, that trait is pretty bit, and even the whole trait doesn't cause any reports on https://getrector.org/demo using all rectors and sets we use on the project. I hope it will help somehow.

rectors:

use Rector\CodeQuality\Rector\Assign\CombinedAssignRector;
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector;
use Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector;
use Rector\CodeQuality\Rector\For_\ForToForeachRector;
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
use Rector\CodeQuality\Rector\If_\CombineIfRector;
use Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector;
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
use Rector\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector;
use Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector;
use Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;


return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();
    $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80);

    $parameters->set(Option::SETS, [
        SetList::CODE_QUALITY,
        SetList::PHP_74,
        SetList::PHP_80,
    ]);

    $parameters->set(Option::SKIP, [
        Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class,
        Rector\Php80\Rector\FunctionLike\UnionTypesRector::class,
        SymplifyQuoteEscapeRector::class,
        SimplifyUselessVariableRector::class,
        RemoveExtraParametersRector::class,
        ArraySpreadInsteadOfArrayMergeRector::class,
        AddLiteralSeparatorToNumberRector::class,
        TypedPropertyRector::class,
        NormalizeNamespaceByPSR4ComposerAutoloadRector::class,
        AddClosureReturnTypeRector::class,
        ClosureToArrowFunctionRector::class,
        FinalizeClassesWithoutChildrenRector::class,
        DateTimeToDateTimeInterfaceRector::class,
    ]);

    $services = $containerConfigurator->services();
    $services->set(ArrayKeyExistsTernaryThenValueToCoalescingRector::class);
    $services->set(CombineIfRector::class);
    $services->set(CombinedAssignRector::class);
    $services->set(CompactToVariablesRector::class);
    $services->set(CompleteDynamicPropertiesRector::class);
    $services->set(ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class);
    $services->set(ExplicitBoolCompareRector::class);
    $services->set(ForRepeatedCountToOwnVariableRector::class);
    $services->set(ForToForeachRector::class);
    $services->set(BinarySwitchToIfElseRector::class);
    $services->set(CallUserFuncCallToVariadicRector::class);
    $services->set(ConsistentImplodeRector::class);
    $services->set(MakeInheritedMethodVisibilitySameAsParentRector::class);
    $services->set(NullableCompareToNullRector::class);
    $services->set(SplitDoubleAssignRector::class);
    $services->set(SplitStringClassConstantToClassConstFetchRector::class);
    $services->set(StrictArraySearchRector::class);
    $services->set(FinalizeClassesWithoutChildrenRector::class);
};

Expected Behaviour

Don’t have a fatal error :)

@alies-dev alies-dev added the bug label Mar 24, 2021
@alies-dev
Copy link
Author

alies-dev commented Mar 24, 2021

I found a minimal amount of code causes this error locally, but it works on demo: https://getrector.org/demo/3a1d056c-85f4-4f36-9d53-2d2288c4c775

I think it's a good idea to display Rector version on demo page.

Note, it's parsing error and it seems it doesn't depend on rectors/rules/services

@alies-dev
Copy link
Author

Additionally, I found it fails only when I use @property \Illuminate\Support\Carbon $created_at PHPDoc annotation (class-level)

@alies-dev
Copy link
Author

Hey @TomasVotruba

I'm sorry, the problem still exist even on master branch, I still can't reproduce it on getrector: https://getrector.org/demo/72ad4a75-83e5-4fa8-9875-11a9b9150c43

Here is a bare minimum php code run get this exception:

<?php declare(strict_types=1);

namespace App\Models\User;

/**
 * @property \DateTime $created_at
 */
final class User
{
    use \App\Models\User\SomeTrait;
}

trait SomeTrait
{
    public function filter(\App\Models\User\User $user)
    {
        return $user->created_at->modify('+2days');
    }
}
                                                                                                                       
 [ERROR] Could not process "app/Models/User/User.php" file, due to:                                                     
         "PHPStan\Reflection\ClassReflection::getProperty(): Argument #2 ($scope) must be of type                       
         PHPStan\Reflection\ClassMemberAccessAnswerer, null given, called in                                            
         vendor/rector/rector/packages/NodeTypeResolver/NodeTypeResolver/PropertyFetchTypeResolver.php:135".            
                                                                        

The problem raises when we modify a class property declared by PHPDoc @Property from a trait but do it for another object instance

@samsonasik
Copy link
Member

please use dev-main instead of dev-master as latest is now dev-main

@alies-dev
Copy link
Author

@samsonasik
oh, yes, nice work!

I used "rector/rector": "@dev", but with dev-main it fixed the problem, sorry for not being check --version.

After update, I've got another error:

                                                                                                                        
 [ERROR] Could not process "app/Utility/MilestoneData.php" file, due to:                                                
         "PhpParser\Node\Expr\Cast\Object_".                                                                            
                                                                                                                        

But I've changed myy implementation to use array of arrays instead array of \stdClass and it "solves" the problem. Again, the problem is not reproducible on getrector and even more: I don't now a version we use at this site, but at least stoped to fail on my project. Thanks a lot!

TomasVotruba added a commit that referenced this issue Jun 19, 2024
rectorphp/rector-src@a787d4c Remove deprecated and empty FinalizeClassesWithoutChildrenRector + FinalizePublicClassConstantRector (#5980)
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