2016-06-24 00:45:46 +02:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
|
|
|
|
|
|
|
use Psalm\Context;
|
2016-11-21 05:31:10 +01:00
|
|
|
use Psalm\Exception\TypeParseTreeException;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2016-11-13 05:59:31 +01:00
|
|
|
use Psalm\FunctionDocblockComment;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
|
|
|
use Psalm\Type;
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
class CommentChecker
|
|
|
|
{
|
2016-11-04 01:51:56 +01:00
|
|
|
const TYPE_REGEX =
|
|
|
|
'(\\\?[A-Za-z0-9_\<,\>\[\]\-\{\}:|\\\]+[A-Za-z0-9_\<,\>\[\]-\{\}:]|\$[a-zA-Z_0-9_\<,\>\|\[\]-\{\}:]+)';
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $comment
|
2016-07-29 20:48:04 +02:00
|
|
|
* @param Context|null $context
|
2016-06-24 00:45:46 +02:00
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param string $var_id
|
|
|
|
* @return Type\Union|null
|
2016-11-02 07:29:00 +01:00
|
|
|
* @throws DocblockParseException If there was a problem parsing the docblock.
|
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(
|
|
|
|
$comment,
|
|
|
|
Context $context = null,
|
|
|
|
StatementsSource $source,
|
|
|
|
$var_id = null
|
|
|
|
) {
|
2016-06-24 00:45:46 +02:00
|
|
|
$type_in_comments_var_id = null;
|
|
|
|
|
|
|
|
$type_in_comments = null;
|
|
|
|
|
2016-10-28 06:11:16 +02:00
|
|
|
$comments = self::parseDocComment($comment);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-11-01 06:02:54 +01:00
|
|
|
if ($comments && isset($comments['specials']['var'][0]) && trim((string)$comments['specials']['var'][0])) {
|
2016-11-01 05:39:41 +01:00
|
|
|
try {
|
2016-11-01 06:02:54 +01:00
|
|
|
$line_parts = self::splitDocLine(trim((string)$comments['specials']['var'][0]));
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (DocblockParseException $e) {
|
2016-11-01 05:39:41 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-11-01 06:02:54 +01:00
|
|
|
if ($line_parts && $line_parts[0]) {
|
2016-11-01 05:39:41 +01:00
|
|
|
$type_in_comments = FunctionLikeChecker::fixUpLocalType(
|
|
|
|
$line_parts[0],
|
2016-11-08 01:16:51 +01:00
|
|
|
$source->getFQCLN(),
|
2016-11-01 05:39:41 +01:00
|
|
|
$source->getNamespace(),
|
|
|
|
$source->getAliasedClasses()
|
|
|
|
);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
// support PHPStorm-style docblocks like
|
|
|
|
// @var Type $variable
|
2016-11-01 05:39:41 +01:00
|
|
|
if (count($line_parts) > 1 && $line_parts[1][0] === '$') {
|
|
|
|
$type_in_comments_var_id = $line_parts[1];
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$type_in_comments) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-21 05:31:10 +01:00
|
|
|
try {
|
|
|
|
$defined_type = Type::parseString($type_in_comments);
|
2016-12-24 19:23:22 +01:00
|
|
|
} catch (TypeParseTreeException $e) {
|
2016-11-21 05:31:10 +01:00
|
|
|
throw new DocblockParseException($type_in_comments . ' is not a valid type');
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
if ($context && $type_in_comments_var_id && $type_in_comments_var_id !== $var_id) {
|
2016-10-15 06:12:57 +02:00
|
|
|
$context->vars_in_scope[$type_in_comments_var_id] = $defined_type;
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $defined_type;
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param string $comment
|
|
|
|
* @param int $line_number
|
2016-11-13 05:59:31 +01:00
|
|
|
* @return FunctionDocblockComment
|
2016-11-02 07:29:00 +01:00
|
|
|
* @throws DocblockParseException If there was a problem parsing the docblock.
|
2016-11-22 01:07:56 +01:00
|
|
|
* @psalm-suppress MixedArrayAccess
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2016-12-04 01:11:30 +01:00
|
|
|
public static function extractDocblockInfo($comment, $line_number)
|
2016-06-24 00:45:46 +02:00
|
|
|
{
|
2016-12-04 01:11:30 +01:00
|
|
|
$comments = self::parseDocComment($comment, $line_number);
|
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'];
|
|
|
|
|
|
|
|
$return_block = trim((string)reset($return_specials));
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-10-28 17:05:51 +02:00
|
|
|
try {
|
|
|
|
$line_parts = self::splitDocLine($return_block);
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (DocblockParseException $e) {
|
2016-10-28 17:05:51 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
2016-10-28 06:11:16 +02:00
|
|
|
|
|
|
|
if (preg_match('/^' . self::TYPE_REGEX . '$/', $line_parts[0])
|
|
|
|
&& !preg_match('/\[[^\]]+\]/', $line_parts[0])
|
2016-10-28 19:24:06 +02:00
|
|
|
&& !strpos($line_parts[0], '::')
|
|
|
|
) {
|
2016-11-13 05:59:31 +01:00
|
|
|
$info->return_type = $line_parts[0];
|
2016-12-04 01:11:30 +01:00
|
|
|
$info->return_type_line_number = array_keys($return_specials)[0];
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['param'])) {
|
2016-12-17 06:48:31 +01:00
|
|
|
/** @var string $param */
|
2016-12-04 01:11:30 +01:00
|
|
|
foreach ($comments['specials']['param'] as $line_number => $param) {
|
2016-10-28 17:05:51 +02:00
|
|
|
try {
|
2016-12-17 06:48:31 +01:00
|
|
|
$line_parts = self::splitDocLine($param);
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (DocblockParseException $e) {
|
2016-10-28 17:05:51 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-12-25 12:32:21 +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], '::')
|
|
|
|
) {
|
|
|
|
if ($line_parts[1][0] === '&') {
|
|
|
|
$line_parts[1] = substr($line_parts[1], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$info->params[] = [
|
|
|
|
'name' => substr($line_parts[1], 1),
|
|
|
|
'type' => $line_parts[0],
|
|
|
|
'line_number' => $line_number
|
|
|
|
];
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-11 20:17:55 +02:00
|
|
|
if (isset($comments['specials']['psalm-suppress'])) {
|
2016-12-17 06:48:31 +01:00
|
|
|
/** @var string $suppress_entry */
|
2016-10-11 20:17:55 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
$info->variadic = isset($comments['specials']['psalm-variadic']);
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $return_block
|
|
|
|
* @return array<string>
|
2016-11-02 07:29:00 +01:00
|
|
|
* @throws DocblockParseException If an invalid string is found.
|
2016-10-28 06:11:16 +02:00
|
|
|
*/
|
|
|
|
protected static function splitDocLine($return_block)
|
|
|
|
{
|
|
|
|
$brackets = '';
|
|
|
|
|
|
|
|
$type = '';
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($return_block); $i++) {
|
|
|
|
$char = $return_block[$i];
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($char === ' ' || $char === "\t") {
|
2016-10-28 06:11:16 +02:00
|
|
|
if ($brackets) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
$remaining = trim(substr($return_block, $i + 1));
|
|
|
|
|
|
|
|
if ($remaining) {
|
|
|
|
return array_merge([$type], preg_split('/[\s\t]+/', $remaining));
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$type];
|
2016-10-28 06:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$type .= $char;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$type];
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-10-28 06:11:16 +02:00
|
|
|
* Parse a docblock comment into its parts.
|
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* Taken from advanced api docmaker, which was taken from
|
|
|
|
* https://github.com/facebook/libphutil/blob/master/src/parser/docblock/PhutilDocblockParser.php
|
2016-10-28 06:11:16 +02:00
|
|
|
*
|
|
|
|
* @param string $docblock
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param int $line_number
|
2016-10-28 06:11:16 +02:00
|
|
|
* @return array Array of the main comment and specials
|
|
|
|
*/
|
2016-12-04 01:11:30 +01:00
|
|
|
public static function parseDocComment($docblock, $line_number = null)
|
2016-10-28 06:11:16 +02:00
|
|
|
{
|
|
|
|
// Strip off comments.
|
|
|
|
$docblock = trim($docblock);
|
|
|
|
$docblock = preg_replace('@^/\*\*@', '', $docblock);
|
|
|
|
$docblock = preg_replace('@\*/$@', '', $docblock);
|
|
|
|
$docblock = preg_replace('@^\s*\*@m', '', $docblock);
|
|
|
|
|
|
|
|
// Normalize multi-line @specials.
|
|
|
|
$lines = explode("\n", $docblock);
|
2016-12-04 01:11:30 +01:00
|
|
|
|
|
|
|
$line_map = [];
|
|
|
|
|
|
|
|
if ($line_number) {
|
|
|
|
$line_number++;
|
|
|
|
}
|
|
|
|
|
2016-12-11 19:48:11 +01:00
|
|
|
/** @var int|false */
|
2016-10-28 06:11:16 +02:00
|
|
|
$last = false;
|
|
|
|
foreach ($lines as $k => $line) {
|
|
|
|
if (preg_match('/^\s?@\w/i', $line)) {
|
|
|
|
$last = $k;
|
|
|
|
} elseif (preg_match('/^\s*$/', $line)) {
|
|
|
|
$last = false;
|
|
|
|
} elseif ($last !== false) {
|
|
|
|
$lines[$last] = rtrim($lines[$last]).' '.trim($line);
|
|
|
|
unset($lines[$k]);
|
|
|
|
}
|
2016-12-04 01:11:30 +01:00
|
|
|
|
|
|
|
if ($line_number) {
|
|
|
|
$line_map[$line] = $line_number++;
|
|
|
|
}
|
2016-10-28 06:11:16 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2016-10-28 06:11:16 +02:00
|
|
|
$docblock = implode("\n", $lines);
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$special = [];
|
2016-10-28 06:11:16 +02:00
|
|
|
|
|
|
|
// Parse @specials.
|
2016-11-01 19:14:35 +01:00
|
|
|
$matches = [];
|
2016-10-28 06:11:16 +02:00
|
|
|
$have_specials = preg_match_all('/^\s?@([\w\-:]+)\s*([^\n]*)/m', $docblock, $matches, PREG_SET_ORDER);
|
|
|
|
if ($have_specials) {
|
|
|
|
$docblock = preg_replace('/^\s?@([\w\-:]+)\s*([^\n]*)/m', '', $docblock);
|
2016-12-17 06:48:31 +01:00
|
|
|
/** @var string[] $match */
|
2016-12-04 01:11:30 +01:00
|
|
|
foreach ($matches as $m => $match) {
|
2016-10-28 06:11:16 +02:00
|
|
|
list($_, $type, $data) = $match;
|
|
|
|
|
|
|
|
if (empty($special[$type])) {
|
2016-12-04 01:11:30 +01:00
|
|
|
$special[$type] = [];
|
2016-10-28 06:11:16 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
$special[$type][$line_map && isset($line_map[$_]) ? $line_map[$_] : $m] = $data;
|
2016-10-28 06:11:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$docblock = str_replace("\t", ' ', $docblock);
|
|
|
|
|
|
|
|
// Smush the whole docblock to the left edge.
|
|
|
|
$min_indent = 80;
|
|
|
|
$indent = 0;
|
|
|
|
foreach (array_filter(explode("\n", $docblock)) as $line) {
|
|
|
|
for ($ii = 0; $ii < strlen($line); $ii++) {
|
|
|
|
if ($line[$ii] != ' ') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$indent++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
$min_indent = min($indent, $min_indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
$docblock = preg_replace('/^' . str_repeat(' ', $min_indent) . '/m', '', $docblock);
|
|
|
|
$docblock = rtrim($docblock);
|
|
|
|
|
|
|
|
// Trim any empty lines off the front, but leave the indent level if there
|
|
|
|
// is one.
|
|
|
|
$docblock = preg_replace('/^\s*\n/', '', $docblock);
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return [
|
|
|
|
'description' => $docblock,
|
|
|
|
'specials' => $special
|
|
|
|
];
|
2016-10-28 06:11:16 +02:00
|
|
|
}
|
2016-11-13 05:59:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array{description:string,specials:array<string,array<string>>} $parsed_doc_comment
|
2016-11-21 19:37:27 +01:00
|
|
|
* @param string $left_padding
|
2016-11-13 05:59:31 +01:00
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
|
|
|
public static function renderDocComment(array $parsed_doc_comment, $left_padding)
|
|
|
|
{
|
|
|
|
$doc_comment_text = [$left_padding . '/**'];
|
|
|
|
|
|
|
|
$description_lines = null;
|
|
|
|
|
|
|
|
$trimmed_description = trim($parsed_doc_comment['description']);
|
|
|
|
|
|
|
|
if (!empty($trimmed_description)) {
|
|
|
|
$description_lines = explode(PHP_EOL, $parsed_doc_comment['description']);
|
|
|
|
|
|
|
|
foreach ($description_lines as $line) {
|
|
|
|
$doc_comment_text[] = $left_padding . ' * ' . $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($description_lines && $parsed_doc_comment['specials']) {
|
|
|
|
$doc_comment_text[] = $left_padding . ' *';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parsed_doc_comment['specials']) {
|
|
|
|
$special_type_lengths = array_map('strlen', array_keys($parsed_doc_comment['specials']));
|
|
|
|
/** @var int */
|
|
|
|
$special_type_width = max($special_type_lengths) + 1;
|
|
|
|
|
|
|
|
foreach ($parsed_doc_comment['specials'] as $type => $lines) {
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$doc_comment_text[] = $left_padding . ' * @' . str_pad($type, $special_type_width) . $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$doc_comment_text[] = $left_padding . ' */';
|
|
|
|
|
|
|
|
return $doc_comment_text;
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|