|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Properties; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\InPropertyHookNode; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use PHPStan\ShouldNotHappenException; |
| 11 | +use PHPStan\Type\VerbosityLevel; |
| 12 | +use function count; |
| 13 | +use function sprintf; |
| 14 | + |
| 15 | +/** |
| 16 | + * @implements Rule<InPropertyHookNode> |
| 17 | + */ |
| 18 | +final class SetPropertyHookParameterRule implements Rule |
| 19 | +{ |
| 20 | + |
| 21 | + public function __construct(private bool $checkPhpDocMethodSignatures) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + public function getNodeType(): string |
| 26 | + { |
| 27 | + return InPropertyHookNode::class; |
| 28 | + } |
| 29 | + |
| 30 | + public function processNode(Node $node, Scope $scope): array |
| 31 | + { |
| 32 | + $hookReflection = $node->getHookReflection(); |
| 33 | + if (!$hookReflection->isPropertyHook()) { |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + if ($hookReflection->getPropertyHookName() !== 'set') { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + $propertyReflection = $node->getPropertyReflection(); |
| 42 | + $parameters = $hookReflection->getParameters(); |
| 43 | + if (!isset($parameters[0])) { |
| 44 | + throw new ShouldNotHappenException(); |
| 45 | + } |
| 46 | + |
| 47 | + $classReflection = $node->getClassReflection(); |
| 48 | + |
| 49 | + $errors = []; |
| 50 | + $parameter = $parameters[0]; |
| 51 | + if (!$propertyReflection->hasNativeType()) { |
| 52 | + if ($parameter->hasNativeType()) { |
| 53 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 54 | + 'Parameter $%s of set hook has a native type but the property %s::$%s does not.', |
| 55 | + $parameter->getName(), |
| 56 | + $classReflection->getDisplayName(), |
| 57 | + $hookReflection->getHookedPropertyName(), |
| 58 | + ))->identifier('propertySetHook.nativeParameterType') |
| 59 | + ->nonIgnorable() |
| 60 | + ->build(); |
| 61 | + } |
| 62 | + } elseif (!$parameter->hasNativeType()) { |
| 63 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 64 | + 'Parameter $%s of set hook does not have a native type but the property %s::$%s does.', |
| 65 | + $parameter->getName(), |
| 66 | + $classReflection->getDisplayName(), |
| 67 | + $hookReflection->getHookedPropertyName(), |
| 68 | + ))->identifier('propertySetHook.nativeParameterType') |
| 69 | + ->nonIgnorable() |
| 70 | + ->build(); |
| 71 | + } else { |
| 72 | + if (!$parameter->getNativeType()->isSuperTypeOf($propertyReflection->getNativeType())->yes()) { |
| 73 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 74 | + 'Native type %s of set hook parameter $%s is not contravariant with native type %s of property %s::$%s.', |
| 75 | + $parameter->getNativeType()->describe(VerbosityLevel::typeOnly()), |
| 76 | + $parameter->getName(), |
| 77 | + $propertyReflection->getNativeType()->describe(VerbosityLevel::typeOnly()), |
| 78 | + $classReflection->getDisplayName(), |
| 79 | + $hookReflection->getHookedPropertyName(), |
| 80 | + ))->identifier('propertySetHook.nativeParameterType') |
| 81 | + ->nonIgnorable() |
| 82 | + ->build(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!$this->checkPhpDocMethodSignatures || count($errors) > 0) { |
| 87 | + return $errors; |
| 88 | + } |
| 89 | + |
| 90 | + if (!$parameter->getType()->isSuperTypeOf($propertyReflection->getReadableType())->yes()) { |
| 91 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 92 | + 'Type %s of set hook parameter $%s is not contravariant with type %s of property %s::$%s.', |
| 93 | + $parameter->getType()->describe(VerbosityLevel::value()), |
| 94 | + $parameter->getName(), |
| 95 | + $propertyReflection->getReadableType()->describe(VerbosityLevel::value()), |
| 96 | + $classReflection->getDisplayName(), |
| 97 | + $hookReflection->getHookedPropertyName(), |
| 98 | + ))->identifier('propertySetHook.parameterType') |
| 99 | + ->build(); |
| 100 | + } |
| 101 | + |
| 102 | + return $errors; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments