Skip to content

Commit c4f1eb7

Browse files
committed
Fixes
1 parent fa7d4bc commit c4f1eb7

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

phpstan-baseline.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ parameters:
340340
count: 1
341341
path: src/Reflection/SignatureMap/Php8SignatureMapProvider.php
342342

343+
-
344+
message: """
345+
#^Call to deprecated method doNotTreatPhpDocTypesAsCertain\\(\\) of class PHPStan\\\\Analyser\\\\Scope\\:
346+
Use getNativeType\\(\\)$#
347+
"""
348+
count: 1
349+
path: src/Rules/Comparison/ImpossibleCheckTypeHelper.php
350+
343351
-
344352
message: "#^Function class_implements\\(\\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\\. Use objects retrieved from ReflectionProvider instead\\.$#"
345353
count: 1

src/Rules/Classes/ImpossibleInstanceOfRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ public function processNode(Node $node, Scope $scope): array
7474
};
7575

7676
if (!$instanceofType->getValue()) {
77+
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
78+
7779
return [
7880
$addTip(RuleErrorBuilder::message(sprintf(
7981
'Instanceof between %s and %s will always evaluate to false.',
80-
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
82+
$exprType->describe(VerbosityLevel::typeOnly()),
8183
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
8284
)))->build(),
8385
];
@@ -86,10 +88,12 @@ public function processNode(Node $node, Scope $scope): array
8688
return [];
8789
}
8890

91+
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
92+
8993
return [
9094
$addTip(RuleErrorBuilder::message(sprintf(
9195
'Instanceof between %s and %s will always evaluate to true.',
92-
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
96+
$exprType->describe(VerbosityLevel::typeOnly()),
9397
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
9498
)))->build(),
9599
];

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ public function findSpecifiedType(
187187
}
188188
}
189189

190-
$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $node, $this->determineContext($scope, $node));
190+
$typeSpecifierScope = $this->treatPhpDocTypesAsCertain ? $scope : $scope->doNotTreatPhpDocTypesAsCertain();
191+
$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($typeSpecifierScope, $node, $this->determineContext($typeSpecifierScope, $node));
191192

192193
// don't validate types on overwrite
193194
if ($specifiedTypes->shouldOverwrite()) {
@@ -199,7 +200,7 @@ public function findSpecifiedType(
199200

200201
$rootExpr = $specifiedTypes->getRootExpr();
201202
if ($rootExpr !== null) {
202-
if (self::isSpecified($scope, $node, $rootExpr)) {
203+
if (self::isSpecified($typeSpecifierScope, $node, $rootExpr)) {
203204
return null;
204205
}
205206

@@ -214,7 +215,7 @@ public function findSpecifiedType(
214215
$results = [];
215216

216217
foreach ($sureTypes as $sureType) {
217-
if (self::isSpecified($scope, $node, $sureType[0])) {
218+
if (self::isSpecified($typeSpecifierScope, $node, $sureType[0])) {
218219
$results[] = TrinaryLogic::createMaybe();
219220
continue;
220221
}
@@ -232,7 +233,7 @@ public function findSpecifiedType(
232233
}
233234

234235
foreach ($sureNotTypes as $sureNotType) {
235-
if (self::isSpecified($scope, $node, $sureNotType[0])) {
236+
if (self::isSpecified($typeSpecifierScope, $node, $sureNotType[0])) {
236237
$results[] = TrinaryLogic::createMaybe();
237238
continue;
238239
}

tests/PHPStan/Analyser/data/bug-7805.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ function foo(array $params)
2121
assertNativeType("array<mixed~'help', mixed>", $params);
2222
$params = $params === [] ? ['list'] : $params;
2323
assertType("array{'list'}", $params);
24-
assertNativeType("array{'list'}", $params);
25-
// assertNativeType("array<mixed~'help', mixed>", $params); // not working yet
24+
assertNativeType("non-empty-array<mixed~'help', mixed>", $params);
2625
array_unshift($params, 'help');
2726
assertType("array{'help', 'list'}", $params);
28-
assertNativeType("array{'list'}", $params);
29-
// assertNativeType("array<mixed~'help', mixed>", $params); // not working yet
27+
assertNativeType("non-empty-array<mixed~'help', mixed>", $params);
3028
}
3129
assertType("array{}|array{'help', 'list'}", $params);
3230
assertNativeType('array', $params);

tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ public function testInstanceof(): void
147147
'Instanceof between ImpossibleInstanceOf\Bar and ImpossibleInstanceOf\BarGrandChild will always evaluate to false.',
148148
322,
149149
],
150-
[
150+
/*[
151151
'Instanceof between mixed and int results in an error.',
152152
353,
153153
],
154154
[
155155
'Instanceof between mixed and ImpossibleInstanceOf\InvalidTypeTest|int results in an error.',
156156
362,
157-
],
157+
],*/
158158
[
159159
'Instanceof between ImpossibleInstanceOf\Foo and ImpossibleInstanceOf\Foo will always evaluate to true.',
160160
388,
@@ -173,6 +173,7 @@ public function testInstanceof(): void
173173
[
174174
'Instanceof between class-string<DateTimeInterface> and class-string<DateTimeInterface> will always evaluate to false.',
175175
419,
176+
$tipText,
176177
],
177178
[
178179
'Instanceof between class-string<DateTimeInterface> and \'DateTimeInterface\' will always evaluate to false.',
@@ -254,14 +255,14 @@ public function testInstanceofWithoutAlwaysTrue(): void
254255
'Instanceof between ImpossibleInstanceOf\Bar and ImpossibleInstanceOf\BarGrandChild will always evaluate to false.',
255256
322,
256257
],
257-
[
258+
/*[
258259
'Instanceof between mixed and int results in an error.',
259260
353,
260261
],
261262
[
262263
'Instanceof between mixed and ImpossibleInstanceOf\InvalidTypeTest|int results in an error.',
263264
362,
264-
],
265+
],*/
265266
[
266267
'Instanceof between T of Exception and Error will always evaluate to false.',
267268
404,
@@ -275,6 +276,7 @@ public function testInstanceofWithoutAlwaysTrue(): void
275276
[
276277
'Instanceof between class-string<DateTimeInterface> and class-string<DateTimeInterface> will always evaluate to false.',
277278
419,
279+
$tipText,
278280
],
279281
[
280282
'Instanceof between class-string<DateTimeInterface> and \'DateTimeInterface\' will always evaluate to false.',

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public function testBugInArrayDateFormat(): void
495495
[
496496
'Call to function in_array() with arguments \'b\', non-empty-array<int, \'a\'> and true will always evaluate to false.',
497497
43,
498-
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
498+
//'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
499499
],
500500
[
501501
'Call to function in_array() with arguments int, array{} and true will always evaluate to false.',

0 commit comments

Comments
 (0)