Skip to content

Commit 5468ddd

Browse files
committed
Refactor TooWideTypeCheck - report errors based on narrowed original type
1 parent 7776279 commit 5468ddd

10 files changed

+255
-167
lines changed

src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function processNode(Node $node, Scope $scope): array
3131
{
3232
$function = $node->getFunctionReflection();
3333

34-
return $this->check->checkFunction(
34+
return $this->check->checkFunctionReturnType(
3535
$node,
36-
$function->getReturnType(),
36+
$function->getNativeReturnType(),
3737
$function->getPhpDocReturnType(),
3838
sprintf(
3939
'Function %s()',

src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public function processNode(Node $node, Scope $scope): array
4949
}
5050
}
5151

52-
return $this->check->checkFunction(
52+
return $this->check->checkFunctionReturnType(
5353
$node,
54-
$method->getReturnType(),
54+
$method->getNativeReturnType(),
5555
$method->getPhpDocReturnType(),
5656
sprintf(
5757
'Method %s::%s()',

src/Rules/TooWideTypehints/TooWideParameterOutTypeCheck.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
final class TooWideParameterOutTypeCheck
2020
{
2121

22+
public function __construct(
23+
private TooWideTypeCheck $tooWideTypeCheck,
24+
)
25+
{
26+
}
27+
2228
/**
2329
* @param list<ExecutionEndNode> $executionEnds
2430
* @param list<ReturnStatement> $returnStatements
@@ -86,35 +92,24 @@ private function processSingleParameter(
8692
$outType = $parameter->getType();
8793
}
8894

89-
$outType = TypeUtils::resolveLateResolvableTypes($outType);
90-
if (!$outType instanceof UnionType) {
91-
return [];
92-
}
93-
9495
$variableExpr = new Variable($parameter->getName());
9596
$variableType = $scope->getType($variableExpr);
9697

97-
$messages = [];
98-
foreach ($outType->getTypes() as $type) {
99-
if (!$type->isSuperTypeOf($variableType)->no()) {
100-
continue;
101-
}
102-
103-
$errorBuilder = RuleErrorBuilder::message(sprintf(
98+
/* sprintf(
10499
'%s never assigns %s to &$%s so it can be removed from the %s.',
105100
$functionDescription,
106101
$type->describe(VerbosityLevel::getRecommendedLevelByType($type)),
107102
$parameter->getName(),
108103
$isParamOutType ? '@param-out type' : 'by-ref type',
109-
))->identifier(sprintf('%s.unusedType', $isParamOutType ? 'paramOut' : 'parameterByRef'));
110-
if (!$isParamOutType) {
111-
$errorBuilder->tip('You can narrow the parameter out type with @param-out PHPDoc tag.');
112-
}
113-
114-
$messages[] = $errorBuilder->build();
115-
}
116-
117-
return $messages;
104+
) */
105+
106+
return $this->tooWideTypeCheck->checkParameterOutType(
107+
$outType,
108+
$variableType,
109+
$scope,
110+
$isParamOutType ? 'paramOut' : 'parameterByRef',
111+
$isParamOutType ? null : 'You can narrow the parameter out type with @param-out PHPDoc tag.',
112+
);
118113
}
119114

120115
}

src/Rules/TooWideTypehints/TooWidePropertyTypeRule.php

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
use PHPStan\DependencyInjection\RegisteredRule;
88
use PHPStan\Node\ClassPropertiesNode;
99
use PHPStan\Reflection\PropertyReflection;
10-
use PHPStan\Rules\Properties\PropertyReflectionFinder;
1110
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
1211
use PHPStan\Rules\Rule;
13-
use PHPStan\Type\TypeCombinator;
14-
use function count;
1512
use function sprintf;
1613

1714
/**
@@ -23,7 +20,6 @@ final class TooWidePropertyTypeRule implements Rule
2320

2421
public function __construct(
2522
private ReadWritePropertiesExtensionProvider $extensionProvider,
26-
private PropertyReflectionFinder $propertyReflectionFinder,
2723
private TooWideTypeCheck $check,
2824
)
2925
{
@@ -55,13 +51,6 @@ public function processNode(Node $node, Scope $scope): array
5551
}
5652

5753
$propertyReflection = $classReflection->getNativeProperty($propertyName);
58-
$propertyType = $propertyReflection->getWritableType();
59-
$phpdocType = $propertyReflection->getPhpDocType();
60-
61-
$propertyType = $this->check->findTypeToCheck($propertyType, $phpdocType, $scope);
62-
if ($propertyType === null) {
63-
continue;
64-
}
6554

6655
foreach ($this->extensionProvider->getExtensions() as $extension) {
6756
if ($extension->isAlwaysRead($propertyReflection, $propertyName)) {
@@ -75,33 +64,17 @@ public function processNode(Node $node, Scope $scope): array
7564
}
7665
}
7766

78-
$assignedTypes = [];
79-
foreach ($node->getPropertyAssigns() as $assign) {
80-
$assignNode = $assign->getAssign();
81-
$assignPropertyReflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($assignNode->getPropertyFetch(), $assign->getScope());
82-
foreach ($assignPropertyReflections as $assignPropertyReflection) {
83-
if ($propertyName !== $assignPropertyReflection->getName()) {
84-
continue;
85-
}
86-
if ($propertyReflection->getDeclaringClass()->getName() !== $assignPropertyReflection->getDeclaringClass()->getName()) {
87-
continue;
88-
}
89-
90-
$assignedTypes[] = $assignPropertyReflection->getScope()->getType($assignNode->getAssignedExpr());
91-
}
92-
}
93-
94-
if ($property->getDefault() !== null) {
95-
$assignedTypes[] = $scope->getType($property->getDefault());
96-
}
97-
98-
if (count($assignedTypes) === 0) {
99-
continue;
100-
}
101-
102-
$assignedType = TypeCombinator::union(...$assignedTypes);
10367
$propertyDescription = $this->describePropertyByName($propertyReflection, $propertyName);
104-
foreach ($this->check->checkProperty($property, $propertyType, $propertyDescription, $assignedType) as $error) {
68+
$propertyErrors = $this->check->checkProperty(
69+
$property,
70+
$propertyReflection->getDeclaringClass(),
71+
$node->getPropertyAssigns(),
72+
$propertyReflection->getNativeType(),
73+
$propertyReflection->getPhpDocType(),
74+
$propertyDescription,
75+
$scope,
76+
);
77+
foreach ($propertyErrors as $error) {
10578
$errors[] = $error;
10679
}
10780
}

0 commit comments

Comments
 (0)