mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
93 lines
2.7 KiB
PHP
93 lines
2.7 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";
|
|
}',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @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',
|
|
],
|
|
];
|
|
}
|
|
}
|