1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 09:19:40 +01:00
psalm/tests/FileManipulation/ThrowsBlockAdditionTest.php

223 lines
8.3 KiB
PHP
Raw Normal View History

<?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
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
2022-10-17 10:21:26 +02:00
'output' => '<?php
/**
* @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,
],
'addMultipleThrowsAnnotationToFunction' => [
2022-10-17 10:21:26 +02:00
'input' => '<?php
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
/**
* @throws InvalidArgumentException|DomainException
*/
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,
],
'preservesExistingThrowsAnnotationToFunction' => [
2022-10-17 10:21:26 +02:00
'input' => '<?php
/**
* @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
/**
* @throws InvalidArgumentException|DomainException
* @throws Exception
*/
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: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
* @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
],
'addThrowsAnnotationToFunctionInNamespace' => [
2022-10-17 10:33:33 +02:00
'input' => '<?php
namespace Foo;
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
2022-10-17 10:33:33 +02:00
'output' => '<?php
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,
],
'addThrowsAnnotationToFunctionFromFunctionFromOtherNamespace' => [
2022-10-17 10:33:33 +02:00
'input' => '<?php
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
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,
],
'addThrowsAnnotationAccountsForUseStatements' => [
2022-10-17 10:33:33 +02:00
'input' => '<?php
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
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,
],
];
}
}