Skip to content

Commit 7a2e77f

Browse files
committed
first stab at allowing constant string unions in narrowing calls to property exists
1 parent 71071d4 commit 7a2e77f

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/Type/Php/PropertyExistsTypeSpecifyingExtension.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ public function specifyTypes(
5555
TypeSpecifierContext $context,
5656
): SpecifiedTypes
5757
{
58-
$propertyNameType = $scope->getType($node->getArgs()[1]->value);
59-
if (!$propertyNameType instanceof ConstantStringType) {
58+
$propertyNameTypes = $scope->getType($node->getArgs()[1]->value)->getConstantStrings();
59+
60+
if ($propertyNameTypes === []) {
6061
return $this->typeSpecifier->create(
6162
new FuncCall(new FullyQualified('property_exists'), $node->getRawArgs()),
6263
new ConstantBooleanType(true),
@@ -65,34 +66,41 @@ public function specifyTypes(
6566
);
6667
}
6768

68-
if ($propertyNameType->getValue() === '') {
69-
return new SpecifiedTypes([], []);
69+
$hasPropertyTypes = [];
70+
foreach ($propertyNameTypes as $propertyNameType) {
71+
$hasPropertyTypes[] = new HasPropertyType($propertyNameType->getValue());
7072
}
7173

7274
$objectType = $scope->getType($node->getArgs()[0]->value);
7375
if ($objectType instanceof ConstantStringType) {
7476
return new SpecifiedTypes([], []);
7577
} elseif ($objectType->isObject()->yes()) {
76-
$propertyNode = new PropertyFetch(
77-
$node->getArgs()[0]->value,
78-
new Identifier($propertyNameType->getValue()),
79-
);
78+
$propertyNodes = [];
79+
80+
foreach($propertyNameTypes as $propertyNameType) {
81+
$propertyNodes[] = new PropertyFetch(
82+
$node->getArgs()[0]->value,
83+
new Identifier($propertyNameType->getValue()),
84+
);
85+
}
8086
} else {
8187
return new SpecifiedTypes([], []);
8288
}
8389

84-
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($propertyNode, $scope);
85-
if ($propertyReflection !== null) {
86-
if (!$propertyReflection->isNative()) {
87-
return new SpecifiedTypes([], []);
90+
foreach($propertyNodes as $propertyNode) {
91+
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($propertyNode, $scope);
92+
if ($propertyReflection !== null) {
93+
if (!$propertyReflection->isNative()) {
94+
return new SpecifiedTypes([], []);
95+
}
8896
}
8997
}
9098

9199
return $this->typeSpecifier->create(
92100
$node->getArgs()[0]->value,
93101
new IntersectionType([
94102
new ObjectWithoutClassType(),
95-
new HasPropertyType($propertyNameType->getValue()),
103+
...$hasPropertyTypes,
96104
]),
97105
$context,
98106
$scope,

0 commit comments

Comments
 (0)