-
Notifications
You must be signed in to change notification settings - Fork 478
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
d6c5e44
commit 9656bc5
Showing
3 changed files
with
70 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,37 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\Trait_> | ||
*/ | ||
class TraitAttributeClassRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Trait_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
foreach ($node->attrGroups as $attrGroup) { | ||
foreach ($attrGroup->attrs as $attr) { | ||
$name = $attr->name->toLowerString(); | ||
if ($name === 'attribute') { | ||
return [ | ||
RuleErrorBuilder::message('Trait cannot be an Attribute class.')->build(), | ||
]; | ||
} | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
tests/PHPStan/Rules/Classes/TraitAttributeClassRuleTest.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,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<TraitAttributeClassRule> | ||
*/ | ||
class TraitAttributeClassRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new TraitAttributeClassRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 80000) { | ||
$this->markTestSkipped('Test requires PHP 8.0.'); | ||
} | ||
$this->analyse([__DIR__ . '/data/non-class-attribute-class.php'], [ | ||
[ | ||
'Trait cannot be an Attribute class.', | ||
11, | ||
], | ||
]); | ||
} | ||
|
||
} |