1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 01:09:38 +01:00
psalm/src/Psalm/Internal/Analyzer/CommentAnalyzer.php

533 lines
18 KiB
PHP
Raw Normal View History

2016-06-24 00:45:46 +02:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer;
use PhpParser;
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
use Psalm\Aliases;
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;
use Psalm\Exception\IncorrectDocblockException;
use Psalm\Exception\TypeParseTreeException;
2018-01-21 18:44:46 +01:00
use Psalm\FileSource;
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;
use Psalm\Internal\Type\TypeAlias;
use Psalm\Internal\Type\TypeExpander;
use Psalm\Internal\Type\TypeParser;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\MissingDocblockType;
use Psalm\IssueBuffer;
2021-12-13 16:28:14 +01:00
use Psalm\Type\Union;
use UnexpectedValueException;
2021-06-08 04:55:21 +02:00
use function array_merge;
use function count;
use function is_string;
2021-06-08 04:55:21 +02:00
use function preg_match;
use function preg_replace;
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
use const PREG_OFFSET_CAPTURE;
/**
* @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
* @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(
PhpParser\Comment\Doc $comment,
2018-01-21 18:44:46 +01:00
FileSource $source,
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
Aliases $aliases,
?array $template_type_map = null,
2019-06-01 18:25:57 +02:00
?array $type_aliases = null
): array {
$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,
);
}
/**
2021-12-13 16:28:14 +01:00
* @param array<string, array<string, Union>>|null $template_type_map
* @param array<string, TypeAlias> $type_aliases
2020-10-17 18:36:44 +02:00
* @return list<VarDocblockComment>
* @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,
FileSource $source,
Aliases $aliases,
?array $template_type_map = null,
?array $type_aliases = null
): array {
$var_id = null;
2016-06-24 00:45:46 +02:00
2018-05-20 23:19:53 +02:00
$var_type_tokens = null;
$original_type = null;
2016-06-24 00:45:46 +02:00
$var_comments = [];
2016-06-24 00:45:46 +02:00
2019-06-01 22:57:33 +02:00
$comment_text = $comment->getText();
$var_line_number = $comment->getStartLine();
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;
$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
);
$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;
$type_end = $type_start + strlen($line_parts[0]);
2020-04-03 20:56:11 +02:00
$line_parts[0] = self::sanitizeDocblockType($line_parts[0]);
if ($line_parts[0] === ''
|| ($line_parts[0][0] === '$'
&& !preg_match('/^\$this(\||$)/', $line_parts[0]))
) {
throw new IncorrectDocblockException('Misplaced variable');
}
try {
$var_type_tokens = TypeTokenizer::getFullyQualifiedTokens(
$line_parts[0],
$aliases,
$template_type_map,
2022-12-18 17:15:15 +01:00
$type_aliases,
);
} catch (TypeParseTreeException $e) {
throw new DocblockParseException($line_parts[0] . ' is not a valid type');
}
2017-03-02 04:27:52 +01:00
$original_type = $line_parts[0];
2017-03-02 04:27:52 +01:00
$var_line_number = $line_number;
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
}
}
2017-03-02 04:27:52 +01:00
2018-05-20 23:19:53 +02:00
if (!$var_type_tokens || !$original_type) {
continue;
2016-06-24 00:45:46 +02:00
}
try {
$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,
);
} catch (TypeParseTreeException $e) {
throw new DocblockParseException(
$line_parts[0] .
' is not a valid type' .
2023-01-25 12:26:59 +01:00
' ('.$e->getMessage().' in ' .
$source->getFilePath() .
':' .
$comment->getStartLine() .
2022-12-18 17:15:15 +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;
$var_comment->description = $description;
2020-05-29 04:14:41 +02:00
self::decorateVarDocblockComment($var_comment, $parsed_docblock);
$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'])
|| isset($parsed_docblock->tags['psalm-allow-private-mutation'])
2020-06-21 17:43:08 +02:00
|| isset($parsed_docblock->tags['psalm-taint-escape'])
|| isset($parsed_docblock->tags['psalm-internal'])
|| isset($parsed_docblock->tags['psalm-suppress'])
|| $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;
}
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
): 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']);
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;
}
}
if (count($var_comment->psalm_internal = DocblockParser::handlePsalmInternal($parsed_docblock)) !== 0) {
$var_comment->internal = true;
2020-05-29 04:14:41 +02:00
}
if (isset($parsed_docblock->tags['psalm-suppress'])) {
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;
}
}
}
2020-05-29 04:14:41 +02:00
}
2020-08-23 19:52:31 +02:00
/**
* @psalm-pure
*/
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);
// Strip out remainders of a line when inline comment is encountered inside curly braces.
if (preg_match('/{(?>[^{}]|(?R))*}/', $docblock_type, $braceMatches, PREG_OFFSET_CAPTURE)) {
$docblock_type =
substr($docblock_type, 0, $braceMatches[0][1])
. preg_replace('%//.*$%m', '', $braceMatches[0][0])
. substr($docblock_type, $braceMatches[0][1] + strlen($braceMatches[0][0]));
}
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
*/
public static function splitDocLine(string $return_block): array
2016-10-28 06:11:16 +02:00
{
$brackets = '';
$type = '';
$expects_callable_return = false;
$return_block = str_replace("\t", ' ', $return_block);
2018-05-20 23:19:53 +02:00
$quote_char = null;
$escaped = false;
for ($i = 0, $l = strlen($return_block); $i < $l; ++$i) {
2016-10-28 06:11:16 +02:00
$char = $return_block[$i];
$next_char = $i < $l - 1 ? $return_block[$i + 1] : null;
$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) {
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;
}
if ($char === ':' && $last_char === ')') {
$expects_callable_return = true;
$type .= $char;
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
}
} elseif ($char === ' ') {
if ($brackets) {
$expects_callable_return = false;
2019-06-01 23:22:33 +02:00
$type .= ' ';
2016-10-28 06:11:16 +02:00
continue;
}
if ($next_char === '{') {
$type .= ' ';
continue;
}
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 . ' ';
continue;
}
}
if ($last_char === '|' || $last_char === '&') {
2019-06-06 19:57:00 +02:00
$type .= ' ';
continue;
}
if ($next_char === ':') {
++$i;
2019-06-06 19:57:00 +02:00
$type .= ' :';
$expects_callable_return = true;
continue;
}
if ($expects_callable_return) {
2019-06-06 20:27:49 +02:00
$type .= ' ';
$expects_callable_return = false;
continue;
}
$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
}
$expects_callable_return = false;
2016-10-28 06:11:16 +02:00
$type .= $char;
}
return [$type];
}
/** @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
}