2018-11-06 03:57:36 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use PhpParser\Comment\Doc;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Scanner\DocblockParser;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Scanner\ParsedDocblock;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function explode;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function in_array;
|
|
|
|
use function preg_match;
|
|
|
|
use function strlen;
|
2021-09-26 22:51:44 +02:00
|
|
|
use function strpos;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function strspn;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function substr;
|
|
|
|
use function trim;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2022-02-04 03:51:18 +01:00
|
|
|
final class DocComment
|
2018-11-06 03:57:36 +01:00
|
|
|
{
|
2020-11-28 03:48:16 +01:00
|
|
|
public const PSALM_ANNOTATIONS = [
|
2020-04-03 04:38:10 +02:00
|
|
|
'return', 'param', 'template', 'var', 'type',
|
|
|
|
'template-covariant', 'property', 'property-read', 'property-write', 'method',
|
|
|
|
'assert', 'assert-if-true', 'assert-if-false', 'suppress',
|
|
|
|
'ignore-nullable-return', 'override-property-visibility',
|
|
|
|
'override-method-visibility', 'seal-properties', 'seal-methods',
|
2023-04-20 08:14:17 +02:00
|
|
|
'no-seal-properties', 'no-seal-methods',
|
2022-04-09 22:15:37 +02:00
|
|
|
'ignore-falsable-return', 'variadic', 'pure',
|
2020-04-03 04:38:10 +02:00
|
|
|
'ignore-variable-method', 'ignore-variable-property', 'internal',
|
|
|
|
'taint-sink', 'taint-source', 'assert-untainted', 'scope-this',
|
|
|
|
'mutation-free', 'external-mutation-free', 'immutable', 'readonly',
|
2020-05-22 04:47:58 +02:00
|
|
|
'allow-private-mutation', 'readonly-allow-private-mutation',
|
2020-06-21 17:43:08 +02:00
|
|
|
'yield', 'trace', 'import-type', 'flow', 'taint-specialize', 'taint-escape',
|
2020-10-19 21:08:18 +02:00
|
|
|
'taint-unescape', 'self-out', 'consistent-constructor', 'stub-override',
|
2021-04-10 19:16:14 +02:00
|
|
|
'require-extends', 'require-implements', 'param-out', 'ignore-var',
|
2022-02-17 17:26:28 +01:00
|
|
|
'consistent-templates', 'if-this-is', 'this-out', 'check-type', 'check-type-exact',
|
2023-04-20 18:25:11 +02:00
|
|
|
'api', 'inheritors',
|
2020-04-03 04:38:10 +02:00
|
|
|
];
|
|
|
|
|
2019-06-02 00:44:59 +02:00
|
|
|
/**
|
|
|
|
* Parse a docblock comment into its parts.
|
|
|
|
*/
|
2021-12-05 18:51:26 +01:00
|
|
|
public static function parsePreservingLength(Doc $docblock): ParsedDocblock
|
2019-06-02 00:44:59 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
$parsed_docblock = DocblockParser::parse(
|
2021-08-06 22:00:37 +02:00
|
|
|
$docblock->getText(),
|
2022-12-18 17:15:15 +01:00
|
|
|
$docblock->getStartFilePos(),
|
2021-08-06 22:00:37 +02:00
|
|
|
);
|
2019-06-02 00:44:59 +02:00
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
foreach ($parsed_docblock->tags as $special_key => $_) {
|
2021-09-26 22:51:44 +02:00
|
|
|
if (strpos($special_key, 'psalm-') === 0) {
|
2018-12-01 21:29:14 +01:00
|
|
|
$special_key = substr($special_key, 6);
|
|
|
|
|
|
|
|
if (!in_array(
|
|
|
|
$special_key,
|
2020-04-03 04:38:10 +02:00
|
|
|
self::PSALM_ANNOTATIONS,
|
2022-12-18 17:15:15 +01:00
|
|
|
true,
|
2018-12-01 21:29:14 +01:00
|
|
|
)) {
|
|
|
|
throw new DocblockParseException('Unrecognised annotation @psalm-' . $special_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
return $parsed_docblock;
|
2020-03-13 17:40:08 +01:00
|
|
|
}
|
2020-09-13 22:41:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
* @return array<int,string>
|
|
|
|
*/
|
|
|
|
public static function parseSuppressList(string $suppress_entry): array
|
|
|
|
{
|
|
|
|
preg_match(
|
|
|
|
'/
|
|
|
|
(?(DEFINE)
|
|
|
|
# either a single issue or comma separated list of issues
|
|
|
|
(?<issue_list> (?&issue) \s* , \s* (?&issue_list) | (?&issue) )
|
|
|
|
|
|
|
|
# definition of a single issue
|
|
|
|
(?<issue> [A-Za-z0-9_-]+ )
|
|
|
|
)
|
|
|
|
^ (?P<issues> (?&issue_list) ) (?P<description> .* ) $
|
|
|
|
/xm',
|
|
|
|
$suppress_entry,
|
2022-12-18 17:15:15 +01:00
|
|
|
$matches,
|
2020-09-13 22:41:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset($matches['issues'])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$issue_offset = 0;
|
|
|
|
$ret = [];
|
|
|
|
|
|
|
|
foreach (explode(',', $matches['issues']) as $suppressed_issue) {
|
|
|
|
$issue_offset += strspn($suppressed_issue, "\t\n\f\r ");
|
|
|
|
$ret[$issue_offset] = trim($suppressed_issue);
|
|
|
|
$issue_offset += strlen($suppressed_issue) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
2018-11-06 03:57:36 +01:00
|
|
|
}
|