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