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

392 lines
13 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;
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;
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\TypeParser;
use Psalm\Internal\Type\TypeTokenizer;
2021-06-08 04:55:21 +02:00
use Psalm\Type;
use function array_merge;
use function count;
2021-06-08 04:55:21 +02:00
use function preg_match;
use function preg_replace;
use function preg_split;
2021-06-08 04:55:21 +02:00
use function reset;
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
/**
* @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
/**
* @param array<string, array<string, Type\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,
$type_aliases
);
}
/**
* @param array<string, array<string, Type\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,
$offset - $comment->getStartFilePos()
);
$description = $parsed_docblock->description;
2019-06-01 22:57:33 +02:00
2017-03-02 04:27:52 +01:00
if ($line_parts && $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,
2018-07-15 23:23:17 +02: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 ?: [],
$type_aliases ?: []
);
} catch (TypeParseTreeException $e) {
throw new DocblockParseException(
$line_parts[0] .
' is not a valid type' .
' (from ' .
$source->getFilePath() .
':' .
$comment->getStartLine() .
')'
);
}
$defined_type->setFromDocblock();
2016-06-24 00:45:46 +02: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'])
2020-06-21 17:43:08 +02:00
|| isset($parsed_docblock->tags['psalm-taint-escape'])
|| isset($parsed_docblock->tags['psalm-internal'])
|| $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 {
$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 (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']);
$var_comment->internal = true;
2020-05-29 04:14:41 +02:00
}
if (isset($parsed_docblock->tags['psalm-suppress'])) {
$var_comment->suppressed_issues = $parsed_docblock->tags['psalm-suppress'];
}
2020-05-29 04:14:41 +02:00
}
2020-08-23 19:52:31 +02:00
/**
* @psalm-pure
*/
2020-11-05 05:25:08 +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);
$docblock_type = preg_replace('/,\n\s+\}/', '}', $docblock_type);
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 === '|' || $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) {
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];
}
2016-06-24 00:45:46 +02:00
}