|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\TooWideTypehints; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\ClassPropertiesNode; |
| 8 | +use PHPStan\Reflection\PropertyReflection; |
| 9 | +use PHPStan\Rules\Properties\PropertyReflectionFinder; |
| 10 | +use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider; |
| 11 | +use PHPStan\Rules\Rule; |
| 12 | +use PHPStan\Rules\RuleErrorBuilder; |
| 13 | +use PHPStan\Type\NullType; |
| 14 | +use PHPStan\Type\TypeCombinator; |
| 15 | +use PHPStan\Type\UnionType; |
| 16 | +use PHPStan\Type\VerbosityLevel; |
| 17 | +use function count; |
| 18 | +use function sprintf; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Rule<ClassPropertiesNode> |
| 22 | + */ |
| 23 | +final class TooWidePropertyTypeRule implements Rule |
| 24 | +{ |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private ReadWritePropertiesExtensionProvider $extensionProvider, |
| 28 | + private PropertyReflectionFinder $propertyReflectionFinder, |
| 29 | + ) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + public function getNodeType(): string |
| 34 | + { |
| 35 | + return ClassPropertiesNode::class; |
| 36 | + } |
| 37 | + |
| 38 | + public function processNode(Node $node, Scope $scope): array |
| 39 | + { |
| 40 | + $errors = []; |
| 41 | + $classReflection = $node->getClassReflection(); |
| 42 | + |
| 43 | + foreach ($node->getProperties() as $property) { |
| 44 | + if (!$property->isPrivate()) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + if ($property->isDeclaredInTrait()) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + if ($property->isPromoted()) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + $propertyName = $property->getName(); |
| 54 | + if (!$classReflection->hasNativeProperty($propertyName)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + $propertyReflection = $classReflection->getNativeProperty($propertyName); |
| 59 | + $propertyType = $propertyReflection->getWritableType(); |
| 60 | + if (!$propertyType instanceof UnionType) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + foreach ($this->extensionProvider->getExtensions() as $extension) { |
| 64 | + if ($extension->isAlwaysWritten($propertyReflection, $propertyName)) { |
| 65 | + continue 2; |
| 66 | + } |
| 67 | + if ($extension->isInitialized($propertyReflection, $propertyName)) { |
| 68 | + continue 2; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $assignedTypes = []; |
| 73 | + foreach ($node->getPropertyAssigns() as $assign) { |
| 74 | + $assignNode = $assign->getAssign(); |
| 75 | + $assignPropertyReflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($assignNode->getPropertyFetch(), $assign->getScope()); |
| 76 | + foreach ($assignPropertyReflections as $assignPropertyReflection) { |
| 77 | + if ($propertyName !== $assignPropertyReflection->getName()) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + if ($propertyReflection->getDeclaringClass()->getName() !== $assignPropertyReflection->getDeclaringClass()->getName()) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + $assignedTypes[] = $assignPropertyReflection->getScope()->getType($assignNode->getAssignedExpr()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if ($property->getDefault() !== null) { |
| 89 | + $assignedTypes[] = $scope->getType($property->getDefault()); |
| 90 | + } |
| 91 | + |
| 92 | + if (count($assignedTypes) === 0) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $assignedType = TypeCombinator::union(...$assignedTypes); |
| 97 | + $propertyDescription = $this->describePropertyByName($propertyReflection, $propertyName); |
| 98 | + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $assignedType); |
| 99 | + foreach ($propertyType->getTypes() as $type) { |
| 100 | + if (!$type->isSuperTypeOf($assignedType)->no()) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if ($property->getNativeType() === null && (new NullType())->isSuperTypeOf($type)->yes()) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 109 | + '%s (%s) is never assigned %s so it can be removed from the property type.', |
| 110 | + $propertyDescription, |
| 111 | + $propertyType->describe($verbosityLevel), |
| 112 | + $type->describe($verbosityLevel), |
| 113 | + )) |
| 114 | + ->identifier('property.unusedType') |
| 115 | + ->line($property->getStartLine()) |
| 116 | + ->build(); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + return $errors; |
| 121 | + } |
| 122 | + |
| 123 | + private function describePropertyByName(PropertyReflection $property, string $propertyName): string |
| 124 | + { |
| 125 | + if (!$property->isStatic()) { |
| 126 | + return sprintf('Property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName); |
| 127 | + } |
| 128 | + |
| 129 | + return sprintf('Static property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments