mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
feat: fix ci + preserve existing throws
This commit is contained in:
parent
c6854cf567
commit
e28831dff8
@ -65,6 +65,8 @@ use function array_keys;
|
||||
use function array_merge;
|
||||
use function array_search;
|
||||
use function array_values;
|
||||
use function assert;
|
||||
use function class_exists;
|
||||
use function count;
|
||||
use function end;
|
||||
use function in_array;
|
||||
@ -723,6 +725,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
|
||||
}
|
||||
|
||||
if (!$is_expected) {
|
||||
assert(class_exists($possibly_thrown_exception));
|
||||
$missingThrowsDocblockErrors[] = $possibly_thrown_exception;
|
||||
foreach ($codelocations as $codelocation) {
|
||||
// issues are suppressed in ThrowAnalyzer, CallAnalyzer, etc.
|
||||
@ -737,8 +740,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$codebase->alter_code
|
||||
if ($codebase->alter_code
|
||||
&& isset($project_analyzer->getIssuesToFix()['MissingThrowsDocblock'])
|
||||
) {
|
||||
$manipulator = FunctionDocblockManipulator::getForFunction(
|
||||
|
@ -14,7 +14,9 @@ use Psalm\Internal\Analyzer\CommentAnalyzer;
|
||||
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
||||
use Psalm\Internal\Scanner\ParsedDocblock;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_merge;
|
||||
use function array_reduce;
|
||||
use function count;
|
||||
use function is_string;
|
||||
use function ltrim;
|
||||
@ -398,15 +400,20 @@ class FunctionDocblockManipulator
|
||||
$modified_docblock = true;
|
||||
$parsed_docblock->tags['psalm-pure'] = [''];
|
||||
}
|
||||
if (\count($this->throwsExceptions) > 0) {
|
||||
if (count($this->throwsExceptions) > 0) {
|
||||
$modified_docblock = true;
|
||||
$parsed_docblock->tags['throws'] = [
|
||||
\array_reduce(
|
||||
$this->throwsExceptions,
|
||||
fn(string $throwsClause, string $exception) => $throwsClause === '' ? $exception : $throwsClause.'|'.$exception,
|
||||
''
|
||||
)
|
||||
];
|
||||
$inferredThrowsClause = array_reduce(
|
||||
$this->throwsExceptions,
|
||||
function (string $throwsClause, string $exception) {
|
||||
return $throwsClause === '' ? $exception : $throwsClause.'|'.$exception;
|
||||
},
|
||||
''
|
||||
);
|
||||
if (array_key_exists('throws', $parsed_docblock->tags)) {
|
||||
$parsed_docblock->tags['throws'][] = $inferredThrowsClause;
|
||||
} else {
|
||||
$parsed_docblock->tags['throws'] = [$inferredThrowsClause];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -32,6 +32,60 @@ class ThrowsBlockAdditionTest extends FileManipulationTestCase
|
||||
['MissingThrowsDocblock'],
|
||||
true,
|
||||
],
|
||||
'addMultipleThrowsAnnotationToFunction' => [
|
||||
'<?php
|
||||
function foo(string $s): string {
|
||||
if("" === $s) {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
if("" === \trim($s)) {
|
||||
throw new \DomainException();
|
||||
}
|
||||
return $s;
|
||||
}',
|
||||
'<?php
|
||||
/**
|
||||
* @throws InvalidArgumentException|DomainException
|
||||
*/
|
||||
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
|
||||
* @throws Exception
|
||||
*/
|
||||
function foo(string $s): string {
|
||||
if("" === $s) {
|
||||
throw new \Exception();
|
||||
}
|
||||
return $s;
|
||||
}',
|
||||
'7.4',
|
||||
['MissingThrowsDocblock'],
|
||||
true,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user