Skip to content

Commit a477323

Browse files
committed
Fix error message on level < 7
1 parent af6a9c5 commit a477323

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,6 @@ parameters:
601601
count: 1
602602
path: src/Rules/RuleLevelHelper.php
603603

604-
-
605-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectWithoutClassType is error\\-prone and deprecated\\. Use Type\\:\\:isObject\\(\\) instead\\.$#"
606-
count: 1
607-
path: src/Rules/RuleLevelHelper.php
608-
609604
-
610605
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
611606
count: 1

src/Rules/RuleLevelHelper.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\MixedType;
1515
use PHPStan\Type\NeverType;
1616
use PHPStan\Type\NullType;
17-
use PHPStan\Type\ObjectWithoutClassType;
1817
use PHPStan\Type\StrictMixedType;
1918
use PHPStan\Type\Type;
2019
use PHPStan\Type\TypeCombinator;
@@ -241,7 +240,7 @@ private function findTypeToCheckImplementation(
241240
return new FoundTypeResult(new ErrorType(), [], $errors, null);
242241
}
243242

244-
if (!$this->checkUnionTypes && $type instanceof ObjectWithoutClassType) {
243+
if (!$this->checkUnionTypes && $type->isObject()->yes() && count($type->getObjectClassNames()) === 0) {
245244
return new FoundTypeResult(new ErrorType(), [], [], null);
246245
}
247246

@@ -286,17 +285,25 @@ private function findTypeToCheckImplementation(
286285
if ($type instanceof IntersectionType) {
287286
$newTypes = [];
288287

288+
$changed = false;
289289
foreach ($type->getTypes() as $innerType) {
290-
$newTypes[] = $this->findTypeToCheckImplementation(
291-
$scope,
292-
$var,
293-
$innerType,
294-
$unknownClassErrorPattern,
295-
$unionTypeCriteriaCallback,
296-
)->getType();
290+
if ($innerType instanceof TemplateMixedType) {
291+
$changed = true;
292+
$newTypes[] = $this->findTypeToCheckImplementation(
293+
$scope,
294+
$var,
295+
$innerType->toStrictMixedType(),
296+
$unknownClassErrorPattern,
297+
$unionTypeCriteriaCallback,
298+
)->getType();
299+
continue;
300+
}
301+
$newTypes[] = $innerType;
297302
}
298303

299-
return new FoundTypeResult(TypeCombinator::intersect(...$newTypes), $directClassNames, [], null);
304+
if ($changed) {
305+
return new FoundTypeResult(TypeCombinator::intersect(...$newTypes), $directClassNames, [], null);
306+
}
300307
}
301308

302309
$tip = null;

tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ public function testAccessProperties(): void
169169
'Cannot access property $selfOrNull on TestAccessProperties\RevertNonNullabilityForIsset|null.',
170170
407,
171171
],
172+
[
173+
'Access to an undefined property object::$baz.',
174+
438,
175+
$tipText,
176+
],
172177
],
173178
);
174179
}

tests/PHPStan/Rules/Properties/data/access-properties.php

+17
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,20 @@ function mustNotReport(?\stdClass $nullable): bool
423423
}
424424

425425
}
426+
427+
class OnObjectAfterIsset
428+
{
429+
430+
/**
431+
* @param mixed $m
432+
*/
433+
public function doFoo($m): void
434+
{
435+
if (isset($m->foo) && isset($m->bar)) {
436+
echo $m->foo;
437+
echo $m->bar;
438+
echo $m->baz;
439+
}
440+
}
441+
442+
}

0 commit comments

Comments
 (0)