-
Notifications
You must be signed in to change notification settings - Fork 12
/
UselessPrivatePropertyNullabilityRule.php
119 lines (92 loc) 路 3.83 KB
/
UselessPrivatePropertyNullabilityRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php declare(strict_types = 1);
namespace ShipMonk\PHPStan\Rule;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertiesNode;
use PHPStan\Node\Property\PropertyWrite;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NullType;
use PHPStan\Type\TypeCombinator;
use ShipMonk\PHPStan\Visitor\ClassPropertyAssignmentVisitor;
/**
* @implements Rule<ClassPropertiesNode>
*/
class UselessPrivatePropertyNullabilityRule implements Rule
{
public function getNodeType(): string
{
return ClassPropertiesNode::class;
}
/**
* @param ClassPropertiesNode $node
* @return list<IdentifierRuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return [];
}
$className = $classReflection->getName();
$nullabilityNeeded = [];
foreach ($node->getPropertyUsages() as $propertyUsage) {
if (!$propertyUsage instanceof PropertyWrite) {
continue;
}
$fetch = $propertyUsage->getFetch();
if ($fetch->name instanceof Expr) {
continue;
}
$propertyName = $fetch->name->toString();
/** @var Expr|null $assignedExpr */
$assignedExpr = $fetch->getAttribute(ClassPropertyAssignmentVisitor::ASSIGNED_EXPR);
if ($assignedExpr === null) { // cases like object->array[] = value etc
continue;
}
$nullType = new NullType();
$assignedType = $propertyUsage->getScope()->getType($assignedExpr);
if ($assignedType->accepts($nullType, $scope->isDeclareStrictTypes())->yes()) {
$nullabilityNeeded[$propertyName] = true;
}
}
[$uninitializedProperties] = $node->getUninitializedProperties($scope, $this->getConstructors($classReflection));
$errors = [];
foreach ($node->getProperties() as $property) {
$shouldBeChecked = ($property->isPrivate() || $property->isReadOnly()) && !$property->isPromoted();
if (!$shouldBeChecked) {
continue;
}
$propertyName = $property->getName();
$defaultValueNode = $property->getDefault();
$propertyReflection = $classReflection->getProperty($propertyName, $scope);
$definitionHasTypehint = $property->getNativeType() !== null;
$definitionIsNullable = TypeCombinator::containsNull($propertyReflection->getWritableType());
$nullIsAssigned = $nullabilityNeeded[$propertyName] ?? false;
$hasNullDefaultValue = $defaultValueNode instanceof ConstFetch && $scope->resolveName($defaultValueNode->name) === 'null';
$isUninitialized = isset($uninitializedProperties[$propertyName]);
if ($definitionHasTypehint && $definitionIsNullable && !$nullIsAssigned && !$hasNullDefaultValue && !$isUninitialized) {
$errors[] = RuleErrorBuilder::message("Property {$className}::{$propertyName} is defined as nullable, but null is never assigned")
->line($property->getLine())
->identifier('shipmonk.uselessPrivatePropertyNullability')
->build();
}
}
return $errors;
}
/**
* @return list<string>
*/
private function getConstructors(ClassReflection $classReflection): array
{
$constructors = [];
if ($classReflection->hasConstructor()) {
$constructors[] = $classReflection->getConstructor()->getName();
}
return $constructors;
}
}