Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ lint:
--exclude tests/PHPStan/Rules/Properties/data/read-only-property-phpdoc-and-native.php \
--exclude tests/PHPStan/Rules/Properties/data/read-only-property-readonly-class.php \
--exclude tests/PHPStan/Rules/Properties/data/overriding-property.php \
--exclude tests/PHPStan/Rules/Properties/data/overriding-property-hooks.php \
--exclude tests/PHPStan/Rules/Properties/data/overriding-property-hooks-invalid-declaration.php \
--exclude tests/PHPStan/Rules/Properties/data/overriding-property-set-visibility.php \
--exclude tests/PHPStan/Rules/Properties/data/property-hook-override-attr.php \
--exclude tests/PHPStan/Rules/Constants/data/overriding-final-constant.php \
--exclude tests/PHPStan/Rules/Properties/data/intersection-types.php \
--exclude tests/PHPStan/Rules/Classes/data/first-class-instantiation-callable.php \
Expand Down
28 changes: 28 additions & 0 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,34 @@ public function getOnlyVariant(): ExtendedParametersAcceptor
return $this->getVariants()[0];
}

/**
* @phpstan-assert-if-true !null $this->getHookedPropertyName()
* @phpstan-assert-if-true !null $this->getPropertyHookName()
*/
public function isPropertyHook(): bool
{
return $this->reflection->getBetterReflection()->isHook();
}

public function getHookedPropertyName(): ?string
{
return $this->reflection->getBetterReflection()->getHookProperty()?->getName();
}

/**
* @return 'get'|'set'|null
*/
public function getPropertyHookName(): ?string
{
$betterReflection = $this->reflection->getBetterReflection();
$hookProperty = $betterReflection->getHookProperty();
if ($hookProperty === null) {
return null;
}

return $hookProperty->getHook('get') === $betterReflection ? 'get' : 'set';
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
66 changes: 57 additions & 9 deletions src/Rules/Constants/OverridingConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function processNode(Node $node, Scope $scope): array
$errors = array_merge(
$errors,
$this->overrideAttributeCheck->check($scope, $node, $constantName, $prototype),
$this->processSingleConstant($scope->getClassReflection(), $constantName, $prototype),
$this->processSingleConstant($scope->getClassReflection(), $constantName),
);
}

Expand All @@ -59,13 +59,21 @@ public function processNode(Node $node, Scope $scope): array
/**
* @return list<IdentifierRuleError>
*/
private function processSingleConstant(ClassReflection $classReflection, string $constantName, ?ClassConstantReflection $prototype): array
private function processSingleConstant(ClassReflection $classReflection, string $constantName): array
{
if ($prototype === null) {
return [];
$errors = [];
foreach ($this->findPrototypes($classReflection, $constantName) as $prototype) {
$errors = array_merge($errors, $this->processPrototype($classReflection, $classReflection->getConstant($constantName), $prototype));
}

$constantReflection = $classReflection->getConstant($constantName);
return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function processPrototype(ClassReflection $classReflection, ClassConstantReflection $constantReflection, ClassConstantReflection $prototype): array
{
$errors = [];
if ($prototype->isFinal()) {
$errors[] = RuleErrorBuilder::message(sprintf(
Expand Down Expand Up @@ -98,10 +106,6 @@ private function processSingleConstant(ClassReflection $classReflection, string
))->identifier('classConstant.visibility')->nonIgnorable()->build();
}

if (!$this->checkPhpDocMethodSignatures) {
return $errors;
}

$prototypeNativeType = $prototype->getNativeType();
$constantNativeType = $constantReflection->getNativeType();
if ($prototypeNativeType !== null) {
Expand Down Expand Up @@ -130,6 +134,10 @@ private function processSingleConstant(ClassReflection $classReflection, string
}
}

if (!$this->checkPhpDocMethodSignatures) {
return $errors;
}

if (!$prototype->hasPhpDocType()) {
return $errors;
}
Expand All @@ -153,4 +161,44 @@ private function processSingleConstant(ClassReflection $classReflection, string
return $errors;
}

/**
* Every constant the declared constant has to be compatible with: the parent class constant,
* and the constants of interfaces the parent class does not already implement.
*
* @return list<ClassConstantReflection>
*/
private function findPrototypes(ClassReflection $classReflection, string $constantName): array
{
$prototypes = [];
$parentClass = $classReflection->getParentClass();
$parentInterfaces = $parentClass !== null ? $parentClass->getInterfaces() : [];
foreach ($classReflection->getInterfaces() as $interfaceName => $interface) {
if (isset($parentInterfaces[$interfaceName])) {
continue;
}
if (!$interface->hasConstant($constantName)) {
continue;
}

$constant = $interface->getConstant($constantName);
if ($constant->getDeclaringClass()->getName() !== $interface->getName()) {
// inherited from a parent interface which is checked separately
continue;
}

$prototypes[] = $constant;
}

if ($parentClass === null || !$parentClass->hasConstant($constantName)) {
return $prototypes;
}

$constant = $parentClass->getConstant($constantName);
if (!$constant->isPrivate()) {
$prototypes[] = $constant;
}

return $prototypes;
}

}
Loading
Loading