Skip to content

Commit 7c119ff

Browse files
committed
property_exists does not need to narrow non-object to object|class-string
1 parent ea44c48 commit 7c119ff

2 files changed

Lines changed: 3 additions & 16 deletions

File tree

src/Type/Php/PropertyExistsTypeSpecifyingExtension.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
use PHPStan\Reflection\FunctionReflection;
1616
use PHPStan\Rules\Properties\PropertyReflectionFinder;
1717
use PHPStan\Type\Accessory\HasPropertyType;
18-
use PHPStan\Type\ClassStringType;
1918
use PHPStan\Type\Constant\ConstantBooleanType;
2019
use PHPStan\Type\FunctionTypeSpecifyingExtension;
2120
use PHPStan\Type\IntersectionType;
2221
use PHPStan\Type\ObjectWithoutClassType;
23-
use PHPStan\Type\UnionType;
2422
use function count;
2523

2624
#[AutowiredService]
@@ -80,18 +78,7 @@ public function specifyTypes(
8078
}
8179

8280
if (!$objectOrStringType->isObject()->yes()) {
83-
return $this->typeSpecifier->create(
84-
$args[0]->value,
85-
new UnionType([
86-
new IntersectionType([
87-
new ObjectWithoutClassType(),
88-
new HasPropertyType($propertyNameType->getValue()),
89-
]),
90-
new ClassStringType(),
91-
]),
92-
$context,
93-
$scope,
94-
);
81+
return new SpecifiedTypes([], []);
9582
}
9683

9784
$propertyNode = new PropertyFetch(

tests/PHPStan/Analyser/nsrt/bug-2861.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
function testObjectOrString($objectOrClass): void {
1111
if (property_exists($objectOrClass, 'foo')) {
12-
assertType('class-string|(object&hasProperty(foo))', $objectOrClass);
12+
assertType('object|string', $objectOrClass);
1313
}
1414
}
1515

@@ -18,6 +18,6 @@ function testObjectOrString($objectOrClass): void {
1818
*/
1919
function testObjectOrClassString($objectOrClass): void {
2020
if (property_exists($objectOrClass, 'bar')) {
21-
assertType('class-string|(object&hasProperty(bar))', $objectOrClass);
21+
assertType('class-string|object', $objectOrClass);
2222
}
2323
}

0 commit comments

Comments
 (0)