mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 12:55:26 +01:00
e419a8fe01
Fixes #550
186 lines
5.9 KiB
PHP
186 lines
5.9 KiB
PHP
<?php
|
|
namespace Psalm\Tests;
|
|
|
|
class TryCatchTest extends TestCase
|
|
{
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerValidCodeParse()
|
|
{
|
|
return [
|
|
'PHP7-addThrowableInterfaceType' => [
|
|
'<?php
|
|
interface CustomThrowable {}
|
|
class CustomException extends Exception implements CustomThrowable {}
|
|
|
|
/** @psalm-suppress InvalidCatch */
|
|
try {
|
|
throw new CustomException("Bad");
|
|
} catch (CustomThrowable $e) {
|
|
echo $e->getMessage();
|
|
}',
|
|
],
|
|
'PHP7-rethrowInterfaceExceptionWithoutInvalidThrow' => [
|
|
'<?php
|
|
interface CustomThrowable {}
|
|
class CustomException extends Exception implements CustomThrowable {}
|
|
|
|
/** @psalm-suppress InvalidCatch */
|
|
try {
|
|
throw new CustomException("Bad");
|
|
} catch (CustomThrowable $e) {
|
|
throw $e;
|
|
}',
|
|
],
|
|
'tryCatchVar' => [
|
|
'<?php
|
|
try {
|
|
$worked = true;
|
|
}
|
|
catch (\Exception $e) {
|
|
$worked = false;
|
|
}',
|
|
'assertions' => [
|
|
'$worked' => 'bool',
|
|
],
|
|
],
|
|
'alwaysReturnsBecauseCatchDoesNothing' => [
|
|
'<?php
|
|
function throws(): void {
|
|
throw new Exception("bad");
|
|
}
|
|
function foo(): string {
|
|
try {
|
|
throws();
|
|
} catch (Exception $e) {
|
|
// do nothing
|
|
}
|
|
|
|
return "hello";
|
|
}',
|
|
],
|
|
'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;
|
|
}
|
|
}',
|
|
],
|
|
'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,
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
{
|
|
return [
|
|
'invalidCatchClass' => [
|
|
'<?php
|
|
class A {}
|
|
try {
|
|
$worked = true;
|
|
}
|
|
catch (A $e) {}',
|
|
'error_message' => 'InvalidCatch',
|
|
],
|
|
'invalidThrowClass' => [
|
|
'<?php
|
|
class A {}
|
|
throw new A();',
|
|
'error_message' => 'InvalidThrow',
|
|
],
|
|
'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',
|
|
],
|
|
];
|
|
}
|
|
}
|