2016-06-24 00:45:46 +02:00
|
|
|
<?php
|
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;
|
2019-06-01 17:53:32 +02:00
|
|
|
use Psalm\Codebase;
|
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;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Scanner\ClassLikeDocblockComment;
|
|
|
|
use Psalm\Internal\Scanner\FunctionDocblockComment;
|
|
|
|
use Psalm\Internal\Scanner\VarDocblockComment;
|
2018-11-18 18:41:47 +01:00
|
|
|
use Psalm\Internal\Type\ParseTree;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\Type;
|
2016-06-24 00:45:46 +02:00
|
|
|
|
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
|
|
|
{
|
2018-04-22 04:13:10 +02:00
|
|
|
const TYPE_REGEX = '(\??\\\?[\(\)A-Za-z0-9_&\<\.=,\>\[\]\-\{\}:|?\\\\]*|\$[a-zA-Z_0-9_]+)';
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-22 20:59:10 +01:00
|
|
|
* @param array<string, array<string, array{Type\Union}>>|null $template_type_map
|
2018-07-15 23:23:17 +02:00
|
|
|
* @param array<string, array<int, string>> $type_aliases
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2018-02-08 05:33:31 +01:00
|
|
|
* @return VarDocblockComment[]
|
2016-11-22 01:07:56 +01:00
|
|
|
* @psalm-suppress MixedArrayAccess
|
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,
|
2018-12-18 05:29:27 +01:00
|
|
|
array $template_type_map = null,
|
2019-06-01 18:25:57 +02:00
|
|
|
?array $type_aliases = null
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
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 = [];
|
2019-06-01 17:53:32 +02:00
|
|
|
$comments = DocComment::parse($comment);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2017-11-03 02:45:17 +01:00
|
|
|
if (!isset($comments['specials']['var']) && !isset($comments['specials']['psalm-var'])) {
|
2018-02-08 05:33:31 +01:00
|
|
|
return [];
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
|
|
|
|
2019-06-01 17:53:32 +02:00
|
|
|
$var_line_number = $comment->getLine();
|
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
if ($comments) {
|
2017-11-03 02:45:17 +01:00
|
|
|
$all_vars = (isset($comments['specials']['var']) ? $comments['specials']['var'] : [])
|
|
|
|
+ (isset($comments['specials']['psalm-var']) ? $comments['specials']['psalm-var'] : []);
|
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
/** @var int $line_number */
|
2017-11-03 02:45:17 +01:00
|
|
|
foreach ($all_vars as $line_number => $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
|
|
|
|
|
|
|
if ($line_parts && $line_parts[0]) {
|
2017-11-15 03:43:31 +01:00
|
|
|
if ($line_parts[0][0] === '$' && $line_parts[0] !== '$this') {
|
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:25:57 +02:00
|
|
|
if (($start_offset = strpos($comment->getText(), $line_parts[0])) !== false) {
|
|
|
|
$type_start = $start_offset + $comment->getFilePos();
|
|
|
|
$type_end = $type_start + strlen($line_parts[0]);
|
|
|
|
}
|
|
|
|
|
2018-04-05 23:18:49 +02:00
|
|
|
try {
|
2018-05-20 23:19:53 +02:00
|
|
|
$var_type_tokens = Type::fixUpLocalType(
|
2018-04-05 23:18:49 +02:00
|
|
|
$line_parts[0],
|
|
|
|
$aliases,
|
2018-12-18 05:29:27 +01:00
|
|
|
$template_type_map,
|
2018-07-15 23:23:17 +02: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;
|
|
|
|
|
|
|
|
if (count($line_parts) > 1 && $line_parts[1][0] === '$') {
|
2017-05-25 07:32:34 +02:00
|
|
|
$var_id = $line_parts[1];
|
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 {
|
2019-02-07 18:25:57 +01:00
|
|
|
$defined_type = Type::parseTokens($var_type_tokens, null, $template_type_map ?: []);
|
2018-02-08 05:33:31 +01:00
|
|
|
} catch (TypeParseTreeException $e) {
|
2019-06-01 17:53:32 +02:00
|
|
|
throw new DocblockParseException(
|
|
|
|
implode('', $var_type_tokens) .
|
|
|
|
' is not a valid type' .
|
|
|
|
' (from ' .
|
|
|
|
$source->getFilePath() .
|
|
|
|
':' .
|
|
|
|
$comment->getLine() .
|
|
|
|
')'
|
|
|
|
);
|
2018-02-08 05:33:31 +01:00
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
$defined_type->setFromDocblock();
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2018-02-08 05:33:31 +01: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 18:25:57 +02:00
|
|
|
$var_comment->type_start = $type_start;
|
|
|
|
$var_comment->type_end = $type_end;
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comment->deprecated = isset($comments['specials']['deprecated']);
|
2018-12-02 00:37:49 +01:00
|
|
|
$var_comment->internal = isset($comments['specials']['internal']);
|
2019-05-11 16:45:25 +02:00
|
|
|
if (isset($comments['specials']['psalm-internal'])) {
|
2019-05-11 21:39:53 +02:00
|
|
|
$psalm_internal = reset($comments['specials']['psalm-internal']);
|
|
|
|
if ($psalm_internal) {
|
|
|
|
$var_comment->psalm_internal = $psalm_internal;
|
2019-05-11 17:55:37 +02:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('psalm-internal annotation used without specifying namespace');
|
|
|
|
}
|
2019-05-11 21:39:53 +02:00
|
|
|
$var_comment->psalm_internal = reset($comments['specials']['psalm-internal']);
|
2019-05-15 00:27:27 +02:00
|
|
|
|
|
|
|
if (!$var_comment->internal) {
|
|
|
|
throw new DocblockParseException('@psalm-internal annotation used without @internal');
|
|
|
|
}
|
2019-05-11 16:45:25 +02:00
|
|
|
}
|
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
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
return $var_comments;
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 23:23:17 +02:00
|
|
|
/**
|
|
|
|
* @param Aliases $aliases
|
|
|
|
* @param array<string, array<int, string>> $type_aliases
|
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
|
|
|
* @return array<string, array<int, string>>
|
|
|
|
*/
|
|
|
|
public static function getTypeAliasesFromComment(
|
2019-06-01 17:53:32 +02:00
|
|
|
PhpParser\Comment\Doc $comment,
|
2018-07-15 23:23:17 +02:00
|
|
|
Aliases $aliases,
|
|
|
|
array $type_aliases = null
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$comments = DocComment::parse($comment);
|
2018-07-15 23:23:17 +02:00
|
|
|
|
|
|
|
if (!isset($comments['specials']['psalm-type'])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getTypeAliasesFromCommentLines(
|
|
|
|
$comments['specials']['psalm-type'],
|
|
|
|
$aliases,
|
|
|
|
$type_aliases
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string> $type_alias_comment_lines
|
|
|
|
* @param Aliases $aliases
|
|
|
|
* @param array<string, array<int, string>> $type_aliases
|
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
|
|
|
* @return array<string, array<int, string>>
|
|
|
|
*/
|
|
|
|
private static function getTypeAliasesFromCommentLines(
|
|
|
|
array $type_alias_comment_lines,
|
|
|
|
Aliases $aliases,
|
|
|
|
array $type_aliases = null
|
|
|
|
) {
|
|
|
|
$type_alias_tokens = [];
|
|
|
|
|
|
|
|
foreach ($type_alias_comment_lines as $var_line) {
|
|
|
|
$var_line = trim($var_line);
|
|
|
|
|
|
|
|
if (!$var_line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_line = preg_replace('/[ \t]+/', ' ', $var_line);
|
|
|
|
|
|
|
|
$var_line_parts = preg_split('/( |=)/', $var_line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
if (!$var_line_parts) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-15 23:23:17 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$type_string = implode('', $var_line_parts);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$type_tokens = Type::fixUpLocalType(
|
|
|
|
$type_string,
|
|
|
|
$aliases,
|
|
|
|
null,
|
2018-09-05 04:27:55 +02:00
|
|
|
$type_alias_tokens + $type_aliases
|
2018-07-15 23:23:17 +02:00
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
|
|
|
throw new DocblockParseException($type_string . ' is not a valid type');
|
|
|
|
}
|
|
|
|
|
|
|
|
$type_alias_tokens[$type_alias] = $type_tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type_alias_tokens;
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param int $line_number
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2016-11-13 05:59:31 +01:00
|
|
|
* @return FunctionDocblockComment
|
2016-11-22 01:07:56 +01:00
|
|
|
* @psalm-suppress MixedArrayAccess
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2019-06-01 17:53:32 +02:00
|
|
|
public static function extractFunctionDocblockInfo(PhpParser\Comment\Doc $comment)
|
2018-07-22 04:55:16 +02:00
|
|
|
{
|
2019-06-01 17:53:32 +02:00
|
|
|
$comments = DocComment::parse($comment);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
$info = new FunctionDocblockComment();
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-10-04 03:36:42 +02:00
|
|
|
if (isset($comments['specials']['return']) || isset($comments['specials']['psalm-return'])) {
|
2016-12-17 01:22:30 +01:00
|
|
|
/** @var array<int, string> */
|
2016-12-04 01:11:30 +01:00
|
|
|
$return_specials = isset($comments['specials']['psalm-return'])
|
|
|
|
? $comments['specials']['psalm-return']
|
|
|
|
: $comments['specials']['return'];
|
|
|
|
|
2019-06-01 19:02:20 +02:00
|
|
|
self::extractReturnType(
|
|
|
|
$comment,
|
|
|
|
(string) reset($return_specials),
|
|
|
|
array_keys($return_specials)[0],
|
|
|
|
$info
|
|
|
|
);
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 02:45:17 +01:00
|
|
|
if (isset($comments['specials']['param']) || isset($comments['specials']['psalm-param'])) {
|
|
|
|
$all_params = (isset($comments['specials']['param']) ? $comments['specials']['param'] : [])
|
|
|
|
+ (isset($comments['specials']['psalm-param']) ? $comments['specials']['psalm-param'] : []);
|
|
|
|
|
2016-12-17 06:48:31 +01:00
|
|
|
/** @var string $param */
|
2017-11-03 02:45:17 +01:00
|
|
|
foreach ($all_params as $line_number => $param) {
|
2019-02-01 02:21:20 +01:00
|
|
|
$line_parts = self::splitDocLine($param);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2017-05-30 05:21:46 +02:00
|
|
|
if (count($line_parts) === 1 && isset($line_parts[0][0]) && $line_parts[0][0] === '$') {
|
2018-03-30 23:46:12 +02:00
|
|
|
continue;
|
2017-01-16 05:49:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-25 12:32:21 +01:00
|
|
|
if (count($line_parts) > 1) {
|
2019-05-24 08:12:58 +02:00
|
|
|
if (preg_match('/^&?(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
|
2017-01-15 16:58:44 +01:00
|
|
|
&& $line_parts[0][0] !== '{'
|
2016-12-25 12:32:21 +01:00
|
|
|
) {
|
2019-04-09 20:23:48 +02:00
|
|
|
$line_parts[1] = str_replace('&', '', $line_parts[1]);
|
2016-12-25 12:32:21 +01:00
|
|
|
|
2018-03-22 22:55:36 +01:00
|
|
|
if ($line_parts[0][0] === '$' && !preg_match('/^\$this(\||$)/', $line_parts[0])) {
|
2017-11-15 03:43:31 +01:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
2017-05-25 03:11:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-28 08:37:52 +01:00
|
|
|
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
|
|
|
|
2016-12-25 12:32:21 +01:00
|
|
|
$info->params[] = [
|
2017-01-17 07:14:43 +01:00
|
|
|
'name' => $line_parts[1],
|
2016-12-25 12:32:21 +01:00
|
|
|
'type' => $line_parts[0],
|
2017-11-19 19:05:35 +01:00
|
|
|
'line_number' => (int)$line_number,
|
2016-12-25 12:32:21 +01:00
|
|
|
];
|
2016-10-15 06:12:57 +02:00
|
|
|
}
|
2016-12-25 12:32:21 +01:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @param');
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:32:43 +01:00
|
|
|
if (isset($comments['specials']['param-out'])) {
|
|
|
|
/** @var string $param */
|
|
|
|
foreach ($comments['specials']['param-out'] as $line_number => $param) {
|
2019-02-01 02:21:20 +01:00
|
|
|
$line_parts = self::splitDocLine($param);
|
2019-01-19 19:32:43 +01: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->params_out[] = [
|
|
|
|
'name' => $line_parts[1],
|
|
|
|
'type' => $line_parts[0],
|
|
|
|
'line_number' => (int)$line_number,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @param');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 15:31:21 +02:00
|
|
|
if (isset($comments['specials']['global'])) {
|
|
|
|
foreach ($comments['specials']['global'] as $line_number => $global) {
|
2019-02-01 02:21:20 +01:00
|
|
|
$line_parts = self::splitDocLine($global);
|
2018-06-08 15:31:21 +02: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],
|
|
|
|
'line_number' => (int)$line_number,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @param');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
if (isset($comments['specials']['deprecated'])) {
|
2016-11-13 05:59:31 +01:00
|
|
|
$info->deprecated = true;
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
if (isset($comments['specials']['internal'])) {
|
|
|
|
$info->internal = true;
|
|
|
|
}
|
|
|
|
|
2019-05-11 16:15:50 +02:00
|
|
|
if (isset($comments['specials']['psalm-internal'])) {
|
2019-05-11 21:39:53 +02:00
|
|
|
$psalm_internal = reset($comments['specials']['psalm-internal']);
|
|
|
|
if ($psalm_internal) {
|
|
|
|
$info->psalm_internal = $psalm_internal;
|
2019-05-11 17:55:37 +02:00
|
|
|
} else {
|
2019-05-15 00:27:27 +02:00
|
|
|
throw new DocblockParseException('@psalm-internal annotation used without specifying namespace');
|
2019-05-11 17:55:37 +02:00
|
|
|
}
|
2019-05-11 21:39:53 +02:00
|
|
|
$info->psalm_internal = reset($comments['specials']['psalm-internal']);
|
2019-05-15 00:27:27 +02:00
|
|
|
|
|
|
|
if (! $info->internal) {
|
|
|
|
throw new DocblockParseException('@psalm-internal annotation used without @internal');
|
|
|
|
}
|
2019-05-11 16:15:50 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 00:27:27 +02:00
|
|
|
|
|
|
|
|
2016-10-11 20:17:55 +02:00
|
|
|
if (isset($comments['specials']['psalm-suppress'])) {
|
|
|
|
foreach ($comments['specials']['psalm-suppress'] as $suppress_entry) {
|
2016-12-17 06:48:31 +01:00
|
|
|
$info->suppress[] = preg_split('/[\s]+/', $suppress_entry)[0];
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 07:13:49 +02:00
|
|
|
if (isset($comments['specials']['throws'])) {
|
|
|
|
foreach ($comments['specials']['throws'] as $throws_entry) {
|
2019-01-06 16:01:35 +01:00
|
|
|
$throws_class = preg_split('/[\s]+/', $throws_entry)[0];
|
|
|
|
|
|
|
|
if (!$throws_class) {
|
|
|
|
throw new IncorrectDocblockException('Unexpectedly empty @throws');
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->throws[] = $throws_class;
|
2018-06-22 07:13:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 18:19:07 +01:00
|
|
|
if (strpos(strtolower($comments['description']), '@inheritdoc') !== false
|
2019-03-08 00:04:02 +01:00
|
|
|
|| isset($comments['specials']['inheritdoc']) || isset($comments['specials']['inheritDoc'])) {
|
2018-12-21 17:01:24 +01:00
|
|
|
$info->inheritdoc = true;
|
|
|
|
}
|
|
|
|
|
2018-07-14 01:09:35 +02:00
|
|
|
if (isset($comments['specials']['template']) || isset($comments['specials']['psalm-template'])) {
|
|
|
|
$all_templates = (isset($comments['specials']['template']) ? $comments['specials']['template'] : [])
|
|
|
|
+ (isset($comments['specials']['psalm-template']) ? $comments['specials']['psalm-template'] : []);
|
|
|
|
|
|
|
|
foreach ($all_templates as $template_line) {
|
2017-02-10 02:35:17 +01:00
|
|
|
$template_type = preg_split('/[\s]+/', $template_line);
|
|
|
|
|
2019-05-06 22:38:08 +02:00
|
|
|
$template_name = array_shift($template_type);
|
|
|
|
|
|
|
|
if (count($template_type) > 1
|
|
|
|
&& in_array(strtolower($template_type[0]), ['as', 'super', 'of'], true)
|
2019-01-16 15:23:18 +01:00
|
|
|
) {
|
2019-05-06 22:38:08 +02:00
|
|
|
$template_modifier = strtolower(array_shift($template_type));
|
2018-12-18 05:29:27 +01:00
|
|
|
$info->templates[] = [
|
2019-05-06 22:38:08 +02:00
|
|
|
$template_name,
|
|
|
|
$template_modifier,
|
|
|
|
implode(' ', $template_type),
|
|
|
|
false
|
2018-06-19 19:19:34 +02:00
|
|
|
];
|
2017-02-10 02:35:17 +01:00
|
|
|
} else {
|
2019-05-06 22:38:08 +02:00
|
|
|
$info->templates[] = [$template_name, null, null, false];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['template-covariant'])
|
|
|
|
|| isset($comments['specials']['psalm-template-covariant'])
|
|
|
|
) {
|
|
|
|
$all_templates =
|
|
|
|
(isset($comments['specials']['template-covariant'])
|
|
|
|
? $comments['specials']['template-covariant']
|
|
|
|
: [])
|
|
|
|
+ (isset($comments['specials']['psalm-template-covariant'])
|
|
|
|
? $comments['specials']['psalm-template-covariant']
|
|
|
|
: []);
|
|
|
|
|
|
|
|
foreach ($all_templates as $template_line) {
|
|
|
|
$template_type = preg_split('/[\s]+/', $template_line);
|
|
|
|
|
|
|
|
$template_name = array_shift($template_type);
|
|
|
|
|
|
|
|
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),
|
|
|
|
true
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$info->templates[] = [$template_name, null, null, true];
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['template-typeof'])) {
|
|
|
|
foreach ($comments['specials']['template-typeof'] as $template_typeof) {
|
|
|
|
$typeof_parts = preg_split('/[\s]+/', $template_typeof);
|
|
|
|
|
|
|
|
if (count($typeof_parts) < 2 || $typeof_parts[1][0] !== '$') {
|
2017-11-15 03:43:31 +01:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$info->template_typeofs[] = [
|
|
|
|
'template_type' => $typeof_parts[0],
|
|
|
|
'param_name' => substr($typeof_parts[1], 1),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
if (isset($comments['specials']['psalm-assert'])) {
|
|
|
|
foreach ($comments['specials']['psalm-assert'] as $assertion) {
|
|
|
|
$assertion_parts = preg_split('/[\s]+/', $assertion);
|
|
|
|
|
|
|
|
if (count($assertion_parts) < 2 || $assertion_parts[1][0] !== '$') {
|
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->assertions[] = [
|
|
|
|
'type' => $assertion_parts[0],
|
|
|
|
'param_name' => substr($assertion_parts[1], 1),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['psalm-assert-if-true'])) {
|
|
|
|
foreach ($comments['specials']['psalm-assert-if-true'] as $assertion) {
|
|
|
|
$assertion_parts = preg_split('/[\s]+/', $assertion);
|
|
|
|
|
|
|
|
if (count($assertion_parts) < 2 || $assertion_parts[1][0] !== '$') {
|
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->if_true_assertions[] = [
|
|
|
|
'type' => $assertion_parts[0],
|
|
|
|
'param_name' => substr($assertion_parts[1], 1),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['psalm-assert-if-false'])) {
|
|
|
|
foreach ($comments['specials']['psalm-assert-if-false'] as $assertion) {
|
|
|
|
$assertion_parts = preg_split('/[\s]+/', $assertion);
|
|
|
|
|
|
|
|
if (count($assertion_parts) < 2 || $assertion_parts[1][0] !== '$') {
|
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->if_false_assertions[] = [
|
|
|
|
'type' => $assertion_parts[0],
|
|
|
|
'param_name' => substr($assertion_parts[1], 1),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
$info->variadic = isset($comments['specials']['psalm-variadic']);
|
2017-05-10 18:36:11 +02:00
|
|
|
$info->ignore_nullable_return = isset($comments['specials']['psalm-ignore-nullable-return']);
|
2018-01-25 00:52:58 +01:00
|
|
|
$info->ignore_falsable_return = isset($comments['specials']['psalm-ignore-falsable-return']);
|
2016-10-18 23:55:07 +02:00
|
|
|
|
2016-06-24 00:45:46 +02:00
|
|
|
return $info;
|
|
|
|
}
|
2016-10-28 06:11:16 +02:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
/**
|
2019-01-09 17:51:29 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-06-01 19:02:20 +02:00
|
|
|
private static function extractReturnType(
|
|
|
|
PhpParser\Comment\Doc $comment,
|
|
|
|
string $return_block,
|
|
|
|
int $line_number,
|
|
|
|
FunctionDocblockComment $info
|
|
|
|
) {
|
2019-01-09 17:51:29 +01:00
|
|
|
$return_lines = explode("\n", $return_block);
|
|
|
|
|
|
|
|
if (!trim($return_lines[0])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_block = trim($return_block);
|
|
|
|
|
|
|
|
if (!$return_block) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:21:20 +01:00
|
|
|
$line_parts = self::splitDocLine($return_block);
|
2019-01-09 17:51:29 +01:00
|
|
|
|
2019-05-24 05:53:48 +02:00
|
|
|
if ($line_parts[0][0] !== '{') {
|
2019-01-09 17:51:29 +01:00
|
|
|
if ($line_parts[0][0] === '$' && !preg_match('/^\$this(\||$)/', $line_parts[0])) {
|
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
|
|
|
}
|
|
|
|
|
2019-06-01 19:02:20 +02:00
|
|
|
if (($start_offset = strpos($comment->getText(), $line_parts[0])) !== false) {
|
|
|
|
$info->return_type_start = $start_offset + $comment->getFilePos();
|
|
|
|
$info->return_type_end = $info->return_type_start + strlen($line_parts[0]);
|
|
|
|
}
|
|
|
|
|
2019-01-09 17:51:29 +01:00
|
|
|
$info->return_type = array_shift($line_parts);
|
|
|
|
$info->return_type_description = $line_parts ? implode(' ', $line_parts) : null;
|
|
|
|
|
2019-06-01 19:02:20 +02:00
|
|
|
$info->return_type_line_number = $line_number;
|
2019-01-09 17:51:29 +01:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @return type');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-27 02:16:18 +02:00
|
|
|
* @throws DocblockParseException if there was a problem parsing the docblock
|
|
|
|
*
|
2017-02-10 02:35:17 +01:00
|
|
|
* @return ClassLikeDocblockComment
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
*/
|
2019-06-01 17:53:32 +02:00
|
|
|
public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, PhpParser\Comment\Doc $comment)
|
2017-02-10 02:35:17 +01:00
|
|
|
{
|
2019-06-01 17:53:32 +02:00
|
|
|
$comments = DocComment::parse($comment);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
|
|
$info = new ClassLikeDocblockComment();
|
|
|
|
|
2019-02-09 17:02:24 +01:00
|
|
|
if (isset($comments['specials']['template']) || isset($comments['specials']['psalm-template'])) {
|
|
|
|
$all_templates = (isset($comments['specials']['template']) ? $comments['specials']['template'] : [])
|
|
|
|
+ (isset($comments['specials']['psalm-template']) ? $comments['specials']['psalm-template'] : []);
|
|
|
|
|
|
|
|
foreach ($all_templates as $template_line) {
|
2017-02-10 02:35:17 +01:00
|
|
|
$template_type = preg_split('/[\s]+/', $template_line);
|
|
|
|
|
2019-03-01 15:20:51 +01:00
|
|
|
$template_name = array_shift($template_type);
|
|
|
|
|
|
|
|
if (count($template_type) > 1
|
|
|
|
&& in_array(strtolower($template_type[0]), ['as', 'super', 'of'], true)
|
2019-01-16 15:23:18 +01:00
|
|
|
) {
|
2019-03-01 15:20:51 +01:00
|
|
|
$template_modifier = strtolower(array_shift($template_type));
|
2018-12-18 05:29:27 +01:00
|
|
|
$info->templates[] = [
|
2019-03-01 15:20:51 +01:00
|
|
|
$template_name,
|
|
|
|
$template_modifier,
|
2019-05-06 22:38:08 +02:00
|
|
|
implode(' ', $template_type),
|
|
|
|
false
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$info->templates[] = [$template_name, null, null, false];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['template-covariant'])
|
|
|
|
|| isset($comments['specials']['psalm-template-covariant'])
|
|
|
|
) {
|
|
|
|
$all_templates =
|
|
|
|
(isset($comments['specials']['template-covariant'])
|
|
|
|
? $comments['specials']['template-covariant']
|
|
|
|
: [])
|
|
|
|
+ (isset($comments['specials']['psalm-template-covariant'])
|
|
|
|
? $comments['specials']['psalm-template-covariant']
|
|
|
|
: []);
|
|
|
|
|
|
|
|
foreach ($all_templates as $template_line) {
|
|
|
|
$template_type = preg_split('/[\s]+/', $template_line);
|
|
|
|
|
|
|
|
$template_name = array_shift($template_type);
|
|
|
|
|
|
|
|
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),
|
|
|
|
true
|
2018-06-19 19:19:34 +02:00
|
|
|
];
|
2017-02-10 02:35:17 +01:00
|
|
|
} else {
|
2019-05-06 22:38:08 +02:00
|
|
|
$info->templates[] = [$template_name, null, null, true];
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 21:12:13 +01:00
|
|
|
if (isset($comments['specials']['template-extends'])
|
|
|
|
|| isset($comments['specials']['inherits'])
|
|
|
|
|| isset($comments['specials']['extends'])
|
|
|
|
) {
|
|
|
|
$all_inheritance = array_merge(
|
2019-01-19 22:01:43 +01:00
|
|
|
$comments['specials']['template-extends'] ?? [],
|
|
|
|
$comments['specials']['inherits'] ?? [],
|
|
|
|
$comments['specials']['extends'] ?? []
|
2019-01-19 21:12:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($all_inheritance as $template_line) {
|
2019-01-10 22:59:44 +01:00
|
|
|
$info->template_extends[] = $template_line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
if (isset($comments['specials']['template-implements'])
|
|
|
|
|| isset($comments['specials']['implements'])
|
|
|
|
) {
|
|
|
|
$all_inheritance = array_merge(
|
|
|
|
$comments['specials']['template-implements'] ?? [],
|
|
|
|
$comments['specials']['implements'] ?? []
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($all_inheritance as $template_line) {
|
|
|
|
$info->template_implements[] = $template_line;
|
2018-05-28 23:26:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 00:35:05 +02:00
|
|
|
if (isset($comments['specials']['deprecated'])) {
|
|
|
|
$info->deprecated = true;
|
|
|
|
}
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
if (isset($comments['specials']['internal'])) {
|
|
|
|
$info->internal = true;
|
|
|
|
}
|
|
|
|
|
2019-05-10 00:33:27 +02:00
|
|
|
if (isset($comments['specials']['psalm-internal'])) {
|
2019-05-11 21:39:53 +02:00
|
|
|
$psalm_internal = reset($comments['specials']['psalm-internal']);
|
|
|
|
if ($psalm_internal) {
|
|
|
|
$info->psalm_internal = $psalm_internal;
|
2019-05-11 17:55:37 +02:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('psalm-internal annotation used without specifying namespace');
|
|
|
|
}
|
2019-05-15 00:27:27 +02:00
|
|
|
|
|
|
|
if (! $info->internal) {
|
|
|
|
throw new DocblockParseException('@psalm-internal annotation used without @internal');
|
|
|
|
}
|
2019-05-10 00:33:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:47:58 +01:00
|
|
|
if (isset($comments['specials']['psalm-seal-properties'])) {
|
|
|
|
$info->sealed_properties = true;
|
|
|
|
}
|
|
|
|
|
2018-04-22 06:40:30 +02:00
|
|
|
if (isset($comments['specials']['psalm-seal-methods'])) {
|
|
|
|
$info->sealed_methods = true;
|
2018-11-25 00:31:00 +01:00
|
|
|
}
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
if (isset($comments['specials']['psalm-override-property-visibility'])) {
|
|
|
|
$info->override_property_visibility = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['psalm-override-method-visibility'])) {
|
|
|
|
$info->override_method_visibility = true;
|
2018-04-22 06:40:30 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:32:13 +02:00
|
|
|
if (isset($comments['specials']['psalm-suppress'])) {
|
|
|
|
foreach ($comments['specials']['psalm-suppress'] as $suppress_entry) {
|
|
|
|
$info->suppressed_issues[] = preg_split('/[\s]+/', $suppress_entry)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 02:42:36 +01:00
|
|
|
if (isset($comments['specials']['method']) || isset($comments['specials']['psalm-method'])) {
|
|
|
|
$all_methods = (isset($comments['specials']['method']) ? $comments['specials']['method'] : [])
|
|
|
|
+ (isset($comments['specials']['psalm-method']) ? $comments['specials']['psalm-method'] : []);
|
|
|
|
|
2019-03-19 02:05:37 +01:00
|
|
|
foreach ($all_methods as $line_number => $method_entry) {
|
2018-04-23 15:52:40 +02:00
|
|
|
$method_entry = preg_replace('/[ \t]+/', ' ', trim($method_entry));
|
2018-04-22 04:13:10 +02:00
|
|
|
|
2018-07-07 06:06:05 +02:00
|
|
|
$docblock_lines = [];
|
2018-04-22 04:13:10 +02:00
|
|
|
|
2019-01-18 17:37:52 +01:00
|
|
|
$is_static = false;
|
|
|
|
|
2018-04-22 04:13:10 +02: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 17:44:12 +01:00
|
|
|
if ($doc_line_parts[0] === 'static' && !strpos($doc_line_parts[1], '(')) {
|
2019-01-18 17:37:52 +01:00
|
|
|
$is_static = true;
|
|
|
|
array_shift($doc_line_parts);
|
|
|
|
}
|
|
|
|
|
2018-07-07 06:06:05 +02:00
|
|
|
$docblock_lines[] = '@return ' . array_shift($doc_line_parts);
|
2018-04-22 04:13:10 +02:00
|
|
|
|
|
|
|
$method_entry = implode(' ', $doc_line_parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_entry = trim(preg_replace('/\/\/.*/', '', $method_entry));
|
|
|
|
|
2018-04-23 15:52:40 +02: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 06:06:05 +02:00
|
|
|
$method_entry = str_replace([', ', '( '], [',', '('], $method_entry);
|
|
|
|
$method_entry = preg_replace('/ (?!(\$|\.\.\.|&))/', '', trim($method_entry));
|
2018-06-28 23:39:25 +02:00
|
|
|
|
2018-07-07 06:06:05 +02:00
|
|
|
try {
|
2018-11-18 18:41:47 +01:00
|
|
|
$method_tree = ParseTree::createFromTokens(Type::tokenize($method_entry, false));
|
2018-07-07 06:06:05 +02:00
|
|
|
} catch (TypeParseTreeException $e) {
|
|
|
|
throw new DocblockParseException($method_entry . ' is not a valid method');
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:41:47 +01:00
|
|
|
if (!$method_tree instanceof ParseTree\MethodWithReturnTypeTree
|
|
|
|
&& !$method_tree instanceof ParseTree\MethodTree) {
|
2018-07-07 06:06:05 +02:00
|
|
|
throw new DocblockParseException($method_entry . ' is not a valid method');
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:41:47 +01:00
|
|
|
if ($method_tree instanceof ParseTree\MethodWithReturnTypeTree) {
|
2018-07-07 06:06:05 +02:00
|
|
|
$docblock_lines[] = '@return ' . Type::getTypeFromTree($method_tree->children[1]);
|
|
|
|
$method_tree = $method_tree->children[0];
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:41:47 +01:00
|
|
|
if (!$method_tree instanceof ParseTree\MethodTree) {
|
2018-07-07 06:06:05 +02:00
|
|
|
throw new DocblockParseException($method_entry . ' is not a valid method');
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = [];
|
|
|
|
|
|
|
|
foreach ($method_tree->children as $method_tree_child) {
|
2018-11-18 18:41:47 +01:00
|
|
|
if (!$method_tree_child instanceof ParseTree\MethodParamTree) {
|
2018-07-07 06:06:05 +02: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 22:01:44 +02:00
|
|
|
. ($method_tree_child->default != '' ? ' = ' . $method_tree_child->default : '');
|
2018-07-07 06:06:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
if ($method_tree_child->children) {
|
|
|
|
$param_type = Type::getTypeFromTree($method_tree_child->children[0]);
|
|
|
|
$docblock_lines[] = '@param ' . $param_type . ' '
|
|
|
|
. ($method_tree_child->variadic ? '...' : '')
|
|
|
|
. $method_tree_child->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$function_string = 'function ' . $method_tree->value . '(' . implode(', ', $args) . ')';
|
|
|
|
|
2019-01-18 17:37:52 +01:00
|
|
|
if ($is_static) {
|
|
|
|
$function_string = 'static ' . $function_string;
|
|
|
|
}
|
|
|
|
|
2018-07-07 06:06:05 +02:00
|
|
|
$function_docblock = $docblock_lines ? "/**\n * " . implode("\n * ", $docblock_lines) . "\n*/\n" : "";
|
2018-06-05 23:59:32 +02:00
|
|
|
|
2018-07-07 06:06:05 +02:00
|
|
|
$php_string = '<?php class A { ' . $function_docblock . ' public ' . $function_string . '{} }';
|
2018-04-22 04:13:10 +02:00
|
|
|
|
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$statements = \Psalm\Internal\Provider\StatementsProvider::parseStatements($php_string);
|
2018-04-22 04:13:10 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new DocblockParseException('Badly-formatted @method string ' . $method_entry);
|
|
|
|
}
|
|
|
|
|
2018-06-14 23:20:02 +02:00
|
|
|
if (!$statements[0] instanceof \PhpParser\Node\Stmt\Class_
|
|
|
|
|| !isset($statements[0]->stmts[0])
|
|
|
|
|| !$statements[0]->stmts[0] instanceof \PhpParser\Node\Stmt\ClassMethod
|
|
|
|
) {
|
2018-04-23 15:52:40 +02:00
|
|
|
throw new DocblockParseException('Badly-formatted @method string ' . $method_entry);
|
2018-04-22 04:13:10 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 02:05:37 +01: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(),
|
|
|
|
$line_number,
|
|
|
|
$node_doc_comment->getFilePos()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-14 23:20:02 +02:00
|
|
|
$info->methods[] = $statements[0]->stmts[0];
|
2018-04-22 04:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 20:22:27 +01:00
|
|
|
self::addMagicPropertyToInfo($info, $comments['specials'], 'property');
|
2019-02-23 02:42:36 +01:00
|
|
|
self::addMagicPropertyToInfo($info, $comments['specials'], 'psalm-property');
|
2017-12-14 20:22:27 +01:00
|
|
|
self::addMagicPropertyToInfo($info, $comments['specials'], 'property-read');
|
|
|
|
self::addMagicPropertyToInfo($info, $comments['specials'], 'property-write');
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-12-14 20:22:27 +01:00
|
|
|
return $info;
|
|
|
|
}
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-12-14 20:22:27 +01:00
|
|
|
/**
|
|
|
|
* @param ClassLikeDocblockComment $info
|
2018-01-10 16:56:43 +01:00
|
|
|
* @param array<string, array<int, string>> $specials
|
2019-02-23 02:42:36 +01:00
|
|
|
* @param string $property_tag ('property', 'psalm-property', 'property-read', or 'property-write')
|
2017-12-15 22:48:06 +01:00
|
|
|
*
|
2017-12-14 20:22:27 +01:00
|
|
|
* @throws DocblockParseException
|
2017-12-15 22:48:06 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2017-12-14 20:22:27 +01:00
|
|
|
*/
|
|
|
|
protected static function addMagicPropertyToInfo(ClassLikeDocblockComment $info, array $specials, $property_tag)
|
|
|
|
{
|
|
|
|
$magic_property_comments = isset($specials[$property_tag]) ? $specials[$property_tag] : [];
|
|
|
|
foreach ($magic_property_comments as $line_number => $property) {
|
2019-02-01 02:21:20 +01:00
|
|
|
$line_parts = self::splitDocLine($property);
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-12-14 20:22:27 +01:00
|
|
|
if (count($line_parts) === 1 && $line_parts[0][0] === '$') {
|
|
|
|
array_unshift($line_parts, 'mixed');
|
|
|
|
}
|
2017-05-25 03:11:18 +02:00
|
|
|
|
2017-12-14 20:22:27 +01:00
|
|
|
if (count($line_parts) > 1) {
|
|
|
|
if (preg_match('/^' . self::TYPE_REGEX . '$/', $line_parts[0])
|
|
|
|
&& !preg_match('/\[[^\]]+\]/', $line_parts[0])
|
|
|
|
&& preg_match('/^(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
|
|
|
|
&& !strpos($line_parts[0], '::')
|
|
|
|
&& $line_parts[0][0] !== '{'
|
|
|
|
) {
|
|
|
|
if ($line_parts[1][0] === '&') {
|
|
|
|
$line_parts[1] = substr($line_parts[1], 1);
|
|
|
|
}
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2018-03-22 22:55:36 +01:00
|
|
|
if ($line_parts[0][0] === '$' && !preg_match('/^\$this(\||$)/', $line_parts[0])) {
|
2017-12-14 20:22:27 +01:00
|
|
|
throw new IncorrectDocblockException('Misplaced variable');
|
2017-05-05 00:35:05 +02:00
|
|
|
}
|
2017-12-14 20:22:27 +01:00
|
|
|
|
|
|
|
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
|
|
|
|
|
|
|
$info->properties[] = [
|
|
|
|
'name' => $line_parts[1],
|
|
|
|
'type' => $line_parts[0],
|
|
|
|
'line_number' => $line_number,
|
|
|
|
'tag' => $property_tag,
|
|
|
|
];
|
2017-05-05 00:35:05 +02:00
|
|
|
} else {
|
2017-12-14 20:22:27 +01:00
|
|
|
throw new DocblockParseException('Badly-formatted @property');
|
2017-05-05 00:35:05 +02:00
|
|
|
}
|
2017-12-14 20:22:27 +01:00
|
|
|
} else {
|
|
|
|
throw new DocblockParseException('Badly-formatted @property');
|
2017-05-05 00:35:05 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 06:11:16 +02:00
|
|
|
/**
|
|
|
|
* @param string $return_block
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
|
|
|
* @throws DocblockParseException if an invalid string is found
|
|
|
|
*
|
2016-10-28 06:11:16 +02:00
|
|
|
* @return array<string>
|
|
|
|
*/
|
2018-01-07 16:23:02 +01:00
|
|
|
public static function splitDocLine($return_block)
|
2016-10-28 06:11:16 +02:00
|
|
|
{
|
|
|
|
$brackets = '';
|
|
|
|
|
|
|
|
$type = '';
|
|
|
|
|
2018-03-27 04:13:10 +02:00
|
|
|
$expects_callable_return = false;
|
|
|
|
|
|
|
|
$return_block = preg_replace('/[ \t]+/', ' ', $return_block);
|
|
|
|
|
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) {
|
|
|
|
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 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;
|
|
|
|
}
|
|
|
|
|
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;
|
2016-10-28 06:11:16 +02:00
|
|
|
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;
|
|
|
|
$type .= $next_char;
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-23 04:53:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 19:52:58 +02:00
|
|
|
if ($last_char === '|' || $last_char === '&') {
|
2018-08-23 04:53:44 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-27 04:13:10 +02:00
|
|
|
if ($next_char === ':') {
|
|
|
|
++$i;
|
2018-03-27 06:12:41 +02:00
|
|
|
$type .= ':';
|
2018-03-27 04:13:10 +02:00
|
|
|
$expects_callable_return = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expects_callable_return) {
|
|
|
|
$expects_callable_return = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
$remaining = trim(substr($return_block, $i + 1));
|
|
|
|
|
|
|
|
if ($remaining) {
|
2018-03-27 04:13:10 +02:00
|
|
|
return array_merge([$type], explode(' ', $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];
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|