Skip to content

Commit 9906ec9

Browse files
committed
Fix build
1 parent 1d85df1 commit 9906ec9

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

src/Rules/Classes/ClassAttributesRule.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,26 @@ public function getNodeType(): string
3232

3333
public function processNode(Node $node, Scope $scope): array
3434
{
35-
$classLikeNode = $node->getOriginalNode();
35+
$classReflection = $node->getClassReflection();
36+
37+
if (count($classReflection->getNativeReflection()->getAttributes('Deprecated')) > 0) {
38+
$typeName = strtolower($classReflection->getClassTypeDescription());
39+
return [
40+
RuleErrorBuilder::message(sprintf('Attribute class Deprecated cannot be used with %s %s.', $typeName, $classReflection->getDisplayName()))
41+
->identifier(sprintf('%s.deprecatedAttribute', $typeName))
42+
->nonIgnorable()
43+
->build(),
44+
];
45+
}
3646

47+
$classLikeNode = $node->getOriginalNode();
3748
$errors = $this->attributesCheck->check(
3849
$scope,
3950
$classLikeNode->attrGroups,
4051
Attribute::TARGET_CLASS,
4152
'class',
4253
);
4354

44-
$classReflection = $node->getClassReflection();
45-
46-
if (count($classReflection->getNativeReflection()->getAttributes('Deprecated')) > 0) {
47-
$typeName = strtolower($classReflection->getClassTypeDescription());
48-
$errors[] = RuleErrorBuilder::message(sprintf('Attribute class Deprecated cannot be used with %s %s.', $typeName, $classReflection->getDisplayName()))
49-
->identifier(sprintf('%s.deprecatedAttribute', $typeName))
50-
->nonIgnorable()
51-
->build();
52-
}
53-
5455
if (
5556
$classReflection->isReadOnly()
5657
|| $classReflection->isEnum()

src/Rules/Traits/TraitAttributesRule.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ public function getNodeType(): string
3434

3535
public function processNode(Node $node, Scope $scope): array
3636
{
37+
if (!$this->phpVersion->supportsDeprecatedTraits()) {
38+
if (count($node->getTraitReflection()->getNativeReflection()->getAttributes('Deprecated')) > 0) {
39+
return [
40+
RuleErrorBuilder::message('Attribute class Deprecated can be used with traits only on PHP 8.5 and later.')
41+
->identifier('trait.deprecatedAttribute')
42+
->nonIgnorable()
43+
->build(),
44+
];
45+
}
46+
}
47+
3748
$originalNode = $node->getOriginalNode();
3849
$errors = $this->attributesCheck->check(
3950
$scope,
@@ -42,15 +53,6 @@ public function processNode(Node $node, Scope $scope): array
4253
'class',
4354
);
4455

45-
if (!$this->phpVersion->supportsDeprecatedTraits()) {
46-
if (count($node->getTraitReflection()->getNativeReflection()->getAttributes('Deprecated')) > 0) {
47-
$errors[] = RuleErrorBuilder::message('Attribute class Deprecated can be used with traits only on PHP 8.5 and later.')
48-
->identifier('trait.deprecatedAttribute')
49-
->nonIgnorable()
50-
->build();
51-
}
52-
}
53-
5456
if (count($node->getTraitReflection()->getNativeReflection()->getAttributes('AllowDynamicProperties')) > 0) {
5557
$errors[] = RuleErrorBuilder::message('Attribute class AllowDynamicProperties cannot be used with trait.')
5658
->identifier('trait.allowDynamicProperties')

tests/PHPStan/Reflection/ClassReflectionTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Attributes\IsAttribute2;
88
use Attributes\IsAttribute3;
99
use Attributes\IsNotAttribute;
10-
use DeprecatedAttributeOnTrait\DeprTrait;
1110
use GenericInheritance\C;
1211
use HasTraitUse\Bar;
1312
use HasTraitUse\Baz;
@@ -321,8 +320,8 @@ public function testIs(): void
321320

322321
public static function dataDeprecatedAttribute(): iterable
323322
{
324-
yield [\DeprecatedAttrOnClass\Foo::class, false];
325-
yield [DeprTrait::class, true]; // @phpstan-ignore classConstant.deprecatedTrait
323+
yield ['DeprecatedAttrOnClass\Foo', false];
324+
yield ['DeprecatedAttributeOnTrait\DeprTrait', true];
326325
}
327326

328327
#[DataProvider('dataDeprecatedAttribute')]

tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Classes;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\AttributesCheck;
67
use PHPStan\Rules\ClassCaseSensitivityCheck;
78
use PHPStan\Rules\ClassForbiddenNameCheck;
@@ -14,6 +15,7 @@
1415
use PHPStan\Rules\RuleLevelHelper;
1516
use PHPStan\Testing\RuleTestCase;
1617
use PHPUnit\Framework\Attributes\RequiresPhp;
18+
use const PHP_VERSION_ID;
1719

1820
/**
1921
* @extends RuleTestCase<ClassAttributesRule>
@@ -49,6 +51,7 @@ protected function getRule(): Rule
4951
),
5052
true,
5153
),
54+
new PhpVersion(PHP_VERSION_ID),
5255
);
5356
}
5457

0 commit comments

Comments
 (0)