File tree Expand file tree Collapse file tree 6 files changed +161
-0
lines changed
tests/PHPStan/Analyser/nsrt Expand file tree Collapse file tree 6 files changed +161
-0
lines changed Original file line number Diff line number Diff line change @@ -3609,6 +3609,15 @@ private function createConditionalExpressions(
36093609 }
36103610
36113611 foreach ($ variableTypeGuards as $ guardExprString => $ guardHolder ) {
3612+ if (
3613+ array_key_exists ($ exprString , $ theirExpressionTypes )
3614+ && $ theirExpressionTypes [$ exprString ]->getCertainty ()->yes ()
3615+ && array_key_exists ($ guardExprString , $ theirExpressionTypes )
3616+ && $ theirExpressionTypes [$ guardExprString ]->getCertainty ()->yes ()
3617+ && !$ guardHolder ->getType ()->isSuperTypeOf ($ theirExpressionTypes [$ guardExprString ]->getType ())->no ()
3618+ ) {
3619+ continue ;
3620+ }
36123621 $ conditionalExpression = new ConditionalExpressionHolder ([$ guardExprString => $ guardHolder ], $ holder );
36133622 $ conditionalExpressions [$ exprString ][$ conditionalExpression ->getKey ()] = $ conditionalExpression ;
36143623 }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Bug10085 ;
6+
7+ use function PHPStan \Testing \assertType ;
8+
9+ class HelloWorld
10+ {
11+ /**
12+ * @param array<int, string> $foo
13+ * @param list<string> $bar
14+ */
15+ public function sayHello (array $ foo , array $ bar ): void
16+ {
17+ $ a = $ foo ;
18+ if ($ a === []) {
19+ $ a = $ bar ;
20+ }
21+
22+ if ($ a === []) {
23+ return ;
24+ }
25+
26+ assertType ('array<int, string> ' , $ foo );
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.1
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Bug11328 ;
6+
7+ use function PHPStan \Testing \assertType ;
8+
9+ enum Status: string {
10+ case Live = 's1 ' ;
11+ case Expired = 's2 ' ;
12+ }
13+
14+ /** @param Status[] $statuses */
15+ function check (array $ statuses ): void {
16+ $ fromDeadline = null ;
17+ $ toDeadline = null ;
18+ if (in_array (Status::Live, $ statuses , true )) {
19+ $ fromDeadline = (new \DateTimeImmutable ())->setTime (23 , 59 , 59 );
20+ }
21+ if (in_array (Status::Expired, $ statuses , true )) {
22+ assertType ('DateTimeImmutable|null ' , $ fromDeadline );
23+ if ($ fromDeadline === null ) {
24+ $ toDeadline = (new \DateTimeImmutable ())->setTime (0 , 0 , 0 );
25+ }
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.0
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Bug14211 ;
6+
7+ use function PHPStan \Testing \assertType ;
8+
9+ /** @param array<mixed> $data */
10+ function DoSomithing (array $ data ): bool {
11+
12+ if (!isset ($ data ['x ' ]))
13+ return false ;
14+
15+ $ m = isset ($ data ['y ' ]);
16+
17+ if ($ m ) {
18+ assertType ('true ' , $ m ); // ok: true
19+ }
20+ assertType ('bool ' , $ m ); // ok: bool
21+
22+ if ($ m ) {
23+ assertType ('true ' , $ m ); // <-- should not be: NEVER
24+ }
25+
26+ return true ;
27+ }
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.0
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Bug14411Regression ;
6+
7+ use function PHPStan \Testing \assertType ;
8+ use function PHPStan \Testing \assertVariableCertainty ;
9+ use PHPStan \TrinaryLogic ;
10+
11+ interface OrderInterface {}
12+
13+ class Event
14+ {
15+ /** @return mixed */
16+ public function getSubject ()
17+ {
18+ return new \stdClass ();
19+ }
20+ }
21+
22+ function getOrder (Event |OrderInterface $ event ): OrderInterface
23+ {
24+ if ($ event instanceof Event) {
25+ $ order = $ event ->getSubject ();
26+ assert ($ order instanceof OrderInterface);
27+ }
28+
29+ if ($ event instanceof OrderInterface) {
30+ $ order = $ event ;
31+ }
32+
33+ assertVariableCertainty (TrinaryLogic::createYes (), $ order );
34+
35+ return $ order ;
36+ }
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.0
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Bug14411 ;
6+
7+ use function PHPStan \Testing \assertType ;
8+
9+ /** @phpstan-impure */
10+ function get_mixed (): mixed {
11+ return random_int (0 , 1 ) ? 'foo ' : null ;
12+ }
13+
14+ /** @phpstan-impure */
15+ function get_optional_int (): ?int {
16+ return random_int (0 , 1 ) ? 42 : null ;
17+ }
18+
19+ function (): void {
20+ $ a = get_mixed ();
21+
22+ if ($ a !== null ) {
23+ $ b = $ a ;
24+ }
25+ else {
26+ $ b = get_optional_int ();
27+ }
28+ if ($ b !== null ) {
29+ assertType ('mixed ' , $ a );
30+ if ($ a === null ) {
31+ echo 'this is absolutely possible ' ;
32+ }
33+ }
34+ };
You can’t perform that action at this time.
0 commit comments