Skip to content

Commit 330ca96

Browse files
calebdwondrejmirtes
authored andcommitted
fix: checking optional properties in object shapes
1 parent b17e969 commit 330ca96

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/Type/ObjectShapeType.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,7 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
212212
try {
213213
$otherProperty = $type->getInstanceProperty((string) $propertyName, $scope);
214214
} catch (MissingPropertyFromReflectionException) {
215-
return AcceptsResult::createNo(
216-
[
217-
sprintf(
218-
'%s does not have property $%s.',
219-
$type->describe(VerbosityLevel::typeOnly()),
220-
$propertyName,
221-
),
222-
],
223-
);
215+
continue;
224216
}
225217

226218
if (!$otherProperty->isPublic()) {
@@ -317,15 +309,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
317309
try {
318310
$otherProperty = $type->getInstanceProperty((string) $propertyName, $scope);
319311
} catch (MissingPropertyFromReflectionException) {
320-
return IsSuperTypeOfResult::createNo(
321-
[
322-
sprintf(
323-
'%s does not have property $%s.',
324-
$type->describe(VerbosityLevel::typeOnly()),
325-
$propertyName,
326-
),
327-
],
328-
);
312+
continue;
329313
}
330314

331315
if (!$otherProperty->isPublic()) {

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,4 +3666,13 @@ public function testBug3396(): void
36663666
$this->analyse([__DIR__ . '/data/bug-3396.php'], []);
36673667
}
36683668

3669+
public function testBug13511(): void
3670+
{
3671+
$this->checkThisOnly = false;
3672+
$this->checkNullables = true;
3673+
$this->checkUnionTypes = true;
3674+
$this->checkExplicitMixed = true;
3675+
$this->analyse([__DIR__ . '/data/bug-13511.php'], []);
3676+
}
3677+
36693678
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Bug13511;
4+
5+
class User
6+
{
7+
public function __construct(
8+
public string $name,
9+
public string $email,
10+
) {}
11+
}
12+
13+
class Printer
14+
{
15+
/** @param object{name: string, email: string, phone?: string} $object */
16+
function printInfo(object $object): void
17+
{
18+
return;
19+
}
20+
}
21+
22+
function (Printer $printer, User $user): void {
23+
$printer->printInfo($user);
24+
};

0 commit comments

Comments
 (0)