1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/ThrowsAnnotationTest.php

700 lines
19 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
use Psalm\Config;
use Psalm\Context;
2021-12-03 20:29:06 +01:00
use Psalm\Exception\CodeException;
class ThrowsAnnotationTest extends TestCase
{
public function testUndefinedClassAsThrows(): void
{
2019-08-13 21:44:18 +02:00
$this->expectExceptionMessage('UndefinedDocblockClass - somefile.php:3:28');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
$this->addFile(
'somefile.php',
'<?php
/**
* @throws Foo
*/
2022-12-18 17:15:15 +01:00
function bar() : void {}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testNonThrowableClassAsThrows(): void
{
$this->expectExceptionMessage('InvalidThrow');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
$this->addFile(
'somefile.php',
'<?php
class Foo {}
/**
* @throws Foo
*/
2022-12-18 17:15:15 +01:00
function bar() : void {}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testInheritedThrowableClassAsThrows(): void
2019-08-13 21:44:18 +02:00
{
$this->addFile(
'somefile.php',
'<?php
class MyException extends Exception {}
class Foo {
/**
* @throws MyException|Throwable
*/
public function bar() : void {}
2022-12-18 17:15:15 +01:00
}',
2019-08-13 21:44:18 +02:00
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testUndocumentedThrow(): void
{
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('MissingThrowsDocblock');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrow(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedParentThrow(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws Exception
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testThrowableInherited(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws Throwable
*/
function foo(int $x, int $y) : int {
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testUndocumentedThrowInFunctionCall(): void
{
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('MissingThrowsDocblock');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
function bar(int $x, int $y) : void {
foo($x, $y);
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInFunctionCallWithThrow(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function bar(int $x, int $y) : void {
foo($x, $y);
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInFunctionCallWithoutThrow(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
class Foo
{
/**
* @throws \TypeError
*/
public static function notReallyThrowing(int $a): string
{
if ($a > 0) {
return "";
}
return (string) $a;
}
public function test(): string
{
try {
return self::notReallyThrowing(2);
} catch (\Throwable $E) {
return "";
}
}
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testCaughtThrowInFunctionCall(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
function bar(int $x, int $y) : void {
try {
foo($x, $y);
} catch (RangeException $e) {
} catch (InvalidArgumentException $e) {}
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testUncaughtThrowInFunctionCall(): void
{
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('MissingThrowsDocblock');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
function bar(int $x, int $y) : void {
try {
foo($x, $y);
} catch (\RangeException $e) {
}
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testEmptyThrows(): void
{
2019-05-17 00:36:36 +02:00
$this->expectExceptionMessage('MissingDocblockType');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws
*/
2022-12-18 17:15:15 +01:00
function foo(int $x, int $y) : int {}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testCaughtAllThrowInFunctionCall(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws RangeException
* @throws InvalidArgumentException
*/
function foo(int $x, int $y) : int {
if ($y === 0) {
throw new \RangeException("Cannot divide by zero");
}
if ($y < 0) {
throw new \InvalidArgumentException("This is also bad");
}
return intdiv($x, $y);
}
function bar(int $x, int $y) : void {
try {
foo($x, $y);
} catch (Exception $e) {}
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInInterfaceWithInheritDocblock(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
/**
* {@inheritdoc}
*/
public function test(): void
{
throw new \InvalidArgumentException();
}
}
2022-12-18 17:15:15 +01:00
',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInInterfaceWithoutInheritDocblock(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
public function test(): void
{
throw new \InvalidArgumentException();
}
}
2022-12-18 17:15:15 +01:00
',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInSubclassWithExtendedInheritDocblock(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
/**
* {@inheritdoc}
* @throws \OutOfBoundsException
*/
public function test(): void
{
throw new \OutOfBoundsException();
}
}
2022-12-18 17:15:15 +01:00
',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInInterfaceWithExtendedInheritDocblock(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
/**
* {@inheritdoc}
* @throws \OutOfBoundsException
*/
public function test(): void
{
throw new \InvalidArgumentException();
}
}
2022-12-18 17:15:15 +01:00
',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInInterfaceWithOverriddenDocblock(): void
{
$this->expectExceptionMessage('MissingThrowsDocblock');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
/**
* @throws \OutOfBoundsException
*/
public function test(): void
{
throw new \InvalidArgumentException();
}
}
2022-12-18 17:15:15 +01:00
',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInsideCatch(): void
{
$this->expectExceptionMessage('MissingThrowsDocblock');
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @return void
*/
function foo() : void {
try {
throw new Exception("foo");
} catch (Exception $e) {
throw new RuntimeException("bar");
}
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testNextCatchShouldIgnoreExceptionsCaughtByPreviousCatch(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @throws \RuntimeException
*/
function method(): void
{
try {
throw new \LogicException();
} catch (\LogicException $e) {
throw new \RuntimeException();
} catch (\Exception $e) {
throw new \RuntimeException();
}
2022-12-18 17:15:15 +01:00
}',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testUnknownExceptionInThrowsOfACalledMethod(): void
{
$this->expectExceptionMessage('MissingThrowsDocblock');
$this->expectException(CodeException::class);
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
final class Monkey {
/** @throws InvalidArgumentException */
public function spendsItsDay(): void {
$this->havingFun();
}
/** @throws \Monkey\Shit */
private function havingFun(): void {}
}
',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
public function testDocumentedThrowInterfaceWithFunctionCallWithImplementedExceptionThrow(): void
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface TestExceptionInterface extends Throwable
{
}
class TestException extends Exception implements TestExceptionInterface
{
}
class Example
{
/**
* @throws Throwable
*/
private function methodOne(): void {
$this->methodTwo();
}
/**
* @throws TestExceptionInterface
*/
private function methodTwo(): void {}
}
',
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
}