2017-12-28 01:49:36 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
class TryCatchTest extends TestCase
|
|
|
|
{
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-12-28 20:40:28 +01:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
2017-12-28 01:49:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
2017-12-28 20:40:28 +01:00
|
|
|
'PHP7-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();
|
|
|
|
}',
|
|
|
|
],
|
2017-12-28 20:40:28 +01:00
|
|
|
'PHP7-rethrowInterfaceExceptionWithoutInvalidThrow' => [
|
|
|
|
'<?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
|
|
|
|
function throws() : void {
|
|
|
|
throw new Exception("bad");
|
|
|
|
}
|
|
|
|
function foo() : string {
|
|
|
|
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";
|
|
|
|
}',
|
|
|
|
],
|
2017-12-28 20:40:28 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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',
|
|
|
|
],
|
2017-12-28 01:49:36 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|