Skip to content

Commit

Permalink
OverridingConstantRule - check visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 18, 2021
1 parent d78d60a commit 9395ecf
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Rules/Constants/OverridingConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ private function processSingleConstant(ClassReflection $classReflection, string
))->nonIgnorable()->build();
}

if ($prototype->isPublic()) {
if (!$constantReflection->isPublic()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s constant %s::%s overriding public constant %s::%s should also be public.',
$constantReflection->isPrivate() ? 'Private' : 'Protected',
$constantReflection->getDeclaringClass()->getDisplayName(),
$constantReflection->getName(),
$prototype->getDeclaringClass()->getDisplayName(),
$prototype->getName()
))->nonIgnorable()->build();
}
} elseif ($constantReflection->isPrivate()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Private constant %s::%s overriding protected constant %s::%s should be protected or public.',
$constantReflection->getDeclaringClass()->getDisplayName(),
$constantReflection->getName(),
$prototype->getDeclaringClass()->getDisplayName(),
$prototype->getName()
))->nonIgnorable()->build();
}

if (!$this->checkPhpDocMethodSignatures) {
return $errors;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Constants/OverridingConstantRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ public function testFinal(): void
51,
];

$errors[] = [
'Private constant OverridingFinalConstant\PrivateDolor::PROTECTED_CONST overriding protected constant OverridingFinalConstant\Dolor::PROTECTED_CONST should be protected or public.',
69,
];
$errors[] = [
'Private constant OverridingFinalConstant\PrivateDolor::PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::PUBLIC_CONST should also be public.',
70,
];
$errors[] = [
'Private constant OverridingFinalConstant\PrivateDolor::ANOTHER_PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::ANOTHER_PUBLIC_CONST should also be public.',
71,
];
$errors[] = [
'Protected constant OverridingFinalConstant\ProtectedDolor::PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::PUBLIC_CONST should also be public.',
80,
];
$errors[] = [
'Protected constant OverridingFinalConstant\ProtectedDolor::ANOTHER_PUBLIC_CONST overriding public constant OverridingFinalConstant\Dolor::ANOTHER_PUBLIC_CONST should also be public.',
81,
];

$this->analyse([__DIR__ . '/data/overriding-final-constant.php'], $errors);
}

Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Constants/data/overriding-final-constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,53 @@ class Lorem implements BarInterface
const FOO = 1;

}

class Dolor
{

private const PRIVATE_CONST = 1;
protected const PROTECTED_CONST = 2;
public const PUBLIC_CONST = 3;
const ANOTHER_PUBLIC_CONST = 4;

}

class PrivateDolor extends Dolor
{

private const PRIVATE_CONST = 1;
private const PROTECTED_CONST = 2; // error
private const PUBLIC_CONST = 3; // error
private const ANOTHER_PUBLIC_CONST = 4; // error

}

class ProtectedDolor extends Dolor
{

protected const PRIVATE_CONST = 1;
protected const PROTECTED_CONST = 2;
protected const PUBLIC_CONST = 3; // error
protected const ANOTHER_PUBLIC_CONST = 4; // error

}

class PublicDolor extends Dolor
{

public const PRIVATE_CONST = 1;
public const PROTECTED_CONST = 2;
public const PUBLIC_CONST = 3;
public const ANOTHER_PUBLIC_CONST = 4;

}

class Public2Dolor extends Dolor
{

const PRIVATE_CONST = 1;
const PROTECTED_CONST = 2;
const PUBLIC_CONST = 3;
const ANOTHER_PUBLIC_CONST = 4;

}

0 comments on commit 9395ecf

Please sign in to comment.