Skip to content

Commit 2ecc069

Browse files
committed
TooWideTypeCheck - do not report PHPDocs that are not subtype of native type
1 parent 0e695a4 commit 2ecc069

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/Rules/TooWideTypehints/TooWideTypeCheck.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function checkProperty(
8888
$boolMessagePattern = '%s (%s) is never assigned %%s so the property type can be changed to %%s.';
8989

9090
if (!$phpDocPropertyType instanceof MixedType || $phpDocPropertyType->isExplicitMixed()) {
91-
$phpDocPropertyType = TypeUtils::resolveLateResolvableTypes($phpDocPropertyType);
91+
$phpDocPropertyType = TypeUtils::resolveLateResolvableTypes(TypehintHelper::decideType($nativePropertyType, $phpDocPropertyType));
9292
$narrowedPhpDocType = $this->narrowType($phpDocPropertyType, $assignedType, $scope, false);
9393
if (!$narrowedPhpDocType->equals($phpDocPropertyType)) {
9494
$phpDocPropertyTypeDescription = $phpDocPropertyType->describe(VerbosityLevel::getRecommendedLevelByType($phpDocPropertyType));
@@ -178,7 +178,7 @@ public function checkFunctionReturnType(
178178
$boolMessagePattern = sprintf('%s never returns %%s so the return type can be changed to %%s.', $functionDescription);
179179

180180
if (!$phpDocFunctionReturnType instanceof MixedType || $phpDocFunctionReturnType->isExplicitMixed()) {
181-
$phpDocFunctionReturnType = TypeUtils::resolveLateResolvableTypes($phpDocFunctionReturnType);
181+
$phpDocFunctionReturnType = TypeUtils::resolveLateResolvableTypes(TypehintHelper::decideType($nativeFunctionReturnType, $phpDocFunctionReturnType));
182182

183183
// Do not require to have @return null/true/false in descendant classes
184184
if (

tests/PHPStan/Rules/TooWideTypehints/data/too-wide-property-type.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,16 @@ public function doFoo(): void
8585
}
8686

8787
}
88+
89+
class InvalidPhpDoc
90+
{
91+
92+
/** @var int|string */
93+
private int $a;
94+
95+
public function doFoo(): void
96+
{
97+
$this->a = 1;
98+
}
99+
100+
}

tests/PHPStan/Rules/TooWideTypehints/data/tooWideMethodReturnType-private.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,16 @@ private function conditionalType(int $flag)
123123

124124

125125
}
126+
127+
class InvalidPhpDoc
128+
{
129+
130+
/**
131+
* @return int|string
132+
*/
133+
private function doFoo(): int
134+
{
135+
return 1;
136+
}
137+
138+
}

0 commit comments

Comments
 (0)