Skip to content

Commit e879fc9

Browse files
committed
Narrow native return types only by the native types of the returned values
A PHPDoc type is not checked at runtime: a getter of a property documented as string that a setter assigns null to, or a value narrowed by an inline @var tag, can still be null. The @return tags are narrowed by the PHPDoc types, the native type keeps the members the native types of the returned values need.
1 parent 60c17fd commit e879fc9

19 files changed

Lines changed: 494 additions & 7 deletions

‎src/Rules/TooWideTypehints/OverridableMemberHelper.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function findOverriddenPropertyHooks(ClassReflection $classReflection, st
127127
* @param string|null $usedMembersTip tip for the errors with %s for the used members, or void
128128
* @param list<string> $memberDescriptions the members of the type in $usedMembersTip
129129
* @param bool $ownOnly whether only the members the member uses itself are not reported, in an open project
130-
* @param array{plan: Plan, startPos: int, endPos: int}|null $fix the plan of the fix of the return type, see TooWideReturnTypeFixer
130+
* @param array{plan: Plan, nativeUsed: list<int>, startPos: int, endPos: int}|null $fix the plan of the fix of the return type, see TooWideReturnTypeFixer
131131
*/
132132
public function emitDeclaration(
133133
Scope&CollectedDataEmitter $scope,
@@ -175,7 +175,7 @@ public function emitDeclaration(
175175
*
176176
* @param list<int> $usedMembers
177177
* @param list<array{string, string, string|null}> $memberErrors
178-
* @param array{plan: Plan, startPos: int, endPos: int}|null $fix
178+
* @param array{plan: Plan, nativeUsed: list<int>, startPos: int, endPos: int}|null $fix
179179
*/
180180
public function emitReturnTypeDeclaration(
181181
Scope&CollectedDataEmitter $scope,

‎src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ private function emitDeclaration(MethodReturnStatementsNode $node, Scope&Collect
149149
$result[0],
150150
$result[1],
151151
$ownOnly,
152-
$plan !== null ? ['plan' => $plan, 'startPos' => $classMethod->getStartFilePos(), 'endPos' => $classMethod->getEndFilePos()] : null,
152+
$plan !== null ? [
153+
'plan' => $plan,
154+
'nativeUsed' => $this->check->getNativelyUsedReturnTypeMembers($node, $method, $result[0], $scope),
155+
'startPos' => $classMethod->getStartFilePos(),
156+
'endPos' => $classMethod->getEndFilePos(),
157+
] : null,
153158
);
154159
}
155160

‎src/Rules/TooWideTypehints/TooWideTypeCheck.php‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
use PHPStan\Type\VerbosityLevel;
3030
use PHPStan\Type\VoidType;
3131
use function array_keys;
32+
use function array_unique;
33+
use function array_values;
3234
use function count;
3335
use function in_array;
3436
use function lcfirst;
@@ -343,7 +345,7 @@ private function createReturnTypeFix(
343345
}
344346

345347
$usedMembers = $this->getUsedMembers($declaredType, $members, $returnType, false);
346-
$fix = $this->returnTypeFixer->createFix($plan, $usedMembers, $usedMembers);
348+
$fix = $this->returnTypeFixer->createFix($plan, $usedMembers, $this->getNativelyUsedMembers($node, $declaredType, $members, $usedMembers));
347349
if ($fix === null) {
348350
return null;
349351
}
@@ -472,10 +474,51 @@ private static function getOverlappingMembers(array $members, Type $type): array
472474
return $overlapping;
473475
}
474476

477+
/**
478+
* getNativelyUsedMembers() of a method whose errors createOverridableReturnTypeErrors() returned.
479+
*
480+
* @param list<int> $usedMembers
481+
* @return list<int>
482+
*/
483+
public function getNativelyUsedReturnTypeMembers(MethodReturnStatementsNode $node, ExtendedParametersAcceptor $methodVariant, array $usedMembers, Scope $scope): array
484+
{
485+
[$declaredType, $native] = $this->getCheckedReturnType($methodVariant);
486+
$members = $this->getRemovableMembers($declaredType, $scope, $native);
487+
if ($members === null) {
488+
return $usedMembers;
489+
}
490+
491+
return $this->getNativelyUsedMembers($node, $declaredType, $members, $usedMembers);
492+
}
493+
494+
/**
495+
* The members of the return type the native type of the function has to keep: those the native types
496+
* of the returned values overlap, like null of a property documented as string, and $usedMembers.
497+
* A PHPDoc type is not checked at runtime, a native return type narrowed by it could throw a TypeError.
498+
*
499+
* @param list<Type> $members
500+
* @param list<int> $usedMembers
501+
* @return list<int>
502+
*/
503+
private function getNativelyUsedMembers(
504+
MethodReturnStatementsNode|FunctionReturnStatementsNode $node,
505+
Type $declaredType,
506+
array $members,
507+
array $usedMembers,
508+
): array
509+
{
510+
$nativeReturnType = $this->getReturnedType($node, true);
511+
if ($nativeReturnType === null) {
512+
return $usedMembers;
513+
}
514+
515+
return array_values(array_unique([...$usedMembers, ...$this->getUsedMembers($declaredType, $members, $nativeReturnType, false)]));
516+
}
517+
475518
/**
476519
* Null when the function is a generator or it does not return anything.
477520
*/
478-
private function getReturnedType(MethodReturnStatementsNode|FunctionReturnStatementsNode $node): ?Type
521+
private function getReturnedType(MethodReturnStatementsNode|FunctionReturnStatementsNode $node, bool $native = false): ?Type
479522
{
480523
$statementResult = $node->getStatementResult();
481524
if ($statementResult->hasYield()) {
@@ -495,7 +538,7 @@ private function getReturnedType(MethodReturnStatementsNode|FunctionReturnStatem
495538
continue;
496539
}
497540

498-
$returnTypes[] = $returnStatement->getScope()->getType($returnNode->expr);
541+
$returnTypes[] = $native ? $returnStatement->getScope()->getNativeType($returnNode->expr) : $returnStatement->getScope()->getType($returnNode->expr);
499542
}
500543

501544
if (!$statementResult->isAlwaysTerminating()) {

‎src/Rules/TooWideTypehints/TooWideTypeInOverridableMemberCollector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* Not registered as a collector, the data is emitted with OverridableMemberHelper::emitDeclaration().
1414
*
15-
* @implements Collector<never, array{kind: string, class: string, member: string, slot: int, file: string|null, fileDescription: string|null, line: int, used: list<int>, errors: list<array{string, string, string|null}>, usedMembersTip: string|null, memberDescriptions: list<string>, ownOnly: bool, fix: array{plan: Plan, startPos: int, endPos: int}|null}>
15+
* @implements Collector<never, array{kind: string, class: string, member: string, slot: int, file: string|null, fileDescription: string|null, line: int, used: list<int>, errors: list<array{string, string, string|null}>, usedMembersTip: string|null, memberDescriptions: list<string>, ownOnly: bool, fix: array{plan: Plan, nativeUsed: list<int>, startPos: int, endPos: int}|null}>
1616
*/
1717
final class TooWideTypeInOverridableMemberCollector implements Collector
1818
{

‎src/Rules/TooWideTypehints/TooWideTypeInOverridableMemberRule.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function processNode(Node $node, Scope $scope): array
9797
$declaredByOverriding = $declaredByOverridingMembers[$key[0]][$key[1]][$key[2]][$key[3]] ?? [];
9898
$phpDocKept = $usedMembers + $usedByOverriding + ($declaredByOverriding[0] ?? []);
9999
$nativeKept = $phpDocKept + ($declaredByOverriding[1] ?? []);
100+
foreach ($declaration['fix']['nativeUsed'] as $i) {
101+
$nativeKept[$i] = true;
102+
}
100103
$fix = $this->fixer->createFix($declaration['fix']['plan'], array_keys($phpDocKept), array_keys($nativeKept));
101104
}
102105

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions-php73.php.fixed‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,42 @@ function nullablePhpDocAndNative(): string
5858
{
5959
return 'foo';
6060
}
61+
62+
final class PhpDocProperty
63+
{
64+
65+
/** @var string */
66+
private $name = 'foo';
67+
68+
public function setName(?string $name): void
69+
{
70+
$this->name = $name;
71+
}
72+
73+
public function getName(): ?string
74+
{
75+
return $this->name;
76+
}
77+
78+
/**
79+
* @return string
80+
*/
81+
public function getNameWithTag(): ?string
82+
{
83+
return $this->name;
84+
}
85+
86+
public function getNameNatively(): string
87+
{
88+
return (string) $this->name;
89+
}
90+
91+
}
92+
93+
function inlineVarTag(): ?\stdClass
94+
{
95+
/** @var \stdClass $object */
96+
$object = rand(0, 1) ? new \stdClass() : null;
97+
98+
return $object;
99+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions-php74.php.fixed‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,42 @@ function nullablePhpDocAndNative(): string
5858
{
5959
return 'foo';
6060
}
61+
62+
final class PhpDocProperty
63+
{
64+
65+
/** @var string */
66+
private $name = 'foo';
67+
68+
public function setName(?string $name): void
69+
{
70+
$this->name = $name;
71+
}
72+
73+
public function getName(): ?string
74+
{
75+
return $this->name;
76+
}
77+
78+
/**
79+
* @return string
80+
*/
81+
public function getNameWithTag(): ?string
82+
{
83+
return $this->name;
84+
}
85+
86+
public function getNameNatively(): string
87+
{
88+
return (string) $this->name;
89+
}
90+
91+
}
92+
93+
function inlineVarTag(): ?\stdClass
94+
{
95+
/** @var \stdClass $object */
96+
$object = rand(0, 1) ? new \stdClass() : null;
97+
98+
return $object;
99+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions-php82.php.fixed‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,42 @@ function nullablePhpDocAndNative(): string
5858
{
5959
return 'foo';
6060
}
61+
62+
final class PhpDocProperty
63+
{
64+
65+
/** @var string */
66+
private $name = 'foo';
67+
68+
public function setName(?string $name): void
69+
{
70+
$this->name = $name;
71+
}
72+
73+
public function getName(): ?string
74+
{
75+
return $this->name;
76+
}
77+
78+
/**
79+
* @return string
80+
*/
81+
public function getNameWithTag(): ?string
82+
{
83+
return $this->name;
84+
}
85+
86+
public function getNameNatively(): string
87+
{
88+
return (string) $this->name;
89+
}
90+
91+
}
92+
93+
function inlineVarTag(): ?\stdClass
94+
{
95+
/** @var \stdClass $object */
96+
$object = rand(0, 1) ? new \stdClass() : null;
97+
98+
return $object;
99+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,42 @@ function nullablePhpDocAndNative(): ?string
5858
{
5959
return 'foo';
6060
}
61+
62+
final class PhpDocProperty
63+
{
64+
65+
/** @var string */
66+
private $name = 'foo';
67+
68+
public function setName(?string $name): void
69+
{
70+
$this->name = $name;
71+
}
72+
73+
public function getName(): ?string
74+
{
75+
return $this->name;
76+
}
77+
78+
/**
79+
* @return string|null
80+
*/
81+
public function getNameWithTag(): ?string
82+
{
83+
return $this->name;
84+
}
85+
86+
public function getNameNatively(): ?string
87+
{
88+
return (string) $this->name;
89+
}
90+
91+
}
92+
93+
function inlineVarTag(): ?\stdClass
94+
{
95+
/** @var \stdClass $object */
96+
$object = rand(0, 1) ? new \stdClass() : null;
97+
98+
return $object;
99+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/methods-closed-project-php73.php.fixed‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,31 @@ final class NativeUnionChild extends NativeUnionParent
178178
}
179179

180180
}
181+
182+
class PhpDocPropertyParent
183+
{
184+
185+
/** @var string */
186+
protected $name = 'foo';
187+
188+
public function setName(?string $name): void
189+
{
190+
$this->name = $name;
191+
}
192+
193+
public function getName(): ?string
194+
{
195+
return $this->name;
196+
}
197+
198+
}
199+
200+
final class PhpDocPropertyChild extends PhpDocPropertyParent
201+
{
202+
203+
public function getName(): ?string
204+
{
205+
return 'child';
206+
}
207+
208+
}

0 commit comments

Comments
 (0)