|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Constants; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Php\PhpVersion; |
| 8 | +use PHPStan\Reflection\ClassConstantReflection; |
| 9 | +use PHPStan\Reflection\ClassReflection; |
| 10 | +use PHPStan\Reflection\ConstantReflection; |
| 11 | +use PHPStan\Rules\Rule; |
| 12 | +use PHPStan\Rules\RuleError; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | +use PHPStan\Type\VerbosityLevel; |
| 15 | + |
| 16 | +/** |
| 17 | + * @implements Rule<Node\Stmt\ClassConst> |
| 18 | + */ |
| 19 | +class OverridingConstantRule implements Rule |
| 20 | +{ |
| 21 | + |
| 22 | + private PhpVersion $phpVersion; |
| 23 | + |
| 24 | + private bool $checkPhpDocMethodSignatures; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + PhpVersion $phpVersion, |
| 28 | + bool $checkPhpDocMethodSignatures |
| 29 | + ) |
| 30 | + { |
| 31 | + $this->phpVersion = $phpVersion; |
| 32 | + $this->checkPhpDocMethodSignatures = $checkPhpDocMethodSignatures; |
| 33 | + } |
| 34 | + |
| 35 | + public function getNodeType(): string |
| 36 | + { |
| 37 | + return Node\Stmt\ClassConst::class; |
| 38 | + } |
| 39 | + |
| 40 | + public function processNode(Node $node, Scope $scope): array |
| 41 | + { |
| 42 | + if (!$scope->isInClass()) { |
| 43 | + throw new \PHPStan\ShouldNotHappenException(); |
| 44 | + } |
| 45 | + |
| 46 | + $errors = []; |
| 47 | + foreach ($node->consts as $const) { |
| 48 | + $constantName = $const->name->toString(); |
| 49 | + $errors = array_merge($errors, $this->processSingleConstant($scope->getClassReflection(), $constantName)); |
| 50 | + } |
| 51 | + |
| 52 | + return $errors; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param string $constantName |
| 57 | + * @return RuleError[] |
| 58 | + */ |
| 59 | + private function processSingleConstant(ClassReflection $classReflection, string $constantName): array |
| 60 | + { |
| 61 | + $prototype = $this->findPrototype($classReflection, $constantName); |
| 62 | + if (!$prototype instanceof ClassConstantReflection) { |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + $constantReflection = $classReflection->getConstant($constantName); |
| 67 | + if (!$constantReflection instanceof ClassConstantReflection) { |
| 68 | + return []; |
| 69 | + } |
| 70 | + |
| 71 | + $errors = []; |
| 72 | + if ( |
| 73 | + $prototype->isFinal() |
| 74 | + || ( |
| 75 | + $this->phpVersion->isInterfaceConstantImplicitlyFinal() |
| 76 | + && $prototype->getDeclaringClass()->isInterface() |
| 77 | + ) |
| 78 | + ) { |
| 79 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 80 | + 'Constant %s::%s overrides final constant %s::%s.', |
| 81 | + $classReflection->getDisplayName(), |
| 82 | + $constantReflection->getName(), |
| 83 | + $prototype->getDeclaringClass()->getDisplayName(), |
| 84 | + $prototype->getName() |
| 85 | + ))->nonIgnorable()->build(); |
| 86 | + } |
| 87 | + |
| 88 | + if (!$this->checkPhpDocMethodSignatures) { |
| 89 | + return $errors; |
| 90 | + } |
| 91 | + |
| 92 | + if (!$prototype->hasPhpDocType()) { |
| 93 | + return $errors; |
| 94 | + } |
| 95 | + |
| 96 | + if (!$constantReflection->hasPhpDocType()) { |
| 97 | + return $errors; |
| 98 | + } |
| 99 | + |
| 100 | + if (!$prototype->getValueType()->isSuperTypeOf($constantReflection->getValueType())->yes()) { |
| 101 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 102 | + 'Type %s of constant %s::%s is not covariant with type %s of constant %s::%s.', |
| 103 | + $constantReflection->getValueType()->describe(VerbosityLevel::value()), |
| 104 | + $constantReflection->getDeclaringClass()->getDisplayName(), |
| 105 | + $constantReflection->getName(), |
| 106 | + $prototype->getValueType()->describe(VerbosityLevel::value()), |
| 107 | + $prototype->getDeclaringClass()->getDisplayName(), |
| 108 | + $prototype->getName() |
| 109 | + ))->build(); |
| 110 | + } |
| 111 | + |
| 112 | + return $errors; |
| 113 | + } |
| 114 | + |
| 115 | + private function findPrototype(ClassReflection $classReflection, string $constantName): ?ConstantReflection |
| 116 | + { |
| 117 | + foreach ($classReflection->getImmediateInterfaces() as $immediateInterface) { |
| 118 | + if ($immediateInterface->hasConstant($constantName)) { |
| 119 | + return $immediateInterface->getConstant($constantName); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + $parentClass = $classReflection->getParentClass(); |
| 124 | + if ($parentClass === false) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + if (!$parentClass->hasConstant($constantName)) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + $constant = $parentClass->getConstant($constantName); |
| 133 | + if ($constant->isPrivate()) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + return $constant; |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments