2017-12-28 01:49:36 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
class TryCatchTest extends TestCase
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
2017-12-28 01:49:36 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-12-28 01:49:36 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-12-28 01:49:36 +01:00
|
|
|
{
|
|
|
|
return [
|
2019-02-07 18:25:57 +01:00
|
|
|
'addThrowableInterfaceType' => [
|
2017-12-28 01:49:36 +01:00
|
|
|
'<?php
|
|
|
|
interface CustomThrowable {}
|
|
|
|
class CustomException extends Exception implements CustomThrowable {}
|
|
|
|
|
2017-12-28 20:40:28 +01:00
|
|
|
/** @psalm-suppress InvalidCatch */
|
2017-12-28 01:49:36 +01:00
|
|
|
try {
|
|
|
|
throw new CustomException("Bad");
|
|
|
|
} catch (CustomThrowable $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
}',
|
|
|
|
],
|
2019-02-07 18:25:57 +01:00
|
|
|
'rethrowInterfaceExceptionWithoutInvalidThrow' => [
|
2017-12-28 20:40:28 +01:00
|
|
|
'<?php
|
|
|
|
interface CustomThrowable {}
|
|
|
|
class CustomException extends Exception implements CustomThrowable {}
|
|
|
|
|
|
|
|
/** @psalm-suppress InvalidCatch */
|
|
|
|
try {
|
|
|
|
throw new CustomException("Bad");
|
|
|
|
} catch (CustomThrowable $e) {
|
|
|
|
throw $e;
|
|
|
|
}',
|
|
|
|
],
|
2017-12-28 01:49:36 +01:00
|
|
|
'tryCatchVar' => [
|
|
|
|
'<?php
|
|
|
|
try {
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
$worked = false;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$worked' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
2018-01-02 22:57:40 +01:00
|
|
|
'alwaysReturnsBecauseCatchDoesNothing' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function throws(): void {
|
2018-01-02 22:57:40 +01:00
|
|
|
throw new Exception("bad");
|
|
|
|
}
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-02 22:57:40 +01:00
|
|
|
try {
|
|
|
|
throws();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2017-12-28 20:40:28 +01:00
|
|
|
|
2018-01-02 22:57:40 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
],
|
2018-01-29 01:02:31 +01:00
|
|
|
'wheresTheCatch' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : bool {
|
|
|
|
try {
|
|
|
|
return true;
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() : bool {
|
|
|
|
try {
|
|
|
|
// do nothing
|
|
|
|
} finally {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'catchWithNoReturnButFinallyReturns' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : bool {
|
|
|
|
try {
|
|
|
|
if (rand(0, 1)) throw new Exception("bad");
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
// do nothing here either
|
|
|
|
} finally {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-03-03 19:20:41 +01:00
|
|
|
'stopAnalysisAfterBadTryIssue' => [
|
|
|
|
'<?php
|
|
|
|
$foo = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$a->bar();
|
|
|
|
} catch (\TypeError $e) {
|
|
|
|
$foo = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$foo) {}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_message' => [
|
|
|
|
'UndefinedGlobalVariable' => \Psalm\Config::REPORT_INFO,
|
|
|
|
'MixedMethodCall' => \Psalm\Config::REPORT_INFO,
|
|
|
|
],
|
|
|
|
],
|
2018-07-13 20:06:01 +02:00
|
|
|
'issetAfterTryCatch' => [
|
|
|
|
'<?php
|
|
|
|
function test(): string {
|
|
|
|
throw new Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$a = "foo";
|
|
|
|
$var = test();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($var)) {}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'issetAfterTryCatchWithCombinedType' => [
|
|
|
|
'<?php
|
|
|
|
function test(): string {
|
|
|
|
throw new Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$a = "foo";
|
|
|
|
$var = test();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$var = "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($var)) {}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
2019-01-06 18:39:18 +01:00
|
|
|
'noRedundantConditionsInFinally' => [
|
|
|
|
'<?php
|
|
|
|
function doThings(): void {}
|
|
|
|
function message(): string { return "message"; }
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
doThings();
|
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
$errors["field"] = message();
|
|
|
|
} catch (LengthException $e) {
|
|
|
|
$errors[rand(0,1) ? "field" : "field2"] = message();
|
|
|
|
} finally {
|
|
|
|
if (!empty($errors)) {
|
|
|
|
return $errors;
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-06 18:39:18 +01:00
|
|
|
],
|
2019-01-20 15:27:46 +01:00
|
|
|
'typeDoesNotContainTypeInCatch' => [
|
|
|
|
'<?php
|
|
|
|
function foo(bool $test, callable $bar): string {
|
|
|
|
try {
|
|
|
|
$bar();
|
|
|
|
|
|
|
|
if ($test) {
|
|
|
|
return "moo";
|
|
|
|
}
|
|
|
|
return "miau";
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
if ($test) {
|
|
|
|
return "moo";
|
|
|
|
}
|
|
|
|
return "miau";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-02-09 21:39:30 +01:00
|
|
|
'notAlwaysUndefinedVarInFinally' => [
|
|
|
|
'<?php
|
|
|
|
function maybeThrows() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
throw new UnexpectedValueException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function doTry() : void {
|
|
|
|
try {
|
|
|
|
maybeThrows();
|
|
|
|
return;
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
throw $exception;
|
|
|
|
} finally {
|
|
|
|
if (isset($exception)) {
|
|
|
|
echo "here";
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-09 21:39:30 +01:00
|
|
|
],
|
2019-05-15 03:22:29 +02:00
|
|
|
'noReturnInsideCatch' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return never-returns
|
|
|
|
*/
|
|
|
|
function example() : void {
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$str = "a";
|
|
|
|
} catch (Exception $e) {
|
|
|
|
example();
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
ord($str);',
|
2019-05-15 03:22:29 +02:00
|
|
|
],
|
2019-07-07 19:52:43 +02:00
|
|
|
'varSetInOnlyCatch' => [
|
|
|
|
'<?php
|
|
|
|
try {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
throw new \Exception("Gotcha!");
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$lastException = $e;
|
|
|
|
}
|
|
|
|
|
2019-07-07 20:01:33 +02:00
|
|
|
echo $lastException->getMessage();'
|
|
|
|
],
|
|
|
|
'varSetInOnlyCatchWithNull' => [
|
|
|
|
'<?php
|
|
|
|
$lastException = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
throw new \Exception("Gotcha!");
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$lastException = $e;
|
|
|
|
}
|
|
|
|
|
2019-07-07 19:52:43 +02:00
|
|
|
echo $lastException->getMessage();'
|
|
|
|
],
|
2017-12-28 20:40:28 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-12-28 20:40:28 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2017-12-28 20:40:28 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'invalidCatchClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
try {
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
catch (A $e) {}',
|
|
|
|
'error_message' => 'InvalidCatch',
|
|
|
|
],
|
|
|
|
'invalidThrowClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
throw new A();',
|
|
|
|
'error_message' => 'InvalidThrow',
|
|
|
|
],
|
2018-01-29 01:02:31 +01:00
|
|
|
'theresNoCatch' => [
|
|
|
|
'<?php
|
|
|
|
function missing_return() : bool {
|
|
|
|
try {
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
|
|
|
'catchDoesNotReturn' => [
|
|
|
|
'<?php
|
|
|
|
function missing_return() : bool {
|
|
|
|
try {
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
|
|
|
'catchWithNoReturnAndFinallyDoesNotReturn' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : bool {
|
|
|
|
try {
|
|
|
|
if (rand(0, 1)) throw new Exception("bad");
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
// do nothing here either
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
|
|
|
'catchWithNoReturnAndNoFinally' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : bool {
|
|
|
|
try {
|
|
|
|
if (rand(0, 1)) throw new Exception("bad");
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
// do nothing here either
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
2017-12-28 01:49:36 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|