Skip to content

Commit

Permalink
Merge pull request #6793 from orklah/unevaluatedCode
Browse files Browse the repository at this point in the history
emit UnevaluatedCode after exit or never returning functionlike
  • Loading branch information
orklah authored Nov 1, 2021
2 parents 81ca05f + 500571b commit 96ae8e7
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public static function analyze(

$statements_analyzer->node_data->setType($real_stmt, $stmt_type);

if ($stmt_type->isNever()) {
$context->has_returned = true;
}

$event = new AfterEveryFunctionCallAnalysisEvent(
$stmt,
$function_call_info->function_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ function (?Type\Union $type_1, Type\Union $type_2) use ($codebase): Type\Union {

if ($stmt_type) {
$statements_analyzer->node_data->setType($stmt, $stmt_type);

if ($stmt_type->isNever()) {
$context->has_returned = true;
}
}

if ($result->returns_by_ref) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public static function analyze(

$statements_analyzer->node_data->setType($stmt, Type::getEmpty());

$context->has_returned = true;

return true;
}
}
2 changes: 0 additions & 2 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public function testGMPOperations()
{
if (class_exists('GMP') === false) {
$this->markTestSkipped('Cannot run test, base class "GMP" does not exist!');

return;
}

$this->addFile(
Expand Down
2 changes: 0 additions & 2 deletions tests/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public function testExtendsMysqli()
{
if (class_exists('mysqli') === false) {
$this->markTestSkipped('Cannot run test, base class "mysqli" does not exist!');

return;
}

$this->addFile(
Expand Down
2 changes: 0 additions & 2 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ public function testIgnoreSymlinkedProjectDirectory()

if (is_array($last_error) && $no_symlinking_error === $last_error['message']) {
$this->markTestSkipped($no_symlinking_error);

return;
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/EndToEnd/DestructiveAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public function testSucceedsWithEmptyFile(): void
{
if (\version_compare(\PHP_VERSION, '7.2.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.2.');

return;
}

$this->runPsalm(['--no-cache'], __DIR__ . '/' . '../fixtures/DestructiveAutoloader/', true);
Expand Down
2 changes: 0 additions & 2 deletions tests/EndToEnd/SuicidalAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public function testSucceedsWithEmptyFile(): void
{
if (\version_compare(\PHP_VERSION, '7.2.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.2.');

return;
}

$this->runPsalm(['--no-cache'], __DIR__ . '/' . '../fixtures/SuicidalAutoloader/');
Expand Down
2 changes: 0 additions & 2 deletions tests/MethodCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public function testExtendDocblockParamType()
{
if (class_exists('SoapClient') === false) {
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');

return;
}

$this->addFile(
Expand Down
10 changes: 0 additions & 10 deletions tests/MethodSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public function testExtendSoapClientWithDocblockTypes()
{
if (class_exists('SoapClient') === false) {
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');

return;
}

$this->addFile(
Expand Down Expand Up @@ -58,8 +56,6 @@ public function testExtendSoapClientWithNoDocblockTypes()
{
if (class_exists('SoapClient') === false) {
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');

return;
}

$this->addFile(
Expand Down Expand Up @@ -89,8 +85,6 @@ public function testExtendSoapClientWithParamType()
{
if (class_exists('SoapClient') === false) {
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');

return;
}

$this->addFile(
Expand Down Expand Up @@ -265,8 +259,6 @@ public function testExtendDocblockParamTypeWithWrongDocblockParam()
$this->expectException(\Psalm\Exception\CodeException::class);
if (class_exists('SoapClient') === false) {
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');

return;
}

$this->addFile(
Expand Down Expand Up @@ -304,8 +296,6 @@ public function testExtendDocblockParamTypeWithWrongParam() : void

if (class_exists('SoapClient') === false) {
$this->markTestSkipped('Cannot run test, base class "SoapClient" does not exist!');

return;
}

$this->addFile(
Expand Down
4 changes: 0 additions & 4 deletions tests/Traits/InvalidCodeAnalysisTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ public function testInvalidCode(
if (strpos($test_name, 'PHP71-') !== false) {
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.1.');

return;
}
} elseif (strpos($test_name, 'PHP80-') !== false) {
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->markTestSkipped('Test case requires PHP 8.0.');

return;
}
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
Expand Down
6 changes: 0 additions & 6 deletions tests/Traits/ValidCodeAnalysisTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,14 @@ public function testValidCode(
if (strpos($test_name, 'PHP73-') !== false) {
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.3.');

return;
}
} elseif (strpos($test_name, 'PHP71-') !== false) {
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.1.');

return;
}
} elseif (strpos($test_name, 'PHP80-') !== false) {
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->markTestSkipped('Test case requires PHP 8.0.');

return;
}
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
Expand Down
39 changes: 39 additions & 0 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,45 @@ function f(IWorker $worker): void {
',
'error_message' => 'UnusedFunctionCall',
],
'functionNeverUnevaluatedCode' => [
'<?php
/** @return never */
function neverReturns() {
die();
}
function f(): void {
neverReturns();
echo "hello";
}
',
'error_message' => 'UnevaluatedCode',
],
'methodNeverUnevaluatedCode' => [
'<?php
class A{
/** @return never */
function neverReturns() {
die();
}
function f(): void {
$this->neverReturns();
echo "hello";
}
}
',
'error_message' => 'UnevaluatedCode',
],
'exitNeverUnevaluatedCode' => [
'<?php
function f(): void {
exit();
echo "hello";
}
',
'error_message' => 'UnevaluatedCode',
],
];
}
}

0 comments on commit 96ae8e7

Please sign in to comment.