Skip to content

Commit 16b48af

Browse files
committed
Report missing #[\Override] on class constants by default only when every analysed PHP version supports it
Amp-Thread-ID: https://ampcode.com/threads/T-01a1df02-11a2-7485-9bf7-a0b7cacc89ad
1 parent b57549f commit 16b48af

7 files changed

Lines changed: 108 additions & 32 deletions

‎src/Php/PhpVersion.php‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,6 @@ public function supportsOverrideAttributeOnProperty(): bool
539539
return $this->versionId >= 80500;
540540
}
541541

542-
public function supportsOverrideAttributeOnClassConstant(): bool
543-
{
544-
return $this->versionId >= 80600;
545-
}
546-
547542
public function deprecatesDecOnNonNumericString(): bool
548543
{
549544
return $this->versionId >= 80300;

‎src/Rules/Constants/OverrideAttributeOnClassConstantCheck.php‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredParameter;
88
use PHPStan\DependencyInjection\AutowiredService;
9-
use PHPStan\Php\PhpVersion;
109
use PHPStan\Reflection\ClassConstantReflection;
1110
use PHPStan\Reflection\ClassReflection;
1211
use PHPStan\Rules\IdentifierRuleError;
@@ -23,7 +22,6 @@ final class OverrideAttributeOnClassConstantCheck
2322
{
2423

2524
public function __construct(
26-
private PhpVersion $phpVersion,
2725
#[AutowiredParameter]
2826
private ?bool $checkMissingOverrideConstantAttribute,
2927
#[AutowiredParameter]
@@ -115,7 +113,7 @@ public function check(
115113
if (
116114
$hasOverrideAttribute
117115
|| $scope->isInTrait()
118-
|| !$this->isMissingOverrideAttributeChecked()
116+
|| !$this->isMissingOverrideAttributeChecked($scope)
119117
) {
120118
return [];
121119
}
@@ -141,14 +139,15 @@ public function check(
141139
return [$builder->build()];
142140
}
143141

144-
private function isMissingOverrideAttributeChecked(): bool
142+
private function isMissingOverrideAttributeChecked(Scope $scope): bool
145143
{
146144
if ($this->checkMissingOverrideConstantAttribute !== null) {
147145
return $this->checkMissingOverrideConstantAttribute;
148146
}
149147

148+
// every analysed PHP version must support the attribute, e.g. not with "php": "^8.5" in composer.json
150149
return ($this->checkMissingOverridePropertyAttribute === true || $this->checkMissingOverrideMethodAttribute)
151-
&& $this->phpVersion->supportsOverrideAttributeOnClassConstant();
150+
&& $scope->getPhpVersion()->supportsOverrideAttributeOnClassConstant()->yes();
152151
}
153152

154153
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Constants;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<OverridingConstantRule>
10+
*/
11+
class OverridingConstantRuleConfigPhpTest extends RuleTestCase
12+
{
13+
14+
private ?bool $checkMissingOverrideConstantAttribute = null;
15+
16+
protected function getRule(): Rule
17+
{
18+
return new OverridingConstantRule(
19+
true,
20+
new OverrideAttributeOnClassConstantCheck(
21+
$this->checkMissingOverrideConstantAttribute,
22+
true,
23+
true,
24+
),
25+
);
26+
}
27+
28+
public function testDefaultReportsOnlyWhenEveryPhpVersionSupportsAttribute(): void
29+
{
30+
$this->analyse([__DIR__ . '/data/constant-missing-override-attr-php-versions.php'], [
31+
[
32+
'Constant ConstantMissingOverrideAttrPhpVersions\SupportedInBranch::FOO overrides constant ConstantMissingOverrideAttrPhpVersions\Foo::FOO but is missing the #[\Override] attribute.',
33+
16,
34+
],
35+
]);
36+
}
37+
38+
public function testEnabledReportsOnEveryPhpVersion(): void
39+
{
40+
$this->checkMissingOverrideConstantAttribute = true;
41+
$this->analyse([__DIR__ . '/data/constant-missing-override-attr-php-versions.php'], [
42+
[
43+
'Constant ConstantMissingOverrideAttrPhpVersions\SupportedInBranch::FOO overrides constant ConstantMissingOverrideAttrPhpVersions\Foo::FOO but is missing the #[\Override] attribute.',
44+
16,
45+
],
46+
[
47+
'Constant ConstantMissingOverrideAttrPhpVersions\DependsOnPhpVersion::FOO overrides constant ConstantMissingOverrideAttrPhpVersions\Foo::FOO but is missing the #[\Override] attribute.',
48+
24,
49+
],
50+
]);
51+
}
52+
53+
public static function getAdditionalConfigFiles(): array
54+
{
55+
return [
56+
__DIR__ . '/data/constant-missing-override-attr-php-versions.neon',
57+
];
58+
}
59+
60+
}

‎tests/PHPStan/Rules/Constants/OverridingConstantRuleTest.php‎

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

33
namespace PHPStan\Rules\Constants;
44

5-
use PHPStan\Php\PhpVersion;
65
use PHPStan\Rules\Rule;
76
use PHPStan\Testing\RuleTestCase;
87
use PHPUnit\Framework\Attributes\DataProvider;
@@ -21,14 +20,11 @@ class OverridingConstantRuleTest extends RuleTestCase
2120

2221
private bool $checkMissingOverrideMethodAttribute = false;
2322

24-
private int $phpVersionId = PHP_VERSION_ID;
25-
2623
protected function getRule(): Rule
2724
{
2825
return new OverridingConstantRule(
2926
true,
3027
new OverrideAttributeOnClassConstantCheck(
31-
new PhpVersion($this->phpVersionId),
3228
$this->checkMissingOverrideConstantAttribute,
3329
$this->checkMissingOverridePropertyAttribute,
3430
$this->checkMissingOverrideMethodAttribute,
@@ -147,20 +143,20 @@ public static function dataOverrideAttribute(): iterable
147143
],
148144
];
149145

150-
// checkMissingOverrideConstantAttribute, checkMissingOverridePropertyAttribute, checkMissingOverrideMethodAttribute, PHP version
151-
yield [null, null, false, 80500, []];
152-
yield [null, null, false, 80600, []];
153-
yield [null, null, true, 80500, []];
154-
yield [null, null, true, 80600, $missingErrors];
155-
yield [null, true, false, 80500, []];
156-
yield [null, true, false, 80600, $missingErrors];
157-
yield [null, false, true, 80600, $missingErrors];
158-
yield [null, false, false, 80600, []];
159-
yield [true, null, false, 80500, $missingErrors];
160-
yield [true, null, false, 80600, $missingErrors];
161-
yield [true, false, false, 80600, $missingErrors];
162-
yield [false, null, true, 80600, []];
163-
yield [false, true, true, 80600, []];
146+
// the default follows the other options only when the analysed PHP version supports the attribute,
147+
// OverridingConstantRuleConfigPhpTest covers PHP version ranges
148+
$defaultErrors = PHP_VERSION_ID >= 80600 ? $missingErrors : [];
149+
150+
// checkMissingOverrideConstantAttribute, checkMissingOverridePropertyAttribute, checkMissingOverrideMethodAttribute
151+
yield [null, null, false, []];
152+
yield [null, null, true, $defaultErrors];
153+
yield [null, true, false, $defaultErrors];
154+
yield [null, false, true, $defaultErrors];
155+
yield [null, false, false, []];
156+
yield [true, null, false, $missingErrors];
157+
yield [true, false, false, $missingErrors];
158+
yield [false, null, true, []];
159+
yield [false, true, true, []];
164160
}
165161

166162
/**
@@ -172,14 +168,12 @@ public function testOverrideAttribute(
172168
?bool $checkMissingOverrideConstantAttribute,
173169
?bool $checkMissingOverridePropertyAttribute,
174170
bool $checkMissingOverrideMethodAttribute,
175-
int $phpVersionId,
176171
array $expectedErrors,
177172
): void
178173
{
179174
$this->checkMissingOverrideConstantAttribute = $checkMissingOverrideConstantAttribute;
180175
$this->checkMissingOverridePropertyAttribute = $checkMissingOverridePropertyAttribute;
181176
$this->checkMissingOverrideMethodAttribute = $checkMissingOverrideMethodAttribute;
182-
$this->phpVersionId = $phpVersionId;
183177

184178
$errors = array_merge([
185179
[
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
phpVersion:
3+
min: 80500
4+
max: 80600
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ConstantMissingOverrideAttrPhpVersions;
4+
5+
class Foo
6+
{
7+
8+
public const FOO = 1;
9+
10+
}
11+
12+
if (PHP_VERSION_ID >= 80600) {
13+
class SupportedInBranch extends Foo
14+
{
15+
16+
public const FOO = 1;
17+
18+
}
19+
}
20+
21+
class DependsOnPhpVersion extends Foo
22+
{
23+
24+
public const FOO = 1;
25+
26+
}

‎tests/PHPStan/Rules/EnumCases/OverridingEnumCaseRuleTest.php‎

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

33
namespace PHPStan\Rules\EnumCases;
44

5-
use PHPStan\Php\PhpVersion;
65
use PHPStan\Rules\Constants\OverrideAttributeOnClassConstantCheck;
76
use PHPStan\Rules\Rule;
87
use PHPStan\Testing\RuleTestCase;
@@ -21,7 +20,6 @@ protected function getRule(): Rule
2120
return new OverridingEnumCaseRule(
2221
true,
2322
new OverrideAttributeOnClassConstantCheck(
24-
new PhpVersion(80600),
2523
$this->checkMissingOverrideConstantAttribute,
2624
null,
2725
false,

0 commit comments

Comments
 (0)