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

Add resolution of generic @method tags #2931

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -160,12 +163,30 @@

foreach (['@method', '@psalm-method', '@phpstan-method'] as $tagName) {
foreach ($phpDocNode->getMethodTagValues($tagName) as $tagValue) {
$templateTags = [];

if (count($tagValue->templateTypes) > 0) {
foreach ($tagValue->templateTypes as $templateType) {
$templateTags[$templateType->name] = new TemplateTag(
$templateType->name,
$templateType->bound !== null
? $this->typeNodeResolver->resolve($templateType->bound, $nameScope)
: new MixedType(),
TemplateTypeVariance::createInvariant()
);
}

Check failure on line 177 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 177 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

$templateTypeScope = TemplateTypeScope::createWithMethod($nameScope->getClassName(), $tagValue->methodName);

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 179 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.
$templateTypeMap = new TemplateTypeMap(array_map(static fn (TemplateTag $tag): Type => TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag), $templateTags));

Check failure on line 180 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 180 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.
$nameScope = $nameScope->withTemplateTypeMap($templateTypeMap);
}

$parameters = [];
foreach ($tagValue->parameters as $parameterNode) {
$parameterName = substr($parameterNode->parameterName, 1);
$type = $parameterNode->type !== null
? $this->typeNodeResolver->resolve($parameterNode->type, $nameScope)
: new MixedType();

Check failure on line 189 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.

Check failure on line 189 in src/PhpDoc/PhpDocNodeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Parameter #1 $className of static method PHPStan\Type\Generic\TemplateTypeScope::createWithMethod() expects string, string|null given.
if ($parameterNode->defaultValue instanceof ConstExprNullNode) {
$type = TypeCombinator::addNull($type);
}
Expand All @@ -191,6 +212,7 @@
: new MixedType(),
$tagValue->isStatic,
$parameters,
$templateTags,
);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/PhpDoc/Tag/MethodTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class MethodTag

/**
* @param array<string, MethodTagParameter> $parameters
* @param array<string, TemplateTag> $templateTags
*/
public function __construct(
private Type $returnType,
private bool $isStatic,
private array $parameters,
private array $templateTags = [],
)
{
}
Expand All @@ -37,4 +39,12 @@ public function getParameters(): array
return $this->parameters;
}

/**
* @return array<string, TemplateTag>
*/
public function getTemplateTags(): array
{
return $this->templateTags;
}

}
3 changes: 2 additions & 1 deletion src/Reflection/Annotations/AnnotationMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
private bool $isStatic,
private bool $isVariadic,
private ?Type $throwType,
private TemplateTypeMap $templateTypeMap,
)
{
}
Expand Down Expand Up @@ -69,7 +70,7 @@ public function getVariants(): array
if ($this->variants === null) {
$this->variants = [
new FunctionVariantWithPhpDocs(
TemplateTypeMap::createEmpty(),
$this->templateTypeMap,
null,
$this->parameters,
$this->isVariadic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

namespace PHPStan\Reflection\Annotations;

use PHPStan\PhpDoc\Tag\TemplateTag;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Type;

use function count;

class AnnotationsMethodsClassReflectionExtension implements MethodsClassReflectionExtension
Expand Down Expand Up @@ -57,6 +63,13 @@ private function findClassReflectionWithMethod(
);
}

$templateTypeScope = TemplateTypeScope::createWithClass($classReflection->getName());

$templateTypeMap = new TemplateTypeMap(array_map(
static fn (TemplateTag $tag): Type => TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag),
$methodTags[$methodName]->getTemplateTags()
));

$isStatic = $methodTags[$methodName]->isStatic();
$nativeCallMethodName = $isStatic ? '__callStatic' : '__call';

Expand All @@ -75,6 +88,7 @@ private function findClassReflectionWithMethod(
$classReflection->hasNativeMethod($nativeCallMethodName)
? $classReflection->getNativeMethod($nativeCallMethodName)->getThrowType()
: null,
$templateTypeMap,
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-decode/narrow_type_with_force_array.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-decode/invalid_type.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/json-decode/json_object_as_array.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/generic-method-tags.php');

require_once __DIR__ . '/data/bug2574.php';

Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/data/generic-method-tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace GenericMethodTags;

use function PHPStan\Testing\assertType;

/**
* @method TVal doThing<TVal of mixed>(TVal $param)
*/
class Test
{
public function __call(): mixed
{
}
}

function test(int $int, string $string): void
{
$test = new Test();

assertType('int', $test->doThing($int));
assertType('string', $test->doThing($string));
}
Loading