-
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.
Bleeding edge - MethodTagTemplateTypeTraitRule
- Loading branch information
1 parent
085fcf4
commit aadbf62
Showing
5 changed files
with
135 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
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,52 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Generics; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\Trait_> | ||
*/ | ||
final class MethodTagTemplateTypeTraitRule implements Rule | ||
{ | ||
|
||
public function __construct( | ||
private MethodTagTemplateTypeCheck $check, | ||
private ReflectionProvider $reflectionProvider, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Trait_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$docComment = $node->getDocComment(); | ||
if ($docComment === null) { | ||
return []; | ||
} | ||
|
||
$traitName = $node->namespacedName; | ||
if ($traitName === null) { | ||
return []; | ||
} | ||
|
||
if (!$this->reflectionProvider->hasClass($traitName->toString())) { | ||
return []; | ||
} | ||
|
||
return $this->check->check( | ||
$this->reflectionProvider->getClass($traitName->toString()), | ||
$scope, | ||
$node, | ||
$docComment->getText(), | ||
); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
tests/PHPStan/Rules/Generics/MethodTagTemplateTypeTraitRuleTest.php
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,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Generics; | ||
|
||
use PHPStan\Rules\ClassCaseSensitivityCheck; | ||
use PHPStan\Rules\ClassForbiddenNameCheck; | ||
use PHPStan\Rules\ClassNameCheck; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPStan\Type\FileTypeMapper; | ||
|
||
/** | ||
* @extends RuleTestCase<MethodTagTemplateTypeTraitRule> | ||
*/ | ||
class MethodTagTemplateTypeTraitRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$reflectionProvider = $this->createReflectionProvider(); | ||
$typeAliasResolver = $this->createTypeAliasResolver(['TypeAlias' => 'int'], $reflectionProvider); | ||
|
||
return new MethodTagTemplateTypeTraitRule( | ||
new MethodTagTemplateTypeCheck( | ||
self::getContainer()->getByType(FileTypeMapper::class), | ||
new TemplateTypeCheck( | ||
$reflectionProvider, | ||
new ClassNameCheck( | ||
new ClassCaseSensitivityCheck($reflectionProvider, true), | ||
new ClassForbiddenNameCheck(self::getContainer()), | ||
), | ||
new GenericObjectTypeCheck(), | ||
$typeAliasResolver, | ||
true, | ||
), | ||
), | ||
$reflectionProvider, | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/method-tag-trait-template.php'], [ | ||
[ | ||
'PHPDoc tag @method template U for method MethodTagTraitTemplate\HelloWorld::sayHello() has invalid bound type MethodTagTraitTemplate\Nonexisting.', | ||
11, | ||
], | ||
[ | ||
'PHPDoc tag @method template for method MethodTagTraitTemplate\HelloWorld::sayHello() cannot have existing class stdClass as its name.', | ||
11, | ||
], | ||
[ | ||
'PHPDoc tag @method template T for method MethodTagTraitTemplate\HelloWorld::sayHello() shadows @template T for class MethodTagTraitTemplate\HelloWorld.', | ||
11, | ||
], | ||
[ | ||
'PHPDoc tag @method template for method MethodTagTraitTemplate\HelloWorld::typeAlias() cannot have existing type alias TypeAlias as its name.', | ||
11, | ||
], | ||
]); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
tests/PHPStan/Rules/Generics/data/method-tag-trait-template.php
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,13 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace MethodTagTraitTemplate; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @method void sayHello<T, U of Nonexisting, stdClass>(T $a, U $b, stdClass $c) | ||
* @method void typeAlias<TypeAlias of mixed>(TypeAlias $a) | ||
*/ | ||
trait HelloWorld | ||
{ | ||
} |