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

Faking scope context for trait reflections #1575

Merged
merged 5 commits into from
Dec 29, 2021
Merged

Faking scope context for trait reflections #1575

merged 5 commits into from
Dec 29, 2021

Conversation

zingimmick
Copy link
Contributor

@zingimmick zingimmick commented Dec 27, 2021

Because PHPStan does not traverse trait without a class, Rector used class Rector\Core\Stubs\DummyTraitClass for faking class reflection for trait reflections. But this solution makes it impossible to get param php doc from the faked scope, which makes the param tags no longer useful in traits.

For example, in this example, because the param tag @param string|null $column can't get in the faked scope, the type of $column resolved from NodeTypeResolver will be mixed not string|null

trait ConstructInTrait
{
    /**
     * @var string
     */
    private $column;

    /**
     * @param string|null $column
     */
    public function __construct(string $property, $column = null)
    {
        // the type of parameter column will be mixed in the faked scope, which should be string|null
        $this->column = $column ?? $property;
    }
}

This PR changing the scope context in a crude way, just for reference.

@zingimmick
Copy link
Contributor Author

Resolved type of the coalesce operator

Class

array (1)
   0 => PHPStan\Type\UnionType #11869
   |  types: array (2)
   |  |  0 => PHPStan\Type\StringType #11803
   |  |  1 => PHPStan\Type\StringType #11805

Trait

array (1)
   0 => PHPStan\Type\UnionType #12816
   |  types: array (2)
   |  |  0 => PHPStan\Type\MixedType #12786
   |  |  |  isExplicitMixed: false
   |  |  |  subtractedType: null
   |  |  1 => PHPStan\Type\StringType #11728

@samsonasik
Copy link
Member

samsonasik commented Dec 29, 2021

@zingimmick could you update PR title and description with the changes/solution applied ? Thank you.

@zingimmick zingimmick changed the title Add failing test fixture for TypedPropertyFromStrictConstructorRector Faking scope context for trait reflections Dec 29, 2021
@zingimmick
Copy link
Contributor Author

@zingimmick could you update PR title and description with the changes/solution applied ? Thank you.

@samsonasik The title and description have been updated. The purpose of this changes is to resolve the method's PhpDoc from the scope.

@TomasVotruba
Copy link
Member

Thank you 👏

@TomasVotruba TomasVotruba merged commit be1c791 into rectorphp:main Dec 29, 2021
@zingimmick zingimmick deleted the patch-1 branch December 30, 2021 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants