Skip to content

Commit 0c408b3

Browse files
committed
fix: checking optional properties in object shapes
1 parent f6b6762 commit 0c408b3

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
@@ -176,15 +176,7 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
176176
try {
177177
$otherProperty = $type->getProperty($propertyName, $scope);
178178
} catch (MissingPropertyFromReflectionException) {
179-
return AcceptsResult::createNo(
180-
[
181-
sprintf(
182-
'%s does not have property $%s.',
183-
$type->describe(VerbosityLevel::typeOnly()),
184-
$propertyName,
185-
),
186-
],
187-
);
179+
continue;
188180
}
189181

190182
if (!$otherProperty->isPublic()) {
@@ -281,15 +273,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
281273
try {
282274
$otherProperty = $type->getProperty($propertyName, $scope);
283275
} catch (MissingPropertyFromReflectionException) {
284-
return IsSuperTypeOfResult::createNo(
285-
[
286-
sprintf(
287-
'%s does not have property $%s.',
288-
$type->describe(VerbosityLevel::typeOnly()),
289-
$propertyName,
290-
),
291-
],
292-
);
276+
continue;
293277
}
294278
if (!$otherProperty->isPublic()) {
295279
return IsSuperTypeOfResult::createNo();

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)