-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix variable certainty for ArrayDimFetch in falsey isset context
- Loading branch information
Showing
11 changed files
with
379 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bug7291; | ||
|
||
use PHPStan\TrinaryLogic; | ||
use function PHPStan\Testing\assertType; | ||
use function PHPStan\Testing\assertVariableCertainty; | ||
|
||
class HelloWorld | ||
{ | ||
public function doFoo(): void | ||
{ | ||
if (rand(0, 1)) { | ||
$a = rand(0, 1) ? new \stdClass() : null; | ||
} | ||
|
||
assertType('stdClass|null', $a); | ||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
|
||
echo $a?->foo; | ||
|
||
assertType('stdClass|null', $a); | ||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace FalseyEmptyCertainty; | ||
|
||
use function PHPStan\Testing\assertVariableCertainty; | ||
use PHPStan\TrinaryLogic; | ||
|
||
function falseyEmptyArrayDimFetch(): void | ||
{ | ||
if (rand() % 2) { | ||
$a = ['bar' => null]; | ||
if (rand() % 3) { | ||
$a = ['bar' => 'hello']; | ||
} | ||
} | ||
|
||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
if (empty($a['bar'])) { | ||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
} else { | ||
assertVariableCertainty(TrinaryLogic::createYes(), $a); | ||
} | ||
|
||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
} | ||
|
||
function falseyEmptyUncertainPropertyFetch(): void | ||
{ | ||
if (rand() % 2) { | ||
$a = new \stdClass(); | ||
if (rand() % 3) { | ||
$a->x = 'hello'; | ||
} | ||
} | ||
|
||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
if (empty($a->x)) { | ||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
} else { | ||
assertVariableCertainty(TrinaryLogic::createYes(), $a); | ||
} | ||
|
||
assertVariableCertainty(TrinaryLogic::createMaybe(), $a); | ||
} | ||
|
||
function justEmpty(): void | ||
{ | ||
assertVariableCertainty(TrinaryLogic::createNo(), $foo); | ||
if (!empty($foo)) { | ||
assertVariableCertainty(TrinaryLogic::createNo(), $foo); | ||
} else { | ||
assertVariableCertainty(TrinaryLogic::createNo(), $foo); | ||
} | ||
assertVariableCertainty(TrinaryLogic::createNo(), $foo); | ||
} | ||
|
||
function maybeEmpty(): void | ||
{ | ||
if (rand() % 2) { | ||
$foo = 1; | ||
} | ||
|
||
assertVariableCertainty(TrinaryLogic::createMaybe(), $foo); | ||
if (!empty($foo)) { | ||
assertVariableCertainty(TrinaryLogic::createYes(), $foo); | ||
} else { | ||
assertVariableCertainty(TrinaryLogic::createMaybe(), $foo); | ||
} | ||
} |
Oops, something went wrong.