-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for generic traits and specifying template types with
@use
- Loading branch information
1 parent
ace100c
commit 8766923
Showing
9 changed files
with
623 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Generics; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\PhpDoc\Tag\UsesTag; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Type\FileTypeMapper; | ||
use PHPStan\Type\Type; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\TraitUse> | ||
*/ | ||
class UsedTraitsRule implements Rule | ||
{ | ||
|
||
private \PHPStan\Type\FileTypeMapper $fileTypeMapper; | ||
|
||
private \PHPStan\Rules\Generics\GenericAncestorsCheck $genericAncestorsCheck; | ||
|
||
public function __construct( | ||
FileTypeMapper $fileTypeMapper, | ||
GenericAncestorsCheck $genericAncestorsCheck | ||
) | ||
{ | ||
$this->fileTypeMapper = $fileTypeMapper; | ||
$this->genericAncestorsCheck = $genericAncestorsCheck; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\TraitUse::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$scope->isInClass()) { | ||
throw new \PHPStan\ShouldNotHappenException(); | ||
} | ||
|
||
$className = $scope->getClassReflection()->getName(); | ||
$traitName = null; | ||
if ($scope->isInTrait()) { | ||
$traitName = $scope->getTraitReflection()->getName(); | ||
} | ||
$useTags = []; | ||
$docComment = $node->getDocComment(); | ||
if ($docComment !== null) { | ||
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( | ||
$scope->getFile(), | ||
$className, | ||
$traitName, | ||
null, | ||
$docComment->getText() | ||
); | ||
$useTags = $resolvedPhpDoc->getUsesTags(); | ||
} | ||
|
||
$description = sprintf('class %s', $className); | ||
$typeDescription = 'class'; | ||
if ($traitName !== null) { | ||
$description = sprintf('trait %s', $traitName); | ||
$typeDescription = 'trait'; | ||
} | ||
|
||
return $this->genericAncestorsCheck->check( | ||
$node->traits, | ||
array_map(static function (UsesTag $tag): Type { | ||
return $tag->getType(); | ||
}, $useTags), | ||
sprintf('%s @use tag contains incompatible type %%s.', ucfirst($description)), | ||
sprintf('%s has @use tag, but does not use any trait.', ucfirst($description)), | ||
sprintf('The @use tag of %s describes %%s but the %s uses %%s.', $description, $typeDescription), | ||
'PHPDoc tag @use contains generic type %s but trait %s is not generic.', | ||
'Generic type %s in PHPDoc tag @use does not specify all template types of trait %s: %s', | ||
'Generic type %s in PHPDoc tag @use specifies %d template types, but trait %s supports only %d: %s', | ||
'Type %s in generic type %s in PHPDoc tag @use is not subtype of template type %s of trait %s.', | ||
'PHPDoc tag @use has invalid type %s.', | ||
sprintf('%s uses generic trait %%s but does not specify its types: %%s', ucfirst($description)), | ||
sprintf('in used type %%s of %s', $description) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug4423; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
class Bar {} | ||
|
||
/** | ||
* @template K | ||
* @property-read Bar<K> $bar | ||
* @method Bar<K> doBar() | ||
*/ | ||
trait Foo { | ||
|
||
/** @var Bar<K> */ | ||
public $baz; | ||
|
||
/** @param K $k */ | ||
public function doFoo($k) | ||
{ | ||
assertType('T (class Bug4423\Child, argument)', $k); | ||
assertType('Bug4423\Bar<T (class Bug4423\Child, argument)>', $this->bar); | ||
assertType('Bug4423\Bar<T (class Bug4423\Child, argument)>', $this->baz); | ||
assertType('Bug4423\Bar<T (class Bug4423\Child, argument)>', $this->doBar()); | ||
assertType('Bug4423\Bar<T (class Bug4423\Child, argument)>', $this->doBaz()); | ||
} | ||
|
||
/** @return Bar<K> */ | ||
public function doBaz() | ||
{ | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* @template T | ||
* @template K | ||
*/ | ||
class Base { | ||
|
||
} | ||
|
||
/** | ||
* @template T | ||
* @extends Base<int, T> | ||
*/ | ||
class Child extends Base { | ||
/** @phpstan-use Foo<T> */ | ||
use Foo; | ||
} | ||
|
||
function (Child $child): void { | ||
/** @var Child<int> $child */ | ||
assertType('Bug4423\Child<int>', $child); | ||
assertType('Bug4423\Bar<int>', $child->bar); | ||
assertType('Bug4423\Bar<int>', $child->baz); | ||
assertType('Bug4423\Bar<int>', $child->doBar()); | ||
assertType('Bug4423\Bar<int>', $child->doBaz()); | ||
}; |
Oops, something went wrong.