Skip to content

Commit cdcb7bd

Browse files
Try
1 parent 8f4e3bc commit cdcb7bd

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ parameters:
14641464
-
14651465
rawMessage: 'Doing instanceof PHPStan\Type\ObjectShapeType is error-prone and deprecated. Use Type::isObject() and Type::hasProperty() instead.'
14661466
identifier: phpstanApi.instanceofType
1467-
count: 3
1467+
count: 2
14681468
path: src/Type/ObjectShapeType.php
14691469

14701470
-

src/Type/ObjectShapeType.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
1111
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1212
use PHPStan\Reflection\ClassMemberAccessAnswerer;
13+
use PHPStan\Reflection\Dummy\DummyPropertyReflection;
1314
use PHPStan\Reflection\ExtendedPropertyReflection;
1415
use PHPStan\Reflection\MissingPropertyFromReflectionException;
1516
use PHPStan\Reflection\Php\UniversalObjectCratesClassReflectionExtension;
@@ -113,15 +114,14 @@ public function getUnresolvedPropertyPrototype(string $propertyName, ClassMember
113114

114115
public function hasInstanceProperty(string $propertyName): TrinaryLogic
115116
{
116-
if (!array_key_exists($propertyName, $this->properties)) {
117-
return TrinaryLogic::createNo();
117+
if (
118+
array_key_exists($propertyName, $this->properties)
119+
&& !in_array($propertyName, $this->optionalProperties, true)
120+
) {
121+
return TrinaryLogic::createYes();
118122
}
119123

120-
if (in_array($propertyName, $this->optionalProperties, true)) {
121-
return TrinaryLogic::createMaybe();
122-
}
123-
124-
return TrinaryLogic::createYes();
124+
return TrinaryLogic::createMaybe();
125125
}
126126

127127
public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
@@ -131,11 +131,12 @@ public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswe
131131

132132
public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
133133
{
134-
if (!array_key_exists($propertyName, $this->properties)) {
135-
throw new ShouldNotHappenException();
134+
if (array_key_exists($propertyName, $this->properties)) {
135+
$property = new ObjectShapePropertyReflection($propertyName, $this->properties[$propertyName]);
136+
} else {
137+
$property = new DummyPropertyReflection($propertyName);
136138
}
137139

138-
$property = new ObjectShapePropertyReflection($propertyName, $this->properties[$propertyName]);
139140
return new CallbackUnresolvedPropertyPrototypeReflection(
140141
$property,
141142
$property->getDeclaringClass(),
@@ -297,10 +298,6 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
297298
foreach ($this->properties as $propertyName => $propertyType) {
298299
$hasProperty = new IsSuperTypeOfResult($type->hasInstanceProperty((string) $propertyName), []);
299300
if ($hasProperty->no()) {
300-
if ($type instanceof self) {
301-
$result = $result->and(IsSuperTypeOfResult::createMaybe());
302-
continue;
303-
}
304301
if (in_array($propertyName, $this->optionalProperties, true)) {
305302
continue;
306303
}

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,7 @@ public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLast
721721
public function testObjectShapes(): void
722722
{
723723
$this->treatPhpDocTypesAsCertain = true;
724-
$this->analyse([__DIR__ . '/data/property-exists-object-shapes.php'], [
725-
[
726-
'Call to function property_exists() with object{foo: int, bar?: string} and \'baz\' will always evaluate to false.',
727-
24,
728-
'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%</>.',
729-
],
730-
]);
724+
$this->analyse([__DIR__ . '/data/property-exists-object-shapes.php'], []);
731725
}
732726

733727
/** @return list<array{0: string, 1: int, 2?: string}> */

tests/PHPStan/Type/ObjectShapeTypeTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ public static function dataIsSuperTypeOf(): iterable
145145
];
146146
}
147147

148-
/**
149-
* @param TrinaryLogic $expectedResult
150-
*/
151148
#[DataProvider('dataIsSuperTypeOf')]
152149
public function testIsSuperTypeOf(ObjectShapeType $type, Type $otherType, TrinaryLogic $expectedResult): void
153150
{

0 commit comments

Comments
 (0)