File tree Expand file tree Collapse file tree 3 files changed +105
-0
lines changed
tests/PHPStan/Analyser/nsrt Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug10055 ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ /**
8+ * @param 'value1'|'value2'|'value3' $param1
9+ * @param ($param1 is 'value3' ? bool : int) $param2
10+ */
11+ function test (string $ param1 , $ param2 ): void
12+ {
13+ if ($ param1 === 'value1 ' ) {
14+ assertType ('int ' , $ param2 );
15+ }
16+ if ($ param1 === 'value2 ' ) {
17+ assertType ('int ' , $ param2 );
18+ }
19+ if ($ param1 === 'value3 ' ) {
20+ assertType ('bool ' , $ param2 );
21+ }
22+
23+ match ($ param1 ) {
24+ 'value1 ' => assertType ('int ' , $ param2 ),
25+ 'value2 ' => assertType ('int ' , $ param2 ),
26+ 'value3 ' => assertType ('bool ' , $ param2 ),
27+ };
28+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug10422 ;
4+
5+ use stdClass ;
6+ use function PHPStan \Testing \assertType ;
7+
8+ class Foo
9+ {
10+
11+ public function other (): bool
12+ {
13+ return true ;
14+ }
15+
16+ public function test (): void
17+ {
18+ }
19+
20+ }
21+
22+ function createOrNotObject (): ?Foo
23+ {
24+ return new Foo ();
25+ }
26+
27+ function testSimple (): void
28+ {
29+ $ test = createOrNotObject ();
30+
31+ $ error = '' ;
32+ if (!$ test ) {
33+ $ error = 'yes ' ;
34+ }
35+ if ($ error ) {
36+ return ;
37+ }
38+ assertType (Foo::class, $ test );
39+ }
40+
41+ function testWithElseIf (): void
42+ {
43+ $ test = createOrNotObject ();
44+
45+ $ error = '' ;
46+ if (!$ test ) {
47+ $ error = 'yes ' ;
48+ } else if ($ test ->other ()) {
49+ $ error = 'yes ' ;
50+ }
51+ if ($ error ) {
52+ return ;
53+ }
54+ assertType (Foo::class . '|null ' , $ test ); // Should be Foo, see https://github.com/phpstan/phpstan/issues/10422
55+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug13591 ;
4+
5+ use InvalidArgumentException ;
6+ use function PHPStan \Testing \assertType ;
7+
8+ class HelloWorld
9+ {
10+
11+ public function test (string $ action , ?int $ hotelId ): void
12+ {
13+ if ($ action === 'get_rooms ' && $ hotelId === null ) {
14+ throw new InvalidArgumentException ('Hotel ID is required ' );
15+ }
16+
17+ if ($ action === 'get_rooms ' ) {
18+ assertType ('int ' , $ hotelId );
19+ }
20+ }
21+
22+ }
You can’t perform that action at this time.
0 commit comments