-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - check
@mixin
PHPDoc tag above traits
- Loading branch information
1 parent
ba59142
commit 0d0de94
Showing
9 changed files
with
245 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,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\Trait_> | ||
*/ | ||
final class MixinTraitRule implements Rule | ||
{ | ||
|
||
public function __construct(private MixinCheck $check, private ReflectionProvider $reflectionProvider) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Trait_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$traitName = $node->namespacedName; | ||
if ($traitName === null) { | ||
return []; | ||
} | ||
|
||
if (!$this->reflectionProvider->hasClass($traitName->toString())) { | ||
return []; | ||
} | ||
|
||
return $this->check->checkInTraitDefinitionContext( | ||
$this->reflectionProvider->getClass($traitName->toString()), | ||
); | ||
} | ||
|
||
} |
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,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InTraitNode; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<InTraitNode> | ||
*/ | ||
final class MixinTraitUseRule implements Rule | ||
{ | ||
|
||
public function __construct(private MixinCheck $check) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InTraitNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
return $this->check->checkInTraitUseContext( | ||
$node->getTraitReflection(), | ||
$node->getImplementingClassReflection(), | ||
$node->getOriginalNode(), | ||
); | ||
} | ||
|
||
} |
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\Classes; | ||
|
||
use PHPStan\Rules\ClassCaseSensitivityCheck; | ||
use PHPStan\Rules\ClassForbiddenNameCheck; | ||
use PHPStan\Rules\ClassNameCheck; | ||
use PHPStan\Rules\Generics\GenericObjectTypeCheck; | ||
use PHPStan\Rules\MissingTypehintCheck; | ||
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<MixinTraitRule> | ||
*/ | ||
class MixinTraitRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$reflectionProvider = $this->createReflectionProvider(); | ||
|
||
return new MixinTraitRule( | ||
new MixinCheck( | ||
$reflectionProvider, | ||
new ClassNameCheck( | ||
new ClassCaseSensitivityCheck($reflectionProvider, true), | ||
new ClassForbiddenNameCheck(self::getContainer()), | ||
), | ||
new GenericObjectTypeCheck(), | ||
new MissingTypehintCheck(true, true, true, true, []), | ||
new UnresolvableTypeHelper(), | ||
true, | ||
true, | ||
), | ||
$reflectionProvider, | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/mixin-trait.php'], [ | ||
[ | ||
'Trait MixinTrait\FooTrait has PHPDoc tag @mixin with no value type specified in iterable type array.', | ||
14, | ||
MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,50 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PHPStan\Rules\ClassCaseSensitivityCheck; | ||
use PHPStan\Rules\ClassForbiddenNameCheck; | ||
use PHPStan\Rules\ClassNameCheck; | ||
use PHPStan\Rules\Generics\GenericObjectTypeCheck; | ||
use PHPStan\Rules\MissingTypehintCheck; | ||
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<MixinTraitUseRule> | ||
*/ | ||
class MixinTraitUseRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$reflectionProvider = $this->createReflectionProvider(); | ||
|
||
return new MixinTraitUseRule( | ||
new MixinCheck( | ||
$reflectionProvider, | ||
new ClassNameCheck( | ||
new ClassCaseSensitivityCheck($reflectionProvider, true), | ||
new ClassForbiddenNameCheck(self::getContainer()), | ||
), | ||
new GenericObjectTypeCheck(), | ||
new MissingTypehintCheck(true, true, true, true, []), | ||
new UnresolvableTypeHelper(), | ||
true, | ||
true, | ||
), | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/mixin-trait-use.php'], [ | ||
[ | ||
'PHPDoc tag @mixin contains unresolvable type.', | ||
22, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace MixinTraitUse; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
interface Foo | ||
{ | ||
|
||
/** @return T */ | ||
public function get(); | ||
|
||
} | ||
|
||
/** | ||
* @mixin Foo<static> | ||
* @mixin string&int | ||
*/ | ||
trait FooTrait | ||
{ | ||
|
||
} | ||
|
||
class Usages | ||
{ | ||
|
||
use FooTrait; | ||
|
||
} | ||
|
||
function (Usages $u): void { | ||
assertType(Usages::class, $u->get()); | ||
}; |
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,17 @@ | ||
<?php | ||
|
||
namespace MixinTrait; | ||
|
||
/** @template T */ | ||
class Foo | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @mixin Foo<array> | ||
*/ | ||
trait FooTrait | ||
{ | ||
|
||
} |