2022-05-21 17:39:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
|
|
|
class ThrowsBlockAdditionTest extends FileManipulationTestCase
|
|
|
|
{
|
|
|
|
public function providerValidCodeParse(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'addThrowsAnnotationToFunction' => [
|
2022-10-17 10:21:26 +02:00
|
|
|
'input' => '<?php
|
2022-05-21 17:39:51 +02:00
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'output' => '<?php
|
2022-05-21 17:39:51 +02:00
|
|
|
/**
|
2022-09-17 10:55:19 +02:00
|
|
|
* @throws InvalidArgumentException
|
2022-05-21 17:39:51 +02:00
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingThrowsDocblock'],
|
|
|
|
'safe_types' => true,
|
2022-05-21 17:39:51 +02:00
|
|
|
],
|
2022-05-22 18:27:38 +02:00
|
|
|
'addMultipleThrowsAnnotationToFunction' => [
|
2022-10-17 10:21:26 +02:00
|
|
|
'input' => '<?php
|
2022-05-22 18:27:38 +02:00
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
if("" === \trim($s)) {
|
|
|
|
throw new \DomainException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'output' => '<?php
|
2022-05-22 18:27:38 +02:00
|
|
|
/**
|
2022-09-17 10:55:19 +02:00
|
|
|
* @throws InvalidArgumentException|DomainException
|
2022-05-22 18:27:38 +02:00
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
if("" === \trim($s)) {
|
|
|
|
throw new \DomainException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingThrowsDocblock'],
|
|
|
|
'safe_types' => true,
|
2022-05-22 18:27:38 +02:00
|
|
|
],
|
|
|
|
'preservesExistingThrowsAnnotationToFunction' => [
|
2022-10-17 10:21:26 +02:00
|
|
|
'input' => '<?php
|
2022-05-22 18:27:38 +02:00
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException|DomainException
|
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'output' => '<?php
|
2022-05-22 18:27:38 +02:00
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException|DomainException
|
2022-09-17 10:55:19 +02:00
|
|
|
* @throws Exception
|
2022-05-22 18:27:38 +02:00
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingThrowsDocblock'],
|
|
|
|
'safe_types' => true,
|
2022-05-22 18:27:38 +02:00
|
|
|
],
|
2022-05-22 18:38:18 +02:00
|
|
|
'doesNotAddDuplicateThrows' => [
|
2022-10-17 10:21:26 +02:00
|
|
|
'input' => '<?php
|
2022-05-22 18:38:18 +02:00
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
if("" === \trim($s)) {
|
|
|
|
throw new \DomainException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'output' => '<?php
|
2022-05-22 18:38:18 +02:00
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
2022-09-17 10:55:19 +02:00
|
|
|
* @throws DomainException
|
2022-05-22 18:38:18 +02:00
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
if("" === \trim($s)) {
|
|
|
|
throw new \DomainException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingThrowsDocblock'],
|
|
|
|
'safe_types' => true,
|
2022-05-22 18:38:18 +02:00
|
|
|
],
|
2022-09-13 11:28:26 +02:00
|
|
|
'addThrowsAnnotationToFunctionInNamespace' => [
|
2022-10-17 10:33:33 +02:00
|
|
|
'input' => '<?php
|
2022-09-13 11:28:26 +02:00
|
|
|
namespace Foo;
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:33:33 +02:00
|
|
|
'output' => '<?php
|
2022-09-13 11:28:26 +02:00
|
|
|
namespace Foo;
|
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
if("" === $s) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingThrowsDocblock'],
|
|
|
|
'safe_types' => true,
|
2022-09-13 11:28:26 +02:00
|
|
|
],
|
2022-09-17 09:58:49 +02:00
|
|
|
'addThrowsAnnotationToFunctionFromFunctionFromOtherNamespace' => [
|
2022-10-17 10:33:33 +02:00
|
|
|
'input' => '<?php
|
2022-09-17 09:58:49 +02:00
|
|
|
namespace Foo {
|
|
|
|
function foo(): void {
|
|
|
|
\Bar\bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace Bar {
|
|
|
|
class BarException extends \DomainException {}
|
|
|
|
/**
|
|
|
|
* @throws BarException
|
|
|
|
*/
|
|
|
|
function bar(): void {
|
|
|
|
throw new BarException();
|
|
|
|
}
|
|
|
|
}',
|
2022-10-17 10:33:33 +02:00
|
|
|
'output' => '<?php
|
2022-09-17 09:58:49 +02:00
|
|
|
namespace Foo {
|
|
|
|
/**
|
|
|
|
* @throws \Bar\BarException
|
|
|
|
*/
|
|
|
|
function foo(): void {
|
|
|
|
\Bar\bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace Bar {
|
|
|
|
class BarException extends \DomainException {}
|
|
|
|
/**
|
|
|
|
* @throws BarException
|
|
|
|
*/
|
|
|
|
function bar(): void {
|
|
|
|
throw new BarException();
|
|
|
|
}
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingThrowsDocblock'],
|
|
|
|
'safe_types' => true,
|
2022-09-17 09:58:49 +02:00
|
|
|
],
|
2022-09-17 10:56:29 +02:00
|
|
|
'addThrowsAnnotationAccountsForUseStatements' => [
|
2022-10-17 10:33:33 +02:00
|
|
|
'input' => '<?php
|
2022-09-17 10:56:29 +02:00
|
|
|
namespace Foo {
|
|
|
|
use Bar\BarException;
|
|
|
|
function foo(): void {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @throws BarException
|
|
|
|
*/
|
|
|
|
function bar(): void {
|
|
|
|
throw new BarException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace Bar {
|
|
|
|
class BarException extends \DomainException {}
|
|
|
|
}',
|
2022-10-17 10:33:33 +02:00
|
|
|
'output' => '<?php
|
2022-09-17 10:56:29 +02:00
|
|
|
namespace Foo {
|
|
|
|
use Bar\BarException;
|
|
|
|
/**
|
|
|
|
* @throws BarException
|
|
|
|
*/
|
|
|
|
function foo(): void {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @throws BarException
|
|
|
|
*/
|
|
|
|
function bar(): void {
|
|
|
|
throw new BarException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace Bar {
|
|
|
|
class BarException extends \DomainException {}
|
|
|
|
}',
|
2022-10-17 10:21:26 +02:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingThrowsDocblock'],
|
|
|
|
'safe_types' => true,
|
2022-09-17 10:56:29 +02:00
|
|
|
],
|
2022-05-21 17:39:51 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|