Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b73a8aa
commit 01b4d4e
Showing
4 changed files
with
139 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Type; | ||
use function count; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<MethodCall> | ||
*/ | ||
class GetTemplateTypeRule implements Rule | ||
{ | ||
|
||
public function __construct(private ReflectionProvider $reflectionProvider) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$args = $node->getArgs(); | ||
if (count($args) < 2) { | ||
return []; | ||
} | ||
if (!$node->name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
|
||
if ($node->name->toLowerString() !== 'gettemplatetype') { | ||
return []; | ||
} | ||
|
||
$calledOnType = $scope->getType($node->var); | ||
$methodReflection = $scope->getMethodReflection($calledOnType, $node->name->toString()); | ||
if ($methodReflection === null) { | ||
return []; | ||
} | ||
|
||
if (!$methodReflection->getDeclaringClass()->is(Type::class)) { | ||
return []; | ||
} | ||
|
||
$classType = $scope->getType($args[0]->value); | ||
$templateType = $scope->getType($args[1]->value); | ||
$errors = []; | ||
foreach ($classType->getConstantStrings() as $classNameType) { | ||
if (!$this->reflectionProvider->hasClass($classNameType->getValue())) { | ||
continue; | ||
} | ||
$classReflection = $this->reflectionProvider->getClass($classNameType->getValue()); | ||
$templateTypeMap = $classReflection->getTemplateTypeMap(); | ||
foreach ($templateType->getConstantStrings() as $templateTypeName) { | ||
if ($templateTypeMap->hasType($templateTypeName->getValue())) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Call to %s::%s() references unknown template type %s on class %s.', | ||
$methodReflection->getDeclaringClass()->getDisplayName(), | ||
$methodReflection->getName(), | ||
$templateTypeName->getValue(), | ||
$classReflection->getDisplayName(), | ||
))->build(); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<GetTemplateTypeRule> | ||
*/ | ||
class GetTemplateTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new GetTemplateTypeRule($this->createReflectionProvider()); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/get-template-type.php'], [ | ||
[ | ||
'Call to PHPStan\Type\Type::getTemplateType() references unknown template type TSendd on class Generator.', | ||
15, | ||
], | ||
[ | ||
'Call to PHPStan\Type\ObjectType::getTemplateType() references unknown template type TSendd on class Generator.', | ||
21, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace GetTemplateType; | ||
|
||
use Generator; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(Type $type): void | ||
{ | ||
$type->getTemplateType(Generator::class, 'TSend'); | ||
$type->getTemplateType(Generator::class, 'TSendd'); | ||
} | ||
|
||
public function doBar(ObjectType $type): void | ||
{ | ||
$type->getTemplateType(Generator::class, 'TSend'); | ||
$type->getTemplateType(Generator::class, 'TSendd'); | ||
} | ||
|
||
} |