Skip to content
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

emit UnevaluatedCode after exit or never returning functionlike #6793

Merged
merged 3 commits into from
Nov 1, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Original file line number Diff line number Diff line change
@@ -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) {
Original file line number Diff line number Diff line change
@@ -112,6 +112,8 @@ public static function analyze(

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

$context->has_returned = true;

return true;
}
}
39 changes: 39 additions & 0 deletions tests/ReturnTypeTest.php
Original file line number Diff line number Diff line change
@@ -1411,6 +1411,45 @@ function f(): object {
',
'error_message' => 'LessSpecificReturnStatement',
],
'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',
],
];
}
}