2018-12-06 04:50:16 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class ThrowsAnnotationTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2018-12-06 04:50:16 +01:00
|
|
|
*/
|
|
|
|
public function testUndocumentedThrow()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('MissingThrowsDocblock');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2018-12-06 04:50:16 +01:00
|
|
|
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);
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDocumentedThrow()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDocumentedParentThrow()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:41:06 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNoThrowWhenSuppressing()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_docblock = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingThrowsDocblock
|
|
|
|
*/
|
|
|
|
function foo() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
throw new \UnexpectedValueException();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
2019-03-29 01:01:26 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNoThrowWhenSuppressingInline()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_docblock = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
/** @psalm-suppress MissingThrowsDocblock */
|
|
|
|
throw new \UnexpectedValueException();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
2018-12-06 04:50:16 +01:00
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2018-12-06 04:50:16 +01:00
|
|
|
*/
|
|
|
|
public function testUndocumentedThrowInFunctionCall()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('MissingThrowsDocblock');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2018-12-06 04:50:16 +01:00
|
|
|
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);
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2018-12-06 04:50:16 +01:00
|
|
|
*/
|
2019-03-28 22:18:11 +01:00
|
|
|
public function testDocumentedThrowInFunctionCallWithThrow()
|
2018-12-06 04:50:16 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2018-12-06 04:50:16 +01:00
|
|
|
*/
|
|
|
|
public function testDocumentedThrowInFunctionCallWithoutThrow()
|
|
|
|
{
|
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCaughtThrowInFunctionCall()
|
|
|
|
{
|
|
|
|
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) {}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2018-12-06 04:50:16 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtThrowInFunctionCall()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('MissingThrowsDocblock');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2018-12-06 04:50:16 +01:00
|
|
|
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) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
2019-01-06 16:01:35 +01:00
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-01-06 16:01:35 +01:00
|
|
|
*/
|
|
|
|
public function testEmptyThrows()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('MissingDocblockType');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2019-01-06 16:01:35 +01:00
|
|
|
Config::getInstance()->check_for_throws_docblock = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @throws
|
|
|
|
*/
|
|
|
|
function foo(int $x, int $y) : int {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
2018-12-06 04:50:16 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCaughtAllThrowInFunctionCall()
|
|
|
|
{
|
|
|
|
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) {}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
}
|