Skip to content
Permalink
Browse files

Fix #1592 - allow methods with a simple exit to have the same treatme…

…nt as those with a simple throw
  • Loading branch information...
muglug committed May 2, 2019
1 parent 0fe378a commit cacced782dcd57407395e1f31b3a79d4df558800
@@ -333,7 +333,7 @@ public static function verifyReturnType(
return null;
}
if (ScopeAnalyzer::onlyThrows($function_stmts)) {
if (ScopeAnalyzer::onlyThrowsOrExits($function_stmts)) {
// if there's a single throw statement, it's presumably an exception saying this method is not to be
// used
return null;
@@ -310,7 +310,7 @@ public static function getFinalControlActions(
*
* @return bool
*/
public static function onlyThrows(array $stmts)
public static function onlyThrowsOrExits(array $stmts)
{
if (empty($stmts)) {
return false;
@@ -319,15 +319,12 @@ public static function onlyThrows(array $stmts)
for ($i = count($stmts) - 1; $i >= 0; --$i) {
$stmt = $stmts[$i];
if ($stmt instanceof PhpParser\Node\Stmt\Throw_) {
if ($stmt instanceof PhpParser\Node\Stmt\Throw_
|| ($stmt instanceof PhpParser\Node\Stmt\Expression
&& $stmt->expr instanceof PhpParser\Node\Expr\Exit_)
) {
return true;
}
if ($stmt instanceof PhpParser\Node\Stmt\Nop) {
continue;
}
return false;
}
return false;
@@ -610,6 +610,25 @@ function g() {
return true;
}'
],
'allowThrowAndExitToOverrideReturnType' => [
'<?php
interface Foo {
public function doFoo(): int;
}
class Bar implements Foo {
public function doFoo(): int {
print "Error\n";
exit(1);
}
}
class Baz implements Foo {
public function doFoo(): int {
throw new \Exception("bad");
}
}',
],
];
}

0 comments on commit cacced7

Please sign in to comment.
You can’t perform that action at this time.