-
Notifications
You must be signed in to change notification settings - Fork 515
Fix missing detection of dead code in expressions #4090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
bda643b
Fix missing detection of dead code in expressions
staabm a3a6a49
Support method calls / static calls
staabm c2b3e7b
fix assignments
staabm a430885
fix regression
staabm 074877a
Fix NullsafeMethodCall
staabm 1707b28
fix build
staabm 6023250
test nullsafe
staabm 5bf7349
more tests
staabm 82c93d4
fix PHP 7.4
staabm 0cc9483
better names
staabm af69075
fix string interpolation
staabm 0610269
fix func args
staabm 0b29958
fix callables
staabm 0b642df
added regression test
staabm e7d00d1
fix arrays
staabm dfe77ba
fix method calls
staabm 95bb39d
ExpressionResult isAlwaysTerminating is consistent with StatementResult
staabm 4e0c9c0
Added ExpressionResultTest
staabm 89d5780
Add $isAlwaysTerminating to every node-type if-branch
staabm f18867b
fix build
staabm 4e00904
fix arrow-fn and suppress
staabm d0d52d9
implement more cases
staabm cc65f3b
Update NodeScopeResolver.php
staabm 70d67c2
Update NodeScopeResolver.php
staabm 24ff8fd
fix nullsafe
staabm ba52625
fix expectations
staabm 8b2806a
simplify ExpressionResultTest
staabm 15f2b01
add more tests
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,171 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PhpParser\Node\Stmt; | ||
use PHPStan\Parser\Parser; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Testing\PHPStanTestCase; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\IntegerType; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use function count; | ||
use function get_class; | ||
use function sprintf; | ||
|
||
class ExpressionResultTest extends PHPStanTestCase | ||
{ | ||
|
||
public static function dataIsAlwaysTerminating(): array | ||
{ | ||
return [ | ||
[ | ||
'sprintf("hello %s", "abc");', | ||
false, | ||
], | ||
[ | ||
'isset($x);', | ||
false, | ||
], | ||
[ | ||
'$x ? "def" : "abc";', | ||
false, | ||
], | ||
[ | ||
'(string) $x;', | ||
false, | ||
], | ||
[ | ||
'$x || exit();', | ||
false, | ||
], | ||
[ | ||
'$x ?? exit();', | ||
false, | ||
], | ||
[ | ||
'sprintf("hello %s", exit());', | ||
true, | ||
], | ||
[ | ||
'(string) exit();', | ||
true, | ||
], | ||
[ | ||
'!exit();', | ||
true, | ||
], | ||
[ | ||
'eval(exit());', | ||
true, | ||
], | ||
[ | ||
'empty(exit());', | ||
true, | ||
], | ||
[ | ||
'isset(exit());', | ||
true, | ||
], | ||
[ | ||
'$x ? "abc" : exit();', | ||
false, | ||
], | ||
[ | ||
'$x ? exit() : "abc";', | ||
false, | ||
], | ||
[ | ||
'fn() => yield (exit());', | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
false, | ||
], | ||
[ | ||
'@exit();', | ||
true, | ||
], | ||
[ | ||
'$x && exit();', | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
false, | ||
], | ||
[ | ||
'exit() && $x;', | ||
true, | ||
], | ||
[ | ||
'exit() || $x;', | ||
true, | ||
], | ||
[ | ||
'exit() ?? $x;', | ||
true, | ||
], | ||
[ | ||
'var_dump(1+exit());', | ||
true, | ||
], | ||
[ | ||
'var_dump(1-exit());', | ||
true, | ||
], | ||
[ | ||
'var_dump(1*exit());', | ||
true, | ||
], | ||
[ | ||
'var_dump(1**exit());', | ||
true, | ||
], | ||
[ | ||
'var_dump(1/exit());', | ||
true, | ||
], | ||
[ | ||
'var_dump("a".exit());', | ||
true, | ||
], | ||
[ | ||
'var_dump(exit()."a");', | ||
true, | ||
], | ||
]; | ||
} | ||
|
||
#[DataProvider('dataIsAlwaysTerminating')] | ||
public function testIsAlwaysTerminating( | ||
string $code, | ||
bool $expectedIsAlwaysTerminating, | ||
): void | ||
{ | ||
/** @var Parser $parser */ | ||
$parser = self::getContainer()->getService('currentPhpVersionRichParser'); | ||
|
||
/** @var Stmt[] $stmts */ | ||
$stmts = $parser->parseString(sprintf('<?php %s', $code)); | ||
if (count($stmts) !== 1) { | ||
throw new ShouldNotHappenException('Expecting code which evaluates to a single statement, got: ' . count($stmts)); | ||
} | ||
if (!$stmts[0] instanceof Stmt\Expression) { | ||
throw new ShouldNotHappenException('Expecting code contains a single statement expression, got: ' . get_class($stmts[0])); | ||
} | ||
$stmt = $stmts[0]; | ||
$expr = $stmt->expr; | ||
|
||
/** @var NodeScopeResolver $nodeScopeResolver */ | ||
$nodeScopeResolver = self::getContainer()->getByType(NodeScopeResolver::class); | ||
/** @var ScopeFactory $scopeFactory */ | ||
$scopeFactory = self::getContainer()->getByType(ScopeFactory::class); | ||
$scope = $scopeFactory->create(ScopeContext::create('test.php')) | ||
->assignVariable('x', new IntegerType(), new IntegerType(), TrinaryLogic::createYes()); | ||
|
||
$result = $nodeScopeResolver->processExprNode( | ||
$stmt, | ||
$expr, | ||
$scope, | ||
static function (): void { | ||
}, | ||
ExpressionContext::createTopLevel(), | ||
); | ||
$this->assertSame($expectedIsAlwaysTerminating, $result->isAlwaysTerminating()); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug11909; | ||
|
||
function doFoo(): never { | ||
throw new LogicException("throws"); | ||
} | ||
|
||
echo doFoo(); | ||
echo "hello"; |
This file contains hidden or 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,84 @@ | ||
<?php | ||
|
||
namespace Bug13232a; | ||
|
||
final class HelloWorld | ||
{ | ||
public function sayHa(): void | ||
{ | ||
echo sprintf("Hello, %s no way", $this->neverReturnsMethod()); | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayHi(): void | ||
{ | ||
echo 'Hello, ' . neverReturns() | ||
. ' no way'; | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayHo(): void | ||
{ | ||
echo "Hello, {$this->neverReturnsMethod()} no way"; | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayHe(): void | ||
{ | ||
$callable = function (): never { | ||
exit(); | ||
}; | ||
echo sprintf("Hello, %s no way", $callable()); | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayHe2(): void | ||
{ | ||
$this->doFoo($this->neverReturnsMethod()); | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayHe3(): void | ||
{ | ||
self::doStaticFoo($this->neverReturnsMethod()); | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayHuu(): void | ||
{ | ||
$x = [ | ||
$this->neverReturnsMethod() | ||
]; | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayClosure(): void | ||
{ | ||
$callable = function (): never { | ||
exit(); | ||
}; | ||
$callable(); | ||
echo 'this will never happen'; | ||
} | ||
|
||
public function sayIIFE(): void | ||
{ | ||
(function (): never { | ||
exit(); | ||
})(); | ||
|
||
echo 'this will never happen'; | ||
} | ||
|
||
function neverReturnsMethod(): never { | ||
exit(); | ||
} | ||
|
||
public function doFoo() {} | ||
|
||
static public function doStaticFoo() {} | ||
} | ||
function neverReturns(): never { | ||
exit(); | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.