2016-06-24 00:45:46 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2019-06-01 17:53:32 +02:00
|
|
|
use PhpParser;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Aliases;
|
2023-02-15 06:20:03 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\CodeLocation\DocblockTypeLocation;
|
|
|
|
use Psalm\Context;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\DocComment;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\IncorrectDocblockException;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Exception\TypeParseTreeException;
|
2018-01-21 18:44:46 +01:00
|
|
|
use Psalm\FileSource;
|
2022-06-25 09:03:26 +02:00
|
|
|
use Psalm\Internal\Scanner\DocblockParser;
|
2020-05-29 04:14:41 +02:00
|
|
|
use Psalm\Internal\Scanner\ParsedDocblock;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Scanner\VarDocblockComment;
|
2020-05-14 01:29:59 +02:00
|
|
|
use Psalm\Internal\Type\TypeAlias;
|
2023-02-15 06:20:03 +01:00
|
|
|
use Psalm\Internal\Type\TypeExpander;
|
2020-05-14 01:12:45 +02:00
|
|
|
use Psalm\Internal\Type\TypeParser;
|
|
|
|
use Psalm\Internal\Type\TypeTokenizer;
|
2023-02-15 06:20:03 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
|
|
|
use Psalm\Issue\MissingDocblockType;
|
|
|
|
use Psalm\IssueBuffer;
|
2021-12-13 16:28:14 +01:00
|
|
|
use Psalm\Type\Union;
|
2023-02-15 06:20:03 +01:00
|
|
|
use UnexpectedValueException;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
|
|
|
use function array_merge;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function count;
|
2023-02-15 06:20:03 +01:00
|
|
|
use function is_string;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function preg_match;
|
|
|
|
use function preg_replace;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function preg_split;
|
|
|
|
use function rtrim;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function str_replace;
|
|
|
|
use function strlen;
|
|
|
|
use function substr;
|
|
|
|
use function substr_count;
|
|
|
|
use function trim;
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2023-07-06 23:54:50 +02:00
|
|
|
use const PREG_OFFSET_CAPTURE;
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class CommentAnalyzer
|
2016-06-24 00:45:46 +02:00
|
|
|
{
|
2020-09-20 18:54:46 +02:00
|
|
|
public const TYPE_REGEX = '(\??\\\?[\(\)A-Za-z0-9_&\<\.=,\>\[\]\-\{\}:|?\\\\]*|\$[a-zA-Z_0-9_]+)';
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
/**
|
2021-12-13 16:28:14 +01:00
|
|
|
* @param array<string, array<string, Union>>|null $template_type_map
|
2020-05-14 01:29:59 +02:00
|
|
|
* @param array<string, TypeAlias> $type_aliases
|
2017-05-27 02:16:18 +02:00
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
2020-10-17 18:36:44 +02:00
|
|
|
* @return list<VarDocblockComment>
|
2016-06-24 00:45:46 +02:00
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
public static function getTypeFromComment(
|
2019-06-01 17:53:32 +02:00
|
|
|
PhpParser\Comment\Doc $comment,
|
2018-01-21 18:44:46 +01:00
|
|
|
FileSource $source,
|
2017-07-25 22:11:02 +02:00
|
|
|
Aliases $aliases,
|
2020-09-07 01:36:47 +02:00
|
|
|
?array $template_type_map = null,
|
2019-06-01 18:25:57 +02:00
|
|
|
?array $type_aliases = null
|
2020-09-04 22:26:33 +02:00
|
|
|
): array {
|
2019-06-26 22:26:14 +02:00
|
|
|
$parsed_docblock = DocComment::parsePreservingLength($comment);
|
|
|
|
|
|
|
|
return self::arrayToDocblocks(
|
|
|
|
$comment,
|
|
|
|
$parsed_docblock,
|
|
|
|
$source,
|
|
|
|
$aliases,
|
|
|
|
$template_type_map,
|
2022-12-18 17:15:15 +01:00
|
|
|
$type_aliases,
|
2019-06-26 22:26:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-13 16:28:14 +01:00
|
|
|
* @param array<string, array<string, Union>>|null $template_type_map
|
2020-05-14 01:29:59 +02:00
|
|
|
* @param array<string, TypeAlias> $type_aliases
|
2020-10-17 18:36:44 +02:00
|
|
|
* @return list<VarDocblockComment>
|
2019-06-26 22:26:14 +02:00
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*/
|
|
|
|
public static function arrayToDocblocks(
|
|
|
|
PhpParser\Comment\Doc $comment,
|
2020-05-29 04:14:41 +02:00
|
|
|
ParsedDocblock $parsed_docblock,
|
2019-06-26 22:26:14 +02:00
|
|
|
FileSource $source,
|
|
|
|
Aliases $aliases,
|
2020-09-07 01:36:47 +02:00
|
|
|
?array $template_type_map = null,
|
2019-06-26 22:26:14 +02:00
|
|
|
?array $type_aliases = null
|
2021-12-05 18:51:26 +01:00
|
|
|
): array {
|
2017-05-25 07:32:34 +02:00
|
|
|
$var_id = null;
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2018-05-20 23:19:53 +02:00
|
|
|
$var_type_tokens = null;
|
2018-01-02 02:04:03 +01:00
|
|
|
$original_type = null;
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments = [];
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2019-06-01 22:57:33 +02:00
|
|
|
$comment_text = $comment->getText();
|
|
|
|
|
2020-09-20 14:56:49 +02:00
|
|
|
$var_line_number = $comment->getStartLine();
|
2019-06-01 17:53:32 +02:00
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
if (isset($parsed_docblock->combined_tags['var'])) {
|
|
|
|
foreach ($parsed_docblock->combined_tags['var'] as $offset => $var_line) {
|
2017-03-02 04:27:52 +01:00
|
|
|
$var_line = trim($var_line);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
if (!$var_line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:25:57 +02:00
|
|
|
$type_start = null;
|
|
|
|
$type_end = null;
|
|
|
|
|
2019-02-01 02:21:20 +01:00
|
|
|
$line_parts = self::splitDocLine($var_line);
|
2017-03-02 04:27:52 +01:00
|
|
|
|
2021-08-07 00:22:47 +02:00
|
|
|
$line_number = $comment->getStartLine() + substr_count(
|
|
|
|
$comment_text,
|
|
|
|
"\n",
|
|
|
|
0,
|
2022-12-18 17:15:15 +01:00
|
|
|
$offset - $comment->getStartFilePos(),
|
2021-08-07 00:22:47 +02:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$description = $parsed_docblock->description;
|
2019-06-01 22:57:33 +02:00
|
|
|
|
2021-09-22 19:33:08 +02:00
|
|
|
if ($line_parts[0]) {
|
2021-08-06 22:00:37 +02:00
|
|
|
$type_start = $offset;
|
2019-06-02 00:44:59 +02:00
|
|
|
$type_end = $type_start + strlen($line_parts[0]);
|
|
|
|
|
2020-04-03 20:56:11 +02:00
|
|
|
$line_parts[0] = self::sanitizeDocblockType($line_parts[0]);
|
2019-06-02 00:44:59 +02:00
|
|
|
|
2019-06-03 21:46:25 +02:00
|
|
|
if ($line_parts[0] === ''
|
|
|
|
|| ($line_parts[0][0] === '$'
|
|
|
|
&& !preg_match('/^\$this(\||$)/', $line_parts[0]))
|
|
|
|
) {
|
2017-11-15 03:43:31 +01:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
2018-04-05 23:18:49 +02:00
|
|
|
try {
|
2020-05-14 01:12:45 +02:00
|
|
|
$var_type_tokens = TypeTokenizer::getFullyQualifiedTokens(
|
2019-06-02 00:44:59 +02:00
|
|
|
$line_parts[0],
|
2018-04-05 23:18:49 +02:00
|
|
|
$aliases,
|
2018-12-18 05:29:27 +01:00
|
|
|
$template_type_map,
|
2022-12-18 17:15:15 +01:00
|
|
|
$type_aliases,
|
2018-04-05 23:18:49 +02:00
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
|
|
|
throw new DocblockParseException($line_parts[0] . ' is not a valid type');
|
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
|
2018-01-02 02:04:03 +01:00
|
|
|
$original_type = $line_parts[0];
|
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
$var_line_number = $line_number;
|
|
|
|
|
2021-02-24 16:14:04 +01:00
|
|
|
if (count($line_parts) > 1) {
|
|
|
|
if ($line_parts[1][0] === '$') {
|
|
|
|
$var_id = $line_parts[1];
|
|
|
|
$description = trim(substr($var_line, strlen($line_parts[0]) + strlen($line_parts[1]) + 2));
|
|
|
|
} else {
|
|
|
|
$description = trim(substr($var_line, strlen($line_parts[0]) + 1));
|
|
|
|
}
|
|
|
|
$description = preg_replace('/\\n \\*\\s+/um', ' ', $description);
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2018-02-08 05:33:31 +01:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
|
2018-05-20 23:19:53 +02:00
|
|
|
if (!$var_type_tokens || !$original_type) {
|
2018-02-08 05:33:31 +01:00
|
|
|
continue;
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
try {
|
2020-05-15 06:15:48 +02:00
|
|
|
$defined_type = TypeParser::parseTokens(
|
|
|
|
$var_type_tokens,
|
|
|
|
null,
|
|
|
|
$template_type_map ?: [],
|
2022-10-03 15:13:47 +02:00
|
|
|
$type_aliases ?: [],
|
2022-12-18 17:15:15 +01:00
|
|
|
true,
|
2020-05-15 06:15:48 +02:00
|
|
|
);
|
2018-02-08 05:33:31 +01:00
|
|
|
} catch (TypeParseTreeException $e) {
|
2019-06-01 17:53:32 +02:00
|
|
|
throw new DocblockParseException(
|
2019-06-23 05:30:40 +02:00
|
|
|
$line_parts[0] .
|
2019-06-01 17:53:32 +02:00
|
|
|
' is not a valid type' .
|
2023-01-25 12:26:59 +01:00
|
|
|
' ('.$e->getMessage().' in ' .
|
2019-06-01 17:53:32 +02:00
|
|
|
$source->getFilePath() .
|
|
|
|
':' .
|
2020-09-20 14:56:49 +02:00
|
|
|
$comment->getStartLine() .
|
2022-12-18 17:15:15 +01:00
|
|
|
')',
|
2019-06-01 17:53:32 +02:00
|
|
|
);
|
2018-02-08 05:33:31 +01:00
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comment = new VarDocblockComment();
|
|
|
|
$var_comment->type = $defined_type;
|
|
|
|
$var_comment->var_id = $var_id;
|
|
|
|
$var_comment->line_number = $var_line_number;
|
2019-06-01 18:25:57 +02:00
|
|
|
$var_comment->type_start = $type_start;
|
|
|
|
$var_comment->type_end = $type_end;
|
2021-02-24 16:14:04 +01:00
|
|
|
$var_comment->description = $description;
|
2019-05-15 00:27:27 +02:00
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
self::decorateVarDocblockComment($var_comment, $parsed_docblock);
|
2017-02-04 04:07:14 +01:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments[] = $var_comment;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2019-08-11 22:01:37 +02:00
|
|
|
if (!$var_comments
|
2020-05-29 04:14:41 +02: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'])
|
2021-12-11 18:45:06 +01:00
|
|
|
|| isset($parsed_docblock->tags['psalm-allow-private-mutation'])
|
2020-06-21 17:43:08 +02:00
|
|
|
|| isset($parsed_docblock->tags['psalm-taint-escape'])
|
2021-02-24 16:14:04 +01:00
|
|
|
|| isset($parsed_docblock->tags['psalm-internal'])
|
2021-12-11 18:45:06 +01:00
|
|
|
|| isset($parsed_docblock->tags['psalm-suppress'])
|
2021-02-24 16:14:04 +01:00
|
|
|
|| $parsed_docblock->description)
|
2019-08-11 22:01:37 +02:00
|
|
|
) {
|
|
|
|
$var_comment = new VarDocblockComment();
|
2020-05-29 04:14:41 +02:00
|
|
|
|
|
|
|
self::decorateVarDocblockComment($var_comment, $parsed_docblock);
|
2019-08-11 22:01:37 +02:00
|
|
|
|
|
|
|
$var_comments[] = $var_comment;
|
|
|
|
}
|
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
return $var_comments;
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
private static function decorateVarDocblockComment(
|
|
|
|
VarDocblockComment $var_comment,
|
|
|
|
ParsedDocblock $parsed_docblock
|
2021-12-05 18:51:26 +01:00
|
|
|
): void {
|
2020-05-29 04:14:41 +02:00
|
|
|
$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']);
|
|
|
|
|
2021-02-24 16:14:04 +01:00
|
|
|
if (!$var_comment->description) {
|
|
|
|
$var_comment->description = $parsed_docblock->description;
|
|
|
|
}
|
|
|
|
|
2020-06-21 17:43:08 +02:00
|
|
|
if (isset($parsed_docblock->tags['psalm-taint-escape'])) {
|
|
|
|
foreach ($parsed_docblock->tags['psalm-taint-escape'] as $param) {
|
2020-05-29 04:14:41 +02:00
|
|
|
$param = trim($param);
|
|
|
|
$var_comment->removed_taints[] = $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 09:03:26 +02:00
|
|
|
if (count($var_comment->psalm_internal = DocblockParser::handlePsalmInternal($parsed_docblock)) !== 0) {
|
2020-07-26 16:47:48 +02:00
|
|
|
$var_comment->internal = true;
|
2020-05-29 04:14:41 +02:00
|
|
|
}
|
2021-01-31 17:32:24 +01:00
|
|
|
|
|
|
|
if (isset($parsed_docblock->tags['psalm-suppress'])) {
|
2021-12-11 18:45:06 +01:00
|
|
|
foreach ($parsed_docblock->tags['psalm-suppress'] as $offset => $suppress_entry) {
|
|
|
|
foreach (DocComment::parseSuppressList($suppress_entry) as $issue_offset => $suppressed_issue) {
|
|
|
|
$var_comment->suppressed_issues[$issue_offset + $offset] = $suppressed_issue;
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 17:32:24 +01:00
|
|
|
}
|
2020-05-29 04:14:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 19:52:31 +02:00
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2021-12-05 18:51:26 +01:00
|
|
|
public static function sanitizeDocblockType(string $docblock_type): string
|
2020-04-03 20:56:11 +02:00
|
|
|
{
|
|
|
|
$docblock_type = preg_replace('@^[ \t]*\*@m', '', $docblock_type);
|
2023-06-13 22:07:51 +02:00
|
|
|
$docblock_type = preg_replace('/,\n\s+}/', '}', $docblock_type);
|
2023-07-06 23:40:13 +02:00
|
|
|
|
2020-04-03 20:56:11 +02:00
|
|
|
return str_replace("\n", '', $docblock_type);
|
|
|
|
}
|
|
|
|
|
2016-10-28 06:11:16 +02:00
|
|
|
/**
|
2017-05-27 02:16:18 +02:00
|
|
|
* @throws DocblockParseException if an invalid string is found
|
2021-03-23 06:30:51 +01:00
|
|
|
* @return non-empty-list<string>
|
2020-08-23 19:52:31 +02:00
|
|
|
* @psalm-pure
|
2016-10-28 06:11:16 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function splitDocLine(string $return_block): array
|
2016-10-28 06:11:16 +02:00
|
|
|
{
|
|
|
|
$brackets = '';
|
|
|
|
|
|
|
|
$type = '';
|
|
|
|
|
2018-03-27 04:13:10 +02:00
|
|
|
$expects_callable_return = false;
|
|
|
|
|
2019-06-02 00:44:59 +02:00
|
|
|
$return_block = str_replace("\t", ' ', $return_block);
|
2018-03-27 04:13:10 +02:00
|
|
|
|
2018-05-20 23:19:53 +02:00
|
|
|
$quote_char = null;
|
|
|
|
$escaped = false;
|
|
|
|
|
2018-03-27 04:13:10 +02:00
|
|
|
for ($i = 0, $l = strlen($return_block); $i < $l; ++$i) {
|
2016-10-28 06:11:16 +02:00
|
|
|
$char = $return_block[$i];
|
2018-03-27 04:13:10 +02:00
|
|
|
$next_char = $i < $l - 1 ? $return_block[$i + 1] : null;
|
2019-02-01 13:50:48 +01:00
|
|
|
$last_char = $i > 0 ? $return_block[$i - 1] : null;
|
2016-10-28 06:11:16 +02:00
|
|
|
|
2018-05-20 23:19:53 +02:00
|
|
|
if ($quote_char) {
|
2021-05-22 18:36:02 +02:00
|
|
|
if ($char === $quote_char && !$escaped) {
|
2018-05-20 23:19:53 +02:00
|
|
|
$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 13:50:48 +01:00
|
|
|
if ($char === ':' && $last_char === ')') {
|
2019-02-01 14:58:52 +01:00
|
|
|
$expects_callable_return = true;
|
2019-02-01 13:50:48 +01:00
|
|
|
|
|
|
|
$type .= $char;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-15 18:43:47 +02:00
|
|
|
if ($char === '/' && $next_char === '/') {
|
|
|
|
// Ignore the rest of the current line
|
|
|
|
$i = strpos($return_block, "\n", $i);
|
|
|
|
|
|
|
|
// Remove trailing whitespaces (needed for `sanitizeDocblockType`)
|
|
|
|
$type = rtrim($type);
|
|
|
|
$type .= "\n";
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-28 06:11:16 +02:00
|
|
|
if ($char === '[' || $char === '{' || $char === '(' || $char === '<') {
|
|
|
|
$brackets .= $char;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($char === ']' || $char === '}' || $char === ')' || $char === '>') {
|
2016-10-28 06:11:16 +02: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 07:29:00 +01:00
|
|
|
throw new DocblockParseException('Invalid string ' . $return_block);
|
2016-10-28 06:11:16 +02:00
|
|
|
}
|
2018-08-23 04:53:44 +02:00
|
|
|
} elseif ($char === ' ') {
|
2019-02-01 14:58:52 +01:00
|
|
|
if ($brackets) {
|
|
|
|
$expects_callable_return = false;
|
2019-06-01 23:22:33 +02:00
|
|
|
$type .= ' ';
|
2016-10-28 06:11:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-06 00:01:00 +02:00
|
|
|
if ($next_char === '{') {
|
|
|
|
$type .= ' ';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-16 19:52:58 +02:00
|
|
|
if ($next_char === '|' || $next_char === '&') {
|
|
|
|
$nexter_char = $i < $l - 2 ? $return_block[$i + 2] : null;
|
|
|
|
|
|
|
|
if ($nexter_char === ' ') {
|
|
|
|
++$i;
|
2019-06-06 19:57:00 +02:00
|
|
|
$type .= $next_char . ' ';
|
2019-05-16 19:52:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-23 04:53:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 19:52:58 +02:00
|
|
|
if ($last_char === '|' || $last_char === '&') {
|
2019-06-06 19:57:00 +02:00
|
|
|
$type .= ' ';
|
2018-08-23 04:53:44 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-27 04:13:10 +02:00
|
|
|
if ($next_char === ':') {
|
|
|
|
++$i;
|
2019-06-06 19:57:00 +02:00
|
|
|
$type .= ' :';
|
2018-03-27 04:13:10 +02:00
|
|
|
$expects_callable_return = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expects_callable_return) {
|
2019-06-06 20:27:49 +02:00
|
|
|
$type .= ' ';
|
2018-03-27 04:13:10 +02:00
|
|
|
$expects_callable_return = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-02 00:44:59 +02:00
|
|
|
$remaining = trim(preg_replace('@^[ \t]*\* *@m', ' ', substr($return_block, $i + 1)));
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
if ($remaining) {
|
2023-06-13 22:07:51 +02:00
|
|
|
return array_merge([rtrim($type)], preg_split('/\s+/', $remaining) ?: []);
|
2016-11-01 05:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [$type];
|
2016-10-28 06:11:16 +02:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:58:52 +01:00
|
|
|
$expects_callable_return = false;
|
2019-02-01 13:50:48 +01:00
|
|
|
|
2016-10-28 06:11:16 +02:00
|
|
|
$type .= $char;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$type];
|
|
|
|
}
|
2023-02-15 06:20:03 +01:00
|
|
|
|
|
|
|
/** @return list<VarDocblockComment> */
|
|
|
|
public static function getVarComments(
|
|
|
|
PhpParser\Comment\Doc $doc_comment,
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\Variable $var
|
|
|
|
): array {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
$parsed_docblock = $statements_analyzer->getParsedDocblock();
|
|
|
|
|
|
|
|
if (!$parsed_docblock) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_comments = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$var_comments = $codebase->config->disable_var_parsing
|
|
|
|
? []
|
|
|
|
: self::arrayToDocblocks(
|
|
|
|
$doc_comment,
|
|
|
|
$parsed_docblock,
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
$statements_analyzer->getSource()->getAliases(),
|
|
|
|
$statements_analyzer->getSource()->getTemplateTypeMap(),
|
|
|
|
);
|
|
|
|
} catch (IncorrectDocblockException $e) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new MissingDocblockType(
|
|
|
|
$e->getMessage(),
|
|
|
|
new CodeLocation($statements_analyzer, $var),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidDocblock(
|
|
|
|
$e->getMessage(),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $var),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $var_comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param list<VarDocblockComment> $var_comments
|
|
|
|
*/
|
|
|
|
public static function populateVarTypesFromDocblock(
|
|
|
|
array $var_comments,
|
|
|
|
PhpParser\Node\Expr\Variable $var,
|
|
|
|
Context $context,
|
|
|
|
StatementsAnalyzer $statements_analyzer
|
|
|
|
): ?Union {
|
|
|
|
if (!is_string($var->name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
$comment_type = null;
|
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
foreach ($var_comments as $var_comment) {
|
|
|
|
if (!$var_comment->type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$var_comment_type = TypeExpander::expandUnion(
|
|
|
|
$codebase,
|
|
|
|
$var_comment->type,
|
|
|
|
$context->self,
|
|
|
|
$context->self,
|
|
|
|
$statements_analyzer->getParentFQCLN(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$var_comment_type = $var_comment_type->setFromDocblock();
|
|
|
|
|
|
|
|
/** @psalm-suppress UnusedMethodCall */
|
|
|
|
$var_comment_type->check(
|
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $var),
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($codebase->alter_code
|
|
|
|
&& $var_comment->type_start
|
|
|
|
&& $var_comment->type_end
|
|
|
|
&& $var_comment->line_number
|
|
|
|
) {
|
|
|
|
$type_location = new DocblockTypeLocation(
|
|
|
|
$statements_analyzer,
|
|
|
|
$var_comment->type_start,
|
|
|
|
$var_comment->type_end,
|
|
|
|
$var_comment->line_number,
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->classlikes->handleDocblockTypeInMigration(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer,
|
|
|
|
$var_comment_type,
|
|
|
|
$type_location,
|
|
|
|
$context->calling_method_id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$var_comment->var_id || $var_comment->var_id === $var_id) {
|
|
|
|
$comment_type = $var_comment_type;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $var_comment_type;
|
|
|
|
} catch (UnexpectedValueException $e) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidDocblock(
|
|
|
|
$e->getMessage(),
|
|
|
|
new CodeLocation($statements_analyzer, $var),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $comment_type;
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|