2016-06-23 18:45:46 -04:00
|
|
|
<?php
|
2018-11-05 21:57:36 -05:00
|
|
|
namespace Psalm\Internal\Analyzer;
|
2016-08-13 14:20:46 -04:00
|
|
|
|
2019-06-01 11:53:32 -04:00
|
|
|
use PhpParser;
|
2017-07-25 16:11:02 -04:00
|
|
|
use Psalm\Aliases;
|
2018-11-05 21:57:36 -05:00
|
|
|
use Psalm\DocComment;
|
2016-11-02 02:29:00 -04:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2017-11-14 21:43:31 -05:00
|
|
|
use Psalm\Exception\IncorrectDocblockException;
|
2017-05-19 00:48:26 -04:00
|
|
|
use Psalm\Exception\TypeParseTreeException;
|
2018-01-21 12:44:46 -05:00
|
|
|
use Psalm\FileSource;
|
2018-11-05 21:57:36 -05:00
|
|
|
use Psalm\Internal\Scanner\ClassLikeDocblockComment;
|
|
|
|
use Psalm\Internal\Scanner\FunctionDocblockComment;
|
|
|
|
use Psalm\Internal\Scanner\VarDocblockComment;
|
2020-05-28 22:14:41 -04:00
|
|
|
use Psalm\Internal\Scanner\ParsedDocblock;
|
2018-11-18 12:41:47 -05:00
|
|
|
use Psalm\Internal\Type\ParseTree;
|
2020-05-28 14:31:09 -04:00
|
|
|
use Psalm\Internal\Type\ParseTreeCreator;
|
2020-05-13 19:29:59 -04:00
|
|
|
use Psalm\Internal\Type\TypeAlias;
|
2020-05-13 19:12:45 -04:00
|
|
|
use Psalm\Internal\Type\TypeParser;
|
|
|
|
use Psalm\Internal\Type\TypeTokenizer;
|
2016-08-13 14:20:46 -04:00
|
|
|
use Psalm\Type;
|
2020-05-30 22:54:16 +02:00
|
|
|
use function array_unique;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function trim;
|
|
|
|
use function substr_count;
|
|
|
|
use function strlen;
|
|
|
|
use function preg_replace;
|
|
|
|
use function str_replace;
|
|
|
|
use function preg_match;
|
|
|
|
use function count;
|
|
|
|
use function reset;
|
|
|
|
use function preg_split;
|
|
|
|
use const PREG_SPLIT_DELIM_CAPTURE;
|
|
|
|
use const PREG_SPLIT_NO_EMPTY;
|
|
|
|
use function array_shift;
|
|
|
|
use function implode;
|
|
|
|
use function substr;
|
|
|
|
use function strpos;
|
|
|
|
use function strtolower;
|
|
|
|
use function in_array;
|
|
|
|
use function explode;
|
|
|
|
use function array_merge;
|
|
|
|
use const PREG_OFFSET_CAPTURE;
|
|
|
|
use function rtrim;
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2018-12-01 18:37:49 -05:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-05 21:57:36 -05:00
|
|
|
class CommentAnalyzer
|
2016-06-23 18:45:46 -04:00
|
|
|
{
|
2018-04-21 22:13:10 -04:00
|
|
|
const TYPE_REGEX = '(\??\\\?[\(\)A-Za-z0-9_&\<\.=,\>\[\]\-\{\}:|?\\\\]*|\$[a-zA-Z_0-9_]+)';
|
2016-06-23 18:45:46 -04:00
|
|
|
|
|
|
|
/**
|
2019-03-22 15:59:10 -04:00
|
|
|
* @param array<string, array<string, array{Type\Union}>>|null $template_type_map
|
2020-05-13 19:29:59 -04:00
|
|
|
* @param array<string, TypeAlias> $type_aliases
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2018-02-07 23:33:31 -05:00
|
|
|
* @return VarDocblockComment[]
|
2016-06-23 18:45:46 -04:00
|
|
|
*/
|
2016-11-02 02:29:00 -04:00
|
|
|
public static function getTypeFromComment(
|
2019-06-01 11:53:32 -04:00
|
|
|
PhpParser\Comment\Doc $comment,
|
2018-01-21 12:44:46 -05:00
|
|
|
FileSource $source,
|
2017-07-25 16:11:02 -04:00
|
|
|
Aliases $aliases,
|
2018-12-17 23:29:27 -05:00
|
|
|
array $template_type_map = null,
|
2019-06-01 12:25:57 -04:00
|
|
|
?array $type_aliases = null
|
2016-11-02 02:29:00 -04:00
|
|
|
) {
|
2019-06-26 16:26:14 -04:00
|
|
|
$parsed_docblock = DocComment::parsePreservingLength($comment);
|
|
|
|
|
|
|
|
return self::arrayToDocblocks(
|
|
|
|
$comment,
|
|
|
|
$parsed_docblock,
|
|
|
|
$source,
|
|
|
|
$aliases,
|
|
|
|
$template_type_map,
|
|
|
|
$type_aliases
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, array<string, array{Type\Union}>>|null $template_type_map
|
2020-05-13 19:29:59 -04:00
|
|
|
* @param array<string, TypeAlias> $type_aliases
|
2019-06-26 16:26:14 -04:00
|
|
|
*
|
|
|
|
* @return VarDocblockComment[]
|
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*/
|
|
|
|
public static function arrayToDocblocks(
|
|
|
|
PhpParser\Comment\Doc $comment,
|
2020-05-28 22:14:41 -04:00
|
|
|
ParsedDocblock $parsed_docblock,
|
2019-06-26 16:26:14 -04:00
|
|
|
FileSource $source,
|
|
|
|
Aliases $aliases,
|
|
|
|
array $template_type_map = null,
|
|
|
|
?array $type_aliases = null
|
|
|
|
) : array {
|
2017-05-25 01:32:34 -04:00
|
|
|
$var_id = null;
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2018-05-20 17:19:53 -04:00
|
|
|
$var_type_tokens = null;
|
2018-01-01 20:04:03 -05:00
|
|
|
$original_type = null;
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2018-02-07 23:33:31 -05:00
|
|
|
$var_comments = [];
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$comment_text = $comment->getText();
|
|
|
|
|
2019-06-01 11:53:32 -04:00
|
|
|
$var_line_number = $comment->getLine();
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['var'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['var'] as $offset => $var_line) {
|
2017-03-01 22:27:52 -05:00
|
|
|
$var_line = trim($var_line);
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2017-03-01 22:27:52 -05:00
|
|
|
if (!$var_line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:25:57 -04:00
|
|
|
$type_start = null;
|
|
|
|
$type_end = null;
|
|
|
|
|
2019-01-31 20:21:20 -05:00
|
|
|
$line_parts = self::splitDocLine($var_line);
|
2017-03-01 22:27:52 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$line_number = $comment->getLine() + substr_count($comment_text, "\n", 0, $offset);
|
|
|
|
|
2017-03-01 22:27:52 -05:00
|
|
|
if ($line_parts && $line_parts[0]) {
|
2019-06-01 18:44:59 -04:00
|
|
|
$type_start = $offset + $comment->getFilePos();
|
|
|
|
$type_end = $type_start + strlen($line_parts[0]);
|
|
|
|
|
2020-04-03 14:56:11 -04:00
|
|
|
$line_parts[0] = self::sanitizeDocblockType($line_parts[0]);
|
2019-06-01 18:44:59 -04:00
|
|
|
|
2019-06-03 15:46:25 -04:00
|
|
|
if ($line_parts[0] === ''
|
|
|
|
|| ($line_parts[0][0] === '$'
|
|
|
|
&& !preg_match('/^\$this(\||$)/', $line_parts[0]))
|
|
|
|
) {
|
2017-11-14 21:43:31 -05:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
2018-04-05 17:18:49 -04:00
|
|
|
try {
|
2020-05-13 19:12:45 -04:00
|
|
|
$var_type_tokens = TypeTokenizer::getFullyQualifiedTokens(
|
2019-06-01 18:44:59 -04:00
|
|
|
$line_parts[0],
|
2018-04-05 17:18:49 -04:00
|
|
|
$aliases,
|
2018-12-17 23:29:27 -05:00
|
|
|
$template_type_map,
|
2018-07-15 17:23:17 -04:00
|
|
|
$type_aliases
|
2018-04-05 17:18:49 -04:00
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
|
|
|
throw new DocblockParseException($line_parts[0] . ' is not a valid type');
|
|
|
|
}
|
2017-03-01 22:27:52 -05:00
|
|
|
|
2018-01-01 20:04:03 -05:00
|
|
|
$original_type = $line_parts[0];
|
|
|
|
|
2017-03-01 22:27:52 -05:00
|
|
|
$var_line_number = $line_number;
|
|
|
|
|
|
|
|
if (count($line_parts) > 1 && $line_parts[1][0] === '$') {
|
2017-05-25 01:32:34 -04:00
|
|
|
$var_id = $line_parts[1];
|
2017-03-01 22:27:52 -05:00
|
|
|
}
|
2018-02-07 23:33:31 -05:00
|
|
|
}
|
2017-03-01 22:27:52 -05:00
|
|
|
|
2018-05-20 17:19:53 -04:00
|
|
|
if (!$var_type_tokens || !$original_type) {
|
2018-02-07 23:33:31 -05:00
|
|
|
continue;
|
2016-06-23 18:45:46 -04:00
|
|
|
}
|
|
|
|
|
2018-02-07 23:33:31 -05:00
|
|
|
try {
|
2020-05-15 00:15:48 -04:00
|
|
|
$defined_type = TypeParser::parseTokens(
|
|
|
|
$var_type_tokens,
|
|
|
|
null,
|
|
|
|
$template_type_map ?: [],
|
|
|
|
$type_aliases ?: []
|
|
|
|
);
|
2018-02-07 23:33:31 -05:00
|
|
|
} catch (TypeParseTreeException $e) {
|
2019-06-01 11:53:32 -04:00
|
|
|
throw new DocblockParseException(
|
2019-06-22 23:30:40 -04:00
|
|
|
$line_parts[0] .
|
2019-06-01 11:53:32 -04:00
|
|
|
' is not a valid type' .
|
|
|
|
' (from ' .
|
|
|
|
$source->getFilePath() .
|
|
|
|
':' .
|
|
|
|
$comment->getLine() .
|
|
|
|
')'
|
|
|
|
);
|
2018-02-07 23:33:31 -05:00
|
|
|
}
|
2017-05-25 01:32:34 -04:00
|
|
|
|
2018-02-07 23:33:31 -05:00
|
|
|
$defined_type->setFromDocblock();
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2018-02-07 23:33:31 -05:00
|
|
|
$var_comment = new VarDocblockComment();
|
|
|
|
$var_comment->type = $defined_type;
|
|
|
|
$var_comment->original_type = $original_type;
|
|
|
|
$var_comment->var_id = $var_id;
|
|
|
|
$var_comment->line_number = $var_line_number;
|
2019-06-01 12:25:57 -04:00
|
|
|
$var_comment->type_start = $type_start;
|
|
|
|
$var_comment->type_end = $type_end;
|
2019-05-14 23:27:27 +01:00
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
self::decorateVarDocblockComment($var_comment, $parsed_docblock);
|
2017-02-03 22:07:14 -05:00
|
|
|
|
2018-02-07 23:33:31 -05:00
|
|
|
$var_comments[] = $var_comment;
|
|
|
|
}
|
|
|
|
}
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2019-08-11 16:01:37 -04:00
|
|
|
if (!$var_comments
|
2020-05-28 22:14:41 -04:00
|
|
|
&& (isset($parsed_docblock->tags['deprecated'])
|
|
|
|
|| isset($parsed_docblock->tags['internal'])
|
|
|
|
|| isset($parsed_docblock->tags['readonly'])
|
|
|
|
|| isset($parsed_docblock->tags['psalm-readonly'])
|
|
|
|
|| isset($parsed_docblock->tags['psalm-readonly-allow-private-mutation'])
|
2020-06-21 11:43:08 -04:00
|
|
|
|| isset($parsed_docblock->tags['psalm-taint-escape'])
|
2020-05-28 22:14:41 -04:00
|
|
|
|| isset($parsed_docblock->tags['psalm-internal']))
|
2019-08-11 16:01:37 -04:00
|
|
|
) {
|
|
|
|
$var_comment = new VarDocblockComment();
|
2020-05-28 22:14:41 -04:00
|
|
|
|
|
|
|
self::decorateVarDocblockComment($var_comment, $parsed_docblock);
|
2019-08-11 16:01:37 -04:00
|
|
|
|
|
|
|
$var_comments[] = $var_comment;
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:33:31 -05:00
|
|
|
return $var_comments;
|
2016-06-23 18:45:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
private static function decorateVarDocblockComment(
|
|
|
|
VarDocblockComment $var_comment,
|
|
|
|
ParsedDocblock $parsed_docblock
|
|
|
|
) : void {
|
|
|
|
$var_comment->deprecated = isset($parsed_docblock->tags['deprecated']);
|
|
|
|
$var_comment->internal = isset($parsed_docblock->tags['internal']);
|
|
|
|
$var_comment->readonly = isset($parsed_docblock->tags['readonly'])
|
|
|
|
|| isset($parsed_docblock->tags['psalm-readonly'])
|
|
|
|
|| isset($parsed_docblock->tags['psalm-readonly-allow-private-mutation']);
|
|
|
|
|
|
|
|
$var_comment->allow_private_mutation
|
|
|
|
= isset($parsed_docblock->tags['psalm-allow-private-mutation'])
|
|
|
|
|| isset($parsed_docblock->tags['psalm-readonly-allow-private-mutation']);
|
|
|
|
|
2020-06-21 11:43:08 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-escape'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-taint-escape'] as $param) {
|
2020-05-28 22:14:41 -04:00
|
|
|
$param = trim($param);
|
|
|
|
$var_comment->removed_taints[] = $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($parsed_docblock->tags['psalm-internal'])) {
|
|
|
|
$psalm_internal = reset($parsed_docblock->tags['psalm-internal']);
|
|
|
|
|
|
|
|
if (!$psalm_internal) {
|
|
|
|
throw new DocblockParseException('psalm-internal annotation used without specifying namespace');
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_comment->psalm_internal = reset($parsed_docblock->tags['psalm-internal']);
|
2020-07-26 10:47:48 -04:00
|
|
|
$var_comment->internal = true;
|
2020-05-28 22:14:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:56:11 -04:00
|
|
|
private static function sanitizeDocblockType(string $docblock_type) : string
|
|
|
|
{
|
|
|
|
$docblock_type = preg_replace('@^[ \t]*\*@m', '', $docblock_type);
|
|
|
|
$docblock_type = preg_replace('/,\n\s+\}/', '}', $docblock_type);
|
|
|
|
return str_replace("\n", '', $docblock_type);
|
|
|
|
}
|
|
|
|
|
2018-07-15 17:23:17 -04:00
|
|
|
/**
|
|
|
|
* @param Aliases $aliases
|
2020-05-13 19:29:59 -04:00
|
|
|
* @param array<string, TypeAlias> $type_aliases
|
2018-07-15 17:23:17 -04:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2020-05-14 00:41:50 -04:00
|
|
|
* @return array<string, TypeAlias\InlineTypeAlias>
|
2018-07-15 17:23:17 -04:00
|
|
|
*/
|
|
|
|
public static function getTypeAliasesFromComment(
|
2019-06-01 11:53:32 -04:00
|
|
|
PhpParser\Comment\Doc $comment,
|
2018-07-15 17:23:17 -04:00
|
|
|
Aliases $aliases,
|
2020-06-07 12:00:55 -04:00
|
|
|
?array $type_aliases,
|
|
|
|
?string $self_fqcln
|
2018-07-15 17:23:17 -04:00
|
|
|
) {
|
2019-06-26 16:26:14 -04:00
|
|
|
$parsed_docblock = DocComment::parsePreservingLength($comment);
|
2018-07-15 17:23:17 -04:00
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (!isset($parsed_docblock->tags['psalm-type'])) {
|
2018-07-15 17:23:17 -04:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getTypeAliasesFromCommentLines(
|
2020-05-28 22:14:41 -04:00
|
|
|
$parsed_docblock->tags['psalm-type'],
|
2018-07-15 17:23:17 -04:00
|
|
|
$aliases,
|
2020-06-07 12:00:55 -04:00
|
|
|
$type_aliases,
|
|
|
|
$self_fqcln
|
2018-07-15 17:23:17 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string> $type_alias_comment_lines
|
|
|
|
* @param Aliases $aliases
|
2020-05-13 19:29:59 -04:00
|
|
|
* @param array<string, TypeAlias> $type_aliases
|
2018-07-15 17:23:17 -04:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2020-05-14 00:41:50 -04:00
|
|
|
* @return array<string, TypeAlias\InlineTypeAlias>
|
2018-07-15 17:23:17 -04:00
|
|
|
*/
|
|
|
|
private static function getTypeAliasesFromCommentLines(
|
|
|
|
array $type_alias_comment_lines,
|
|
|
|
Aliases $aliases,
|
2020-06-07 12:00:55 -04:00
|
|
|
?array $type_aliases,
|
|
|
|
?string $self_fqcln
|
2018-07-15 17:23:17 -04:00
|
|
|
) {
|
|
|
|
$type_alias_tokens = [];
|
|
|
|
|
|
|
|
foreach ($type_alias_comment_lines as $var_line) {
|
|
|
|
$var_line = trim($var_line);
|
|
|
|
|
|
|
|
if (!$var_line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:44:59 -04:00
|
|
|
$var_line = preg_replace('/[ \t]+/', ' ', preg_replace('@^[ \t]*\*@m', '', $var_line));
|
2020-03-05 17:31:58 +00:00
|
|
|
$var_line = preg_replace('/,\n\s+\}/', '}', $var_line);
|
|
|
|
$var_line = str_replace("\n", '', $var_line);
|
2018-07-15 17:23:17 -04:00
|
|
|
|
|
|
|
$var_line_parts = preg_split('/( |=)/', $var_line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
2018-12-19 16:15:19 -05:00
|
|
|
if (!$var_line_parts) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-15 17:23:17 -04:00
|
|
|
$type_alias = array_shift($var_line_parts);
|
|
|
|
|
|
|
|
if (!isset($var_line_parts[0])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_line_parts[0] === ' ') {
|
|
|
|
array_shift($var_line_parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_line_parts[0] === '=') {
|
|
|
|
array_shift($var_line_parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($var_line_parts[0])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_line_parts[0] === ' ') {
|
|
|
|
array_shift($var_line_parts);
|
|
|
|
}
|
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$type_string = str_replace("\n", '', implode('', $var_line_parts));
|
2018-07-15 17:23:17 -04:00
|
|
|
|
2019-06-16 12:45:02 -04:00
|
|
|
$type_string = preg_replace('/>[^>^\}]*$/', '>', $type_string);
|
|
|
|
$type_string = preg_replace('/\}[^>^\}]*$/', '}', $type_string);
|
|
|
|
|
2018-07-15 17:23:17 -04:00
|
|
|
try {
|
2020-05-13 19:12:45 -04:00
|
|
|
$type_tokens = TypeTokenizer::getFullyQualifiedTokens(
|
2018-07-15 17:23:17 -04:00
|
|
|
$type_string,
|
|
|
|
$aliases,
|
|
|
|
null,
|
2020-06-07 12:00:55 -04:00
|
|
|
$type_alias_tokens + $type_aliases,
|
|
|
|
$self_fqcln
|
2018-07-15 17:23:17 -04:00
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
|
|
|
throw new DocblockParseException($type_string . ' is not a valid type');
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:41:50 -04:00
|
|
|
$type_alias_tokens[$type_alias] = new TypeAlias\InlineTypeAlias($type_tokens);
|
2018-07-15 17:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $type_alias_tokens;
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:53:43 -04:00
|
|
|
/**
|
2016-12-03 19:11:30 -05:00
|
|
|
* @param int $line_number
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2016-11-12 23:59:31 -05:00
|
|
|
* @return FunctionDocblockComment
|
2016-10-14 00:53:43 -04:00
|
|
|
*/
|
2019-06-01 11:53:32 -04:00
|
|
|
public static function extractFunctionDocblockInfo(PhpParser\Comment\Doc $comment)
|
2018-07-21 22:55:16 -04:00
|
|
|
{
|
2019-06-26 16:26:14 -04:00
|
|
|
$parsed_docblock = DocComment::parsePreservingLength($comment);
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$comment_text = $comment->getText();
|
|
|
|
|
2016-11-12 23:59:31 -05:00
|
|
|
$info = new FunctionDocblockComment();
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2020-05-30 22:54:16 +02:00
|
|
|
self::checkDuplicatedTags($parsed_docblock);
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['return'])) {
|
2019-06-01 13:02:20 -04:00
|
|
|
self::extractReturnType(
|
|
|
|
$comment,
|
2020-05-28 22:14:41 -04:00
|
|
|
$parsed_docblock->combined_tags['return'],
|
2019-06-01 13:02:20 -04:00
|
|
|
$info
|
|
|
|
);
|
2016-06-23 18:45:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['param'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['param'] as $offset => $param) {
|
2019-01-31 20:21:20 -05:00
|
|
|
$line_parts = self::splitDocLine($param);
|
2016-06-23 18:45:46 -04:00
|
|
|
|
2017-05-29 23:21:46 -04:00
|
|
|
if (count($line_parts) === 1 && isset($line_parts[0][0]) && $line_parts[0][0] === '$') {
|
2018-03-30 17:46:12 -04:00
|
|
|
continue;
|
2017-01-15 23:49:58 -05:00
|
|
|
}
|
|
|
|
|
2016-12-25 11:32:21 +00:00
|
|
|
if (count($line_parts) > 1) {
|
2019-05-24 02:12:58 -04:00
|
|
|
if (preg_match('/^&?(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
|
2017-01-15 10:58:44 -05:00
|
|
|
&& $line_parts[0][0] !== '{'
|
2016-12-25 11:32:21 +00:00
|
|
|
) {
|
2019-04-09 14:23:48 -04:00
|
|
|
$line_parts[1] = str_replace('&', '', $line_parts[1]);
|
2016-12-25 11:32:21 +00:00
|
|
|
|
2017-01-28 00:37:52 -07:00
|
|
|
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$start = $offset + $comment->getFilePos();
|
|
|
|
$end = $start + strlen($line_parts[0]);
|
|
|
|
|
2020-04-03 14:56:11 -04:00
|
|
|
$line_parts[0] = self::sanitizeDocblockType($line_parts[0]);
|
2019-06-01 18:44:59 -04:00
|
|
|
|
2019-06-03 15:46:25 -04:00
|
|
|
if ($line_parts[0] === ''
|
|
|
|
|| ($line_parts[0][0] === '$'
|
|
|
|
&& !preg_match('/^\$this(\||$)/', $line_parts[0]))
|
|
|
|
) {
|
2019-06-01 18:44:59 -04:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
2016-12-25 11:32:21 +00:00
|
|
|
$info->params[] = [
|
2019-06-01 16:57:33 -04:00
|
|
|
'name' => trim($line_parts[1]),
|
2019-06-01 18:44:59 -04:00
|
|
|
'type' => $line_parts[0],
|
2019-06-01 16:57:33 -04:00
|
|
|
'line_number' => $comment->getLine() + substr_count($comment_text, "\n", 0, $offset),
|
|
|
|
'start' => $start,
|
|
|
|
'end' => $end,
|
2016-12-25 11:32:21 +00:00
|
|
|
];
|
2016-10-15 00:12:57 -04:00
|
|
|
}
|
2016-12-25 11:32:21 +00:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @param');
|
2016-06-23 18:45:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['param-out'])) {
|
|
|
|
foreach ($parsed_docblock->tags['param-out'] as $offset => $param) {
|
2019-01-31 20:21:20 -05:00
|
|
|
$line_parts = self::splitDocLine($param);
|
2019-01-19 13:32:43 -05:00
|
|
|
|
|
|
|
if (count($line_parts) === 1 && isset($line_parts[0][0]) && $line_parts[0][0] === '$') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($line_parts) > 1) {
|
|
|
|
if (!preg_match('/\[[^\]]+\]/', $line_parts[0])
|
|
|
|
&& preg_match('/^(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
|
|
|
|
&& $line_parts[0][0] !== '{'
|
|
|
|
) {
|
|
|
|
if ($line_parts[1][0] === '&') {
|
|
|
|
$line_parts[1] = substr($line_parts[1], 1);
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:46:25 -04:00
|
|
|
$line_parts[0] = str_replace("\n", '', preg_replace('@^[ \t]*\*@m', '', $line_parts[0]));
|
|
|
|
|
|
|
|
if ($line_parts[0] === ''
|
|
|
|
|| ($line_parts[0][0] === '$'
|
|
|
|
&& !preg_match('/^\$this(\||$)/', $line_parts[0]))
|
|
|
|
) {
|
2019-01-19 13:32:43 -05:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
|
|
|
|
|
|
|
$info->params_out[] = [
|
2019-06-01 16:57:33 -04:00
|
|
|
'name' => trim($line_parts[1]),
|
|
|
|
'type' => str_replace("\n", '', $line_parts[0]),
|
|
|
|
'line_number' => $comment->getLine() + substr_count($comment_text, "\n", 0, $offset),
|
2019-01-19 13:32:43 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @param');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 00:10:24 +02:00
|
|
|
if (isset($parsed_docblock->tags['psalm-self-out'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-self-out'] as $offset => $param) {
|
|
|
|
$line_parts = self::splitDocLine($param);
|
|
|
|
|
|
|
|
if (count($line_parts) > 0) {
|
|
|
|
$line_parts[0] = str_replace("\n", '', preg_replace('@^[ \t]*\*@m', '', $line_parts[0]));
|
|
|
|
|
|
|
|
$info->self_out = [
|
|
|
|
'type' => str_replace("\n", '', $line_parts[0]),
|
|
|
|
'line_number' => $comment->getLine() + substr_count($comment_text, "\n", 0, $offset),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-flow'])) {
|
2020-06-22 17:53:03 -04:00
|
|
|
foreach ($parsed_docblock->tags['psalm-flow'] as $param) {
|
|
|
|
$info->flows[] = trim($param);
|
|
|
|
}
|
2020-05-21 22:47:58 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-sink'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-taint-sink'] as $param) {
|
2020-05-21 22:47:58 -04:00
|
|
|
$param_parts = preg_split('/\s+/', trim($param));
|
|
|
|
|
|
|
|
if (count($param_parts) === 2) {
|
2020-06-21 11:43:08 -04:00
|
|
|
$info->taint_sink_params[] = ['name' => $param_parts[1], 'taint' => $param_parts[0]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// support for MediaWiki taint plugin
|
|
|
|
if (isset($parsed_docblock->tags['param-taint'])) {
|
|
|
|
foreach ($parsed_docblock->tags['param-taint'] as $param) {
|
|
|
|
$param_parts = preg_split('/\s+/', trim($param));
|
|
|
|
|
|
|
|
if (count($param_parts) === 2) {
|
|
|
|
$taint_type = $param_parts[1];
|
|
|
|
|
|
|
|
if (substr($taint_type, 0, 5) === 'exec_') {
|
|
|
|
$taint_type = substr($taint_type, 5);
|
|
|
|
|
|
|
|
if ($taint_type === 'tainted') {
|
|
|
|
$taint_type = 'input';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($taint_type === 'misc') {
|
|
|
|
$taint_type = 'text';
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->taint_sink_params[] = ['name' => $param_parts[0], 'taint' => $taint_type];
|
|
|
|
}
|
2020-05-21 22:47:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 00:58:56 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-source'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-taint-source'] as $param) {
|
|
|
|
$param_parts = preg_split('/\s+/', trim($param));
|
|
|
|
|
2020-06-21 11:43:08 -04:00
|
|
|
if ($param_parts[0]) {
|
|
|
|
$info->taint_source_types[] = $param_parts[0];
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 09:12:00 -04:00
|
|
|
} elseif (isset($parsed_docblock->tags['return-taint'])) {
|
|
|
|
// support for MediaWiki taint plugin
|
2020-06-21 11:43:08 -04:00
|
|
|
foreach ($parsed_docblock->tags['return-taint'] as $param) {
|
|
|
|
$param_parts = preg_split('/\s+/', trim($param));
|
|
|
|
|
|
|
|
if ($param_parts[0]) {
|
|
|
|
if ($param_parts[0] === 'tainted') {
|
|
|
|
$param_parts[0] = 'input';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_parts[0] === 'misc') {
|
|
|
|
$param_parts[0] = 'text';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_parts[0] !== 'none') {
|
|
|
|
$info->taint_source_types[] = $param_parts[0];
|
|
|
|
}
|
2020-06-21 00:58:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 11:43:08 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-unescape'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-taint-unescape'] as $param) {
|
2019-08-04 10:37:36 -04:00
|
|
|
$param = trim($param);
|
2020-05-21 22:47:58 -04:00
|
|
|
$info->added_taints[] = $param;
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 10:37:36 -04:00
|
|
|
|
2020-06-21 11:43:08 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-escape'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-taint-escape'] as $param) {
|
2020-05-21 22:47:58 -04:00
|
|
|
$param = trim($param);
|
|
|
|
$info->removed_taints[] = $param;
|
2019-08-04 10:37:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-assert-untainted'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-assert-untainted'] as $param) {
|
2019-08-04 10:37:36 -04:00
|
|
|
$param = trim($param);
|
|
|
|
|
|
|
|
$info->assert_untainted_params[] = ['name' => $param];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-specialize'])) {
|
2020-05-22 12:33:04 -04:00
|
|
|
$info->specialize_call = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['global'])) {
|
|
|
|
foreach ($parsed_docblock->tags['global'] as $offset => $global) {
|
2019-01-31 20:21:20 -05:00
|
|
|
$line_parts = self::splitDocLine($global);
|
2018-06-08 09:31:21 -04:00
|
|
|
|
|
|
|
if (count($line_parts) === 1 && isset($line_parts[0][0]) && $line_parts[0][0] === '$') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($line_parts) > 1) {
|
|
|
|
if (!preg_match('/\[[^\]]+\]/', $line_parts[0])
|
|
|
|
&& preg_match('/^(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
|
|
|
|
&& $line_parts[0][0] !== '{'
|
|
|
|
) {
|
|
|
|
if ($line_parts[1][0] === '&') {
|
|
|
|
$line_parts[1] = substr($line_parts[1], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($line_parts[0][0] === '$' && !preg_match('/^\$this(\||$)/', $line_parts[0])) {
|
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
|
|
|
|
|
|
|
$info->globals[] = [
|
|
|
|
'name' => $line_parts[1],
|
|
|
|
'type' => $line_parts[0],
|
2019-06-01 16:57:33 -04:00
|
|
|
'line_number' => $comment->getLine() + substr_count($comment_text, "\n", 0, $offset),
|
2018-06-08 09:31:21 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @param');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['deprecated'])) {
|
2016-11-12 23:59:31 -05:00
|
|
|
$info->deprecated = true;
|
2016-07-22 13:29:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['internal'])) {
|
2018-12-01 18:37:49 -05:00
|
|
|
$info->internal = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-internal'])) {
|
|
|
|
$psalm_internal = reset($parsed_docblock->tags['psalm-internal']);
|
2019-05-11 20:39:53 +01:00
|
|
|
if ($psalm_internal) {
|
|
|
|
$info->psalm_internal = $psalm_internal;
|
2019-05-11 16:55:37 +01:00
|
|
|
} else {
|
2019-05-14 23:27:27 +01:00
|
|
|
throw new DocblockParseException('@psalm-internal annotation used without specifying namespace');
|
2019-05-11 16:55:37 +01:00
|
|
|
}
|
2020-05-28 22:14:41 -04:00
|
|
|
$info->psalm_internal = reset($parsed_docblock->tags['psalm-internal']);
|
2020-07-26 10:47:48 -04:00
|
|
|
$info->internal = true;
|
2019-05-11 15:15:50 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-suppress'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-suppress'] as $offset => $suppress_entry) {
|
2019-08-18 14:27:50 -04:00
|
|
|
$info->suppressed_issues[$offset + $comment->getFilePos()] = preg_split('/[\s]+/', $suppress_entry)[0];
|
2016-07-22 13:29:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['throws'])) {
|
|
|
|
foreach ($parsed_docblock->tags['throws'] as $offset => $throws_entry) {
|
2019-01-06 10:01:35 -05:00
|
|
|
$throws_class = preg_split('/[\s]+/', $throws_entry)[0];
|
|
|
|
|
|
|
|
if (!$throws_class) {
|
|
|
|
throw new IncorrectDocblockException('Unexpectedly empty @throws');
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:44:18 -04:00
|
|
|
$info->throws[] = [
|
|
|
|
$throws_class,
|
|
|
|
$offset + $comment->getFilePos(),
|
|
|
|
$comment->getLine() + substr_count($comment->getText(), "\n", 0, $offset)
|
|
|
|
];
|
2018-06-22 01:13:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (strpos(strtolower($parsed_docblock->description), '@inheritdoc') !== false
|
|
|
|
|| isset($parsed_docblock->tags['inheritdoc'])
|
|
|
|
|| isset($parsed_docblock->tags['inheritDoc'])
|
|
|
|
) {
|
2018-12-21 11:01:24 -05:00
|
|
|
$info->inheritdoc = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['template'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['template'] as $template_line) {
|
2019-06-01 18:44:59 -04:00
|
|
|
$template_type = preg_split('/[\s]+/', preg_replace('@^[ \t]*\*@m', '', $template_line));
|
2017-02-09 20:35:17 -05:00
|
|
|
|
2019-05-06 16:38:08 -04:00
|
|
|
$template_name = array_shift($template_type);
|
|
|
|
|
2019-12-08 21:19:29 -05:00
|
|
|
if (!$template_name) {
|
|
|
|
throw new IncorrectDocblockException('Empty @template tag');
|
|
|
|
}
|
|
|
|
|
2019-05-06 16:38:08 -04:00
|
|
|
if (count($template_type) > 1
|
|
|
|
&& in_array(strtolower($template_type[0]), ['as', 'super', 'of'], true)
|
2019-01-16 09:23:18 -05:00
|
|
|
) {
|
2019-05-06 16:38:08 -04:00
|
|
|
$template_modifier = strtolower(array_shift($template_type));
|
2018-12-17 23:29:27 -05:00
|
|
|
$info->templates[] = [
|
2019-05-06 16:38:08 -04:00
|
|
|
$template_name,
|
|
|
|
$template_modifier,
|
|
|
|
implode(' ', $template_type),
|
|
|
|
false
|
2018-06-19 13:19:34 -04:00
|
|
|
];
|
2017-02-09 20:35:17 -05:00
|
|
|
} else {
|
2019-05-06 16:38:08 -04:00
|
|
|
$info->templates[] = [$template_name, null, null, false];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['template-typeof'])) {
|
|
|
|
foreach ($parsed_docblock->tags['template-typeof'] as $template_typeof) {
|
2019-06-01 18:44:59 -04:00
|
|
|
$typeof_parts = preg_split('/[\s]+/', preg_replace('@^[ \t]*\*@m', '', $template_typeof));
|
2017-02-09 20:35:17 -05:00
|
|
|
|
2020-03-24 14:49:05 -04:00
|
|
|
if ($typeof_parts === false || count($typeof_parts) < 2 || $typeof_parts[1][0] !== '$') {
|
2017-11-14 21:43:31 -05:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
2017-02-09 20:35:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$info->template_typeofs[] = [
|
|
|
|
'template_type' => $typeof_parts[0],
|
|
|
|
'param_name' => substr($typeof_parts[1], 1),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-assert'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-assert'] as $assertion) {
|
2019-09-21 21:50:11 -04:00
|
|
|
$line_parts = self::splitDocLine($assertion);
|
2018-05-28 20:07:42 +01:00
|
|
|
|
2019-09-21 21:50:11 -04:00
|
|
|
if (count($line_parts) < 2 || $line_parts[1][0] !== '$') {
|
2018-05-28 20:07:42 +01:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:56:11 -04:00
|
|
|
$line_parts[0] = self::sanitizeDocblockType($line_parts[0]);
|
|
|
|
|
2018-05-28 20:07:42 +01:00
|
|
|
$info->assertions[] = [
|
2019-09-21 21:50:11 -04:00
|
|
|
'type' => $line_parts[0],
|
|
|
|
'param_name' => substr($line_parts[1], 1),
|
2018-05-28 20:07:42 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-assert-if-true'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-assert-if-true'] as $assertion) {
|
2019-12-19 19:24:26 +00:00
|
|
|
$line_parts = self::splitDocLine($assertion);
|
2018-05-28 20:07:42 +01:00
|
|
|
|
2019-12-19 19:24:26 +00:00
|
|
|
if (count($line_parts) < 2 || $line_parts[1][0] !== '$') {
|
2018-05-28 20:07:42 +01:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->if_true_assertions[] = [
|
2019-12-19 19:24:26 +00:00
|
|
|
'type' => $line_parts[0],
|
|
|
|
'param_name' => substr($line_parts[1], 1),
|
2018-05-28 20:07:42 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-assert-if-false'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-assert-if-false'] as $assertion) {
|
2019-12-19 19:24:26 +00:00
|
|
|
$line_parts = self::splitDocLine($assertion);
|
2018-05-28 20:07:42 +01:00
|
|
|
|
2019-12-19 19:24:26 +00:00
|
|
|
if (count($line_parts) < 2 || $line_parts[1][0] !== '$') {
|
2018-05-28 20:07:42 +01:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->if_false_assertions[] = [
|
2019-12-19 19:24:26 +00:00
|
|
|
'type' => $line_parts[0],
|
|
|
|
'param_name' => substr($line_parts[1], 1),
|
2018-05-28 20:07:42 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
$info->variadic = isset($parsed_docblock->tags['psalm-variadic']);
|
|
|
|
$info->pure = isset($parsed_docblock->tags['psalm-pure'])
|
|
|
|
|| isset($parsed_docblock->tags['pure']);
|
2019-08-31 00:47:12 -04:00
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-mutation-free'])) {
|
2019-08-31 00:47:12 -04:00
|
|
|
$info->mutation_free = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-external-mutation-free'])) {
|
2019-08-31 00:47:12 -04:00
|
|
|
$info->external_mutation_free = true;
|
|
|
|
}
|
|
|
|
|
2020-08-14 00:27:33 -04:00
|
|
|
if (isset($parsed_docblock->tags['no-named-arguments'])) {
|
|
|
|
$info->no_named_args = true;
|
2020-08-10 09:58:43 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
$info->ignore_nullable_return = isset($parsed_docblock->tags['psalm-ignore-nullable-return']);
|
|
|
|
$info->ignore_falsable_return = isset($parsed_docblock->tags['psalm-ignore-falsable-return']);
|
2016-10-18 17:55:07 -04:00
|
|
|
|
2016-06-23 18:45:46 -04:00
|
|
|
return $info;
|
|
|
|
}
|
2016-10-28 00:11:16 -04:00
|
|
|
|
2017-02-09 20:35:17 -05:00
|
|
|
/**
|
2019-06-01 16:57:33 -04:00
|
|
|
* @param array<int, string> $return_specials
|
2019-01-09 11:51:29 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-06-01 13:02:20 -04:00
|
|
|
private static function extractReturnType(
|
|
|
|
PhpParser\Comment\Doc $comment,
|
2019-06-01 16:57:33 -04:00
|
|
|
array $return_specials,
|
2019-06-01 13:02:20 -04:00
|
|
|
FunctionDocblockComment $info
|
|
|
|
) {
|
2019-06-01 16:57:33 -04:00
|
|
|
foreach ($return_specials as $offset => $return_block) {
|
|
|
|
$return_lines = explode("\n", $return_block);
|
2019-01-09 11:51:29 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
if (!trim($return_lines[0])) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-09 11:51:29 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$return_block = trim($return_block);
|
2019-01-09 11:51:29 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
if (!$return_block) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-09 11:51:29 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$line_parts = self::splitDocLine($return_block);
|
2019-01-09 11:51:29 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
if ($line_parts[0][0] !== '{') {
|
|
|
|
if ($line_parts[0][0] === '$' && !preg_match('/^\$this(\||$)/', $line_parts[0])) {
|
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
2019-01-09 11:51:29 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$start = $offset + $comment->getFilePos();
|
|
|
|
$end = $start + strlen($line_parts[0]);
|
2019-06-01 13:02:20 -04:00
|
|
|
|
2020-04-03 14:56:11 -04:00
|
|
|
$line_parts[0] = self::sanitizeDocblockType($line_parts[0]);
|
2019-06-01 18:44:59 -04:00
|
|
|
|
2020-02-12 09:18:43 -05:00
|
|
|
$info->return_type = array_shift($line_parts);
|
2019-06-01 16:57:33 -04:00
|
|
|
$info->return_type_description = $line_parts ? implode(' ', $line_parts) : null;
|
2019-01-09 11:51:29 -05:00
|
|
|
|
2019-06-01 16:57:33 -04:00
|
|
|
$info->return_type_line_number
|
|
|
|
= $comment->getLine() + substr_count($comment->getText(), "\n", 0, $offset);
|
|
|
|
$info->return_type_start = $start;
|
|
|
|
$info->return_type_end = $end;
|
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @return type');
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2019-01-09 11:51:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-26 20:16:18 -04:00
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2017-02-09 20:35:17 -05:00
|
|
|
* @return ClassLikeDocblockComment
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
*/
|
2019-08-08 17:25:56 -04:00
|
|
|
public static function extractClassLikeDocblockInfo(
|
|
|
|
\PhpParser\Node $node,
|
|
|
|
PhpParser\Comment\Doc $comment,
|
|
|
|
Aliases $aliases
|
|
|
|
) {
|
2019-06-26 16:26:14 -04:00
|
|
|
$parsed_docblock = DocComment::parsePreservingLength($comment);
|
2020-04-08 06:35:53 -07:00
|
|
|
$codebase = ProjectAnalyzer::getInstance()->getCodebase();
|
2017-02-09 20:35:17 -05:00
|
|
|
|
|
|
|
$info = new ClassLikeDocblockComment();
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['template'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['template'] as $offset => $template_line) {
|
2019-06-01 18:44:59 -04:00
|
|
|
$template_type = preg_split('/[\s]+/', preg_replace('@^[ \t]*\*@m', '', $template_line));
|
2017-02-09 20:35:17 -05:00
|
|
|
|
2019-03-01 09:20:51 -05:00
|
|
|
$template_name = array_shift($template_type);
|
|
|
|
|
2019-12-08 21:19:29 -05:00
|
|
|
if (!$template_name) {
|
|
|
|
throw new IncorrectDocblockException('Empty @template tag');
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:20:51 -05:00
|
|
|
if (count($template_type) > 1
|
|
|
|
&& in_array(strtolower($template_type[0]), ['as', 'super', 'of'], true)
|
2019-01-16 09:23:18 -05:00
|
|
|
) {
|
2019-03-01 09:20:51 -05:00
|
|
|
$template_modifier = strtolower(array_shift($template_type));
|
2018-12-17 23:29:27 -05:00
|
|
|
$info->templates[] = [
|
2019-03-01 09:20:51 -05:00
|
|
|
$template_name,
|
|
|
|
$template_modifier,
|
2019-05-06 16:38:08 -04:00
|
|
|
implode(' ', $template_type),
|
2019-11-30 01:02:23 -05:00
|
|
|
false,
|
|
|
|
$offset
|
2019-05-06 16:38:08 -04:00
|
|
|
];
|
|
|
|
} else {
|
2019-11-30 01:02:23 -05:00
|
|
|
$info->templates[] = [$template_name, null, null, false, $offset];
|
2019-05-06 16:38:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['template-covariant'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['template-covariant'] as $offset => $template_line) {
|
2019-06-01 18:44:59 -04:00
|
|
|
$template_type = preg_split('/[\s]+/', preg_replace('@^[ \t]*\*@m', '', $template_line));
|
2019-05-06 16:38:08 -04:00
|
|
|
|
|
|
|
$template_name = array_shift($template_type);
|
|
|
|
|
2019-12-08 21:19:29 -05:00
|
|
|
if (!$template_name) {
|
|
|
|
throw new IncorrectDocblockException('Empty @template-covariant tag');
|
|
|
|
}
|
|
|
|
|
2019-05-06 16:38:08 -04:00
|
|
|
if (count($template_type) > 1
|
|
|
|
&& in_array(strtolower($template_type[0]), ['as', 'super', 'of'], true)
|
|
|
|
) {
|
|
|
|
$template_modifier = strtolower(array_shift($template_type));
|
|
|
|
$info->templates[] = [
|
|
|
|
$template_name,
|
|
|
|
$template_modifier,
|
|
|
|
implode(' ', $template_type),
|
2019-11-30 01:02:23 -05:00
|
|
|
true,
|
|
|
|
$offset
|
2018-06-19 13:19:34 -04:00
|
|
|
];
|
2017-02-09 20:35:17 -05:00
|
|
|
} else {
|
2019-11-30 01:02:23 -05:00
|
|
|
$info->templates[] = [$template_name, null, null, true, $offset];
|
2017-02-09 20:35:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['extends'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['extends'] as $template_line) {
|
2019-06-03 10:28:54 -04:00
|
|
|
$info->template_extends[] = trim(preg_replace('@^[ \t]*\*@m', '', $template_line));
|
2019-01-10 16:59:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['implements'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['implements'] as $template_line) {
|
2019-06-03 10:28:54 -04:00
|
|
|
$info->template_implements[] = trim(preg_replace('@^[ \t]*\*@m', '', $template_line));
|
2018-05-28 22:26:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-yield'])
|
2020-04-02 22:38:10 -04:00
|
|
|
) {
|
2020-05-28 22:14:41 -04:00
|
|
|
$yield = reset($parsed_docblock->tags['psalm-yield']);
|
2020-04-02 22:38:10 -04:00
|
|
|
|
|
|
|
$info->yield = trim(preg_replace('@^[ \t]*\*@m', '', $yield));
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['deprecated'])) {
|
2017-05-04 18:35:05 -04:00
|
|
|
$info->deprecated = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['internal'])) {
|
2018-12-01 18:37:49 -05:00
|
|
|
$info->internal = true;
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:59 +03:00
|
|
|
if (isset($parsed_docblock->tags['final'])) {
|
|
|
|
$info->final = true;
|
|
|
|
}
|
|
|
|
|
2020-08-05 19:39:27 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-consistent-constructor'])) {
|
|
|
|
$info->consistent_constructor = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-internal'])) {
|
|
|
|
$psalm_internal = reset($parsed_docblock->tags['psalm-internal']);
|
2019-05-11 20:39:53 +01:00
|
|
|
if ($psalm_internal) {
|
|
|
|
$info->psalm_internal = $psalm_internal;
|
2019-05-11 16:55:37 +01:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('psalm-internal annotation used without specifying namespace');
|
|
|
|
}
|
2019-05-14 23:27:27 +01:00
|
|
|
|
2020-07-26 10:47:48 -04:00
|
|
|
$info->internal = true;
|
2019-05-09 23:33:27 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['mixin'])) {
|
2020-08-05 21:49:19 +02:00
|
|
|
foreach ($parsed_docblock->tags['mixin'] as $rawMixin) {
|
|
|
|
$mixin = trim($rawMixin);
|
|
|
|
$doc_line_parts = self::splitDocLine($mixin);
|
|
|
|
$mixin = $doc_line_parts[0];
|
2020-01-02 23:50:19 -05:00
|
|
|
|
2020-08-05 21:49:19 +02:00
|
|
|
if ($mixin) {
|
|
|
|
$info->mixins[] = $mixin;
|
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('@mixin annotation used without specifying class');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// backwards compatibility
|
|
|
|
if ($info->mixins) {
|
|
|
|
/** @psalm-suppress DeprecatedProperty */
|
|
|
|
$info->mixin = reset($info->mixins);
|
2020-01-02 23:50:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-seal-properties'])) {
|
2017-11-16 20:47:58 -05:00
|
|
|
$info->sealed_properties = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-seal-methods'])) {
|
2018-04-22 00:40:30 -04:00
|
|
|
$info->sealed_methods = true;
|
2018-11-24 18:31:00 -05:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-immutable'])
|
|
|
|
|| isset($parsed_docblock->tags['psalm-mutation-free'])
|
2019-08-30 12:36:35 -04:00
|
|
|
) {
|
|
|
|
$info->mutation_free = true;
|
2019-08-31 10:07:23 -04:00
|
|
|
$info->external_mutation_free = true;
|
2020-06-19 01:22:51 -04:00
|
|
|
$info->taint_specialize = true;
|
2019-08-30 12:36:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-external-mutation-free'])) {
|
2019-08-30 12:36:35 -04:00
|
|
|
$info->external_mutation_free = true;
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:22:51 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-specialize'])) {
|
|
|
|
$info->taint_specialize = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-override-property-visibility'])) {
|
2018-11-25 11:11:33 -05:00
|
|
|
$info->override_property_visibility = true;
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-override-method-visibility'])) {
|
2018-11-25 11:11:33 -05:00
|
|
|
$info->override_method_visibility = true;
|
2018-04-22 00:40:30 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-suppress'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-suppress'] as $offset => $suppress_entry) {
|
2019-08-18 14:27:50 -04:00
|
|
|
$info->suppressed_issues[$offset + $comment->getFilePos()] = preg_split('/[\s]+/', $suppress_entry)[0];
|
2017-09-13 11:32:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->tags['psalm-import-type'])) {
|
2020-08-17 05:53:53 +03:00
|
|
|
foreach ($parsed_docblock->tags['psalm-import-type'] as $offset => $imported_type_entry) {
|
|
|
|
$info->imported_types[] = [
|
|
|
|
'line_number' => $comment->getLine() + substr_count($comment->getText(), "\n", 0, $offset),
|
|
|
|
'start_offset' => $comment->getFilePos() + $offset,
|
|
|
|
'end_offset' => $comment->getFilePos() + $offset + strlen($imported_type_entry),
|
|
|
|
'parts' => self::splitDocLine($imported_type_entry) ?: []
|
|
|
|
];
|
2020-05-14 00:41:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
if (isset($parsed_docblock->combined_tags['method'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['method'] as $offset => $method_entry) {
|
2018-04-23 09:52:40 -04:00
|
|
|
$method_entry = preg_replace('/[ \t]+/', ' ', trim($method_entry));
|
2018-04-21 22:13:10 -04:00
|
|
|
|
2018-07-07 00:06:05 -04:00
|
|
|
$docblock_lines = [];
|
2018-04-21 22:13:10 -04:00
|
|
|
|
2019-01-18 11:37:52 -05:00
|
|
|
$is_static = false;
|
|
|
|
|
2020-06-20 15:30:47 -04:00
|
|
|
$has_return = false;
|
|
|
|
|
2018-04-21 22:13:10 -04:00
|
|
|
if (!preg_match('/^([a-z_A-Z][a-z_0-9A-Z]+) *\(/', $method_entry, $matches)) {
|
|
|
|
$doc_line_parts = self::splitDocLine($method_entry);
|
|
|
|
|
2019-01-30 11:44:12 -05:00
|
|
|
if ($doc_line_parts[0] === 'static' && !strpos($doc_line_parts[1], '(')) {
|
2019-01-18 11:37:52 -05:00
|
|
|
$is_static = true;
|
|
|
|
array_shift($doc_line_parts);
|
|
|
|
}
|
|
|
|
|
2019-09-07 22:30:09 -04:00
|
|
|
if (count($doc_line_parts) > 1) {
|
|
|
|
$docblock_lines[] = '@return ' . array_shift($doc_line_parts);
|
2020-06-20 15:30:47 -04:00
|
|
|
$has_return = true;
|
2018-04-21 22:13:10 -04:00
|
|
|
|
2019-09-07 22:30:09 -04:00
|
|
|
$method_entry = implode(' ', $doc_line_parts);
|
|
|
|
}
|
2018-04-21 22:13:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$method_entry = trim(preg_replace('/\/\/.*/', '', $method_entry));
|
|
|
|
|
2019-12-11 11:40:47 -05:00
|
|
|
$method_entry = preg_replace(
|
|
|
|
'/array\(([0-9a-zA-Z_\'\" ]+,)*([0-9a-zA-Z_\'\" ]+)\)/',
|
|
|
|
'[]',
|
|
|
|
$method_entry
|
|
|
|
);
|
2019-12-11 11:29:00 -05:00
|
|
|
|
2018-04-23 09:52:40 -04:00
|
|
|
$end_of_method_regex = '/(?<!array\()\) ?(\: ?(\??[\\\\a-zA-Z0-9_]+))?/';
|
|
|
|
|
|
|
|
if (preg_match($end_of_method_regex, $method_entry, $matches, PREG_OFFSET_CAPTURE)) {
|
|
|
|
$method_entry = substr($method_entry, 0, (int) $matches[0][1] + strlen((string) $matches[0][0]));
|
|
|
|
}
|
|
|
|
|
2018-07-07 00:06:05 -04:00
|
|
|
$method_entry = str_replace([', ', '( '], [',', '('], $method_entry);
|
|
|
|
$method_entry = preg_replace('/ (?!(\$|\.\.\.|&))/', '', trim($method_entry));
|
2018-06-28 17:39:25 -04:00
|
|
|
|
2019-12-11 11:29:00 -05:00
|
|
|
// replace array bracket contents
|
|
|
|
$method_entry = preg_replace('/\[([0-9a-zA-Z_\'\" ]+,)*([0-9a-zA-Z_\'\" ]+)\]/', '[]', $method_entry);
|
|
|
|
|
2020-07-01 09:00:33 -04:00
|
|
|
if (!$method_entry) {
|
|
|
|
throw new DocblockParseException('No @method entry specified');
|
|
|
|
}
|
|
|
|
|
2018-07-07 00:06:05 -04:00
|
|
|
try {
|
2020-05-28 14:31:09 -04:00
|
|
|
$parse_tree_creator = new ParseTreeCreator(
|
2020-05-13 19:12:45 -04:00
|
|
|
TypeTokenizer::getFullyQualifiedTokens(
|
2019-08-08 17:25:56 -04:00
|
|
|
$method_entry,
|
|
|
|
$aliases,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
);
|
2020-05-28 14:31:09 -04:00
|
|
|
|
|
|
|
$method_tree = $parse_tree_creator->create();
|
2018-07-07 00:06:05 -04:00
|
|
|
} catch (TypeParseTreeException $e) {
|
|
|
|
throw new DocblockParseException($method_entry . ' is not a valid method');
|
|
|
|
}
|
|
|
|
|
2018-11-18 12:41:47 -05:00
|
|
|
if (!$method_tree instanceof ParseTree\MethodWithReturnTypeTree
|
|
|
|
&& !$method_tree instanceof ParseTree\MethodTree) {
|
2018-07-07 00:06:05 -04:00
|
|
|
throw new DocblockParseException($method_entry . ' is not a valid method');
|
|
|
|
}
|
|
|
|
|
2018-11-18 12:41:47 -05:00
|
|
|
if ($method_tree instanceof ParseTree\MethodWithReturnTypeTree) {
|
2020-06-20 15:30:47 -04:00
|
|
|
if (!$has_return) {
|
|
|
|
$docblock_lines[] = '@return ' . TypeParser::getTypeFromTree(
|
|
|
|
$method_tree->children[1],
|
|
|
|
$codebase
|
|
|
|
)->toNamespacedString($aliases->namespace, $aliases->uses, null, false);
|
|
|
|
}
|
|
|
|
|
2018-07-07 00:06:05 -04:00
|
|
|
$method_tree = $method_tree->children[0];
|
|
|
|
}
|
|
|
|
|
2018-11-18 12:41:47 -05:00
|
|
|
if (!$method_tree instanceof ParseTree\MethodTree) {
|
2018-07-07 00:06:05 -04:00
|
|
|
throw new DocblockParseException($method_entry . ' is not a valid method');
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = [];
|
|
|
|
|
|
|
|
foreach ($method_tree->children as $method_tree_child) {
|
2018-11-18 12:41:47 -05:00
|
|
|
if (!$method_tree_child instanceof ParseTree\MethodParamTree) {
|
2018-07-07 00:06:05 -04:00
|
|
|
throw new DocblockParseException($method_entry . ' is not a valid method');
|
|
|
|
}
|
|
|
|
|
|
|
|
$args[] = ($method_tree_child->byref ? '&' : '')
|
|
|
|
. ($method_tree_child->variadic ? '...' : '')
|
|
|
|
. $method_tree_child->name
|
2018-07-10 13:01:44 -07:00
|
|
|
. ($method_tree_child->default != '' ? ' = ' . $method_tree_child->default : '');
|
2018-07-07 00:06:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
if ($method_tree_child->children) {
|
2020-05-11 18:39:07 -04:00
|
|
|
try {
|
2020-05-13 19:12:45 -04:00
|
|
|
$param_type = TypeParser::getTypeFromTree($method_tree_child->children[0], $codebase);
|
2020-05-11 18:39:07 -04:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new DocblockParseException(
|
|
|
|
'Badly-formatted @method string ' . $method_entry . ' - ' . $e
|
|
|
|
);
|
|
|
|
}
|
2019-08-08 17:25:56 -04:00
|
|
|
$docblock_lines[] = '@param \\' . $param_type . ' '
|
2018-07-07 00:06:05 -04:00
|
|
|
. ($method_tree_child->variadic ? '...' : '')
|
|
|
|
. $method_tree_child->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$function_string = 'function ' . $method_tree->value . '(' . implode(', ', $args) . ')';
|
|
|
|
|
2019-01-18 11:37:52 -05:00
|
|
|
if ($is_static) {
|
|
|
|
$function_string = 'static ' . $function_string;
|
|
|
|
}
|
|
|
|
|
2018-07-07 00:06:05 -04:00
|
|
|
$function_docblock = $docblock_lines ? "/**\n * " . implode("\n * ", $docblock_lines) . "\n*/\n" : "";
|
2018-06-05 17:59:32 -04:00
|
|
|
|
2018-07-07 00:06:05 -04:00
|
|
|
$php_string = '<?php class A { ' . $function_docblock . ' public ' . $function_string . '{} }';
|
2018-04-21 22:13:10 -04:00
|
|
|
|
|
|
|
try {
|
2020-08-09 16:23:43 -04:00
|
|
|
$statements = \Psalm\Internal\Provider\StatementsProvider::parseStatements(
|
|
|
|
$php_string,
|
|
|
|
$codebase->php_major_version . '.' . $codebase->php_minor_version
|
|
|
|
);
|
2018-04-21 22:13:10 -04:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new DocblockParseException('Badly-formatted @method string ' . $method_entry);
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:16:58 -04:00
|
|
|
if (!$statements
|
|
|
|
|| !$statements[0] instanceof \PhpParser\Node\Stmt\Class_
|
2018-06-14 17:20:02 -04:00
|
|
|
|| !isset($statements[0]->stmts[0])
|
|
|
|
|| !$statements[0]->stmts[0] instanceof \PhpParser\Node\Stmt\ClassMethod
|
|
|
|
) {
|
2018-04-23 09:52:40 -04:00
|
|
|
throw new DocblockParseException('Badly-formatted @method string ' . $method_entry);
|
2018-04-21 22:13:10 -04:00
|
|
|
}
|
|
|
|
|
2019-03-18 21:05:37 -04:00
|
|
|
/** @var \PhpParser\Comment\Doc */
|
|
|
|
$node_doc_comment = $node->getDocComment();
|
|
|
|
|
|
|
|
$statements[0]->stmts[0]->setAttribute('startLine', $node_doc_comment->getLine());
|
|
|
|
$statements[0]->stmts[0]->setAttribute('startFilePos', $node_doc_comment->getFilePos());
|
|
|
|
$statements[0]->stmts[0]->setAttribute('endFilePos', $node->getAttribute('startFilePos'));
|
|
|
|
|
|
|
|
if ($doc_comment = $statements[0]->stmts[0]->getDocComment()) {
|
|
|
|
$statements[0]->stmts[0]->setDocComment(
|
|
|
|
new \PhpParser\Comment\Doc(
|
|
|
|
$doc_comment->getText(),
|
2019-06-01 16:57:33 -04:00
|
|
|
$comment->getLine() + substr_count($comment->getText(), "\n", 0, $offset),
|
2019-03-18 21:05:37 -04:00
|
|
|
$node_doc_comment->getFilePos()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:20:02 -04:00
|
|
|
$info->methods[] = $statements[0]->stmts[0];
|
2018-04-21 22:13:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:14:41 -04:00
|
|
|
self::addMagicPropertyToInfo($comment, $info, $parsed_docblock->tags, 'property');
|
|
|
|
self::addMagicPropertyToInfo($comment, $info, $parsed_docblock->tags, 'psalm-property');
|
|
|
|
self::addMagicPropertyToInfo($comment, $info, $parsed_docblock->tags, 'property-read');
|
|
|
|
self::addMagicPropertyToInfo($comment, $info, $parsed_docblock->tags, 'psalm-property-read');
|
|
|
|
self::addMagicPropertyToInfo($comment, $info, $parsed_docblock->tags, 'property-write');
|
|
|
|
self::addMagicPropertyToInfo($comment, $info, $parsed_docblock->tags, 'psalm-property-write');
|
2017-05-04 18:35:05 -04:00
|
|
|
|
2017-12-14 11:22:27 -08:00
|
|
|
return $info;
|
|
|
|
}
|
2017-05-04 18:35:05 -04:00
|
|
|
|
2017-12-14 11:22:27 -08:00
|
|
|
/**
|
|
|
|
* @param ClassLikeDocblockComment $info
|
2018-01-10 10:56:43 -05:00
|
|
|
* @param array<string, array<int, string>> $specials
|
2020-03-24 18:32:57 -04:00
|
|
|
* @param 'property'|'psalm-property'|'property-read'|
|
|
|
|
* 'psalm-property-read'|'property-write'|'psalm-property-write' $property_tag
|
2017-12-15 16:48:06 -05:00
|
|
|
*
|
2017-12-14 11:22:27 -08:00
|
|
|
* @throws DocblockParseException
|
2017-12-15 16:48:06 -05:00
|
|
|
*
|
|
|
|
* @return void
|
2017-12-14 11:22:27 -08:00
|
|
|
*/
|
2019-06-01 16:57:33 -04:00
|
|
|
protected static function addMagicPropertyToInfo(
|
|
|
|
PhpParser\Comment\Doc $comment,
|
|
|
|
ClassLikeDocblockComment $info,
|
|
|
|
array $specials,
|
|
|
|
string $property_tag
|
|
|
|
) : void {
|
2017-12-14 11:22:27 -08:00
|
|
|
$magic_property_comments = isset($specials[$property_tag]) ? $specials[$property_tag] : [];
|
2019-06-01 16:57:33 -04:00
|
|
|
|
|
|
|
foreach ($magic_property_comments as $offset => $property) {
|
2019-01-31 20:21:20 -05:00
|
|
|
$line_parts = self::splitDocLine($property);
|
2017-05-04 18:35:05 -04:00
|
|
|
|
2019-06-14 10:45:00 -04:00
|
|
|
if (count($line_parts) === 1 && isset($line_parts[0][0]) && $line_parts[0][0] === '$') {
|
|
|
|
continue;
|
2017-12-14 11:22:27 -08:00
|
|
|
}
|
2017-05-24 21:11:18 -04:00
|
|
|
|
2017-12-14 11:22:27 -08:00
|
|
|
if (count($line_parts) > 1) {
|
2019-06-14 10:45:00 -04:00
|
|
|
if (preg_match('/^&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
|
2017-12-14 11:22:27 -08:00
|
|
|
&& $line_parts[0][0] !== '{'
|
|
|
|
) {
|
2019-06-14 10:45:00 -04:00
|
|
|
$line_parts[1] = str_replace('&', '', $line_parts[1]);
|
|
|
|
|
|
|
|
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
|
|
|
|
|
|
|
$start = $offset + $comment->getFilePos();
|
|
|
|
$end = $start + strlen($line_parts[0]);
|
|
|
|
|
|
|
|
$line_parts[0] = str_replace("\n", '', preg_replace('@^[ \t]*\*@m', '', $line_parts[0]));
|
2017-05-04 18:35:05 -04:00
|
|
|
|
2019-06-14 10:45:00 -04:00
|
|
|
if ($line_parts[0] === ''
|
|
|
|
|| ($line_parts[0][0] === '$'
|
|
|
|
&& !preg_match('/^\$this(\||$)/', $line_parts[0]))
|
|
|
|
) {
|
2017-12-14 11:22:27 -08:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
2017-05-04 18:35:05 -04:00
|
|
|
}
|
2017-12-14 11:22:27 -08:00
|
|
|
|
2020-04-26 10:10:14 -04:00
|
|
|
$name = trim($line_parts[1]);
|
|
|
|
|
|
|
|
if (!preg_match('/^\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$/', $name)) {
|
|
|
|
throw new DocblockParseException('Badly-formatted @property name');
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:22:27 -08:00
|
|
|
$info->properties[] = [
|
2020-04-26 10:10:14 -04:00
|
|
|
'name' => $name,
|
2017-12-14 11:22:27 -08:00
|
|
|
'type' => $line_parts[0],
|
2019-06-01 16:57:33 -04:00
|
|
|
'line_number' => $comment->getLine() + substr_count($comment->getText(), "\n", 0, $offset),
|
2017-12-14 11:22:27 -08:00
|
|
|
'tag' => $property_tag,
|
2019-06-14 10:45:00 -04:00
|
|
|
'start' => $start,
|
|
|
|
'end' => $end,
|
2017-12-14 11:22:27 -08:00
|
|
|
];
|
2017-05-04 18:35:05 -04:00
|
|
|
}
|
2017-12-14 11:22:27 -08:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @property');
|
2017-05-04 18:35:05 -04:00
|
|
|
}
|
|
|
|
}
|
2017-02-09 20:35:17 -05:00
|
|
|
}
|
|
|
|
|
2016-10-28 00:11:16 -04:00
|
|
|
/**
|
|
|
|
* @param string $return_block
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if an invalid string is found
|
|
|
|
*
|
2020-08-17 05:53:53 +03:00
|
|
|
* @return list<string>
|
2016-10-28 00:11:16 -04:00
|
|
|
*/
|
2018-01-07 10:23:02 -05:00
|
|
|
public static function splitDocLine($return_block)
|
2016-10-28 00:11:16 -04:00
|
|
|
{
|
|
|
|
$brackets = '';
|
|
|
|
|
|
|
|
$type = '';
|
|
|
|
|
2018-03-26 22:13:10 -04:00
|
|
|
$expects_callable_return = false;
|
|
|
|
|
2019-06-01 18:44:59 -04:00
|
|
|
$return_block = str_replace("\t", ' ', $return_block);
|
2018-03-26 22:13:10 -04:00
|
|
|
|
2018-05-20 17:19:53 -04:00
|
|
|
$quote_char = null;
|
|
|
|
$escaped = false;
|
|
|
|
|
2018-03-26 22:13:10 -04:00
|
|
|
for ($i = 0, $l = strlen($return_block); $i < $l; ++$i) {
|
2016-10-28 00:11:16 -04:00
|
|
|
$char = $return_block[$i];
|
2018-03-26 22:13:10 -04:00
|
|
|
$next_char = $i < $l - 1 ? $return_block[$i + 1] : null;
|
2019-02-01 07:50:48 -05:00
|
|
|
$last_char = $i > 0 ? $return_block[$i - 1] : null;
|
2016-10-28 00:11:16 -04:00
|
|
|
|
2018-05-20 17:19:53 -04:00
|
|
|
if ($quote_char) {
|
|
|
|
if ($char === $quote_char && $i > 1 && !$escaped) {
|
|
|
|
$quote_char = null;
|
|
|
|
|
|
|
|
$type .= $char;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($char === '\\' && !$escaped && ($next_char === $quote_char || $next_char === '\\')) {
|
|
|
|
$escaped = true;
|
|
|
|
|
|
|
|
$type .= $char;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$escaped = false;
|
|
|
|
|
|
|
|
$type .= $char;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($char === '"' || $char === '\'') {
|
|
|
|
$quote_char = $char;
|
|
|
|
|
|
|
|
$type .= $char;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:50:48 -05:00
|
|
|
if ($char === ':' && $last_char === ')') {
|
2019-02-01 08:58:52 -05:00
|
|
|
$expects_callable_return = true;
|
2019-02-01 07:50:48 -05:00
|
|
|
|
|
|
|
$type .= $char;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-28 00:11:16 -04:00
|
|
|
if ($char === '[' || $char === '{' || $char === '(' || $char === '<') {
|
|
|
|
$brackets .= $char;
|
2016-11-02 02:29:00 -04:00
|
|
|
} elseif ($char === ']' || $char === '}' || $char === ')' || $char === '>') {
|
2016-10-28 00:11:16 -04:00
|
|
|
$last_bracket = substr($brackets, -1);
|
|
|
|
$brackets = substr($brackets, 0, -1);
|
|
|
|
|
|
|
|
if (($char === ']' && $last_bracket !== '[')
|
|
|
|
|| ($char === '}' && $last_bracket !== '{')
|
|
|
|
|| ($char === ')' && $last_bracket !== '(')
|
|
|
|
|| ($char === '>' && $last_bracket !== '<')
|
|
|
|
) {
|
2016-11-02 02:29:00 -04:00
|
|
|
throw new DocblockParseException('Invalid string ' . $return_block);
|
2016-10-28 00:11:16 -04:00
|
|
|
}
|
2018-08-22 22:53:44 -04:00
|
|
|
} elseif ($char === ' ') {
|
2019-02-01 08:58:52 -05:00
|
|
|
if ($brackets) {
|
|
|
|
$expects_callable_return = false;
|
2019-06-01 17:22:33 -04:00
|
|
|
$type .= ' ';
|
2016-10-28 00:11:16 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:52:58 -04:00
|
|
|
if ($next_char === '|' || $next_char === '&') {
|
|
|
|
$nexter_char = $i < $l - 2 ? $return_block[$i + 2] : null;
|
|
|
|
|
|
|
|
if ($nexter_char === ' ') {
|
|
|
|
++$i;
|
2019-06-06 13:57:00 -04:00
|
|
|
$type .= $next_char . ' ';
|
2019-05-16 13:52:58 -04:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-22 22:53:44 -04:00
|
|
|
}
|
|
|
|
|
2019-05-16 13:52:58 -04:00
|
|
|
if ($last_char === '|' || $last_char === '&') {
|
2019-06-06 13:57:00 -04:00
|
|
|
$type .= ' ';
|
2018-08-22 22:53:44 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-26 22:13:10 -04:00
|
|
|
if ($next_char === ':') {
|
|
|
|
++$i;
|
2019-06-06 13:57:00 -04:00
|
|
|
$type .= ' :';
|
2018-03-26 22:13:10 -04:00
|
|
|
$expects_callable_return = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expects_callable_return) {
|
2019-06-06 14:27:49 -04:00
|
|
|
$type .= ' ';
|
2018-03-26 22:13:10 -04:00
|
|
|
$expects_callable_return = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:44:59 -04:00
|
|
|
$remaining = trim(preg_replace('@^[ \t]*\* *@m', ' ', substr($return_block, $i + 1)));
|
2016-11-01 00:39:41 -04:00
|
|
|
|
|
|
|
if ($remaining) {
|
2019-06-01 18:44:59 -04:00
|
|
|
return array_merge([rtrim($type)], preg_split('/[ \s]+/', $remaining));
|
2016-11-01 00:39:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return [$type];
|
2016-10-28 00:11:16 -04:00
|
|
|
}
|
|
|
|
|
2019-02-01 08:58:52 -05:00
|
|
|
$expects_callable_return = false;
|
2019-02-01 07:50:48 -05:00
|
|
|
|
2016-10-28 00:11:16 -04:00
|
|
|
$type .= $char;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$type];
|
|
|
|
}
|
2020-05-30 22:54:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ParsedDocblock $parsed_docblock
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @throws DocblockParseException if a duplicate is found
|
|
|
|
*/
|
|
|
|
private static function checkDuplicatedTags(ParsedDocblock $parsed_docblock)
|
|
|
|
{
|
|
|
|
if (count($parsed_docblock->tags['return'] ?? []) > 1
|
|
|
|
|| count($parsed_docblock->tags['psalm-return'] ?? []) > 1
|
|
|
|
|| count($parsed_docblock->tags['phpstan-return'] ?? []) > 1
|
|
|
|
) {
|
|
|
|
throw new DocblockParseException('Found duplicated @return or prefixed @return tag');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::checkDuplicatedParams($parsed_docblock->tags['param'] ?? []);
|
|
|
|
self::checkDuplicatedParams($parsed_docblock->tags['psalm-param'] ?? []);
|
|
|
|
self::checkDuplicatedParams($parsed_docblock->tags['phpstan-param'] ?? []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $param
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @throws DocblockParseException if a duplicate is found
|
|
|
|
*/
|
|
|
|
private static function checkDuplicatedParams(array $param)
|
|
|
|
{
|
|
|
|
$list_names = self::extractAllParamNames($param);
|
|
|
|
|
|
|
|
if (count($list_names) !== count(array_unique($list_names))) {
|
|
|
|
throw new DocblockParseException('Found duplicated @param or prefixed @param tag');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $lines
|
|
|
|
*
|
|
|
|
* @return list<string>
|
|
|
|
*/
|
|
|
|
private static function extractAllParamNames(array $lines)
|
|
|
|
{
|
|
|
|
$names = [];
|
|
|
|
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$split_by_dollar = explode('$', $line, 2);
|
|
|
|
if (count($split_by_dollar) > 1) {
|
|
|
|
$split_by_space = explode(' ', $split_by_dollar[1], 2);
|
|
|
|
$names[] = $split_by_space[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $names;
|
|
|
|
}
|
2016-06-23 18:45:46 -04:00
|
|
|
}
|