Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bae03bd

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

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-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/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,12 @@ public function testBug13310(): void
15411541
$this->assertNoErrors($errors);
15421542
}
15431543

1544+
public function testBug13511(): void
1545+
{
1546+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-13511.php');
1547+
$this->assertNoErrors($errors);
1548+
}
1549+
15441550
/**
15451551
* @param string[]|null $allAnalysedFiles
15461552
* @return Error[]
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)

AltStyle によって変換されたページ (->オリジナル) /