1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/FileManipulation/ThrowsBlockAdditionTest.php
2022-10-17 10:21:26 +02:00

226 lines
8.5 KiB
PHP

<?php
namespace Psalm\Tests\FileManipulation;
class ThrowsBlockAdditionTest extends FileManipulationTestCase
{
/**
* @return array<string,array{input:string,output:string,php_version:string,issues_to_fix:string[],safe_types:bool}>
*/
public function providerValidCodeParse(): array
{
return [
'addThrowsAnnotationToFunction' => [
'input' => '<?php
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
'output' => '<?php
/**
* @throws InvalidArgumentException
*/
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingThrowsDocblock'],
'safe_types' => true,
],
'addMultipleThrowsAnnotationToFunction' => [
'input' => '<?php
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
if("" === \trim($s)) {
throw new \DomainException();
}
return $s;
}',
'output' => '<?php
/**
* @throws InvalidArgumentException|DomainException
*/
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
if("" === \trim($s)) {
throw new \DomainException();
}
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingThrowsDocblock'],
'safe_types' => true,
],
'preservesExistingThrowsAnnotationToFunction' => [
'input' => '<?php
/**
* @throws InvalidArgumentException|DomainException
*/
function foo(string $s): string {
if("" === $s) {
throw new \Exception();
}
return $s;
}',
'output' => '<?php
/**
* @throws InvalidArgumentException|DomainException
* @throws Exception
*/
function foo(string $s): string {
if("" === $s) {
throw new \Exception();
}
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingThrowsDocblock'],
'safe_types' => true,
],
'doesNotAddDuplicateThrows' => [
'input' => '<?php
/**
* @throws InvalidArgumentException
*/
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
if("" === \trim($s)) {
throw new \DomainException();
}
return $s;
}',
'output' => '<?php
/**
* @throws InvalidArgumentException
* @throws DomainException
*/
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
if("" === \trim($s)) {
throw new \DomainException();
}
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingThrowsDocblock'],
'safe_types' => true,
],
'addThrowsAnnotationToFunctionInNamespace' => [
'input_type' => '<?php
namespace Foo;
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
'output_type' => '<?php
namespace Foo;
/**
* @throws \InvalidArgumentException
*/
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingThrowsDocblock'],
'safe_types' => true,
],
'addThrowsAnnotationToFunctionFromFunctionFromOtherNamespace' => [
'input_type' => '<?php
namespace Foo {
function foo(): void {
\Bar\bar();
}
}
namespace Bar {
class BarException extends \DomainException {}
/**
* @throws BarException
*/
function bar(): void {
throw new BarException();
}
}',
'output_type' => '<?php
namespace Foo {
/**
* @throws \Bar\BarException
*/
function foo(): void {
\Bar\bar();
}
}
namespace Bar {
class BarException extends \DomainException {}
/**
* @throws BarException
*/
function bar(): void {
throw new BarException();
}
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingThrowsDocblock'],
'safe_types' => true,
],
'addThrowsAnnotationAccountsForUseStatements' => [
'input_type' => '<?php
namespace Foo {
use Bar\BarException;
function foo(): void {
bar();
}
/**
* @throws BarException
*/
function bar(): void {
throw new BarException();
}
}
namespace Bar {
class BarException extends \DomainException {}
}',
'output_type' => '<?php
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 {}
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingThrowsDocblock'],
'safe_types' => true,
],
];
}
}