Skip to content

Commit 9395ecf

Browse files
committed
OverridingConstantRule - check visibility
1 parent d78d60a commit 9395ecf

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/Rules/Constants/OverridingConstantRule.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ private function processSingleConstant(ClassReflection $classReflection, string
7474
))->nonIgnorable()->build();
7575
}
7676

77+
if ($prototype->isPublic()) {
78+
if (!$constantReflection->isPublic()) {
79+
$errors[] = RuleErrorBuilder::message(sprintf(
80+
'%s constant %s::%s overriding public constant %s::%s should also be public.',
81+
$constantReflection->isPrivate() ? 'Private' : 'Protected',
82+
$constantReflection->getDeclaringClass()->getDisplayName(),
83+
$constantReflection->getName(),
84+
$prototype->getDeclaringClass()->getDisplayName(),
85+
$prototype->getName()
86+
))->nonIgnorable()->build();
87+
}
88+
} elseif ($constantReflection->isPrivate()) {
89+
$errors[] = RuleErrorBuilder::message(sprintf(
90+
'Private constant %s::%s overriding protected constant %s::%s should be protected or public.',
91+
$constantReflection->getDeclaringClass()->getDisplayName(),
92+
$constantReflection->getName(),
93+
$prototype->getDeclaringClass()->getDisplayName(),
94+
$prototype->getName()
95+
))->nonIgnorable()->build();
96+
}
97+
7798
if (!$this->checkPhpDocMethodSignatures) {
7899
return $errors;
79100
}

tests/PHPStan/Rules/Constants/OverridingConstantRuleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ public function testFinal(): void
6868
51,
6969
];
7070

71+
$errors[] = [
72+
'Private constant OverridingFinalConstant\PrivateDolor::PROTECTED_CONST overriding protected constant OverridingFinalConstant\Dolor::PROTECTED_CONST should be protected or public.',
73+
69,
74+
];
75+
$errors[] = [
76+
'Private constant OverridingFinalConstant\PrivateDolor::PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::PUBLIC_CONST should also be public.',
77+
70,
78+
];
79+
$errors[] = [
80+
'Private constant OverridingFinalConstant\PrivateDolor::ANOTHER_PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::ANOTHER_PUBLIC_CONST should also be public.',
81+
71,
82+
];
83+
$errors[] = [
84+
'Protected constant OverridingFinalConstant\ProtectedDolor::PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::PUBLIC_CONST should also be public.',
85+
80,
86+
];
87+
$errors[] = [
88+
'Protected constant OverridingFinalConstant\ProtectedDolor::ANOTHER_PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::ANOTHER_PUBLIC_CONST should also be public.',
89+
81,
90+
];
91+
7192
$this->analyse([__DIR__ . '/data/overriding-final-constant.php'], $errors);
7293
}
7394

tests/PHPStan/Rules/Constants/data/overriding-final-constant.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,53 @@ class Lorem implements BarInterface
5151
const FOO = 1;
5252

5353
}
54+
55+
class Dolor
56+
{
57+
58+
private const PRIVATE_CONST = 1;
59+
protected const PROTECTED_CONST = 2;
60+
public const PUBLIC_CONST = 3;
61+
const ANOTHER_PUBLIC_CONST = 4;
62+
63+
}
64+
65+
class PrivateDolor extends Dolor
66+
{
67+
68+
private const PRIVATE_CONST = 1;
69+
private const PROTECTED_CONST = 2; // error
70+
private const PUBLIC_CONST = 3; // error
71+
private const ANOTHER_PUBLIC_CONST = 4; // error
72+
73+
}
74+
75+
class ProtectedDolor extends Dolor
76+
{
77+
78+
protected const PRIVATE_CONST = 1;
79+
protected const PROTECTED_CONST = 2;
80+
protected const PUBLIC_CONST = 3; // error
81+
protected const ANOTHER_PUBLIC_CONST = 4; // error
82+
83+
}
84+
85+
class PublicDolor extends Dolor
86+
{
87+
88+
public const PRIVATE_CONST = 1;
89+
public const PROTECTED_CONST = 2;
90+
public const PUBLIC_CONST = 3;
91+
public const ANOTHER_PUBLIC_CONST = 4;
92+
93+
}
94+
95+
class Public2Dolor extends Dolor
96+
{
97+
98+
const PRIVATE_CONST = 1;
99+
const PROTECTED_CONST = 2;
100+
const PUBLIC_CONST = 3;
101+
const ANOTHER_PUBLIC_CONST = 4;
102+
103+
}

0 commit comments

Comments
 (0)