Skip to content

Commit e21fbbd

Browse files
committed
Test type acceptance of optional property
1 parent e549a4b commit e21fbbd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,6 +2979,16 @@ public function testObjectShapes(): void
29792979
107,
29802980
'Property ($d) type array{foo: string} does not accept type array{foo: int}: Offset \'foo\' (string) does not accept type int.',
29812981
],
2982+
[
2983+
'Parameter #1 $o of method ObjectShapesAcceptance\OptionalProperty::doBar() expects object{foo?: int}, object{foo?: string} given.',
2984+
155,
2985+
'Property ($foo) type int does not accept type string.',
2986+
],
2987+
[
2988+
'Parameter #1 $o of method ObjectShapesAcceptance\OptionalProperty::doBaz() expects object{foo: int}, object{foo?: string} given.',
2989+
156,
2990+
'Property ($foo) type int does not accept type string.',
2991+
],
29822992
]);
29832993
}
29842994

tests/PHPStan/Rules/Methods/data/object-shapes.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,33 @@ public function doIpsum(object $o): void
143143
}
144144

145145
}
146+
147+
class OptionalProperty
148+
{
149+
150+
/**
151+
* @param object{foo?: string} $o
152+
*/
153+
public function doFoo(object $o): void
154+
{
155+
$this->doBar($o);
156+
$this->doBaz($o);
157+
}
158+
159+
/**
160+
* @param object{foo?: int} $o
161+
*/
162+
public function doBar(object $o): void
163+
{
164+
165+
}
166+
167+
/**
168+
* @param object{foo: int} $o
169+
*/
170+
public function doBaz(object $o): void
171+
{
172+
173+
}
174+
175+
}

0 commit comments

Comments
 (0)