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

114 lines
4.0 KiB
PHP
Raw Normal View History

2016-06-24 00:45:46 +02:00
<?php
namespace Psalm\Checker;
use Psalm\Context;
use Psalm\StatementsSource;
use Psalm\Type;
2016-06-24 00:45:46 +02:00
class CommentChecker
{
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
* @param Context|null $context
2016-06-24 00:45:46 +02:00
* @param StatementsSource $source
* @param string $var_id
* @return Type\Union|null
*/
public static function getTypeFromComment($comment, Context $context = null, StatementsSource $source, $var_id = null)
{
$type_in_comments_var_id = null;
$type_in_comments = null;
$comments = StatementsChecker::parseDocComment($comment);
if ($comments && isset($comments['specials']['var'][0])) {
2016-10-14 06:53:43 +02:00
$var_parts = array_filter(preg_split('/[\s\t]+/', (string)$comments['specials']['var'][0]));
2016-06-24 00:45:46 +02:00
if ($var_parts) {
2016-08-14 05:26:45 +02:00
$type_in_comments = FunctionLikeChecker::fixUpLocalType($var_parts[0], $source->getAbsoluteClass(), $source->getNamespace(), $source->getAliasedClasses());
2016-06-24 00:45:46 +02:00
// support PHPStorm-style docblocks like
// @var Type $variable
if (count($var_parts) > 1 && $var_parts[1][0] === '$') {
2016-10-14 01:01:12 +02:00
$type_in_comments_var_id = $var_parts[1];
2016-06-24 00:45:46 +02:00
}
}
}
if (!$type_in_comments) {
return null;
}
$defined_type = Type::parseString($type_in_comments);
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
/**
* @param string $comment
* @psalm-return object-like{return_type:null|string,params:array<object-like{name:string,type:string},deprecated:bool,suppress:array<string>}
*/
2016-06-24 00:45:46 +02:00
public static function extractDocblockInfo($comment)
{
$comments = StatementsChecker::parseDocComment($comment);
$info = ['return_type' => null, 'params' => [], 'deprecated' => false, 'suppress' => []];
2016-06-24 00:45:46 +02:00
if (isset($comments['specials']['return']) || isset($comments['specials']['psalm-return'])) {
$return_blocks = preg_split(
'/[\s]+/',
isset($comments['specials']['psalm-return'])
2016-10-14 06:53:43 +02:00
? (string)$comments['specials']['psalm-return'][0]
: (string)$comments['specials']['return'][0]
);
2016-06-24 00:45:46 +02:00
if (preg_match('/^' . self::TYPE_REGEX . '$/', $return_blocks[0])
&& !preg_match('/\[[^\]]+\]/', $return_blocks[0])
&& !strpos($return_blocks[0], '::')) {
2016-06-24 00:45:46 +02:00
$info['return_type'] = $return_blocks[0];
}
}
if (isset($comments['specials']['param'])) {
foreach ($comments['specials']['param'] as $param) {
2016-10-14 06:53:43 +02:00
$param_blocks = preg_split('/[\s]+/', (string)$param);
2016-06-24 00:45:46 +02:00
if (count($param_blocks) > 1
&& preg_match('/^' . self::TYPE_REGEX . '$/', $param_blocks[0])
&& !preg_match('/\[[^\]]+\]/', $param_blocks[0])
2016-10-15 06:12:57 +02:00
&& preg_match('/^&?\$[A-Za-z0-9_]+$/', $param_blocks[1])
&& !strpos($param_blocks[0], '::')
2016-06-24 00:45:46 +02:00
) {
2016-10-15 06:12:57 +02:00
if ($param_blocks[1][0] === '&') {
$param_blocks[1] = substr($param_blocks[1], 1);
}
2016-06-25 00:18:11 +02:00
$info['params'][] = ['name' => substr($param_blocks[1], 1), 'type' => $param_blocks[0]];
2016-06-24 00:45:46 +02:00
}
}
}
if (isset($comments['specials']['deprecated'])) {
$info['deprecated'] = true;
}
2016-10-11 20:17:55 +02:00
if (isset($comments['specials']['psalm-suppress'])) {
foreach ($comments['specials']['psalm-suppress'] as $suppress_entry) {
2016-10-14 06:53:43 +02:00
$info['suppress'][] = preg_split('/[\s]+/', (string)$suppress_entry)[0];
}
}
2016-06-24 00:45:46 +02:00
return $info;
}
}