2017-07-25 22:11:02 +02:00
|
|
|
<?php
|
2020-03-15 04:54:42 +01:00
|
|
|
namespace Psalm\Internal\PhpVisitor;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function array_filter;
|
|
|
|
use function array_key_exists;
|
|
|
|
use function array_merge;
|
|
|
|
use function array_pop;
|
|
|
|
use function assert;
|
|
|
|
use function class_exists;
|
|
|
|
use function count;
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
use function dirname;
|
|
|
|
use function end;
|
|
|
|
use function explode;
|
|
|
|
use function function_exists;
|
|
|
|
use function implode;
|
|
|
|
use function in_array;
|
|
|
|
use function interface_exists;
|
|
|
|
use function is_string;
|
2017-07-25 22:11:02 +02:00
|
|
|
use PhpParser;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function preg_match;
|
|
|
|
use function preg_replace;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Aliases;
|
2018-01-21 19:38:51 +01:00
|
|
|
use Psalm\Codebase;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Config;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\DocComment;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
|
|
|
use Psalm\Exception\FileIncludeException;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\IncorrectDocblockException;
|
2017-10-22 07:04:35 +02:00
|
|
|
use Psalm\Exception\TypeParseTreeException;
|
2018-02-23 21:39:33 +01:00
|
|
|
use Psalm\FileSource;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Internal\Analyzer\ClassAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\CommentAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\IncludeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Internal\Codebase\CallMap;
|
|
|
|
use Psalm\Internal\Codebase\PropertyMap;
|
|
|
|
use Psalm\Internal\Scanner\FileScanner;
|
|
|
|
use Psalm\Internal\Scanner\PhpStormMetaScanner;
|
2019-09-14 20:26:31 +02:00
|
|
|
use Psalm\Internal\Scanner\UnresolvedConstant;
|
|
|
|
use Psalm\Internal\Scanner\UnresolvedConstantComponent;
|
2018-04-07 17:38:41 +02:00
|
|
|
use Psalm\Issue\DuplicateClass;
|
2019-01-06 22:40:44 +01:00
|
|
|
use Psalm\Issue\DuplicateFunction;
|
2019-01-02 17:18:22 +01:00
|
|
|
use Psalm\Issue\DuplicateMethod;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Issue\DuplicateParam;
|
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\MissingDocblockType;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2017-07-29 21:05:06 +02:00
|
|
|
use Psalm\Storage\FileStorage;
|
2018-05-12 00:35:02 +02:00
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Storage\FunctionLikeStorage;
|
|
|
|
use Psalm\Storage\MethodStorage;
|
|
|
|
use Psalm\Storage\PropertyStorage;
|
|
|
|
use Psalm\Type;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strpos;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function strtolower;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function substr;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function trim;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-13 20:09:37 +01:00
|
|
|
class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor, FileSource
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
/** @var Aliases */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $aliases;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
/**
|
2017-08-14 21:46:01 +02:00
|
|
|
* @var string[]
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
2018-01-21 18:44:46 +01:00
|
|
|
private $fq_classlike_names = [];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-01-21 18:44:46 +01:00
|
|
|
/** @var FileScanner */
|
|
|
|
private $file_scanner;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/** @var Codebase */
|
|
|
|
private $codebase;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/** @var string */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $file_path;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
/** @var bool */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $scan_deep;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
/** @var Config */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $config;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
/** @var array<string, array<string, array{Type\Union}>> */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $class_template_types = [];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
/** @var array<string, array<string, array{Type\Union}>> */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $function_template_types = [];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
/** @var FunctionLikeStorage[] */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $functionlike_storages = [];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/** @var FileStorage */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $file_storage;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
/** @var ClassLikeStorage[] */
|
2018-01-21 18:44:46 +01:00
|
|
|
private $classlike_storages = [];
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2019-01-02 15:00:45 +01:00
|
|
|
/** @var class-string<\Psalm\Plugin\Hook\AfterClassLikeVisitInterface>[] */
|
2018-02-12 04:49:19 +01:00
|
|
|
private $after_classlike_check_plugins;
|
2017-11-07 20:46:53 +01:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
/** @var int */
|
|
|
|
private $php_major_version;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $php_minor_version;
|
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
/** @var PhpParser\Node\Name|null */
|
|
|
|
private $namespace_name;
|
|
|
|
|
2019-06-10 05:50:18 +02:00
|
|
|
/** @var PhpParser\Node\Expr\FuncCall|null */
|
|
|
|
private $exists_cond_expr;
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall|null */
|
|
|
|
private $not_exists_cond_expr;
|
|
|
|
|
|
|
|
/**
|
2019-07-11 00:58:56 +02:00
|
|
|
* @var ?int
|
2019-06-10 05:50:18 +02:00
|
|
|
*/
|
2019-07-11 00:58:56 +02:00
|
|
|
private $skip_if_descendants = null;
|
2019-06-10 05:50:18 +02:00
|
|
|
|
2018-07-15 23:23:17 +02:00
|
|
|
/**
|
2019-06-23 05:30:40 +02:00
|
|
|
* @var array<string, array<int, array{0: string, 1: int}>>
|
2018-07-15 23:23:17 +02:00
|
|
|
*/
|
|
|
|
private $type_aliases = [];
|
|
|
|
|
2018-02-23 04:22:31 +01:00
|
|
|
public function __construct(
|
|
|
|
Codebase $codebase,
|
|
|
|
FileStorage $file_storage,
|
2018-02-23 04:52:22 +01:00
|
|
|
FileScanner $file_scanner
|
2018-02-23 04:22:31 +01:00
|
|
|
) {
|
2018-01-21 19:38:51 +01:00
|
|
|
$this->codebase = $codebase;
|
2018-01-21 18:44:46 +01:00
|
|
|
$this->file_scanner = $file_scanner;
|
2018-01-21 19:38:51 +01:00
|
|
|
$this->file_path = $file_scanner->file_path;
|
2018-01-21 18:44:46 +01:00
|
|
|
$this->scan_deep = $file_scanner->will_analyze;
|
2018-01-21 19:38:51 +01:00
|
|
|
$this->config = $codebase->config;
|
2018-01-21 18:44:46 +01:00
|
|
|
$this->file_storage = $file_storage;
|
2019-06-30 03:32:26 +02:00
|
|
|
$this->aliases = $this->file_storage->aliases = new Aliases();
|
2018-02-12 04:49:19 +01:00
|
|
|
$this->after_classlike_check_plugins = $this->config->after_visit_classlikes;
|
2019-02-07 18:25:57 +01:00
|
|
|
$this->php_major_version = $codebase->php_major_version;
|
|
|
|
$this->php_minor_version = $codebase->php_minor_version;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node $node
|
|
|
|
*
|
2018-01-28 23:07:09 +01:00
|
|
|
* @return null|int
|
2017-11-26 22:03:17 +01:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public function enterNode(PhpParser\Node $node)
|
|
|
|
{
|
2018-07-22 04:24:33 +02:00
|
|
|
foreach ($node->getComments() as $comment) {
|
|
|
|
if ($comment instanceof PhpParser\Comment\Doc) {
|
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$type_alias_tokens = CommentAnalyzer::getTypeAliasesFromComment(
|
2019-06-01 17:53:32 +02:00
|
|
|
$comment,
|
2018-07-22 04:24:33 +02:00
|
|
|
$this->aliases,
|
|
|
|
$this->type_aliases
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($type_alias_tokens as $type_tokens) {
|
|
|
|
// finds issues, if there are any
|
|
|
|
Type::parseTokens($type_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->type_aliases += $type_alias_tokens;
|
|
|
|
} catch (DocblockParseException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$this->file_storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2018-07-22 04:24:33 +02:00
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$this->file_storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($node instanceof PhpParser\Node\Stmt\Namespace_) {
|
2019-06-30 03:32:26 +02:00
|
|
|
$this->file_storage->aliases = $this->aliases;
|
2019-06-04 22:36:32 +02:00
|
|
|
|
|
|
|
$this->namespace_name = $node->name;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->aliases = new Aliases(
|
|
|
|
$node->name ? implode('\\', $node->name->parts) : '',
|
|
|
|
$this->aliases->uses,
|
|
|
|
$this->aliases->functions,
|
2019-06-01 16:07:38 +02:00
|
|
|
$this->aliases->constants,
|
|
|
|
$this->aliases->uses_flipped,
|
|
|
|
$this->aliases->functions_flipped,
|
|
|
|
$this->aliases->constants_flipped
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2019-06-30 03:32:26 +02:00
|
|
|
|
|
|
|
$this->file_storage->namespace_aliases[(int) $node->getAttribute('startFilePos')] = $this->aliases;
|
|
|
|
|
|
|
|
if ($node->stmts) {
|
|
|
|
$this->aliases->namespace_first_stmt_start = (int) $node->stmts[0]->getAttribute('startFilePos');
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
foreach ($node->uses as $use) {
|
|
|
|
$use_path = implode('\\', $use->name->parts);
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
$use_alias = $use->alias ? $use->alias->name : $use->name->getLast();
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $node->type) {
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
|
2018-04-17 18:16:25 +02:00
|
|
|
$this->aliases->functions[strtolower($use_alias)] = $use_path;
|
2019-06-01 16:07:38 +02:00
|
|
|
$this->aliases->functions_flipped[strtolower($use_path)] = $use_alias;
|
2017-07-25 22:11:02 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
|
2018-04-17 18:16:25 +02:00
|
|
|
$this->aliases->constants[$use_alias] = $use_path;
|
2019-06-01 16:07:38 +02:00
|
|
|
$this->aliases->constants_flipped[$use_path] = $use_alias;
|
2017-07-25 22:11:02 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
|
2018-04-17 18:16:25 +02:00
|
|
|
$this->aliases->uses[strtolower($use_alias)] = $use_path;
|
2019-06-01 16:07:38 +02:00
|
|
|
$this->aliases->uses_flipped[strtolower($use_path)] = $use_alias;
|
2017-07-25 22:11:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 03:32:26 +02:00
|
|
|
|
|
|
|
if (!$this->aliases->uses_start) {
|
|
|
|
$this->aliases->uses_start = (int) $node->getAttribute('startFilePos');
|
|
|
|
}
|
|
|
|
|
2019-06-30 17:12:50 +02:00
|
|
|
$this->aliases->uses_end = (int) $node->getAttribute('endFilePos') + 1;
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\GroupUse) {
|
|
|
|
$use_prefix = implode('\\', $node->prefix->parts);
|
|
|
|
|
|
|
|
foreach ($node->uses as $use) {
|
|
|
|
$use_path = $use_prefix . '\\' . implode('\\', $use->name->parts);
|
2018-04-17 18:16:25 +02:00
|
|
|
$use_alias = $use->alias ? $use->alias->name : $use->name->getLast();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $node->type) {
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
|
2018-04-17 18:16:25 +02:00
|
|
|
$this->aliases->functions[strtolower($use_alias)] = $use_path;
|
2019-06-01 16:07:38 +02:00
|
|
|
$this->aliases->functions_flipped[strtolower($use_path)] = $use_alias;
|
2017-07-25 22:11:02 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
|
2018-04-17 18:16:25 +02:00
|
|
|
$this->aliases->constants[$use_alias] = $use_path;
|
2019-06-01 16:07:38 +02:00
|
|
|
$this->aliases->constants_flipped[$use_path] = $use_alias;
|
2017-07-25 22:11:02 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
|
2018-04-17 18:16:25 +02:00
|
|
|
$this->aliases->uses[strtolower($use_alias)] = $use_path;
|
2019-06-01 16:07:38 +02:00
|
|
|
$this->aliases->uses_flipped[strtolower($use_path)] = $use_alias;
|
2017-07-25 22:11:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 03:32:26 +02:00
|
|
|
|
|
|
|
if (!$this->aliases->uses_start) {
|
|
|
|
$this->aliases->uses_start = (int) $node->getAttribute('startFilePos');
|
|
|
|
}
|
|
|
|
|
2019-06-30 17:12:50 +02:00
|
|
|
$this->aliases->uses_end = (int) $node->getAttribute('endFilePos') + 1;
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\ClassLike) {
|
2019-06-28 16:17:59 +02:00
|
|
|
if ($this->skip_if_descendants) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
if ($this->registerClassLike($node) === false) {
|
|
|
|
return PhpParser\NodeTraverser::STOP_TRAVERSAL;
|
2018-01-21 18:44:46 +01:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif (($node instanceof PhpParser\Node\Expr\New_
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\Instanceof_
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\StaticPropertyFetch
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\StaticCall)
|
|
|
|
&& $node->class instanceof PhpParser\Node\Name
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$fq_classlike_name = ClassLikeAnalyzer::getFQCLNFromNameObject($node->class, $this->aliases);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-11-15 03:56:29 +01:00
|
|
|
if (!in_array(strtolower($fq_classlike_name), ['self', 'static', 'parent'], true)) {
|
2018-05-22 17:59:57 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning(
|
|
|
|
$fq_classlike_name,
|
|
|
|
false,
|
|
|
|
!($node instanceof PhpParser\Node\Expr\ClassConstFetch)
|
|
|
|
|| !($node->name instanceof PhpParser\Node\Identifier)
|
|
|
|
|| strtolower($node->name->name) !== 'class'
|
|
|
|
);
|
2018-02-23 04:22:31 +01:00
|
|
|
$this->file_storage->referenced_classlikes[strtolower($fq_classlike_name)] = $fq_classlike_name;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\TryCatch) {
|
|
|
|
foreach ($node->catches as $catch) {
|
|
|
|
foreach ($catch->types as $catch_type) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$catch_fqcln = ClassLikeAnalyzer::getFQCLNFromNameObject($catch_type, $this->aliases);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-11-15 03:56:29 +01:00
|
|
|
if (!in_array(strtolower($catch_fqcln), ['self', 'static', 'parent'], true)) {
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning($catch_fqcln);
|
2018-02-23 04:22:31 +01:00
|
|
|
$this->file_storage->referenced_classlikes[strtolower($catch_fqcln)] = $catch_fqcln;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-28 00:07:38 +01:00
|
|
|
} elseif ($node instanceof PhpParser\Node\FunctionLike) {
|
2019-06-28 16:17:59 +02:00
|
|
|
if ($node instanceof PhpParser\Node\Stmt\Function_
|
|
|
|
|| $node instanceof PhpParser\Node\Stmt\ClassMethod
|
|
|
|
) {
|
|
|
|
if ($this->skip_if_descendants) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->registerFunctionLike($node);
|
|
|
|
|
2019-02-10 22:23:31 +01:00
|
|
|
if ($node instanceof PhpParser\Node\Expr\Closure) {
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning('Closure');
|
2019-02-10 22:23:31 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!$this->scan_deep) {
|
|
|
|
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
|
|
|
}
|
2018-05-31 04:09:46 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Global_) {
|
|
|
|
$function_like_storage = end($this->functionlike_storages);
|
|
|
|
|
|
|
|
if ($function_like_storage) {
|
|
|
|
foreach ($node->vars as $var) {
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable) {
|
2018-05-31 04:56:46 +02:00
|
|
|
if (is_string($var->name) && $var->name !== 'argv' && $var->name !== 'argc') {
|
2018-05-31 04:09:46 +02:00
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
$function_like_storage->global_variables[$var_id] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Expr\FuncCall && $node->name instanceof PhpParser\Node\Name) {
|
|
|
|
$function_id = implode('\\', $node->name->parts);
|
2018-02-04 00:52:35 +01:00
|
|
|
if (CallMap::inCallMap($function_id)) {
|
2019-01-28 23:54:50 +01:00
|
|
|
$this->registerClassMapFunctionCall($function_id, $node);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\TraitUse) {
|
2020-01-17 16:25:05 +01:00
|
|
|
if ($this->skip_if_descendants) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
if (!$this->classlike_storages) {
|
|
|
|
throw new \LogicException('$this->classlike_storages should not be empty');
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
$storage = $this->classlike_storages[count($this->classlike_storages) - 1];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-03-13 04:37:21 +01:00
|
|
|
$method_map = $storage->trait_alias_map ?: [];
|
2019-01-07 17:53:22 +01:00
|
|
|
$visibility_map = $storage->trait_visibility_map ?: [];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
foreach ($node->adaptations as $adaptation) {
|
|
|
|
if ($adaptation instanceof PhpParser\Node\Stmt\TraitUseAdaptation\Alias) {
|
2019-01-07 17:53:22 +01:00
|
|
|
$old_name = strtolower($adaptation->method->name);
|
|
|
|
$new_name = $old_name;
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($adaptation->newName) {
|
2019-01-07 17:53:22 +01:00
|
|
|
$new_name = strtolower($adaptation->newName->name);
|
|
|
|
|
|
|
|
if ($new_name !== $old_name) {
|
|
|
|
$method_map[$new_name] = $old_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($adaptation->newModifier) {
|
|
|
|
switch ($adaptation->newModifier) {
|
|
|
|
case 1:
|
|
|
|
$visibility_map[$new_name] = ClassLikeAnalyzer::VISIBILITY_PUBLIC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
$visibility_map[$new_name] = ClassLikeAnalyzer::VISIBILITY_PROTECTED;
|
|
|
|
break;
|
2019-01-13 16:19:27 +01:00
|
|
|
|
|
|
|
case 4:
|
|
|
|
$visibility_map[$new_name] = ClassLikeAnalyzer::VISIBILITY_PRIVATE;
|
|
|
|
break;
|
2019-01-07 17:53:22 +01:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->trait_alias_map = $method_map;
|
2019-01-07 17:53:22 +01:00
|
|
|
$storage->trait_visibility_map = $visibility_map;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
foreach ($node->traits as $trait) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$trait_fqcln = ClassLikeAnalyzer::getFQCLNFromNameObject($trait, $this->aliases);
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning($trait_fqcln, $this->scan_deep);
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage->used_traits[strtolower($trait_fqcln)] = $trait_fqcln;
|
2018-02-23 04:22:31 +01:00
|
|
|
$this->file_storage->required_classes[strtolower($trait_fqcln)] = $trait_fqcln;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2019-01-28 05:12:40 +01:00
|
|
|
|
|
|
|
if ($node_comment = $node->getDocComment()) {
|
2019-06-02 00:44:59 +02:00
|
|
|
$comments = DocComment::parsePreservingLength($node_comment);
|
2019-01-28 05:12:40 +01:00
|
|
|
|
|
|
|
if (isset($comments['specials']['template-use'])
|
|
|
|
|| isset($comments['specials']['use'])
|
2019-12-20 11:49:10 +01:00
|
|
|
|| isset($comments['specials']['phpstan-use'])
|
|
|
|
|| isset($comments['specials']['psalm-use'])
|
2019-01-28 05:12:40 +01:00
|
|
|
) {
|
2019-12-20 11:49:10 +01:00
|
|
|
$all_inheritance = ($comments['specials']['template-use'] ?? [])
|
|
|
|
+ ($comments['specials']['use'] ?? [])
|
|
|
|
+ ($comments['specials']['phpstan-use'] ?? [])
|
|
|
|
+ ($comments['specials']['psalm-use'] ?? []);
|
2019-01-28 05:12:40 +01:00
|
|
|
|
|
|
|
foreach ($all_inheritance as $template_line) {
|
2019-06-03 17:26:25 +02:00
|
|
|
$this->useTemplatedType(
|
|
|
|
$storage,
|
|
|
|
$node,
|
|
|
|
trim(preg_replace('@^[ \t]*\*@m', '', $template_line))
|
|
|
|
);
|
2019-01-28 05:12:40 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-28 22:56:42 +01:00
|
|
|
|
|
|
|
if (isset($comments['specials']['template-extends'])
|
|
|
|
|| isset($comments['specials']['extends'])
|
|
|
|
|| isset($comments['specials']['template-implements'])
|
|
|
|
|| isset($comments['specials']['implements'])
|
|
|
|
) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'You must use @use or @template-use to parameterize traits',
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-01-28 22:56:42 +01:00
|
|
|
}
|
2019-01-28 05:12:40 +01:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Expr\Include_) {
|
|
|
|
$this->visitInclude($node);
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\AssignOp
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\AssignRef
|
2018-10-09 01:39:36 +02:00
|
|
|
|| $node instanceof PhpParser\Node\Stmt\For_
|
|
|
|
|| $node instanceof PhpParser\Node\Stmt\Foreach_
|
|
|
|
|| $node instanceof PhpParser\Node\Stmt\While_
|
|
|
|
|| $node instanceof PhpParser\Node\Stmt\Do_
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
|
|
|
if ($doc_comment = $node->getDocComment()) {
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments = [];
|
2017-11-15 03:43:31 +01:00
|
|
|
|
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_comments = CommentAnalyzer::getTypeFromComment(
|
2019-06-01 17:53:32 +02:00
|
|
|
$doc_comment,
|
2018-01-21 18:44:46 +01:00
|
|
|
$this->file_scanner,
|
2017-11-15 03:43:31 +01:00
|
|
|
$this->aliases,
|
|
|
|
null,
|
2018-07-15 23:23:17 +02:00
|
|
|
$this->type_aliases
|
2017-11-15 03:43:31 +01:00
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
foreach ($var_comments as $var_comment) {
|
2019-08-21 17:25:08 +02:00
|
|
|
if (!$var_comment->type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-02 02:04:03 +01:00
|
|
|
$var_type = $var_comment->type;
|
2018-02-19 06:27:39 +01:00
|
|
|
$var_type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-08 00:34:18 +02:00
|
|
|
|
|
|
|
if ($node instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\AssignOp
|
|
|
|
|| $node instanceof PhpParser\Node\Expr\AssignRef
|
|
|
|
) {
|
|
|
|
if ($node->var instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
&& $node->var->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $node->var->var->name === 'this'
|
|
|
|
&& $node->var->name instanceof PhpParser\Node\Identifier
|
|
|
|
) {
|
|
|
|
$functionlike_storage = end($this->functionlike_storages);
|
|
|
|
|
|
|
|
if ($functionlike_storage instanceof MethodStorage) {
|
|
|
|
$functionlike_storage->this_property_mutations[$node->var->name->name] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-27 01:54:22 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Const_) {
|
2017-07-28 16:42:30 +02:00
|
|
|
foreach ($node->consts as $const) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$const_type = StatementsAnalyzer::getSimpleType(
|
|
|
|
$this->codebase,
|
|
|
|
new \Psalm\Internal\Provider\NodeDataProvider(),
|
|
|
|
$const->value,
|
|
|
|
$this->aliases
|
|
|
|
) ?: Type::getMixed();
|
2017-07-28 16:42:30 +02:00
|
|
|
|
2018-08-08 22:13:37 +02:00
|
|
|
$fq_const_name = Type::getFQCLNFromString($const->name->name, $this->aliases);
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
if ($this->codebase->register_stub_files || $this->codebase->register_autoload_files) {
|
2018-08-08 22:13:37 +02:00
|
|
|
$this->codebase->addGlobalConstantType($fq_const_name, $const_type);
|
2017-07-27 01:54:22 +02:00
|
|
|
}
|
2018-06-29 21:28:45 +02:00
|
|
|
|
2018-08-08 22:13:37 +02:00
|
|
|
$this->file_storage->constants[$fq_const_name] = $const_type;
|
|
|
|
$this->file_storage->declaring_constants[$fq_const_name] = $this->file_path;
|
2017-07-27 01:54:22 +02:00
|
|
|
}
|
2019-07-11 00:58:56 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\If_ && !$this->skip_if_descendants) {
|
2019-06-10 06:02:36 +02:00
|
|
|
if (!$this->fq_classlike_names && !$this->functionlike_storages) {
|
|
|
|
if ($node->cond instanceof PhpParser\Node\Expr\BooleanNot) {
|
|
|
|
if ($node->cond->expr instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
&& $node->cond->expr->name instanceof PhpParser\Node\Name
|
2019-07-05 22:24:00 +02:00
|
|
|
&& (
|
|
|
|
$node->cond->expr->name->parts === ['function_exists']
|
2019-06-10 06:02:36 +02:00
|
|
|
|| $node->cond->expr->name->parts === ['class_exists']
|
|
|
|
|| $node->cond->expr->name->parts === ['interface_exists']
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$this->not_exists_cond_expr = $node->cond->expr;
|
|
|
|
}
|
|
|
|
} elseif ($node->cond instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
&& $node->cond->name instanceof PhpParser\Node\Name
|
2019-07-05 22:24:00 +02:00
|
|
|
&& (
|
|
|
|
$node->cond->name->parts === ['function_exists']
|
2019-06-10 06:02:36 +02:00
|
|
|
|| $node->cond->name->parts === ['class_exists']
|
|
|
|
|| $node->cond->name->parts === ['interface_exists']
|
2019-06-10 05:50:18 +02:00
|
|
|
)
|
2018-04-27 21:00:22 +02:00
|
|
|
) {
|
2019-06-10 06:02:36 +02:00
|
|
|
$this->exists_cond_expr = $node->cond;
|
2019-11-27 06:44:41 +01:00
|
|
|
} elseif ($node->cond instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|
|
|
|
&& $node->cond->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
&& $node->cond->left->name instanceof PhpParser\Node\Name
|
|
|
|
&& (
|
|
|
|
$node->cond->left->name->parts === ['function_exists']
|
|
|
|
|| $node->cond->left->name->parts === ['class_exists']
|
|
|
|
|| $node->cond->left->name->parts === ['interface_exists']
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$this->exists_cond_expr = $node->cond->left;
|
2020-01-11 18:33:58 +01:00
|
|
|
} elseif ($node->cond instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|
|
|
|
&& $node->cond->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
&& $node->cond->right->name instanceof PhpParser\Node\Name
|
|
|
|
&& (
|
|
|
|
$node->cond->right->name->parts === ['function_exists']
|
|
|
|
|| $node->cond->right->name->parts === ['class_exists']
|
|
|
|
|| $node->cond->right->name->parts === ['interface_exists']
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$this->exists_cond_expr = $node->cond->right;
|
2018-04-27 21:00:22 +02:00
|
|
|
}
|
2019-06-10 05:50:18 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 00:58:56 +02:00
|
|
|
if (($this->exists_cond_expr && !$this->enterConditional($this->exists_cond_expr))
|
|
|
|
|| ($this->not_exists_cond_expr && $this->enterConditional($this->not_exists_cond_expr))
|
|
|
|
) {
|
|
|
|
// the else node should terminate the agreement
|
|
|
|
$this->skip_if_descendants = $node->else ? $node->else->getLine() : $node->getLine();
|
2019-06-10 05:50:18 +02:00
|
|
|
}
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Else_) {
|
2019-07-11 00:58:56 +02:00
|
|
|
if ($this->skip_if_descendants === $node->getLine()) {
|
|
|
|
$this->skip_if_descendants = null;
|
|
|
|
$this->exists_cond_expr = null;
|
|
|
|
$this->not_exists_cond_expr = null;
|
|
|
|
} elseif (!$this->skip_if_descendants) {
|
|
|
|
if (($this->exists_cond_expr && $this->enterConditional($this->exists_cond_expr))
|
|
|
|
|| ($this->not_exists_cond_expr && !$this->enterConditional($this->not_exists_cond_expr))
|
|
|
|
) {
|
|
|
|
$this->skip_if_descendants = $node->getLine();
|
|
|
|
}
|
2018-04-27 21:00:22 +02:00
|
|
|
}
|
2018-09-04 19:29:35 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Expr\Yield_ || $node instanceof PhpParser\Node\Expr\YieldFrom) {
|
2018-07-15 23:47:58 +02:00
|
|
|
$function_like_storage = end($this->functionlike_storages);
|
|
|
|
|
|
|
|
if ($function_like_storage) {
|
|
|
|
$function_like_storage->has_yield = true;
|
|
|
|
}
|
2018-10-11 15:14:34 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning('stdClass', false, false);
|
2020-02-17 17:33:09 +01:00
|
|
|
$this->file_storage->referenced_classlikes['stdclass'] = 'stdClass';
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
2018-03-07 17:16:56 +01:00
|
|
|
* @return null
|
2017-11-26 22:03:17 +01:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public function leaveNode(PhpParser\Node $node)
|
|
|
|
{
|
|
|
|
if ($node instanceof PhpParser\Node\Stmt\Namespace_) {
|
2019-06-30 03:32:26 +02:00
|
|
|
if (!$this->file_storage->aliases) {
|
|
|
|
throw new \UnexpectedValueException('File storage liases should not be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->aliases = $this->file_storage->aliases;
|
2019-02-17 00:50:25 +01:00
|
|
|
|
|
|
|
if ($this->codebase->register_stub_files
|
|
|
|
&& $node->name
|
|
|
|
&& $node->name->parts === ['PHPSTORM_META']
|
|
|
|
) {
|
|
|
|
foreach ($node->stmts as $meta_stmt) {
|
|
|
|
if ($meta_stmt instanceof PhpParser\Node\Stmt\Expression
|
|
|
|
&& $meta_stmt->expr instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
&& $meta_stmt->expr->name instanceof PhpParser\Node\Name
|
|
|
|
&& $meta_stmt->expr->name->parts === ['override']
|
|
|
|
&& count($meta_stmt->expr->args) > 1
|
|
|
|
) {
|
|
|
|
PhpStormMetaScanner::handleOverride($meta_stmt->expr->args, $this->codebase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\ClassLike) {
|
2019-06-28 16:17:59 +02:00
|
|
|
if ($this->skip_if_descendants) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
if (!$this->fq_classlike_names) {
|
|
|
|
throw new \LogicException('$this->fq_classlike_names should not be empty');
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 20:40:38 +02:00
|
|
|
$fq_classlike_name = array_pop($this->fq_classlike_names);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-02-04 18:34:08 +01:00
|
|
|
if (PropertyMap::inPropertyMap($fq_classlike_name)) {
|
2020-03-11 22:41:05 +01:00
|
|
|
$mapped_properties = PropertyMap::getPropertyMap()[strtolower($fq_classlike_name)];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
if (!$this->classlike_storages) {
|
|
|
|
throw new \UnexpectedValueException('$this->classlike_storages cannot be empty');
|
2017-07-29 21:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
$storage = $this->classlike_storages[count($this->classlike_storages) - 1];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-03-11 22:41:05 +01:00
|
|
|
foreach ($mapped_properties as $property_name => $public_mapped_property) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$property_type = Type::parseString($public_mapped_property);
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
$property_type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
if (!isset($storage->properties[$property_name])) {
|
|
|
|
$storage->properties[$property_name] = new PropertyStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->properties[$property_name]->type = $property_type;
|
|
|
|
|
|
|
|
$property_id = $fq_classlike_name . '::$' . $property_name;
|
|
|
|
|
2018-07-22 01:56:26 +02:00
|
|
|
$storage->declaring_property_ids[$property_name] = $fq_classlike_name;
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage->appearing_property_ids[$property_name] = $property_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
if (!$this->classlike_storages) {
|
|
|
|
throw new \LogicException('$this->classlike_storages should not be empty');
|
|
|
|
}
|
|
|
|
|
2017-11-07 20:46:53 +01:00
|
|
|
$classlike_storage = array_pop($this->classlike_storages);
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2018-05-29 16:13:26 +02:00
|
|
|
if ($classlike_storage->has_visitor_issues) {
|
|
|
|
$this->file_storage->has_visitor_issues = true;
|
|
|
|
}
|
|
|
|
|
2019-09-27 18:58:32 +02:00
|
|
|
if ($node->name) {
|
|
|
|
$this->class_template_types = [];
|
|
|
|
}
|
2017-11-07 20:46:53 +01:00
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
if ($this->after_classlike_check_plugins) {
|
2017-11-07 20:46:53 +01:00
|
|
|
$file_manipulations = [];
|
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
foreach ($this->after_classlike_check_plugins as $plugin_fq_class_name) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$plugin_fq_class_name::afterClassLikeVisit(
|
2017-11-07 20:46:53 +01:00
|
|
|
$node,
|
|
|
|
$classlike_storage,
|
2018-11-13 20:09:37 +01:00
|
|
|
$this,
|
|
|
|
$this->codebase,
|
2017-11-07 20:46:53 +01:00
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 06:27:39 +01:00
|
|
|
|
2018-05-29 16:13:26 +02:00
|
|
|
if (!$this->file_storage->has_visitor_issues) {
|
|
|
|
$this->codebase->cacheClassLikeStorage($classlike_storage, $this->file_path);
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\FunctionLike) {
|
2019-11-14 15:27:19 +01:00
|
|
|
if ($node instanceof PhpParser\Node\Stmt\Function_
|
|
|
|
|| $node instanceof PhpParser\Node\Stmt\ClassMethod
|
|
|
|
) {
|
|
|
|
$this->function_template_types = [];
|
|
|
|
}
|
|
|
|
|
2019-06-28 16:17:59 +02:00
|
|
|
if ($this->skip_if_descendants) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
if (!$this->functionlike_storages) {
|
2019-11-14 18:54:35 +01:00
|
|
|
if ($this->file_storage->has_visitor_issues) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-14 17:03:41 +01:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'There should be function storages for line ' . $this->file_path . ':' . $node->getLine()
|
|
|
|
);
|
2018-12-19 22:15:19 +01:00
|
|
|
}
|
|
|
|
|
2018-05-29 16:13:26 +02:00
|
|
|
$functionlike_storage = array_pop($this->functionlike_storages);
|
|
|
|
|
|
|
|
if ($functionlike_storage->has_visitor_issues) {
|
|
|
|
$this->file_storage->has_visitor_issues = true;
|
|
|
|
}
|
2019-07-11 00:58:56 +02:00
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\If_ && $node->getLine() === $this->skip_if_descendants) {
|
|
|
|
$this->exists_cond_expr = null;
|
|
|
|
$this->not_exists_cond_expr = null;
|
|
|
|
$this->skip_if_descendants = null;
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Else_ && $node->getLine() === $this->skip_if_descendants) {
|
2019-06-10 05:50:18 +02:00
|
|
|
$this->exists_cond_expr = null;
|
|
|
|
$this->not_exists_cond_expr = null;
|
2019-07-11 00:58:56 +02:00
|
|
|
$this->skip_if_descendants = null;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2017-11-26 22:03:17 +01:00
|
|
|
|
|
|
|
return null;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 05:50:18 +02:00
|
|
|
private function enterConditional(PhpParser\Node\Expr\FuncCall $function) : bool
|
|
|
|
{
|
|
|
|
if (!$function->name instanceof PhpParser\Node\Name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function->name->parts === ['function_exists']
|
|
|
|
&& isset($function->args[0])
|
|
|
|
&& $function->args[0]->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
&& function_exists($function->args[0]->value->value)
|
|
|
|
) {
|
|
|
|
$reflection_function = new \ReflectionFunction($function->args[0]->value->value);
|
|
|
|
|
|
|
|
if ($reflection_function->isInternal()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} elseif ($function->name->parts === ['class_exists']
|
|
|
|
&& isset($function->args[0])
|
|
|
|
) {
|
|
|
|
$string_value = null;
|
|
|
|
|
|
|
|
if ($function->args[0]->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$string_value = $function->args[0]->value->value;
|
|
|
|
} elseif ($function->args[0]->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $function->args[0]->value->class instanceof PhpParser\Node\Name
|
|
|
|
&& $function->args[0]->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& strtolower($function->args[0]->value->name->name) === 'class'
|
|
|
|
) {
|
|
|
|
$string_value = (string) $function->args[0]->value->class->getAttribute('resolvedName');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($string_value && class_exists($string_value)) {
|
|
|
|
$reflection_class = new \ReflectionClass($string_value);
|
|
|
|
|
|
|
|
if ($reflection_class->getFileName() !== $this->file_path) {
|
|
|
|
$this->codebase->scanner->queueClassLikeForScanning(
|
2020-04-02 23:17:55 +02:00
|
|
|
$string_value
|
2019-06-10 05:50:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($function->name->parts === ['interface_exists']
|
|
|
|
&& isset($function->args[0])
|
|
|
|
) {
|
|
|
|
$string_value = null;
|
|
|
|
|
|
|
|
if ($function->args[0]->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$string_value = $function->args[0]->value->value;
|
|
|
|
} elseif ($function->args[0]->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $function->args[0]->value->class instanceof PhpParser\Node\Name
|
|
|
|
&& $function->args[0]->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& strtolower($function->args[0]->value->name->name) === 'class'
|
|
|
|
) {
|
|
|
|
$string_value = (string) $function->args[0]->value->class->getAttribute('resolvedName');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($string_value && interface_exists($string_value)) {
|
|
|
|
$reflection_class = new \ReflectionClass($string_value);
|
|
|
|
|
|
|
|
if ($reflection_class->getFileName() !== $this->file_path) {
|
|
|
|
$this->codebase->scanner->queueClassLikeForScanning(
|
2020-04-02 23:17:55 +02:00
|
|
|
$string_value
|
2019-06-10 05:50:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-28 23:54:50 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function registerClassMapFunctionCall(
|
|
|
|
string $function_id,
|
|
|
|
PhpParser\Node\Expr\FuncCall $node
|
|
|
|
) {
|
2019-06-15 22:10:48 +02:00
|
|
|
$callables = CallMap::getCallablesFromCallMap($function_id);
|
2019-01-28 23:54:50 +01:00
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
if ($callables) {
|
|
|
|
foreach ($callables as $callable) {
|
|
|
|
assert($callable->params !== null);
|
|
|
|
|
|
|
|
foreach ($callable->params as $function_param) {
|
2019-01-28 23:54:50 +01:00
|
|
|
if ($function_param->type) {
|
|
|
|
$function_param->type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 22:10:48 +02:00
|
|
|
|
|
|
|
if ($callable->return_type && !$callable->return_type->hasMixed()) {
|
|
|
|
$callable->return_type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
|
|
|
}
|
2019-01-28 23:54:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_id === 'define') {
|
|
|
|
$first_arg_value = isset($node->args[0]) ? $node->args[0]->value : null;
|
|
|
|
$second_arg_value = isset($node->args[1]) ? $node->args[1]->value : null;
|
2019-02-18 19:51:27 +01:00
|
|
|
if ($first_arg_value && $second_arg_value) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider = new \Psalm\Internal\Provider\NodeDataProvider();
|
|
|
|
$const_name = StatementsAnalyzer::getConstName(
|
|
|
|
$first_arg_value,
|
|
|
|
$type_provider,
|
|
|
|
$this->codebase,
|
|
|
|
$this->aliases
|
|
|
|
);
|
2019-02-22 00:19:12 +01:00
|
|
|
|
2019-02-18 19:51:27 +01:00
|
|
|
if ($const_name !== null) {
|
|
|
|
$const_type = StatementsAnalyzer::getSimpleType(
|
|
|
|
$this->codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider,
|
2019-02-18 19:51:27 +01:00
|
|
|
$second_arg_value,
|
|
|
|
$this->aliases
|
|
|
|
) ?: Type::getMixed();
|
2019-03-25 05:07:32 +01:00
|
|
|
|
2019-02-18 19:51:27 +01:00
|
|
|
if ($this->functionlike_storages && !$this->config->hoist_constants) {
|
|
|
|
$functionlike_storage =
|
|
|
|
$this->functionlike_storages[count($this->functionlike_storages) - 1];
|
|
|
|
$functionlike_storage->defined_constants[$const_name] = $const_type;
|
|
|
|
} else {
|
|
|
|
$this->file_storage->constants[$const_name] = $const_type;
|
|
|
|
$this->file_storage->declaring_constants[$const_name] = $this->file_path;
|
|
|
|
}
|
2019-03-25 05:07:32 +01:00
|
|
|
|
|
|
|
if ($this->codebase->register_stub_files || $this->codebase->register_autoload_files) {
|
|
|
|
$this->codebase->addGlobalConstantType($const_name, $const_type);
|
|
|
|
}
|
2019-01-28 23:54:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mapping_function_ids = [];
|
|
|
|
|
|
|
|
if (($function_id === 'array_map' && isset($node->args[0]))
|
|
|
|
|| ($function_id === 'array_filter' && isset($node->args[1]))
|
|
|
|
) {
|
2019-06-29 18:32:44 +02:00
|
|
|
$node_arg_value = $function_id === 'array_map' ? $node->args[0]->value : $node->args[1]->value;
|
2019-01-28 23:54:50 +01:00
|
|
|
|
|
|
|
if ($node_arg_value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $node_arg_value instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|| $node_arg_value instanceof PhpParser\Node\Expr\BinaryOp\Concat
|
|
|
|
) {
|
|
|
|
$mapping_function_ids = CallAnalyzer::getFunctionIdsFromCallableArg(
|
|
|
|
$this->file_scanner,
|
|
|
|
$node_arg_value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($mapping_function_ids as $potential_method_id) {
|
|
|
|
if (strpos($potential_method_id, '::') === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($callable_fqcln) = explode('::', $potential_method_id);
|
|
|
|
|
|
|
|
if (!in_array(strtolower($callable_fqcln), ['self', 'parent', 'static'], true)) {
|
|
|
|
$this->codebase->scanner->queueClassLikeForScanning(
|
2020-04-02 23:17:55 +02:00
|
|
|
$callable_fqcln
|
2019-01-28 23:54:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_id === 'func_get_arg'
|
|
|
|
|| $function_id === 'func_get_args'
|
|
|
|
|| $function_id === 'func_num_args'
|
|
|
|
) {
|
|
|
|
$function_like_storage = end($this->functionlike_storages);
|
|
|
|
|
|
|
|
if ($function_like_storage) {
|
|
|
|
$function_like_storage->variadic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function_id === 'is_a' || $function_id === 'is_subclass_of') {
|
|
|
|
$second_arg = $node->args[1]->value ?? null;
|
|
|
|
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$this->codebase->scanner->queueClassLikeForScanning(
|
2020-04-02 23:17:55 +02:00
|
|
|
$second_arg->value
|
2019-01-28 23:54:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 21:46:11 +02:00
|
|
|
if ($function_id === 'class_alias' && !$this->skip_if_descendants) {
|
2019-01-28 23:54:50 +01:00
|
|
|
$first_arg = $node->args[0]->value ?? null;
|
|
|
|
$second_arg = $node->args[1]->value ?? null;
|
|
|
|
|
|
|
|
if ($first_arg instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$first_arg_value = $first_arg->value;
|
|
|
|
} elseif ($first_arg instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $first_arg->class instanceof PhpParser\Node\Name
|
|
|
|
&& $first_arg->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& strtolower($first_arg->name->name) === 'class'
|
|
|
|
) {
|
|
|
|
/** @var string */
|
|
|
|
$first_arg_value = $first_arg->class->getAttribute('resolvedName');
|
|
|
|
} else {
|
|
|
|
$first_arg_value = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$second_arg_value = $second_arg->value;
|
|
|
|
} elseif ($second_arg instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $second_arg->class instanceof PhpParser\Node\Name
|
|
|
|
&& $second_arg->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& strtolower($second_arg->name->name) === 'class'
|
|
|
|
) {
|
|
|
|
/** @var string */
|
|
|
|
$second_arg_value = $second_arg->class->getAttribute('resolvedName');
|
|
|
|
} else {
|
|
|
|
$second_arg_value = null;
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($first_arg_value !== null && $second_arg_value !== null) {
|
|
|
|
$second_arg_value = strtolower($second_arg_value);
|
|
|
|
|
2019-01-28 23:54:50 +01:00
|
|
|
$this->codebase->classlikes->addClassAlias(
|
|
|
|
$first_arg_value,
|
|
|
|
$second_arg_value
|
|
|
|
);
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$this->file_storage->classlike_aliases[$second_arg_value] = $first_arg_value;
|
2019-01-28 23:54:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
private function registerClassLike(PhpParser\Node\Stmt\ClassLike $node)
|
|
|
|
{
|
2019-06-01 06:56:54 +02:00
|
|
|
$class_location = new CodeLocation($this->file_scanner, $node);
|
|
|
|
$name_location = null;
|
2018-07-22 04:24:33 +02:00
|
|
|
|
|
|
|
$storage = null;
|
|
|
|
|
2019-01-20 17:10:12 +01:00
|
|
|
$class_name = null;
|
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
if ($node->name === null) {
|
|
|
|
if (!$node instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
throw new \LogicException('Anonymous classes are always classes');
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$fq_classlike_name = ClassAnalyzer::getAnonymousClassName($node, $this->file_path);
|
2018-07-22 04:24:33 +02:00
|
|
|
} else {
|
2019-06-01 06:56:54 +02:00
|
|
|
$name_location = new CodeLocation($this->file_scanner, $node->name);
|
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
$fq_classlike_name =
|
|
|
|
($this->aliases->namespace ? $this->aliases->namespace . '\\' : '') . $node->name->name;
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
2019-01-20 17:10:12 +01:00
|
|
|
$class_name = $node->name->name;
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($this->codebase->classlike_storage_provider->has($fq_classlike_name_lc)) {
|
|
|
|
$duplicate_storage = $this->codebase->classlike_storage_provider->get($fq_classlike_name_lc);
|
2018-07-22 04:24:33 +02:00
|
|
|
|
|
|
|
if (!$this->codebase->register_stub_files) {
|
2019-06-01 06:56:54 +02:00
|
|
|
if (!$duplicate_storage->stmt_location
|
|
|
|
|| $duplicate_storage->stmt_location->file_path !== $this->file_path
|
|
|
|
|| $class_location->getHash() !== $duplicate_storage->stmt_location->getHash()
|
2018-07-22 04:24:33 +02:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateClass(
|
|
|
|
'Class ' . $fq_classlike_name . ' has already been defined'
|
|
|
|
. ($duplicate_storage->location
|
|
|
|
? ' in ' . $duplicate_storage->location->file_path
|
|
|
|
: ''),
|
2019-06-01 06:56:54 +02:00
|
|
|
$name_location
|
2018-07-22 04:24:33 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
}
|
|
|
|
|
2018-11-01 22:03:08 +01:00
|
|
|
$this->file_storage->has_visitor_issues = true;
|
|
|
|
|
2018-09-04 20:34:14 +02:00
|
|
|
$duplicate_storage->has_visitor_issues = true;
|
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif (!$duplicate_storage->location
|
|
|
|
|| $duplicate_storage->location->file_path !== $this->file_path
|
|
|
|
|| $class_location->getHash() !== $duplicate_storage->location->getHash()
|
|
|
|
) {
|
|
|
|
// we're overwriting some methods
|
|
|
|
$storage = $duplicate_storage;
|
2019-01-10 22:59:44 +01:00
|
|
|
$this->codebase->classlike_storage_provider->makeNew(strtolower($fq_classlike_name));
|
2019-01-24 21:03:13 +01:00
|
|
|
$storage->populated = false;
|
2019-01-26 22:58:49 +01:00
|
|
|
$storage->class_implements = []; // we do this because reflection reports
|
|
|
|
$storage->parent_interfaces = [];
|
2020-01-08 23:23:40 +01:00
|
|
|
$storage->stubbed = true;
|
2020-01-06 00:37:07 +01:00
|
|
|
$storage->aliases = $this->aliases;
|
2019-01-24 21:03:13 +01:00
|
|
|
|
|
|
|
foreach ($storage->dependent_classlikes as $dependent_name_lc => $_) {
|
|
|
|
$dependent_storage = $this->codebase->classlike_storage_provider->get($dependent_name_lc);
|
|
|
|
$dependent_storage->populated = false;
|
|
|
|
$this->codebase->classlike_storage_provider->makeNew($dependent_name_lc);
|
|
|
|
}
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
|
|
$this->file_storage->classlikes_in_file[$fq_classlike_name_lc] = $fq_classlike_name;
|
|
|
|
|
|
|
|
$this->fq_classlike_names[] = $fq_classlike_name;
|
|
|
|
|
|
|
|
if (!$storage) {
|
2019-01-10 22:59:44 +01:00
|
|
|
$storage = $this->codebase->classlike_storage_provider->create($fq_classlike_name);
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 17:10:12 +01:00
|
|
|
if ($class_name
|
|
|
|
&& isset($this->aliases->uses[strtolower($class_name)])
|
2019-01-20 17:29:25 +01:00
|
|
|
&& $this->aliases->uses[strtolower($class_name)] !== $fq_classlike_name
|
2019-01-20 17:10:12 +01:00
|
|
|
) {
|
|
|
|
IssueBuffer::add(
|
|
|
|
new \Psalm\Issue\ParseError(
|
|
|
|
'Class name ' . $class_name . ' clashes with a use statement alias',
|
2019-06-01 06:56:54 +02:00
|
|
|
$name_location ?: $class_location
|
2019-01-20 17:10:12 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$storage->has_visitor_issues = true;
|
|
|
|
$this->file_storage->has_visitor_issues = true;
|
|
|
|
}
|
|
|
|
|
2019-06-01 06:56:54 +02:00
|
|
|
$storage->stmt_location = $class_location;
|
|
|
|
$storage->location = $name_location;
|
2019-06-04 22:36:32 +02:00
|
|
|
if ($this->namespace_name) {
|
|
|
|
$storage->namespace_name_location = new CodeLocation($this->file_scanner, $this->namespace_name);
|
|
|
|
}
|
2018-07-22 04:24:33 +02:00
|
|
|
$storage->user_defined = !$this->codebase->register_stub_files;
|
|
|
|
$storage->stubbed = $this->codebase->register_stub_files;
|
2019-06-01 16:07:38 +02:00
|
|
|
$storage->aliases = $this->aliases;
|
2018-07-22 04:24:33 +02:00
|
|
|
|
|
|
|
$doc_comment = $node->getDocComment();
|
|
|
|
|
|
|
|
$this->classlike_storages[] = $storage;
|
|
|
|
|
2019-01-10 22:59:44 +01:00
|
|
|
if ($node instanceof PhpParser\Node\Stmt\Class_) {
|
2019-07-19 05:37:36 +02:00
|
|
|
$storage->abstract = $node->isAbstract();
|
|
|
|
$storage->final = $node->isFinal();
|
2019-01-10 22:59:44 +01:00
|
|
|
|
|
|
|
$this->codebase->classlikes->addFullyQualifiedClassName($fq_classlike_name, $this->file_path);
|
|
|
|
|
|
|
|
if ($node->extends) {
|
|
|
|
$parent_fqcln = ClassLikeAnalyzer::getFQCLNFromNameObject($node->extends, $this->aliases);
|
2020-01-01 23:23:13 +01:00
|
|
|
$parent_fqcln = $this->codebase->classlikes->getUnAliasedName($parent_fqcln);
|
2019-01-10 22:59:44 +01:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning(
|
|
|
|
$parent_fqcln,
|
|
|
|
$this->scan_deep
|
|
|
|
);
|
|
|
|
$parent_fqcln_lc = strtolower($parent_fqcln);
|
2019-05-25 17:51:09 +02:00
|
|
|
$storage->parent_class = $parent_fqcln;
|
2019-03-08 23:35:09 +01:00
|
|
|
$storage->parent_classes[$parent_fqcln_lc] = $parent_fqcln;
|
2019-01-10 22:59:44 +01:00
|
|
|
$this->file_storage->required_classes[strtolower($parent_fqcln)] = $parent_fqcln;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->implements as $interface) {
|
|
|
|
$interface_fqcln = ClassLikeAnalyzer::getFQCLNFromNameObject($interface, $this->aliases);
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning($interface_fqcln);
|
2019-01-10 22:59:44 +01:00
|
|
|
$storage->class_implements[strtolower($interface_fqcln)] = $interface_fqcln;
|
2020-01-08 23:23:40 +01:00
|
|
|
$storage->direct_class_interfaces[strtolower($interface_fqcln)] = $interface_fqcln;
|
2019-01-10 22:59:44 +01:00
|
|
|
$this->file_storage->required_interfaces[strtolower($interface_fqcln)] = $interface_fqcln;
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Interface_) {
|
|
|
|
$storage->is_interface = true;
|
|
|
|
$this->codebase->classlikes->addFullyQualifiedInterfaceName($fq_classlike_name, $this->file_path);
|
|
|
|
|
|
|
|
foreach ($node->extends as $interface) {
|
|
|
|
$interface_fqcln = ClassLikeAnalyzer::getFQCLNFromNameObject($interface, $this->aliases);
|
2020-01-01 23:23:13 +01:00
|
|
|
$interface_fqcln = $this->codebase->classlikes->getUnAliasedName($interface_fqcln);
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning($interface_fqcln);
|
2019-01-10 22:59:44 +01:00
|
|
|
$storage->parent_interfaces[strtolower($interface_fqcln)] = $interface_fqcln;
|
2020-01-08 23:23:40 +01:00
|
|
|
$storage->direct_interface_parents[strtolower($interface_fqcln)] = $interface_fqcln;
|
2019-01-10 22:59:44 +01:00
|
|
|
$this->file_storage->required_interfaces[strtolower($interface_fqcln)] = $interface_fqcln;
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof PhpParser\Node\Stmt\Trait_) {
|
|
|
|
$storage->is_trait = true;
|
|
|
|
$this->file_storage->has_trait = true;
|
|
|
|
$this->codebase->classlikes->addFullyQualifiedTraitName($fq_classlike_name, $this->file_path);
|
|
|
|
}
|
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
if ($doc_comment) {
|
|
|
|
$docblock_info = null;
|
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$docblock_info = CommentAnalyzer::extractClassLikeDocblockInfo(
|
2019-03-19 02:05:37 +01:00
|
|
|
$node,
|
2019-08-08 23:25:56 +02:00
|
|
|
$doc_comment,
|
|
|
|
$this->aliases
|
2018-07-22 04:24:33 +02:00
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
$name_location ?: $class_location
|
|
|
|
);
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info) {
|
2018-12-18 05:29:27 +01:00
|
|
|
if ($docblock_info->templates) {
|
2018-07-22 04:24:33 +02:00
|
|
|
$storage->template_types = [];
|
|
|
|
|
2019-11-30 07:06:48 +01:00
|
|
|
\usort(
|
2019-11-30 07:02:23 +01:00
|
|
|
$docblock_info->templates,
|
2019-11-30 07:06:48 +01:00
|
|
|
function (array $l, array $r) : int {
|
2019-11-30 07:02:23 +01:00
|
|
|
return $l[4] > $r[4] ? 1 : -1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-05-06 22:38:08 +02:00
|
|
|
foreach ($docblock_info->templates as $i => $template_map) {
|
|
|
|
$template_name = $template_map[0];
|
|
|
|
|
|
|
|
if ($template_map[1] !== null && $template_map[2] !== null) {
|
|
|
|
if (trim($template_map[2])) {
|
2019-01-10 22:59:44 +01:00
|
|
|
try {
|
|
|
|
$template_type = Type::parseTokens(
|
2019-01-10 18:13:49 +01:00
|
|
|
Type::fixUpLocalType(
|
2019-05-06 22:38:08 +02:00
|
|
|
$template_map[2],
|
2019-01-10 18:13:49 +01:00
|
|
|
$this->aliases,
|
|
|
|
null,
|
|
|
|
$this->type_aliases
|
|
|
|
)
|
2019-01-10 22:59:44 +01:00
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for '
|
|
|
|
. implode('.', $this->fq_classlike_names),
|
|
|
|
$name_location ?: $class_location
|
|
|
|
);
|
2019-01-10 22:59:44 +01:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->template_types[$template_name] = [
|
2019-07-05 22:24:00 +02:00
|
|
|
$fq_classlike_name => [$template_type],
|
2019-01-10 18:13:49 +01:00
|
|
|
];
|
2019-01-08 21:11:57 +01:00
|
|
|
} else {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Template missing as type',
|
|
|
|
$name_location ?: $class_location
|
|
|
|
);
|
2019-01-08 21:11:57 +01:00
|
|
|
}
|
2018-07-22 04:24:33 +02:00
|
|
|
} else {
|
2020-03-28 22:18:21 +01:00
|
|
|
/** @psalm-suppress PropertyTypeCoercion due to a Psalm bug */
|
2019-03-22 20:59:10 +01:00
|
|
|
$storage->template_types[$template_name][$fq_classlike_name] = [Type::getMixed()];
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
2019-11-01 18:25:07 +01:00
|
|
|
|
|
|
|
$storage->template_covariants[$i] = $template_map[3];
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->class_template_types = $storage->template_types;
|
2019-01-10 23:58:32 +01:00
|
|
|
}
|
2018-07-22 04:24:33 +02:00
|
|
|
|
2019-01-10 23:58:32 +01:00
|
|
|
foreach ($docblock_info->template_extends as $extended_class_name) {
|
2019-01-28 03:00:27 +01:00
|
|
|
$this->extendTemplatedType($storage, $node, $extended_class_name);
|
|
|
|
}
|
2019-01-10 22:59:44 +01:00
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
foreach ($docblock_info->template_implements as $implemented_class_name) {
|
|
|
|
$this->implementTemplatedType($storage, $node, $implemented_class_name);
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 04:38:10 +02:00
|
|
|
if ($docblock_info->yield) {
|
|
|
|
$yield_type_tokens = Type::fixUpLocalType(
|
|
|
|
$docblock_info->yield,
|
|
|
|
$this->aliases,
|
|
|
|
$storage->template_types,
|
|
|
|
$this->type_aliases
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$yield_type = Type::parseTokens(
|
|
|
|
$yield_type_tokens,
|
|
|
|
null,
|
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
|
|
|
$yield_type->setFromDocblock();
|
|
|
|
$yield_type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage,
|
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
|
|
|
|
|
|
|
$storage->yield = $yield_type;
|
|
|
|
} catch (TypeParseTreeException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 14:44:52 +01:00
|
|
|
$storage->sealed_properties = $docblock_info->sealed_properties;
|
|
|
|
$storage->sealed_methods = $docblock_info->sealed_methods;
|
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
if ($docblock_info->properties) {
|
|
|
|
foreach ($docblock_info->properties as $property) {
|
|
|
|
$pseudo_property_type_tokens = Type::fixUpLocalType(
|
|
|
|
$property['type'],
|
|
|
|
$this->aliases,
|
|
|
|
null,
|
|
|
|
$this->type_aliases
|
|
|
|
);
|
|
|
|
|
2018-11-02 04:04:00 +01:00
|
|
|
try {
|
|
|
|
$pseudo_property_type = Type::parseTokens($pseudo_property_type_tokens);
|
|
|
|
$pseudo_property_type->setFromDocblock();
|
2019-11-30 06:27:07 +01:00
|
|
|
$pseudo_property_type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage,
|
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
2018-07-22 04:24:33 +02:00
|
|
|
|
2020-03-24 23:32:57 +01:00
|
|
|
if ($property['tag'] !== 'property-read' && $property['tag'] !== 'psalm-property-read') {
|
2018-11-02 04:04:00 +01:00
|
|
|
$storage->pseudo_property_set_types[$property['name']] = $pseudo_property_type;
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:32:57 +01:00
|
|
|
if ($property['tag'] !== 'property-write' && $property['tag'] !== 'psalm-property-write') {
|
2018-11-02 04:04:00 +01:00
|
|
|
$storage->pseudo_property_get_types[$property['name']] = $pseudo_property_type;
|
|
|
|
}
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
$name_location ?: $class_location
|
|
|
|
);
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 14:44:52 +01:00
|
|
|
|
|
|
|
$storage->sealed_properties = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($docblock_info->methods as $method) {
|
|
|
|
/** @var MethodStorage */
|
|
|
|
$pseudo_method_storage = $this->registerFunctionLike($method, true);
|
|
|
|
|
|
|
|
if ($pseudo_method_storage->is_static) {
|
|
|
|
$storage->pseudo_static_methods[strtolower($method->name->name)] = $pseudo_method_storage;
|
|
|
|
} else {
|
|
|
|
$storage->pseudo_methods[strtolower($method->name->name)] = $pseudo_method_storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->sealed_methods = true;
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$storage->deprecated = $docblock_info->deprecated;
|
2018-12-02 00:37:49 +01:00
|
|
|
$storage->internal = $docblock_info->internal;
|
2019-05-11 21:39:53 +02:00
|
|
|
$storage->psalm_internal = $docblock_info->psalm_internal;
|
2018-07-22 04:24:33 +02:00
|
|
|
|
2020-01-03 05:50:19 +01:00
|
|
|
if ($docblock_info->mixin) {
|
|
|
|
if (isset($this->class_template_types[$docblock_info->mixin])) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Templates are not currently supported for @mixin',
|
|
|
|
$name_location ?: $class_location
|
|
|
|
);
|
2020-01-03 05:50:19 +01:00
|
|
|
} else {
|
|
|
|
$storage->mixin_fqcln = Type::getFQCLNFromString(
|
|
|
|
$docblock_info->mixin,
|
|
|
|
$this->aliases
|
|
|
|
);
|
2020-01-03 14:34:11 +01:00
|
|
|
|
|
|
|
// if there's a mixin, assume it's the reason for the __call
|
|
|
|
$storage->sealed_properties = true;
|
|
|
|
$storage->sealed_methods = true;
|
2020-01-03 05:50:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:36:35 +02:00
|
|
|
$storage->mutation_free = $docblock_info->mutation_free;
|
|
|
|
$storage->external_mutation_free = $docblock_info->external_mutation_free;
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
$storage->override_property_visibility = $docblock_info->override_property_visibility;
|
|
|
|
$storage->override_method_visibility = $docblock_info->override_method_visibility;
|
2018-11-25 00:31:00 +01:00
|
|
|
|
2018-07-22 04:24:33 +02:00
|
|
|
$storage->suppressed_issues = $docblock_info->suppressed_issues;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->stmts as $node_stmt) {
|
|
|
|
if ($node_stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
|
|
|
$this->visitClassConstDeclaration($node_stmt, $storage, $fq_classlike_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->stmts as $node_stmt) {
|
|
|
|
if ($node_stmt instanceof PhpParser\Node\Stmt\Property) {
|
|
|
|
$this->visitPropertyDeclaration($node_stmt, $this->config, $storage, $fq_classlike_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function extendTemplatedType(
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
PhpParser\Node\Stmt\ClassLike $node,
|
|
|
|
string $extended_class_name
|
|
|
|
) {
|
2019-07-28 22:47:06 +02:00
|
|
|
if (trim($extended_class_name) === '') {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Extended class cannot be empty in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-07-28 22:47:06 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
try {
|
|
|
|
$extended_union_type = Type::parseTokens(
|
|
|
|
Type::fixUpLocalType(
|
|
|
|
$extended_class_name,
|
|
|
|
$this->aliases,
|
|
|
|
$this->class_template_types,
|
|
|
|
$this->type_aliases
|
|
|
|
),
|
2019-02-07 18:25:57 +01:00
|
|
|
null,
|
2019-01-28 03:00:27 +01:00
|
|
|
$this->class_template_types
|
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$extended_union_type->isSingle()) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-extends cannot be a union type',
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 00:41:26 +02:00
|
|
|
$extended_union_type->setFromDocblock();
|
|
|
|
|
2020-03-05 06:58:23 +01:00
|
|
|
$extended_union_type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage,
|
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($extended_union_type->getAtomicTypes() as $atomic_type) {
|
2019-01-28 03:00:27 +01:00
|
|
|
if (!$atomic_type instanceof Type\Atomic\TGenericObject) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-extends has invalid class ' . $atomic_type->getId(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$generic_class_lc = strtolower($atomic_type->value);
|
|
|
|
|
|
|
|
if (!isset($storage->parent_classes[$generic_class_lc])
|
|
|
|
&& !isset($storage->parent_interfaces[$generic_class_lc])
|
|
|
|
) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-extends must include the name of an extended class,'
|
|
|
|
. ' got ' . $atomic_type->getId(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$extended_type_parameters = [];
|
|
|
|
|
|
|
|
$storage->template_type_extends_count = count($atomic_type->type_params);
|
|
|
|
|
|
|
|
foreach ($atomic_type->type_params as $type_param) {
|
2019-03-16 16:15:25 +01:00
|
|
|
$extended_type_parameters[] = $type_param;
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:04:34 +02:00
|
|
|
$storage->template_type_extends[$atomic_type->value] = $extended_type_parameters;
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function implementTemplatedType(
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
PhpParser\Node\Stmt\ClassLike $node,
|
|
|
|
string $implemented_class_name
|
|
|
|
) {
|
2019-07-28 22:47:06 +02:00
|
|
|
if (trim($implemented_class_name) === '') {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Extended class cannot be empty in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-07-28 22:47:06 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
try {
|
|
|
|
$implemented_union_type = Type::parseTokens(
|
|
|
|
Type::fixUpLocalType(
|
|
|
|
$implemented_class_name,
|
|
|
|
$this->aliases,
|
|
|
|
$this->class_template_types,
|
|
|
|
$this->type_aliases
|
|
|
|
),
|
2019-02-07 18:25:57 +01:00
|
|
|
null,
|
2019-01-28 03:00:27 +01:00
|
|
|
$this->class_template_types
|
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$implemented_union_type->isSingle()) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-implements cannot be a union type',
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
|
|
|
return;
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 00:41:26 +02:00
|
|
|
$implemented_union_type->setFromDocblock();
|
|
|
|
|
2020-03-05 06:58:23 +01:00
|
|
|
$implemented_union_type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage,
|
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($implemented_union_type->getAtomicTypes() as $atomic_type) {
|
2019-01-28 03:00:27 +01:00
|
|
|
if (!$atomic_type instanceof Type\Atomic\TGenericObject) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-implements has invalid class ' . $atomic_type->getId(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
2019-01-28 03:00:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$generic_class_lc = strtolower($atomic_type->value);
|
|
|
|
|
|
|
|
if (!isset($storage->class_implements[$generic_class_lc])) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-implements must include the name of an implemented class,'
|
|
|
|
. ' got ' . $atomic_type->getId(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
|
|
|
return;
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$implemented_type_parameters = [];
|
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
$storage->template_type_implements_count[$generic_class_lc] = count($atomic_type->type_params);
|
2019-01-28 03:00:27 +01:00
|
|
|
|
|
|
|
foreach ($atomic_type->type_params as $type_param) {
|
2019-03-16 16:15:25 +01:00
|
|
|
$implemented_type_parameters[] = $type_param;
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:04:34 +02:00
|
|
|
$storage->template_type_extends[$atomic_type->value] = $implemented_type_parameters;
|
2019-01-28 03:00:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function useTemplatedType(
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
PhpParser\Node\Stmt\TraitUse $node,
|
|
|
|
string $used_class_name
|
|
|
|
) {
|
2019-07-28 22:47:06 +02:00
|
|
|
if (trim($used_class_name) === '') {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Extended class cannot be empty in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-07-28 22:47:06 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
try {
|
|
|
|
$used_union_type = Type::parseTokens(
|
|
|
|
Type::fixUpLocalType(
|
|
|
|
$used_class_name,
|
|
|
|
$this->aliases,
|
|
|
|
$this->class_template_types,
|
|
|
|
$this->type_aliases
|
|
|
|
),
|
2019-02-07 18:25:57 +01:00
|
|
|
null,
|
2019-01-28 05:12:40 +01:00
|
|
|
$this->class_template_types
|
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . implode('.', $this->fq_classlike_names),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$used_union_type->isSingle()) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-use cannot be a union type',
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
|
|
|
return;
|
2019-01-28 05:12:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 00:41:26 +02:00
|
|
|
$used_union_type->setFromDocblock();
|
|
|
|
|
2020-03-05 06:58:23 +01:00
|
|
|
$used_union_type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage,
|
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($used_union_type->getAtomicTypes() as $atomic_type) {
|
2019-01-28 05:12:40 +01:00
|
|
|
if (!$atomic_type instanceof Type\Atomic\TGenericObject) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-use has invalid class ' . $atomic_type->getId(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$generic_class_lc = strtolower($atomic_type->value);
|
|
|
|
|
|
|
|
if (!isset($storage->used_traits[$generic_class_lc])) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'@template-use must include the name of an used class,'
|
|
|
|
. ' got ' . $atomic_type->getId(),
|
|
|
|
new CodeLocation($this->file_scanner, $node, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
|
|
|
return;
|
2019-01-28 05:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$used_type_parameters = [];
|
|
|
|
|
|
|
|
$storage->template_type_uses_count[$generic_class_lc] = count($atomic_type->type_params);
|
|
|
|
|
|
|
|
foreach ($atomic_type->type_params as $type_param) {
|
2019-03-16 16:15:25 +01:00
|
|
|
$used_type_parameters[] = $type_param;
|
2019-01-28 05:12:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:04:34 +02:00
|
|
|
$storage->template_type_extends[$atomic_type->value] = $used_type_parameters;
|
2019-01-28 05:12:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\FunctionLike $stmt
|
2018-04-22 04:13:10 +02:00
|
|
|
* @param bool $fake_method in the case of @method annotations we do something a little strange
|
2017-07-25 22:11:02 +02:00
|
|
|
*
|
2019-01-02 17:18:22 +01:00
|
|
|
* @return FunctionLikeStorage|false
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
2018-04-22 04:13:10 +02:00
|
|
|
private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_method = false)
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
$class_storage = null;
|
2019-11-30 18:54:08 +01:00
|
|
|
$fq_classlike_name = null;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-06-14 23:20:02 +02:00
|
|
|
if ($fake_method && $stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2018-04-22 04:13:10 +02:00
|
|
|
$cased_function_id = '@method ' . $stmt->name->name;
|
|
|
|
|
2018-11-30 21:13:25 +01:00
|
|
|
$storage = new MethodStorage();
|
2019-01-28 05:12:40 +01:00
|
|
|
$storage->defining_fqcln = '';
|
2019-07-19 05:37:36 +02:00
|
|
|
$storage->is_static = $stmt->isStatic();
|
2020-02-02 00:21:32 +01:00
|
|
|
$class_storage = $this->classlike_storages[count($this->classlike_storages) - 1];
|
2018-04-22 04:13:10 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
2018-04-17 18:16:25 +02:00
|
|
|
$cased_function_id =
|
|
|
|
($this->aliases->namespace ? $this->aliases->namespace . '\\' : '') . $stmt->name->name;
|
2017-07-25 22:11:02 +02:00
|
|
|
$function_id = strtolower($cased_function_id);
|
|
|
|
|
2019-01-06 23:24:46 +01:00
|
|
|
$storage = new FunctionLikeStorage();
|
|
|
|
|
2019-01-06 22:40:44 +01:00
|
|
|
if ($this->codebase->register_stub_files || $this->codebase->register_autoload_files) {
|
2019-05-13 06:24:31 +02:00
|
|
|
if (isset($this->file_storage->functions[$function_id])
|
|
|
|
&& ($this->codebase->register_stub_files
|
|
|
|
|| !$this->codebase->functions->hasStubbedFunction($function_id))
|
|
|
|
) {
|
2018-06-30 21:29:37 +02:00
|
|
|
$this->codebase->functions->addGlobalFunction(
|
|
|
|
$function_id,
|
|
|
|
$this->file_storage->functions[$function_id]
|
|
|
|
);
|
2019-01-06 22:40:44 +01:00
|
|
|
|
2019-11-14 17:03:41 +01:00
|
|
|
$storage = $this->file_storage->functions[$function_id];
|
|
|
|
$this->functionlike_storages[] = $storage;
|
|
|
|
|
|
|
|
return $storage;
|
2018-06-30 21:29:37 +02:00
|
|
|
}
|
2019-01-06 22:40:44 +01:00
|
|
|
} else {
|
|
|
|
if (isset($this->file_storage->functions[$function_id])) {
|
|
|
|
$duplicate_function_storage = $this->file_storage->functions[$function_id];
|
|
|
|
|
2019-01-20 17:10:12 +01:00
|
|
|
if ($duplicate_function_storage->location
|
|
|
|
&& $duplicate_function_storage->location->getLineNumber() === $stmt->getLine()
|
|
|
|
) {
|
2019-11-14 18:17:17 +01:00
|
|
|
$storage = $this->file_storage->functions[$function_id];
|
|
|
|
$this->functionlike_storages[] = $storage;
|
|
|
|
|
|
|
|
return $storage;
|
2019-01-20 17:10:12 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 22:40:44 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateFunction(
|
|
|
|
'Method ' . $function_id . ' has already been defined'
|
|
|
|
. ($duplicate_function_storage->location
|
|
|
|
? ' in ' . $duplicate_function_storage->location->file_path
|
|
|
|
: ''),
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-06-30 21:29:37 +02:00
|
|
|
|
2019-01-06 22:40:44 +01:00
|
|
|
$this->file_storage->has_visitor_issues = true;
|
|
|
|
|
|
|
|
$duplicate_function_storage->has_visitor_issues = true;
|
|
|
|
|
2019-11-14 18:17:17 +01:00
|
|
|
$storage = $this->file_storage->functions[$function_id];
|
|
|
|
$this->functionlike_storages[] = $storage;
|
|
|
|
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->config->getPredefinedFunctions()[$function_id])) {
|
2019-04-10 00:09:57 +02:00
|
|
|
/** @psalm-suppress TypeCoercion */
|
2019-01-06 23:24:46 +01:00
|
|
|
$reflection_function = new \ReflectionFunction($function_id);
|
2019-01-06 23:03:13 +01:00
|
|
|
|
2019-01-06 23:24:46 +01:00
|
|
|
if ($reflection_function->getFileName() !== $this->file_path) {
|
2019-01-06 23:03:13 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateFunction(
|
|
|
|
'Method ' . $function_id . ' has already been defined as a core function',
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2019-01-06 22:40:44 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-29 21:28:45 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 06:24:31 +02:00
|
|
|
if ($this->codebase->register_stub_files
|
|
|
|
|| ($this->codebase->register_autoload_files
|
|
|
|
&& !$this->codebase->functions->hasStubbedFunction($function_id))
|
|
|
|
) {
|
2018-06-30 21:29:37 +02:00
|
|
|
$this->codebase->functions->addGlobalFunction($function_id, $storage);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2018-06-29 21:28:45 +02:00
|
|
|
|
|
|
|
$this->file_storage->functions[$function_id] = $storage;
|
|
|
|
$this->file_storage->declaring_function_ids[$function_id] = strtolower($this->file_path);
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2017-08-14 21:46:01 +02:00
|
|
|
if (!$this->fq_classlike_names) {
|
|
|
|
throw new \LogicException('$this->fq_classlike_names should not be null');
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
$fq_classlike_name = $this->fq_classlike_names[count($this->fq_classlike_names) - 1];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$method_name_lc = strtolower($stmt->name->name);
|
|
|
|
|
|
|
|
$function_id = $fq_classlike_name . '::' . $method_name_lc;
|
2018-04-17 18:16:25 +02:00
|
|
|
$cased_function_id = $fq_classlike_name . '::' . $stmt->name->name;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
if (!$this->classlike_storages) {
|
|
|
|
throw new \UnexpectedValueException('$class_storages cannot be empty for ' . $function_id);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
$class_storage = $this->classlike_storages[count($this->classlike_storages) - 1];
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
$storage = null;
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if (isset($class_storage->methods[$method_name_lc])) {
|
2018-06-30 21:29:37 +02:00
|
|
|
if (!$this->codebase->register_stub_files) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$duplicate_method_storage = $class_storage->methods[$method_name_lc];
|
2019-01-02 17:18:22 +01:00
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateMethod(
|
|
|
|
'Method ' . $function_id . ' has already been defined'
|
|
|
|
. ($duplicate_method_storage->location
|
|
|
|
? ' in ' . $duplicate_method_storage->location->file_path
|
|
|
|
: ''),
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->file_storage->has_visitor_issues = true;
|
|
|
|
|
|
|
|
$duplicate_method_storage->has_visitor_issues = true;
|
|
|
|
|
|
|
|
return false;
|
2018-06-30 21:29:37 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$storage = $class_storage->methods[$method_name_lc];
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
if (!$storage) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$storage = $class_storage->methods[$method_name_lc] = new MethodStorage();
|
2018-06-30 21:29:37 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
$storage->defining_fqcln = $fq_classlike_name;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$class_name_parts = explode('\\', $fq_classlike_name);
|
|
|
|
$class_name = array_pop($class_name_parts);
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($method_name_lc === strtolower($class_name) &&
|
2017-07-25 22:11:02 +02:00
|
|
|
!isset($class_storage->methods['__construct']) &&
|
|
|
|
strpos($fq_classlike_name, '\\') === false
|
|
|
|
) {
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->codebase->methods->setDeclaringMethodId(
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_classlike_name,
|
2020-02-15 04:24:39 +01:00
|
|
|
'__construct',
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_classlike_name,
|
|
|
|
$method_name_lc
|
2017-07-29 21:05:06 +02:00
|
|
|
);
|
2020-02-15 02:54:26 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->codebase->methods->setAppearingMethodId(
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_classlike_name,
|
2020-02-15 04:24:39 +01:00
|
|
|
'__construct',
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_classlike_name,
|
|
|
|
$method_name_lc
|
2017-07-29 21:05:06 +02:00
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$method_id = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$fq_classlike_name,
|
|
|
|
$method_name_lc
|
|
|
|
);
|
|
|
|
|
|
|
|
$class_storage->declaring_method_ids[$method_name_lc]
|
|
|
|
= $class_storage->appearing_method_ids[$method_name_lc]
|
|
|
|
= $method_id;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if (!$stmt->isPrivate() || $method_name_lc === '__construct' || $class_storage->is_trait) {
|
|
|
|
$class_storage->inheritable_method_ids[$method_name_lc] = $method_id;
|
2017-11-09 03:27:23 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if (!isset($class_storage->overridden_method_ids[$method_name_lc])) {
|
|
|
|
$class_storage->overridden_method_ids[$method_name_lc] = [];
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 05:37:36 +02:00
|
|
|
$storage->is_static = $stmt->isStatic();
|
|
|
|
$storage->abstract = $stmt->isAbstract();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-11-30 05:46:56 +01:00
|
|
|
$storage->final = $class_storage->final || $stmt->isFinal();
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($stmt->isPrivate()) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$storage->visibility = ClassLikeAnalyzer::VISIBILITY_PRIVATE;
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($stmt->isProtected()) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$storage->visibility = ClassLikeAnalyzer::VISIBILITY_PROTECTED;
|
2017-07-25 22:11:02 +02:00
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
$storage->visibility = ClassLikeAnalyzer::VISIBILITY_PUBLIC;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-04 20:48:02 +01:00
|
|
|
$function_id = $cased_function_id = strtolower($this->file_path)
|
2018-05-25 10:21:54 +02:00
|
|
|
. ':' . $stmt->getLine()
|
|
|
|
. ':' . (int) $stmt->getAttribute('startFilePos') . ':-:closure';
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage = $this->file_storage->functions[$function_id] = new FunctionLikeStorage();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
$this->functionlike_storages[] = $storage;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-06-29 21:28:45 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2018-04-17 18:16:25 +02:00
|
|
|
$storage->cased_name = $stmt->name->name;
|
2018-06-29 21:28:45 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
$storage->cased_name =
|
|
|
|
($this->aliases->namespace ? $this->aliases->namespace . '\\' : '') . $stmt->name->name;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 22:07:48 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod || $stmt instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
$storage->location = new CodeLocation($this->file_scanner, $stmt->name, null, true);
|
|
|
|
} else {
|
|
|
|
$storage->location = new CodeLocation($this->file_scanner, $stmt, null, true);
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-04-17 17:12:18 +02:00
|
|
|
$storage->stmt_location = new CodeLocation($this->file_scanner, $stmt);
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$required_param_count = 0;
|
|
|
|
$i = 0;
|
|
|
|
$has_optional_param = false;
|
|
|
|
|
2017-09-02 17:18:56 +02:00
|
|
|
$existing_params = [];
|
2018-06-30 21:29:37 +02:00
|
|
|
$storage->params = [];
|
2017-09-02 17:18:56 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
foreach ($stmt->getParams() as $param) {
|
2018-06-03 16:05:50 +02:00
|
|
|
if ($param->var instanceof PhpParser\Node\Expr\Error) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Param' . ($i + 1) . ' of ' . $cased_function_id . ' has invalid syntax',
|
|
|
|
new CodeLocation($this->file_scanner, $param, null, true)
|
|
|
|
);
|
2018-11-01 22:03:08 +01:00
|
|
|
|
2018-06-04 04:13:19 +02:00
|
|
|
++$i;
|
2018-06-03 16:05:50 +02:00
|
|
|
|
2018-06-04 04:13:19 +02:00
|
|
|
continue;
|
2018-06-03 16:05:50 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 18:54:08 +01:00
|
|
|
$param_array = $this->getTranslatedFunctionParam($param, $stmt, $fake_method, $fq_classlike_name);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-02-23 21:39:33 +01:00
|
|
|
if (isset($existing_params['$' . $param_array->name])) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new DuplicateParam(
|
|
|
|
'Duplicate param $' . $param_array->name . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($this->file_scanner, $param, null, true)
|
|
|
|
);
|
2018-06-03 16:05:50 +02:00
|
|
|
|
2018-11-01 22:03:08 +01:00
|
|
|
++$i;
|
|
|
|
|
|
|
|
continue;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-02-23 21:39:33 +01:00
|
|
|
$existing_params['$' . $param_array->name] = $i;
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage->param_types[$param_array->name] = $param_array->type;
|
|
|
|
$storage->params[] = $param_array;
|
|
|
|
|
2019-03-02 21:26:18 +01:00
|
|
|
if (!$param_array->is_optional && !$param_array->is_variadic) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$required_param_count = $i + 1;
|
|
|
|
|
2018-06-03 16:05:50 +02:00
|
|
|
if (!$param->variadic
|
|
|
|
&& $has_optional_param
|
|
|
|
) {
|
2019-06-07 15:25:03 +02:00
|
|
|
foreach ($storage->params as $param) {
|
|
|
|
$param->is_optional = false;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$has_optional_param = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
++$i;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->required_param_count = $required_param_count;
|
|
|
|
|
2019-12-27 22:51:49 +01:00
|
|
|
if (($stmt instanceof PhpParser\Node\Stmt\Function_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\ClassMethod)
|
|
|
|
&& $stmt->stmts
|
|
|
|
) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
|
|
|
|
&& $storage instanceof MethodStorage
|
|
|
|
&& $class_storage
|
|
|
|
&& !$class_storage->mutation_free
|
|
|
|
&& count($stmt->stmts) === 1
|
|
|
|
&& !count($stmt->params)
|
|
|
|
&& $stmt->stmts[0] instanceof PhpParser\Node\Stmt\Return_
|
|
|
|
&& $stmt->stmts[0]->expr instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
&& $stmt->stmts[0]->expr->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $stmt->stmts[0]->expr->var->name === 'this'
|
|
|
|
) {
|
|
|
|
$storage->mutation_free = true;
|
|
|
|
$storage->external_mutation_free = true;
|
|
|
|
$storage->mutation_free_inferred = true;
|
|
|
|
} elseif (strpos($stmt->name->name, 'assert') === 0) {
|
|
|
|
$var_assertions = [];
|
|
|
|
|
|
|
|
foreach ($stmt->stmts as $function_stmt) {
|
|
|
|
if ($function_stmt instanceof PhpParser\Node\Stmt\If_) {
|
|
|
|
$final_actions = \Psalm\Internal\Analyzer\ScopeAnalyzer::getFinalControlActions(
|
|
|
|
$function_stmt->stmts,
|
|
|
|
null,
|
|
|
|
$this->config->exit_functions,
|
2020-01-27 18:17:12 +01:00
|
|
|
[],
|
2019-12-27 22:51:49 +01:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($final_actions !== [\Psalm\Internal\Analyzer\ScopeAnalyzer::ACTION_END]) {
|
|
|
|
$var_assertions = [];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$if_clauses = \Psalm\Type\Algebra::getFormula(
|
|
|
|
\spl_object_id($function_stmt->cond),
|
|
|
|
$function_stmt->cond,
|
|
|
|
$this->fq_classlike_names
|
|
|
|
? $this->fq_classlike_names[count($this->fq_classlike_names) - 1]
|
|
|
|
: null,
|
|
|
|
$this->file_scanner,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$negated_formula = \Psalm\Type\Algebra::negateFormula($if_clauses);
|
|
|
|
|
|
|
|
$rules = \Psalm\Type\Algebra::getTruthsFromFormula($negated_formula);
|
|
|
|
|
|
|
|
if (!$rules) {
|
|
|
|
$var_assertions = [];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rules as $var_id => $rule) {
|
|
|
|
foreach ($rule as $rule_part) {
|
|
|
|
if (count($rule_part) > 1) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($existing_params[$var_id])) {
|
|
|
|
$param_offset = $existing_params[$var_id];
|
|
|
|
|
|
|
|
$var_assertions[] = new \Psalm\Storage\Assertion(
|
|
|
|
$param_offset,
|
|
|
|
$rule
|
|
|
|
);
|
|
|
|
} elseif (strpos($var_id, '$this->') === 0) {
|
|
|
|
$var_assertions[] = new \Psalm\Storage\Assertion(
|
|
|
|
$var_id,
|
|
|
|
$rule
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$var_assertions = [];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->assertions = $var_assertions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 14:22:28 +01:00
|
|
|
if (!$this->scan_deep
|
|
|
|
&& ($stmt instanceof PhpParser\Node\Stmt\Function_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\ClassMethod
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\Closure)
|
|
|
|
&& $stmt->stmts
|
|
|
|
) {
|
|
|
|
// pick up func_get_args that would otherwise be missed
|
|
|
|
foreach ($stmt->stmts as $function_stmt) {
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($function_stmt instanceof PhpParser\Node\Stmt\Expression
|
|
|
|
&& $function_stmt->expr instanceof PhpParser\Node\Expr\Assign
|
|
|
|
&& ($function_stmt->expr->expr instanceof PhpParser\Node\Expr\FuncCall)
|
|
|
|
&& ($function_stmt->expr->expr->name instanceof PhpParser\Node\Name)
|
2018-03-17 14:22:28 +01:00
|
|
|
) {
|
2018-04-17 18:16:25 +02:00
|
|
|
$function_id = implode('\\', $function_stmt->expr->expr->name->parts);
|
2018-03-17 14:22:28 +01:00
|
|
|
|
|
|
|
if ($function_id === 'func_get_arg'
|
|
|
|
|| $function_id === 'func_get_args'
|
|
|
|
|| $function_id === 'func_num_args'
|
|
|
|
) {
|
|
|
|
$storage->variadic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
$parser_return_type = $stmt->getReturnType();
|
|
|
|
|
|
|
|
if ($parser_return_type) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$suffix = '';
|
|
|
|
|
2019-03-08 05:19:17 +01:00
|
|
|
$original_type = $parser_return_type;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($parser_return_type instanceof PhpParser\Node\NullableType) {
|
|
|
|
$suffix = '|null';
|
|
|
|
$parser_return_type = $parser_return_type->type;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($parser_return_type instanceof PhpParser\Node\Identifier) {
|
|
|
|
$return_type_string = $parser_return_type->name . $suffix;
|
2019-11-27 06:00:46 +01:00
|
|
|
} elseif ($parser_return_type instanceof PhpParser\Node\UnionType) {
|
|
|
|
// for now unsupported
|
|
|
|
$return_type_string = 'mixed';
|
2017-07-25 22:11:02 +02:00
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
$return_type_fq_classlike_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2017-07-25 22:11:02 +02:00
|
|
|
$parser_return_type,
|
|
|
|
$this->aliases
|
|
|
|
);
|
|
|
|
|
|
|
|
$return_type_string = $return_type_fq_classlike_name . $suffix;
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
$storage->return_type = Type::parseString(
|
|
|
|
$return_type_string,
|
|
|
|
[$this->php_major_version, $this->php_minor_version]
|
|
|
|
);
|
2018-10-17 20:10:16 +02:00
|
|
|
$storage->return_type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage->return_type_location = new CodeLocation(
|
2018-01-21 18:44:46 +01:00
|
|
|
$this->file_scanner,
|
2019-03-08 05:19:17 +01:00
|
|
|
$original_type
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
|
|
|
|
2018-01-12 05:18:13 +01:00
|
|
|
if ($stmt->returnsByRef()) {
|
|
|
|
$storage->return_type->by_ref = true;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage->signature_return_type = $storage->return_type;
|
|
|
|
$storage->signature_return_type_location = $storage->return_type_location;
|
|
|
|
}
|
|
|
|
|
2018-01-13 07:25:13 +01:00
|
|
|
if ($stmt->returnsByRef()) {
|
|
|
|
$storage->returns_by_ref = true;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$doc_comment = $stmt->getDocComment();
|
|
|
|
|
|
|
|
if (!$doc_comment) {
|
2019-08-27 16:37:39 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
|
|
|
|
&& $stmt->name->name === '__construct'
|
|
|
|
&& $class_storage
|
2019-08-31 00:06:45 +02:00
|
|
|
&& $storage instanceof MethodStorage
|
2019-08-27 16:37:39 +02:00
|
|
|
&& $storage->params
|
|
|
|
&& $this->config->infer_property_types_from_constructor
|
|
|
|
) {
|
|
|
|
$this->inferPropertyTypeFromConstructor($stmt, $storage, $class_storage);
|
|
|
|
}
|
|
|
|
|
2018-04-22 04:13:10 +02:00
|
|
|
return $storage;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-06-01 17:53:32 +02:00
|
|
|
$docblock_info = CommentAnalyzer::extractFunctionDocblockInfo($doc_comment);
|
2017-11-15 03:43:31 +01:00
|
|
|
} catch (IncorrectDocblockException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new MissingDocblockType(
|
|
|
|
$e->getMessage() . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2017-11-28 06:46:41 +01:00
|
|
|
|
|
|
|
$docblock_info = null;
|
2017-07-25 22:11:02 +02:00
|
|
|
} catch (DocblockParseException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2018-11-01 22:03:08 +01:00
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$docblock_info = null;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$docblock_info) {
|
2018-04-22 04:13:10 +02:00
|
|
|
return $storage;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:36:35 +02:00
|
|
|
if ($docblock_info->mutation_free) {
|
|
|
|
$storage->mutation_free = true;
|
|
|
|
|
|
|
|
if ($storage instanceof MethodStorage) {
|
|
|
|
$storage->external_mutation_free = true;
|
2019-12-27 22:51:49 +01:00
|
|
|
$storage->mutation_free_inferred = false;
|
2019-08-30 18:36:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage instanceof MethodStorage && $docblock_info->external_mutation_free) {
|
|
|
|
$storage->external_mutation_free = true;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($docblock_info->deprecated) {
|
|
|
|
$storage->deprecated = true;
|
|
|
|
}
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
if ($docblock_info->internal) {
|
|
|
|
$storage->internal = true;
|
|
|
|
}
|
|
|
|
|
2019-05-11 21:39:53 +02:00
|
|
|
if ($docblock_info->psalm_internal) {
|
|
|
|
$storage->psalm_internal = $docblock_info->psalm_internal;
|
2019-05-11 16:15:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($docblock_info->variadic) {
|
|
|
|
$storage->variadic = true;
|
|
|
|
}
|
|
|
|
|
2019-07-18 07:31:48 +02:00
|
|
|
if ($docblock_info->pure) {
|
|
|
|
$storage->pure = true;
|
2019-08-30 18:36:35 +02:00
|
|
|
$storage->mutation_free = true;
|
|
|
|
if ($storage instanceof MethodStorage) {
|
|
|
|
$storage->external_mutation_free = true;
|
|
|
|
}
|
2019-07-18 07:31:48 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 22:25:27 +02:00
|
|
|
if ($docblock_info->remove_taint) {
|
|
|
|
$storage->remove_taint = true;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($docblock_info->ignore_nullable_return && $storage->return_type) {
|
|
|
|
$storage->return_type->ignore_nullable_issues = true;
|
|
|
|
}
|
|
|
|
|
2018-01-25 00:52:58 +01:00
|
|
|
if ($docblock_info->ignore_falsable_return && $storage->return_type) {
|
|
|
|
$storage->return_type->ignore_falsable_issues = true;
|
|
|
|
}
|
|
|
|
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues = $docblock_info->suppressed_issues;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-08-13 21:44:18 +02:00
|
|
|
foreach ($docblock_info->throws as [$throw, $offset, $line]) {
|
|
|
|
$throw_location = new CodeLocation\DocblockTypeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$offset,
|
|
|
|
$offset + \strlen($throw),
|
|
|
|
$line
|
2019-08-13 05:42:51 +02:00
|
|
|
);
|
2018-06-22 07:13:49 +02:00
|
|
|
|
2019-08-13 21:44:18 +02:00
|
|
|
foreach (\array_map('trim', explode('|', $throw)) as $throw_class) {
|
2020-02-13 19:01:53 +01:00
|
|
|
if ($throw_class !== 'self' && $throw_class !== 'static' && $throw_class !== 'parent') {
|
|
|
|
$exception_fqcln = Type::getFQCLNFromString(
|
|
|
|
$throw_class,
|
|
|
|
$this->aliases
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$exception_fqcln = $throw_class;
|
|
|
|
}
|
2018-11-25 22:14:50 +01:00
|
|
|
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning($exception_fqcln);
|
2019-08-13 21:44:18 +02:00
|
|
|
$this->file_storage->referenced_classlikes[strtolower($exception_fqcln)] = $exception_fqcln;
|
|
|
|
$storage->throws[$exception_fqcln] = true;
|
|
|
|
$storage->throw_locations[$exception_fqcln] = $throw_location;
|
|
|
|
}
|
2018-06-22 07:13:49 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!$this->config->use_docblock_types) {
|
2018-04-22 04:13:10 +02:00
|
|
|
return $storage;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 17:01:24 +01:00
|
|
|
if ($storage instanceof MethodStorage && $docblock_info->inheritdoc) {
|
|
|
|
$storage->inheritdoc = true;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$template_types = $class_storage && $class_storage->template_types ? $class_storage->template_types : null;
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
if ($docblock_info->templates) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage->template_types = [];
|
|
|
|
|
2019-05-06 22:38:08 +02:00
|
|
|
foreach ($docblock_info->templates as $i => $template_map) {
|
|
|
|
$template_name = $template_map[0];
|
|
|
|
|
|
|
|
if ($template_map[1] !== null && $template_map[2] !== null) {
|
2019-02-05 23:39:11 +01:00
|
|
|
if (trim($template_map[2])) {
|
2019-05-24 08:12:58 +02:00
|
|
|
try {
|
|
|
|
$template_type = Type::parseTokens(
|
|
|
|
Type::fixUpLocalType(
|
|
|
|
$template_map[2],
|
|
|
|
$this->aliases,
|
|
|
|
$storage->template_types + ($template_types ?: []),
|
|
|
|
$this->type_aliases
|
|
|
|
),
|
|
|
|
null,
|
|
|
|
$storage->template_types + ($template_types ?: [])
|
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Template ' . $template_name . ' has invalid as type - ' . $e->getMessage(),
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
2019-05-24 08:15:35 +02:00
|
|
|
$template_type = Type::getMixed();
|
2019-05-24 08:12:58 +02:00
|
|
|
}
|
2019-01-08 21:11:57 +01:00
|
|
|
} else {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Template ' . $template_name . ' missing as type',
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
2019-02-05 23:39:11 +01:00
|
|
|
$template_type = Type::getMixed();
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
} else {
|
2019-02-05 23:39:11 +01:00
|
|
|
$template_type = Type::getMixed();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2019-02-05 23:39:11 +01:00
|
|
|
|
2019-05-06 22:38:08 +02:00
|
|
|
if (isset($template_types[$template_name])) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Duplicate template param ' . $template_name . ' in docblock for '
|
|
|
|
. $cased_function_id,
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2019-03-01 06:06:35 +01:00
|
|
|
} else {
|
2019-05-06 22:38:08 +02:00
|
|
|
$storage->template_types[$template_name] = [
|
2019-12-29 00:37:55 +01:00
|
|
|
'fn-' . strtolower($cased_function_id) => [$template_type],
|
2019-03-01 06:06:35 +01:00
|
|
|
];
|
2019-03-01 05:43:55 +01:00
|
|
|
}
|
2019-11-01 18:25:07 +01:00
|
|
|
|
|
|
|
$storage->template_covariants[$i] = $template_map[3];
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$template_types = array_merge($template_types ?: [], $storage->template_types);
|
|
|
|
|
|
|
|
$this->function_template_types = $template_types;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
if ($docblock_info->assertions) {
|
|
|
|
$storage->assertions = [];
|
|
|
|
|
|
|
|
foreach ($docblock_info->assertions as $assertion) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$assertion_type_parts = $this->getAssertionParts(
|
|
|
|
$storage,
|
|
|
|
$assertion['type'],
|
|
|
|
$stmt
|
|
|
|
);
|
2019-06-05 06:46:55 +02:00
|
|
|
|
2019-07-04 21:05:55 +02:00
|
|
|
if (!$assertion_type_parts) {
|
2019-06-05 06:46:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
foreach ($storage->params as $i => $param) {
|
|
|
|
if ($param->name === $assertion['param_name']) {
|
|
|
|
$storage->assertions[] = new \Psalm\Storage\Assertion(
|
|
|
|
$i,
|
2019-07-04 21:05:55 +02:00
|
|
|
[$assertion_type_parts]
|
2018-05-28 21:07:42 +02:00
|
|
|
);
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->assertions[] = new \Psalm\Storage\Assertion(
|
2018-11-14 21:40:56 +01:00
|
|
|
'$' . $assertion['param_name'],
|
2019-07-04 21:05:55 +02:00
|
|
|
[$assertion_type_parts]
|
2018-05-28 21:07:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info->if_true_assertions) {
|
2019-07-04 21:05:55 +02:00
|
|
|
$storage->if_true_assertions = [];
|
2018-05-28 21:07:42 +02:00
|
|
|
|
|
|
|
foreach ($docblock_info->if_true_assertions as $assertion) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$assertion_type_parts = $this->getAssertionParts(
|
|
|
|
$storage,
|
|
|
|
$assertion['type'],
|
|
|
|
$stmt
|
|
|
|
);
|
2019-07-04 21:05:55 +02:00
|
|
|
|
|
|
|
if (!$assertion_type_parts) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
foreach ($storage->params as $i => $param) {
|
|
|
|
if ($param->name === $assertion['param_name']) {
|
|
|
|
$storage->if_true_assertions[] = new \Psalm\Storage\Assertion(
|
|
|
|
$i,
|
2019-07-04 21:05:55 +02:00
|
|
|
[$assertion_type_parts]
|
2018-05-28 21:07:42 +02:00
|
|
|
);
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->if_true_assertions[] = new \Psalm\Storage\Assertion(
|
2018-11-14 21:40:56 +01:00
|
|
|
'$' . $assertion['param_name'],
|
2019-07-04 21:05:55 +02:00
|
|
|
[$assertion_type_parts]
|
2018-05-28 21:07:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info->if_false_assertions) {
|
2019-07-04 21:05:55 +02:00
|
|
|
$storage->if_false_assertions = [];
|
2018-05-28 21:07:42 +02:00
|
|
|
|
|
|
|
foreach ($docblock_info->if_false_assertions as $assertion) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$assertion_type_parts = $this->getAssertionParts(
|
|
|
|
$storage,
|
|
|
|
$assertion['type'],
|
|
|
|
$stmt
|
|
|
|
);
|
2019-07-04 21:05:55 +02:00
|
|
|
|
|
|
|
if (!$assertion_type_parts) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
foreach ($storage->params as $i => $param) {
|
|
|
|
if ($param->name === $assertion['param_name']) {
|
|
|
|
$storage->if_false_assertions[] = new \Psalm\Storage\Assertion(
|
|
|
|
$i,
|
2019-07-04 21:05:55 +02:00
|
|
|
[$assertion_type_parts]
|
2018-05-28 21:07:42 +02:00
|
|
|
);
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->if_false_assertions[] = new \Psalm\Storage\Assertion(
|
2018-11-14 21:40:56 +01:00
|
|
|
'$' . $assertion['param_name'],
|
2019-07-04 21:05:55 +02:00
|
|
|
[$assertion_type_parts]
|
2018-05-28 21:07:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 15:31:21 +02:00
|
|
|
foreach ($docblock_info->globals as $global) {
|
|
|
|
try {
|
|
|
|
$storage->global_types[$global['name']] = Type::parseTokens(
|
|
|
|
Type::fixUpLocalType(
|
|
|
|
$global['type'],
|
2018-07-15 23:23:17 +02:00
|
|
|
$this->aliases,
|
|
|
|
null,
|
|
|
|
$this->type_aliases
|
2018-06-08 15:31:21 +02:00
|
|
|
),
|
2019-02-07 18:25:57 +01:00
|
|
|
null
|
2018-06-08 15:31:21 +02:00
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2018-11-01 22:03:08 +01:00
|
|
|
|
2018-06-08 15:31:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($docblock_info->params) {
|
|
|
|
$this->improveParamsFromDocblock(
|
|
|
|
$storage,
|
|
|
|
$docblock_info->params,
|
2019-03-19 02:05:37 +01:00
|
|
|
$stmt,
|
2019-06-21 05:38:10 +02:00
|
|
|
$fake_method,
|
2019-06-26 06:28:43 +02:00
|
|
|
$class_storage && !$class_storage->is_trait ? $class_storage->name : null
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
|
|
|
}
|
2018-04-22 04:13:10 +02:00
|
|
|
|
2019-03-25 16:25:43 +01:00
|
|
|
if ($storage instanceof MethodStorage) {
|
|
|
|
$storage->has_docblock_param_types = (bool) array_filter(
|
|
|
|
$storage->params,
|
|
|
|
/** @return bool */
|
|
|
|
function (FunctionLikeParameter $p) {
|
|
|
|
return $p->type !== null && $p->has_docblock_type;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:19:34 +01:00
|
|
|
$class_template_types = $this->class_template_types;
|
2019-12-14 15:40:09 +01:00
|
|
|
|
2019-01-19 19:32:43 +01:00
|
|
|
foreach ($docblock_info->params_out as $docblock_param_out) {
|
|
|
|
$param_name = substr($docblock_param_out['name'], 1);
|
|
|
|
|
2020-01-11 16:47:31 +01:00
|
|
|
try {
|
|
|
|
$out_type = Type::parseTokens(
|
|
|
|
Type::fixUpLocalType(
|
|
|
|
$docblock_param_out['type'],
|
|
|
|
$this->aliases,
|
|
|
|
$this->function_template_types + $class_template_types,
|
|
|
|
$this->type_aliases
|
|
|
|
),
|
|
|
|
null,
|
|
|
|
$this->function_template_types + $class_template_types
|
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2019-01-19 19:32:43 +01:00
|
|
|
|
2020-01-11 16:47:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out_type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage,
|
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($storage->params as $i => $param_storage) {
|
|
|
|
if ($param_storage->name === $param_name) {
|
2019-01-19 19:32:43 +01:00
|
|
|
$storage->param_out_types[$i] = $out_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 16:37:36 +02:00
|
|
|
foreach ($docblock_info->taint_sink_params as $taint_sink_param) {
|
|
|
|
$param_name = substr($taint_sink_param['name'], 1);
|
|
|
|
|
|
|
|
foreach ($storage->params as $param_storage) {
|
|
|
|
if ($param_storage->name === $param_name) {
|
2019-08-13 05:16:05 +02:00
|
|
|
$param_storage->sink = (int) Type\Union::TAINTED_INPUT;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($docblock_info->assert_untainted_params as $untainted_assert_param) {
|
|
|
|
$param_name = substr($untainted_assert_param['name'], 1);
|
|
|
|
|
|
|
|
foreach ($storage->params as $param_storage) {
|
|
|
|
if ($param_storage->name === $param_name) {
|
|
|
|
$param_storage->assert_untainted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 06:15:53 +01:00
|
|
|
if ($docblock_info->template_typeofs) {
|
|
|
|
foreach ($docblock_info->template_typeofs as $template_typeof) {
|
2019-05-13 02:49:37 +02:00
|
|
|
foreach ($storage->params as $param) {
|
2019-01-05 06:15:53 +01:00
|
|
|
if ($param->name === $template_typeof['param_name']) {
|
|
|
|
$param_type_nullable = $param->type && $param->type->isNullable();
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
$template_type = null;
|
|
|
|
$template_class = null;
|
2019-01-05 16:32:39 +01:00
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
if (isset($template_types[$template_typeof['template_type']])) {
|
|
|
|
foreach ($template_types[$template_typeof['template_type']] as $class => $map) {
|
|
|
|
$template_type = $map[0];
|
|
|
|
$template_class = $class;
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 20:29:04 +01:00
|
|
|
|
2019-01-20 04:45:58 +01:00
|
|
|
$template_atomic_type = null;
|
|
|
|
|
|
|
|
if ($template_type) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($template_type->getAtomicTypes() as $tat) {
|
2019-01-20 04:45:58 +01:00
|
|
|
if ($tat instanceof Type\Atomic\TNamedObject) {
|
|
|
|
$template_atomic_type = $tat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 06:15:53 +01:00
|
|
|
$param->type = new Type\Union([
|
2019-02-22 03:40:06 +01:00
|
|
|
new Type\Atomic\TTemplateParamClass(
|
2019-01-05 06:15:53 +01:00
|
|
|
$template_typeof['template_type'],
|
2019-01-05 16:32:39 +01:00
|
|
|
$template_type && !$template_type->isMixed()
|
|
|
|
? (string)$template_type
|
|
|
|
: 'object',
|
2019-01-20 04:45:58 +01:00
|
|
|
$template_atomic_type,
|
2019-12-29 00:37:55 +01:00
|
|
|
$template_class ?: 'fn-' . strtolower($cased_function_id)
|
2019-07-05 22:24:00 +02:00
|
|
|
),
|
2019-01-05 06:15:53 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($param_type_nullable) {
|
|
|
|
$param->type->addType(new Type\Atomic\TNull);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 17:08:08 +02:00
|
|
|
if ($docblock_info->return_type) {
|
|
|
|
$docblock_return_type = $docblock_info->return_type;
|
|
|
|
|
|
|
|
if (!$fake_method
|
|
|
|
&& $docblock_info->return_type_line_number
|
|
|
|
&& $docblock_info->return_type_start
|
|
|
|
&& $docblock_info->return_type_end
|
|
|
|
) {
|
|
|
|
$storage->return_type_location = new CodeLocation\DocblockTypeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$docblock_info->return_type_start,
|
|
|
|
$docblock_info->return_type_end,
|
|
|
|
$docblock_info->return_type_line_number
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$storage->return_type_location = new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$stmt,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
!$fake_method
|
|
|
|
? CodeLocation::FUNCTION_PHPDOC_RETURN_TYPE
|
|
|
|
: CodeLocation::FUNCTION_PHPDOC_METHOD,
|
|
|
|
$docblock_info->return_type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
try {
|
|
|
|
$fixed_type_tokens = Type::fixUpLocalType(
|
|
|
|
$docblock_return_type,
|
|
|
|
$this->aliases,
|
2019-12-14 15:40:09 +01:00
|
|
|
$this->function_template_types + $class_template_types,
|
2019-12-13 21:51:43 +01:00
|
|
|
$this->type_aliases,
|
|
|
|
$class_storage && !$class_storage->is_trait ? $class_storage->name : null
|
|
|
|
);
|
2019-06-08 17:08:08 +02:00
|
|
|
|
2020-04-04 15:31:12 +02:00
|
|
|
$param_type_mapping = [];
|
|
|
|
|
|
|
|
// This checks for param references in the return type tokens
|
|
|
|
// If found, the param is replaced with a generated template param
|
|
|
|
foreach ($fixed_type_tokens as $i => $type_token) {
|
|
|
|
if ($type_token[0][0] === '$') {
|
|
|
|
$token_body = $type_token[0];
|
|
|
|
|
|
|
|
foreach ($storage->params as $j => $param_storage) {
|
|
|
|
if ('$' . $param_storage->name === $token_body) {
|
|
|
|
if (!isset($param_type_mapping[$token_body])) {
|
|
|
|
$template_name = 'TGeneratedFromParam' . $j;
|
|
|
|
|
|
|
|
$storage->template_types[$template_name] = [
|
|
|
|
'fn-' . strtolower($cased_function_id) => [
|
|
|
|
$param_storage->type ? clone $param_storage->type : Type::getMixed()
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->function_template_types[$template_name]
|
|
|
|
= $storage->template_types[$template_name];
|
|
|
|
|
|
|
|
$param_type_mapping[$token_body] = $template_name;
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:04:00 +02:00
|
|
|
// spaces are allowed before $foo in get(string $foo) magic method
|
|
|
|
// definitions, but we want to remove them in this instance
|
|
|
|
if (isset($fixed_type_tokens[$i - 1])
|
|
|
|
&& $fixed_type_tokens[$i - 1][0][0] === ' '
|
|
|
|
) {
|
|
|
|
unset($fixed_type_tokens[$i - 1]);
|
|
|
|
}
|
|
|
|
|
2020-04-04 15:31:12 +02:00
|
|
|
$fixed_type_tokens[$i][0] = $param_type_mapping[$token_body];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
$storage->return_type = Type::parseTokens(
|
2020-04-04 17:16:26 +02:00
|
|
|
\array_values($fixed_type_tokens),
|
2019-12-13 21:51:43 +01:00
|
|
|
null,
|
2019-12-14 15:40:09 +01:00
|
|
|
$this->function_template_types + $class_template_types
|
2019-12-13 21:51:43 +01:00
|
|
|
);
|
2019-06-08 17:08:08 +02:00
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
$storage->return_type->setFromDocblock();
|
2019-06-08 17:08:08 +02:00
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
if ($storage->signature_return_type) {
|
|
|
|
$all_typehint_types_match = true;
|
2020-01-04 18:20:26 +01:00
|
|
|
$signature_return_atomic_types = $storage->signature_return_type->getAtomicTypes();
|
2019-06-08 17:08:08 +02:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($storage->return_type->getAtomicTypes() as $key => $type) {
|
2019-12-13 21:51:43 +01:00
|
|
|
if (isset($signature_return_atomic_types[$key])) {
|
|
|
|
$type->from_docblock = false;
|
|
|
|
} else {
|
|
|
|
$all_typehint_types_match = false;
|
2019-06-08 17:08:08 +02:00
|
|
|
}
|
2019-12-13 21:51:43 +01:00
|
|
|
}
|
2019-06-08 17:08:08 +02:00
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
if ($all_typehint_types_match) {
|
|
|
|
$storage->return_type->from_docblock = false;
|
2019-06-08 17:08:08 +02:00
|
|
|
}
|
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
if ($storage->signature_return_type->isNullable()
|
|
|
|
&& !$storage->return_type->isNullable()
|
|
|
|
) {
|
|
|
|
$storage->return_type->addType(new Type\Atomic\TNull());
|
2019-06-08 17:08:08 +02:00
|
|
|
}
|
2019-12-13 21:51:43 +01:00
|
|
|
}
|
2019-06-08 17:08:08 +02:00
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
$storage->return_type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2019-06-08 17:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage->return_type && $docblock_info->ignore_nullable_return) {
|
|
|
|
$storage->return_type->ignore_nullable_issues = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage->return_type && $docblock_info->ignore_falsable_return) {
|
|
|
|
$storage->return_type->ignore_falsable_issues = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->returnsByRef() && $storage->return_type) {
|
|
|
|
$storage->return_type->by_ref = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info->return_type_line_number && !$fake_method) {
|
|
|
|
$storage->return_type_location->setCommentLine($docblock_info->return_type_line_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->return_type_description = $docblock_info->return_type_description;
|
|
|
|
}
|
|
|
|
|
2019-08-27 16:37:39 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
|
|
|
|
&& $stmt->name->name === '__construct'
|
|
|
|
&& $class_storage
|
2019-08-31 00:06:45 +02:00
|
|
|
&& $storage instanceof MethodStorage
|
2019-08-27 16:37:39 +02:00
|
|
|
&& $storage->params
|
|
|
|
&& $this->config->infer_property_types_from_constructor
|
|
|
|
) {
|
|
|
|
$this->inferPropertyTypeFromConstructor($stmt, $storage, $class_storage);
|
|
|
|
}
|
|
|
|
|
2018-04-22 04:13:10 +02:00
|
|
|
return $storage;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 16:37:39 +02:00
|
|
|
private function inferPropertyTypeFromConstructor(
|
|
|
|
PhpParser\Node\Stmt\ClassMethod $stmt,
|
2019-08-31 00:06:45 +02:00
|
|
|
MethodStorage $storage,
|
2019-08-27 16:37:39 +02:00
|
|
|
ClassLikeStorage $class_storage
|
|
|
|
) : void {
|
|
|
|
if (!$stmt->stmts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$assigned_properties = [];
|
|
|
|
|
|
|
|
foreach ($stmt->stmts as $function_stmt) {
|
|
|
|
if ($function_stmt instanceof PhpParser\Node\Stmt\Expression
|
|
|
|
&& $function_stmt->expr instanceof PhpParser\Node\Expr\Assign
|
|
|
|
&& $function_stmt->expr->var instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
&& $function_stmt->expr->var->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $function_stmt->expr->var->var->name === 'this'
|
|
|
|
&& $function_stmt->expr->var->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& ($property_name = $function_stmt->expr->var->name->name)
|
|
|
|
&& isset($class_storage->properties[$property_name])
|
|
|
|
&& $function_stmt->expr->expr instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& is_string($function_stmt->expr->expr->name)
|
|
|
|
&& ($param_name = $function_stmt->expr->expr->name)
|
|
|
|
&& array_key_exists($param_name, $storage->param_types)
|
|
|
|
) {
|
|
|
|
if ($class_storage->properties[$property_name]->type
|
|
|
|
|| !isset($storage->param_types[$param_name])
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_index = \array_search($param_name, \array_keys($storage->param_types), true);
|
|
|
|
|
|
|
|
if ($param_index === false || !isset($storage->params[$param_index]->type)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_type = $storage->params[$param_index]->type;
|
|
|
|
|
|
|
|
$assigned_properties[$property_name] =
|
|
|
|
$storage->params[$param_index]->is_variadic
|
|
|
|
? new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
$param_type,
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
: $param_type;
|
|
|
|
} else {
|
|
|
|
$assigned_properties = [];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:06:45 +02:00
|
|
|
if (!$assigned_properties) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->external_mutation_free = true;
|
|
|
|
|
2019-08-27 16:37:39 +02:00
|
|
|
foreach ($assigned_properties as $property_name => $property_type) {
|
|
|
|
$class_storage->properties[$property_name]->type = clone $property_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 21:05:55 +02:00
|
|
|
/**
|
2019-12-31 15:13:18 +01:00
|
|
|
* @return ?list<string>
|
2019-07-04 21:05:55 +02:00
|
|
|
*/
|
|
|
|
private function getAssertionParts(
|
2020-03-29 00:54:55 +01:00
|
|
|
FunctionLikeStorage $storage,
|
2019-07-04 21:05:55 +02:00
|
|
|
string $assertion_type,
|
2019-07-07 15:39:21 +02:00
|
|
|
PhpParser\Node\FunctionLike $stmt
|
2019-07-04 21:05:55 +02:00
|
|
|
) : ?array {
|
|
|
|
$prefix = '';
|
2020-01-28 05:15:50 +01:00
|
|
|
|
2019-07-04 21:05:55 +02:00
|
|
|
if ($assertion_type[0] === '!') {
|
|
|
|
$prefix = '!';
|
|
|
|
$assertion_type = substr($assertion_type, 1);
|
|
|
|
}
|
2020-01-28 05:15:50 +01:00
|
|
|
|
2019-07-04 21:05:55 +02:00
|
|
|
if ($assertion_type[0] === '~') {
|
|
|
|
$prefix .= '~';
|
|
|
|
$assertion_type = substr($assertion_type, 1);
|
|
|
|
}
|
2020-01-28 05:15:50 +01:00
|
|
|
|
2019-07-04 21:05:55 +02:00
|
|
|
if ($assertion_type[0] === '=') {
|
|
|
|
$prefix .= '=';
|
|
|
|
$assertion_type = substr($assertion_type, 1);
|
|
|
|
}
|
|
|
|
|
2019-12-14 15:40:09 +01:00
|
|
|
$class_template_types = !$stmt instanceof PhpParser\Node\Stmt\ClassMethod || !$stmt->isStatic()
|
|
|
|
? $this->class_template_types
|
|
|
|
: [];
|
|
|
|
|
2019-12-31 15:10:14 +01:00
|
|
|
$namespaced_type = Type::parseTokens(
|
|
|
|
Type::fixUpLocalType(
|
|
|
|
$assertion_type,
|
|
|
|
$this->aliases,
|
|
|
|
$this->function_template_types + $class_template_types,
|
|
|
|
$this->type_aliases,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2020-01-28 05:15:50 +01:00
|
|
|
if ($prefix && count($namespaced_type->getAtomicTypes()) > 1) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
'Docblock assertions cannot contain | characters together with ' . $prefix,
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2020-02-29 15:57:28 +01:00
|
|
|
|
2020-01-28 05:15:50 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-12-31 15:10:14 +01:00
|
|
|
$namespaced_type->queueClassLikesForScanning(
|
|
|
|
$this->codebase,
|
|
|
|
$this->file_storage,
|
|
|
|
$this->function_template_types + $class_template_types
|
|
|
|
);
|
|
|
|
|
2019-12-31 15:13:18 +01:00
|
|
|
$assertion_type_parts = [];
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($namespaced_type->getAtomicTypes() as $namespaced_type_part) {
|
2019-12-31 15:10:14 +01:00
|
|
|
if ($namespaced_type_part instanceof Type\Atomic\TAssertionFalsy
|
|
|
|
|| ($namespaced_type_part instanceof Type\Atomic\TList
|
2020-01-17 15:06:29 +01:00
|
|
|
&& !$namespaced_type_part instanceof Type\Atomic\TNonEmptyList
|
2019-12-31 15:10:14 +01:00
|
|
|
&& $namespaced_type_part->type_param->isMixed())
|
|
|
|
|| ($namespaced_type_part instanceof Type\Atomic\TArray
|
|
|
|
&& $namespaced_type_part->type_params[0]->isArrayKey()
|
|
|
|
&& $namespaced_type_part->type_params[1]->isMixed())
|
|
|
|
|| ($namespaced_type_part instanceof Type\Atomic\TIterable
|
|
|
|
&& $namespaced_type_part->type_params[0]->isMixed()
|
|
|
|
&& $namespaced_type_part->type_params[1]->isMixed())
|
2019-07-09 13:38:37 +02:00
|
|
|
) {
|
2019-12-31 15:10:14 +01:00
|
|
|
$assertion_type_parts[] = $prefix . $namespaced_type_part->getAssertionString();
|
2019-07-04 21:05:55 +02:00
|
|
|
} else {
|
2019-12-31 15:10:14 +01:00
|
|
|
$assertion_type_parts[] = $prefix . $namespaced_type_part->getId();
|
2019-07-04 21:05:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $assertion_type_parts;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Param $param
|
|
|
|
*
|
|
|
|
* @return FunctionLikeParameter
|
|
|
|
*/
|
2019-03-19 02:05:37 +01:00
|
|
|
public function getTranslatedFunctionParam(
|
|
|
|
PhpParser\Node\Param $param,
|
|
|
|
PhpParser\Node\FunctionLike $stmt,
|
2019-11-30 18:54:08 +01:00
|
|
|
bool $fake_method,
|
|
|
|
?string $fq_classlike_name
|
2019-03-19 02:05:37 +01:00
|
|
|
) : FunctionLikeParameter {
|
2017-07-25 22:11:02 +02:00
|
|
|
$param_type = null;
|
|
|
|
|
|
|
|
$is_nullable = $param->default !== null &&
|
|
|
|
$param->default instanceof PhpParser\Node\Expr\ConstFetch &&
|
|
|
|
strtolower($param->default->name->parts[0]) === 'null';
|
|
|
|
|
|
|
|
$param_typehint = $param->type;
|
|
|
|
|
|
|
|
if ($param_typehint instanceof PhpParser\Node\NullableType) {
|
|
|
|
$is_nullable = true;
|
|
|
|
$param_typehint = $param_typehint->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_typehint) {
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($param_typehint instanceof PhpParser\Node\Identifier) {
|
|
|
|
$param_type_string = $param_typehint->name;
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($param_typehint instanceof PhpParser\Node\Name\FullyQualified) {
|
|
|
|
$param_type_string = (string)$param_typehint;
|
2019-05-03 17:55:27 +02:00
|
|
|
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning($param_type_string);
|
2019-05-03 17:55:27 +02:00
|
|
|
$this->file_storage->referenced_classlikes[strtolower($param_type_string)] = $param_type_string;
|
2019-11-27 06:00:46 +01:00
|
|
|
} elseif ($param_typehint instanceof PhpParser\Node\UnionType) {
|
|
|
|
// not yet supported
|
|
|
|
$param_type_string = 'mixed';
|
2017-07-25 22:11:02 +02:00
|
|
|
} else {
|
2019-03-17 16:31:04 +01:00
|
|
|
if ($this->classlike_storages
|
|
|
|
&& strtolower($param_typehint->parts[0]) === 'self'
|
|
|
|
&& !end($this->classlike_storages)->is_trait
|
|
|
|
) {
|
|
|
|
$param_type_string = $this->fq_classlike_names[count($this->fq_classlike_names) - 1];
|
|
|
|
} else {
|
|
|
|
$param_type_string = ClassLikeAnalyzer::getFQCLNFromNameObject($param_typehint, $this->aliases);
|
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
|
2017-11-15 03:56:29 +01:00
|
|
|
if (!in_array(strtolower($param_type_string), ['self', 'static', 'parent'], true)) {
|
2020-04-02 23:17:55 +02:00
|
|
|
$this->codebase->scanner->queueClassLikeForScanning($param_type_string);
|
2018-02-23 04:22:31 +01:00
|
|
|
$this->file_storage->referenced_classlikes[strtolower($param_type_string)] = $param_type_string;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_type_string) {
|
2019-02-07 18:25:57 +01:00
|
|
|
$param_type = Type::parseString(
|
|
|
|
$param_type_string,
|
|
|
|
[$this->php_major_version, $this->php_minor_version],
|
|
|
|
[]
|
|
|
|
);
|
2018-08-28 18:37:25 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($is_nullable) {
|
2018-08-28 18:37:25 +02:00
|
|
|
$param_type->addType(new Type\Atomic\TNull);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2019-04-09 19:58:49 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$is_optional = $param->default !== null;
|
|
|
|
|
2018-06-03 16:05:50 +02:00
|
|
|
if ($param->var instanceof PhpParser\Node\Expr\Error || !is_string($param->var->name)) {
|
2018-04-17 18:16:25 +02:00
|
|
|
throw new \UnexpectedValueException('Not expecting param name to be non-string');
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
return new FunctionLikeParameter(
|
2018-04-17 18:16:25 +02:00
|
|
|
$param->var->name,
|
2017-07-25 22:11:02 +02:00
|
|
|
$param->byRef,
|
2017-09-02 17:18:56 +02:00
|
|
|
$param_type,
|
2019-03-19 02:05:37 +01:00
|
|
|
new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$fake_method ? $stmt : $param,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
!$fake_method
|
|
|
|
? CodeLocation::FUNCTION_PARAM_VAR
|
|
|
|
: CodeLocation::FUNCTION_PHPDOC_METHOD
|
|
|
|
),
|
2017-12-30 03:28:21 +01:00
|
|
|
$param_typehint
|
2019-03-19 02:05:37 +01:00
|
|
|
? new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$fake_method ? $stmt : $param,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
CodeLocation::FUNCTION_PARAM_TYPE
|
|
|
|
)
|
2017-12-30 03:28:21 +01:00
|
|
|
: null,
|
2017-07-25 22:11:02 +02:00
|
|
|
$is_optional,
|
|
|
|
$is_nullable,
|
2018-06-07 18:23:10 +02:00
|
|
|
$param->variadic,
|
2019-11-25 17:44:54 +01:00
|
|
|
$param->default
|
|
|
|
? StatementsAnalyzer::getSimpleType(
|
|
|
|
$this->codebase,
|
|
|
|
new \Psalm\Internal\Provider\NodeDataProvider(),
|
|
|
|
$param->default,
|
2019-11-30 18:54:08 +01:00
|
|
|
$this->aliases,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$fq_classlike_name
|
2019-11-25 17:44:54 +01:00
|
|
|
)
|
|
|
|
: null
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FunctionLikeStorage $storage
|
2019-06-01 22:57:33 +02:00
|
|
|
* @param array<int, array{type:string,name:string,line_number:int,start:int,end:int}> $docblock_params
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param PhpParser\Node\FunctionLike $function
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-21 19:38:51 +01:00
|
|
|
private function improveParamsFromDocblock(
|
2017-07-25 22:11:02 +02:00
|
|
|
FunctionLikeStorage $storage,
|
|
|
|
array $docblock_params,
|
2019-03-19 02:05:37 +01:00
|
|
|
PhpParser\Node\FunctionLike $function,
|
2019-06-21 05:38:10 +02:00
|
|
|
bool $fake_method,
|
|
|
|
?string $fq_classlike_name
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
2017-08-14 21:46:01 +02:00
|
|
|
$base = $this->fq_classlike_names
|
|
|
|
? $this->fq_classlike_names[count($this->fq_classlike_names) - 1] . '::'
|
|
|
|
: '';
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
$cased_method_id = $base . $storage->cased_name;
|
|
|
|
|
2019-02-23 17:02:04 +01:00
|
|
|
$unused_docblock_params = [];
|
|
|
|
|
2019-12-14 15:40:09 +01:00
|
|
|
$class_template_types = !$function instanceof PhpParser\Node\Stmt\ClassMethod || !$function->isStatic()
|
|
|
|
? $this->class_template_types
|
|
|
|
: [];
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
foreach ($docblock_params as $docblock_param) {
|
|
|
|
$param_name = $docblock_param['name'];
|
|
|
|
$docblock_param_variadic = false;
|
|
|
|
|
|
|
|
if (substr($param_name, 0, 3) === '...') {
|
|
|
|
$docblock_param_variadic = true;
|
|
|
|
$param_name = substr($param_name, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_name = substr($param_name, 1);
|
|
|
|
|
|
|
|
$storage_param = null;
|
|
|
|
|
|
|
|
foreach ($storage->params as $function_signature_param) {
|
|
|
|
if ($function_signature_param->name === $param_name) {
|
|
|
|
$storage_param = $function_signature_param;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 02:05:37 +01:00
|
|
|
if (!$fake_method) {
|
2019-06-01 22:57:33 +02:00
|
|
|
$docblock_type_location = new CodeLocation\DocblockTypeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$docblock_param['start'],
|
|
|
|
$docblock_param['end'],
|
|
|
|
$docblock_param['line_number']
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$docblock_type_location = new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$function,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
CodeLocation::FUNCTION_PHPDOC_METHOD,
|
|
|
|
null
|
|
|
|
);
|
2019-03-19 02:05:37 +01:00
|
|
|
}
|
2019-02-23 17:02:04 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($storage_param === null) {
|
2019-02-27 20:17:11 +01:00
|
|
|
$param_location = new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$function,
|
|
|
|
null,
|
|
|
|
true,
|
2019-02-27 22:00:44 +01:00
|
|
|
CodeLocation::FUNCTION_PARAM_VAR
|
2019-02-27 20:17:11 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$param_location->setCommentLine($docblock_param['line_number']);
|
|
|
|
$unused_docblock_params[$param_name] = $param_location;
|
2019-02-23 17:02:04 +01:00
|
|
|
|
2018-12-01 01:21:14 +01:00
|
|
|
if (!$docblock_param_variadic || $storage->params || $this->scan_deep) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage_param = new FunctionLikeParameter(
|
|
|
|
$param_name,
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$storage->params[] = $storage_param;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 07:04:35 +02:00
|
|
|
try {
|
2018-05-20 23:19:53 +02:00
|
|
|
$new_param_type = Type::parseTokens(
|
2018-02-04 18:23:32 +01:00
|
|
|
Type::fixUpLocalType(
|
2017-10-22 07:04:35 +02:00
|
|
|
$docblock_param['type'],
|
|
|
|
$this->aliases,
|
2019-12-14 15:40:09 +01:00
|
|
|
$this->function_template_types + $class_template_types,
|
2019-06-21 05:38:10 +02:00
|
|
|
$this->type_aliases,
|
|
|
|
$fq_classlike_name
|
2018-04-20 16:52:23 +02:00
|
|
|
),
|
2019-02-07 18:25:57 +01:00
|
|
|
null,
|
2019-12-14 15:40:09 +01:00
|
|
|
$this->function_template_types + $class_template_types
|
2017-10-22 07:04:35 +02:00
|
|
|
);
|
|
|
|
} catch (TypeParseTreeException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . $cased_method_id,
|
|
|
|
$docblock_type_location
|
|
|
|
);
|
2018-11-01 22:03:08 +01:00
|
|
|
|
2017-10-22 07:04:35 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-01-19 18:46:39 +01:00
|
|
|
$storage_param->has_docblock_type = true;
|
2018-04-14 19:41:25 +02:00
|
|
|
$new_param_type->setFromDocblock();
|
|
|
|
|
2017-07-27 22:12:16 +02:00
|
|
|
$new_param_type->queueClassLikesForScanning(
|
2018-01-21 19:38:51 +01:00
|
|
|
$this->codebase,
|
2018-02-19 06:27:39 +01:00
|
|
|
$this->file_storage,
|
2017-07-27 22:12:16 +02:00
|
|
|
$storage->template_types ?: []
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-06-08 17:08:08 +02:00
|
|
|
if ($storage->template_types) {
|
|
|
|
foreach ($storage->template_types as $t => $type_map) {
|
|
|
|
foreach ($type_map as $obj => [$type]) {
|
|
|
|
if ($type->isMixed() && $docblock_param['type'] === 'class-string<' . $t . '>') {
|
|
|
|
$storage->template_types[$t][$obj] = [Type::getObject()];
|
|
|
|
|
|
|
|
if (isset($this->function_template_types[$t])) {
|
|
|
|
$this->function_template_types[$t][$obj] = $storage->template_types[$t][$obj];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 19:58:49 +02:00
|
|
|
if (!$docblock_param_variadic && $storage_param->is_variadic && $new_param_type->hasArray()) {
|
2019-10-01 21:44:43 +02:00
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-11 02:16:43 +02:00
|
|
|
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
|
2019-10-01 21:44:43 +02:00
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
$array_type = $new_param_type->getAtomicTypes()['array'];
|
2019-04-09 19:58:49 +02:00
|
|
|
|
|
|
|
if ($array_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$new_param_type = $array_type->getGenericValueType();
|
2019-10-11 02:16:43 +02:00
|
|
|
} elseif ($array_type instanceof Type\Atomic\TList) {
|
|
|
|
$new_param_type = $array_type->type_param;
|
2019-04-09 19:58:49 +02:00
|
|
|
} else {
|
|
|
|
$new_param_type = $array_type->type_params[1];
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$existing_param_type_nullable = $storage_param->is_nullable;
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if (!$storage_param->type || $storage_param->type->hasMixed() || $storage->template_types) {
|
2019-12-14 03:40:09 +01:00
|
|
|
if ($existing_param_type_nullable
|
|
|
|
&& !$new_param_type->isNullable()
|
|
|
|
&& !$new_param_type->hasTemplate()
|
|
|
|
) {
|
2018-01-09 21:05:48 +01:00
|
|
|
$new_param_type->addType(new Type\Atomic\TNull());
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 18:23:10 +02:00
|
|
|
if ($this->config->add_param_default_to_docblock_type
|
|
|
|
&& $storage_param->default_type
|
2018-12-08 19:18:55 +01:00
|
|
|
&& !$storage_param->default_type->hasMixed()
|
|
|
|
&& (!$storage_param->type || !$storage_param->type->hasMixed())
|
2018-06-07 18:23:10 +02:00
|
|
|
) {
|
|
|
|
$new_param_type = Type::combineUnionTypes($new_param_type, $storage_param->default_type);
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage_param->type = $new_param_type;
|
2019-03-19 02:05:37 +01:00
|
|
|
$storage_param->type_location = $docblock_type_location;
|
2017-07-25 22:11:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
$storage_param_atomic_types = $storage_param->type->getAtomicTypes();
|
2018-02-01 05:27:25 +01:00
|
|
|
|
2018-02-05 22:57:33 +01:00
|
|
|
$all_typehint_types_match = true;
|
2018-02-01 05:27:25 +01:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($new_param_type->getAtomicTypes() as $key => $type) {
|
2018-02-01 05:27:25 +01:00
|
|
|
if (isset($storage_param_atomic_types[$key])) {
|
|
|
|
$type->from_docblock = false;
|
2019-01-05 06:15:53 +01:00
|
|
|
|
|
|
|
if ($storage_param_atomic_types[$key] instanceof Type\Atomic\TArray
|
|
|
|
&& $type instanceof Type\Atomic\TArray
|
|
|
|
&& $type->type_params[0]->hasArrayKey()
|
|
|
|
) {
|
|
|
|
$type->type_params[0]->from_docblock = false;
|
|
|
|
}
|
2018-02-01 05:27:25 +01:00
|
|
|
} else {
|
2018-02-05 22:57:33 +01:00
|
|
|
$all_typehint_types_match = false;
|
2018-02-01 05:27:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:57:33 +01:00
|
|
|
if ($all_typehint_types_match) {
|
|
|
|
$new_param_type->from_docblock = false;
|
|
|
|
}
|
|
|
|
|
2018-01-05 06:19:35 +01:00
|
|
|
if ($existing_param_type_nullable && !$new_param_type->isNullable()) {
|
2018-01-09 21:05:48 +01:00
|
|
|
$new_param_type->addType(new Type\Atomic\TNull());
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 06:19:35 +01:00
|
|
|
$storage_param->type = $new_param_type;
|
2019-03-19 02:05:37 +01:00
|
|
|
$storage_param->type_location = $docblock_type_location;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2019-02-23 17:02:04 +01:00
|
|
|
|
2019-02-23 17:39:00 +01:00
|
|
|
$params_without_docblock_type = array_filter(
|
2019-02-23 17:02:04 +01:00
|
|
|
$storage->params,
|
|
|
|
function (FunctionLikeParameter $p) : bool {
|
2019-02-23 17:39:00 +01:00
|
|
|
return !$p->has_docblock_type && (!$p->type || $p->type->hasArray());
|
2019-02-23 17:02:04 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-02-23 17:39:00 +01:00
|
|
|
if ($params_without_docblock_type) {
|
2019-02-27 22:00:44 +01:00
|
|
|
$storage->unused_docblock_params = $unused_docblock_params;
|
2019-02-23 17:02:04 +01:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Property $stmt
|
|
|
|
* @param Config $config
|
2018-05-10 20:12:50 +02:00
|
|
|
* @param string $fq_classlike_name
|
2017-07-25 22:11:02 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function visitPropertyDeclaration(
|
|
|
|
PhpParser\Node\Stmt\Property $stmt,
|
2018-01-21 18:44:46 +01:00
|
|
|
Config $config,
|
2018-05-10 20:12:50 +02:00
|
|
|
ClassLikeStorage $storage,
|
|
|
|
$fq_classlike_name
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
2017-08-14 21:46:01 +02:00
|
|
|
if (!$this->fq_classlike_names) {
|
|
|
|
throw new \LogicException('$this->fq_classlike_names should not be empty');
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
$var_comment = null;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2017-12-27 12:27:59 +01:00
|
|
|
$property_is_initialized = false;
|
|
|
|
|
2018-01-21 18:44:46 +01:00
|
|
|
$existing_constants = $storage->protected_class_constants
|
|
|
|
+ $storage->private_class_constants
|
|
|
|
+ $storage->public_class_constants;
|
|
|
|
|
2018-01-13 06:32:20 +01:00
|
|
|
if ($comment && $comment->getText() && ($config->use_docblock_types || $config->use_docblock_property_types)) {
|
2017-12-27 12:27:59 +01:00
|
|
|
if (preg_match('/[ \t\*]+@psalm-suppress[ \t]+PropertyNotSetInConstructor/', (string)$comment)) {
|
|
|
|
$property_is_initialized = true;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_comments = CommentAnalyzer::getTypeFromComment(
|
2019-06-01 17:53:32 +02:00
|
|
|
$comment,
|
2018-01-21 18:44:46 +01:00
|
|
|
$this->file_scanner,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->aliases,
|
2019-12-14 15:40:09 +01:00
|
|
|
$this->function_template_types + (!$stmt->isStatic() ? $this->class_template_types : []),
|
2018-07-15 23:23:17 +02:00
|
|
|
$this->type_aliases
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2018-02-08 05:33:31 +01:00
|
|
|
|
|
|
|
$var_comment = array_pop($var_comments);
|
2017-11-15 03:43:31 +01:00
|
|
|
} catch (IncorrectDocblockException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new MissingDocblockType(
|
|
|
|
$e->getMessage(),
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
} catch (DocblockParseException $e) {
|
2020-03-29 00:54:55 +01:00
|
|
|
$storage->docblock_issues[] = new InvalidDocblock(
|
|
|
|
$e->getMessage(),
|
|
|
|
new CodeLocation($this->file_scanner, $stmt, null, true)
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
$signature_type = null;
|
|
|
|
$signature_type_location = null;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-06-14 22:53:40 +02:00
|
|
|
if ($stmt->type) {
|
2019-01-11 23:21:50 +01:00
|
|
|
$suffix = '';
|
|
|
|
|
2019-06-14 22:53:40 +02:00
|
|
|
$parser_property_type = $stmt->type;
|
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
if ($parser_property_type instanceof PhpParser\Node\NullableType) {
|
|
|
|
$suffix = '|null';
|
|
|
|
$parser_property_type = $parser_property_type->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parser_property_type instanceof PhpParser\Node\Identifier) {
|
|
|
|
$property_type_string = $parser_property_type->name . $suffix;
|
2019-11-27 06:00:46 +01:00
|
|
|
} elseif ($parser_property_type instanceof PhpParser\Node\UnionType) {
|
|
|
|
// not yet supported
|
|
|
|
$property_type_string = 'mixed';
|
2019-01-11 23:21:50 +01:00
|
|
|
} else {
|
|
|
|
$property_type_fq_classlike_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
|
|
|
$parser_property_type,
|
|
|
|
$this->aliases
|
|
|
|
);
|
|
|
|
|
|
|
|
$property_type_string = $property_type_fq_classlike_name . $suffix;
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
$signature_type = Type::parseString(
|
|
|
|
$property_type_string,
|
|
|
|
[$this->php_major_version, $this->php_minor_version]
|
|
|
|
);
|
2019-01-11 23:21:50 +01:00
|
|
|
$signature_type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
|
|
|
|
|
|
|
$signature_type_location = new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$parser_property_type,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
CodeLocation::FUNCTION_RETURN_TYPE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$doc_var_group_type = $var_comment ? $var_comment->type : null;
|
|
|
|
|
|
|
|
if ($doc_var_group_type) {
|
|
|
|
$doc_var_group_type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
|
|
|
$doc_var_group_type->setFromDocblock();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->props as $property) {
|
2019-01-11 23:21:50 +01:00
|
|
|
$doc_var_location = null;
|
|
|
|
|
|
|
|
$property_storage = $storage->properties[$property->name->name] = new PropertyStorage();
|
2019-07-19 05:37:36 +02:00
|
|
|
$property_storage->is_static = $stmt->isStatic();
|
2019-01-11 23:21:50 +01:00
|
|
|
$property_storage->type = $signature_type;
|
|
|
|
$property_storage->signature_type = $signature_type;
|
|
|
|
$property_storage->signature_type_location = $signature_type_location;
|
|
|
|
$property_storage->type_location = $signature_type_location;
|
|
|
|
$property_storage->location = new CodeLocation($this->file_scanner, $property->name);
|
2019-04-17 17:12:18 +02:00
|
|
|
$property_storage->stmt_location = new CodeLocation($this->file_scanner, $stmt);
|
2019-01-11 23:21:50 +01:00
|
|
|
$property_storage->has_default = $property->default ? true : false;
|
|
|
|
$property_storage->deprecated = $var_comment ? $var_comment->deprecated : false;
|
|
|
|
$property_storage->internal = $var_comment ? $var_comment->internal : false;
|
2019-05-11 21:39:53 +02:00
|
|
|
$property_storage->psalm_internal = $var_comment ? $var_comment->psalm_internal : null;
|
2019-08-11 22:01:37 +02:00
|
|
|
$property_storage->readonly = $var_comment ? $var_comment->readonly : false;
|
2020-02-02 21:35:12 +01:00
|
|
|
$property_storage->allow_private_mutation = $var_comment ? $var_comment->allow_private_mutation : false;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
if (!$signature_type && !$doc_var_group_type) {
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($property->default) {
|
2019-01-11 23:21:50 +01:00
|
|
|
$property_storage->suggested_type = StatementsAnalyzer::getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
$this->codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
new \Psalm\Internal\Provider\NodeDataProvider(),
|
2018-05-10 20:12:50 +02:00
|
|
|
$property->default,
|
2018-06-28 03:53:25 +02:00
|
|
|
$this->aliases,
|
|
|
|
null,
|
2018-05-10 20:12:50 +02:00
|
|
|
$existing_constants,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2018-01-13 06:32:20 +01:00
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
$property_storage->type = null;
|
2017-07-25 22:11:02 +02:00
|
|
|
} else {
|
2019-06-01 18:25:57 +02:00
|
|
|
if ($var_comment
|
|
|
|
&& $var_comment->type_start
|
|
|
|
&& $var_comment->type_end
|
|
|
|
&& $var_comment->line_number
|
|
|
|
) {
|
|
|
|
$doc_var_location = new CodeLocation\DocblockTypeLocation(
|
2018-01-21 18:44:46 +01:00
|
|
|
$this->file_scanner,
|
2019-06-01 18:25:57 +02:00
|
|
|
$var_comment->type_start,
|
|
|
|
$var_comment->type_end,
|
|
|
|
$var_comment->line_number
|
2018-01-02 02:04:03 +01:00
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
if ($doc_var_group_type) {
|
|
|
|
$property_storage->type = count($stmt->props) === 1
|
|
|
|
? $doc_var_group_type
|
|
|
|
: clone $doc_var_group_type;
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
if ($property_storage->type
|
|
|
|
&& $property_storage->type !== $property_storage->signature_type
|
|
|
|
) {
|
|
|
|
if (!$property_storage->signature_type) {
|
|
|
|
$property_storage->type_location = $doc_var_location;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property_storage->signature_type) {
|
|
|
|
$all_typehint_types_match = true;
|
2020-01-04 18:20:26 +01:00
|
|
|
$signature_atomic_types = $property_storage->signature_type->getAtomicTypes();
|
2019-01-11 23:21:50 +01:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($property_storage->type->getAtomicTypes() as $key => $type) {
|
2019-01-11 23:21:50 +01:00
|
|
|
if (isset($signature_atomic_types[$key])) {
|
|
|
|
$type->from_docblock = false;
|
|
|
|
} else {
|
|
|
|
$all_typehint_types_match = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($all_typehint_types_match) {
|
|
|
|
$property_storage->type->from_docblock = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property_storage->signature_type->isNullable()
|
|
|
|
&& !$property_storage->type->isNullable()
|
|
|
|
) {
|
|
|
|
$property_storage->type->addType(new Type\Atomic\TNull());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_storage->type->queueClassLikesForScanning($this->codebase, $this->file_storage);
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
if ($stmt->isPublic()) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$property_storage->visibility = ClassLikeAnalyzer::VISIBILITY_PUBLIC;
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($stmt->isProtected()) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$property_storage->visibility = ClassLikeAnalyzer::VISIBILITY_PROTECTED;
|
2017-07-25 22:11:02 +02:00
|
|
|
} elseif ($stmt->isPrivate()) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$property_storage->visibility = ClassLikeAnalyzer::VISIBILITY_PRIVATE;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
$fq_classlike_name = $this->fq_classlike_names[count($this->fq_classlike_names) - 1];
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
$property_id = $fq_classlike_name . '::$' . $property->name->name;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-07-22 01:56:26 +02:00
|
|
|
$storage->declaring_property_ids[$property->name->name] = $fq_classlike_name;
|
2018-04-17 18:16:25 +02:00
|
|
|
$storage->appearing_property_ids[$property->name->name] = $property_id;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-12-27 12:27:59 +01:00
|
|
|
if ($property_is_initialized) {
|
2018-04-17 18:16:25 +02:00
|
|
|
$storage->initialized_properties[$property->name->name] = true;
|
2017-12-27 12:27:59 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!$stmt->isPrivate()) {
|
2018-04-17 18:16:25 +02:00
|
|
|
$storage->inheritable_property_ids[$property->name->name] = $property_id;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\ClassConst $stmt
|
2018-05-10 20:12:50 +02:00
|
|
|
* @param string $fq_classlike_name
|
2017-07-25 22:11:02 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-05-10 20:12:50 +02:00
|
|
|
private function visitClassConstDeclaration(
|
|
|
|
PhpParser\Node\Stmt\ClassConst $stmt,
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
$fq_classlike_name
|
|
|
|
) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$existing_constants = $storage->protected_class_constants
|
|
|
|
+ $storage->private_class_constants
|
|
|
|
+ $storage->public_class_constants;
|
|
|
|
|
2018-08-10 19:25:25 +02:00
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
$deprecated = false;
|
|
|
|
$config = $this->config;
|
|
|
|
|
|
|
|
if ($comment && $comment->getText() && ($config->use_docblock_types || $config->use_docblock_property_types)) {
|
2019-06-02 00:44:59 +02:00
|
|
|
$comments = DocComment::parsePreservingLength($comment);
|
2018-08-10 19:25:25 +02:00
|
|
|
|
|
|
|
if (isset($comments['specials']['deprecated'])) {
|
|
|
|
$deprecated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
foreach ($stmt->consts as $const) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$const_type = StatementsAnalyzer::getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
$this->codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
new \Psalm\Internal\Provider\NodeDataProvider(),
|
2017-07-25 22:11:02 +02:00
|
|
|
$const->value,
|
2018-06-28 03:53:25 +02:00
|
|
|
$this->aliases,
|
|
|
|
null,
|
2018-05-10 20:12:50 +02:00
|
|
|
$existing_constants,
|
|
|
|
$fq_classlike_name
|
2018-06-28 03:53:25 +02:00
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if ($const_type) {
|
|
|
|
$existing_constants[$const->name->name] = $const_type;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if ($stmt->isProtected()) {
|
|
|
|
$storage->protected_class_constants[$const->name->name] = $const_type;
|
|
|
|
} elseif ($stmt->isPrivate()) {
|
|
|
|
$storage->private_class_constants[$const->name->name] = $const_type;
|
|
|
|
} else {
|
|
|
|
$storage->public_class_constants[$const->name->name] = $const_type;
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$storage->class_constant_locations[$const->name->name] = new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$const->name
|
|
|
|
);
|
2019-06-04 17:14:49 +02:00
|
|
|
|
|
|
|
$storage->class_constant_stmt_locations[$const->name->name] = new CodeLocation(
|
|
|
|
$this->file_scanner,
|
|
|
|
$const
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
} else {
|
2019-09-14 20:26:31 +02:00
|
|
|
$unresolved_const_expr = self::getUnresolvedClassConstExpr(
|
|
|
|
$const->value,
|
|
|
|
$this->aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if ($stmt->isProtected()) {
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($unresolved_const_expr) {
|
|
|
|
$storage->protected_class_constant_nodes[$const->name->name] = $unresolved_const_expr;
|
|
|
|
} else {
|
|
|
|
$storage->protected_class_constants[$const->name->name] = Type::getMixed();
|
|
|
|
}
|
2018-06-28 03:53:25 +02:00
|
|
|
} elseif ($stmt->isPrivate()) {
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($unresolved_const_expr) {
|
|
|
|
$storage->private_class_constant_nodes[$const->name->name] = $unresolved_const_expr;
|
|
|
|
} else {
|
|
|
|
$storage->private_class_constants[$const->name->name] = Type::getMixed();
|
|
|
|
}
|
2018-06-28 03:53:25 +02:00
|
|
|
} else {
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($unresolved_const_expr) {
|
|
|
|
$storage->public_class_constant_nodes[$const->name->name] = $unresolved_const_expr;
|
|
|
|
} else {
|
|
|
|
$storage->public_class_constants[$const->name->name] = Type::getMixed();
|
|
|
|
}
|
2018-06-28 03:53:25 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2018-08-10 19:25:25 +02:00
|
|
|
|
|
|
|
if ($deprecated) {
|
|
|
|
$storage->deprecated_constants[$const->name->name] = true;
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 20:26:31 +02:00
|
|
|
public function getUnresolvedClassConstExpr(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Aliases $aliases,
|
|
|
|
string $fq_classlike_name
|
|
|
|
) : ?UnresolvedConstantComponent {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
|
|
|
|
$left = self::getUnresolvedClassConstExpr(
|
|
|
|
$stmt->left,
|
|
|
|
$aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$right = self::getUnresolvedClassConstExpr(
|
|
|
|
$stmt->right,
|
|
|
|
$aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$left || !$right) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
|
|
|
|
return new UnresolvedConstant\UnresolvedAdditionOp($left, $right);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus) {
|
|
|
|
return new UnresolvedConstant\UnresolvedSubtractionOp($left, $right);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul) {
|
|
|
|
return new UnresolvedConstant\UnresolvedMultiplicationOp($left, $right);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div) {
|
|
|
|
return new UnresolvedConstant\UnresolvedDivisionOp($left, $right);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
|
|
|
return new UnresolvedConstant\UnresolvedConcatOp($left, $right);
|
|
|
|
}
|
2020-03-09 05:54:26 +01:00
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
|
|
|
|
return new UnresolvedConstant\UnresolvedBitwiseOr($left, $right);
|
|
|
|
}
|
2019-09-14 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 17:29:41 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Ternary) {
|
|
|
|
$cond = self::getUnresolvedClassConstExpr(
|
|
|
|
$stmt->cond,
|
|
|
|
$aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$if = null;
|
|
|
|
|
|
|
|
if ($stmt->if) {
|
|
|
|
$if = self::getUnresolvedClassConstExpr(
|
|
|
|
$stmt->if,
|
|
|
|
$aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($if === null) {
|
|
|
|
$if = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$else = self::getUnresolvedClassConstExpr(
|
|
|
|
$stmt->else,
|
|
|
|
$aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($cond && $else && $if !== false) {
|
|
|
|
return new UnresolvedConstant\UnresolvedTernary($cond, $if, $else);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
|
|
|
if (strtolower($stmt->name->parts[0]) === 'false') {
|
|
|
|
return new UnresolvedConstant\ScalarValue(false);
|
|
|
|
} elseif (strtolower($stmt->name->parts[0]) === 'true') {
|
|
|
|
return new UnresolvedConstant\ScalarValue(true);
|
|
|
|
} elseif (strtolower($stmt->name->parts[0]) === 'null') {
|
|
|
|
return new UnresolvedConstant\ScalarValue(null);
|
|
|
|
} elseif ($stmt->name->parts[0] === '__NAMESPACE__') {
|
|
|
|
return new UnresolvedConstant\ScalarValue($aliases->namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new UnresolvedConstant\Constant(
|
|
|
|
implode('\\', $stmt->name->parts),
|
|
|
|
$stmt->name instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Namespace_) {
|
|
|
|
return new UnresolvedConstant\ScalarValue($aliases->namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name
|
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& $fq_classlike_name
|
|
|
|
&& $stmt->class->parts !== ['static']
|
|
|
|
&& $stmt->class->parts !== ['parent']
|
|
|
|
) {
|
|
|
|
if ($stmt->class->parts === ['self']) {
|
|
|
|
$const_fq_class_name = $fq_classlike_name;
|
|
|
|
} else {
|
|
|
|
$const_fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
|
|
|
$stmt->class,
|
|
|
|
$aliases
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new UnresolvedConstant\ClassConstant($const_fq_class_name, $stmt->name->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|| $stmt instanceof PhpParser\Node\Scalar\DNumber
|
|
|
|
) {
|
|
|
|
return new UnresolvedConstant\ScalarValue($stmt->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Array_) {
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach ($stmt->items as $item) {
|
|
|
|
if ($item === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item->key) {
|
|
|
|
$item_key_type = self::getUnresolvedClassConstExpr(
|
|
|
|
$item->key,
|
|
|
|
$aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$item_key_type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$item_key_type = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item_value_type = self::getUnresolvedClassConstExpr(
|
|
|
|
$item->value,
|
|
|
|
$aliases,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$item_value_type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$items[] = new UnresolvedConstant\KeyValuePair($item_key_type, $item_value_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new UnresolvedConstant\ArrayValue($items);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\Include_ $stmt
|
|
|
|
*
|
2018-03-07 17:16:56 +01:00
|
|
|
* @return void
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
|
|
|
public function visitInclude(PhpParser\Node\Expr\Include_ $stmt)
|
|
|
|
{
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
if (!$config->allow_includes) {
|
|
|
|
throw new FileIncludeException(
|
|
|
|
'File includes are not allowed per your Psalm config - check the allowFileIncludes flag.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->expr instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$path_to_file = $stmt->expr->value;
|
|
|
|
|
|
|
|
// attempts to resolve using get_include_path dirs
|
2018-11-06 03:57:36 +01:00
|
|
|
$include_path = IncludeAnalyzer::resolveIncludePath($path_to_file, dirname($this->file_path));
|
2017-07-25 22:11:02 +02:00
|
|
|
$path_to_file = $include_path ? $include_path : $path_to_file;
|
|
|
|
|
2018-11-18 23:06:11 +01:00
|
|
|
if (DIRECTORY_SEPARATOR === '/') {
|
|
|
|
$is_path_relative = $path_to_file[0] !== DIRECTORY_SEPARATOR;
|
|
|
|
} else {
|
|
|
|
$is_path_relative = !preg_match('~^[A-Z]:\\\\~i', $path_to_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_path_relative) {
|
2018-11-18 22:53:46 +01:00
|
|
|
$path_to_file = $config->base_dir . DIRECTORY_SEPARATOR . $path_to_file;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$path_to_file = IncludeAnalyzer::getPathTo(
|
|
|
|
$stmt->expr,
|
|
|
|
null,
|
|
|
|
$this->file_path,
|
|
|
|
$this->config
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($path_to_file) {
|
2018-11-18 23:18:05 +01:00
|
|
|
$path_to_file = IncludeAnalyzer::normalizeFilePath($path_to_file);
|
2018-10-01 19:50:55 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($this->file_path === $path_to_file) {
|
2018-03-07 17:16:56 +01:00
|
|
|
return;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
if ($this->codebase->fileExists($path_to_file)) {
|
2018-05-23 05:38:27 +02:00
|
|
|
if ($this->scan_deep) {
|
|
|
|
$this->codebase->scanner->addFileToDeepScan($path_to_file);
|
|
|
|
} else {
|
|
|
|
$this->codebase->scanner->addFileToShallowScan($path_to_file);
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-05-30 18:23:53 +02:00
|
|
|
$this->file_storage->required_file_paths[strtolower($path_to_file)] = $path_to_file;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-03-07 17:16:56 +01:00
|
|
|
return;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFilePath()
|
|
|
|
{
|
|
|
|
return $this->file_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileName()
|
|
|
|
{
|
|
|
|
return $this->file_scanner->getFileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-05-30 22:19:18 +02:00
|
|
|
public function getRootFilePath()
|
2018-02-23 21:39:33 +01:00
|
|
|
{
|
2018-05-30 22:19:18 +02:00
|
|
|
return $this->file_scanner->getRootFilePath();
|
2018-02-23 21:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-05-30 22:19:18 +02:00
|
|
|
public function getRootFileName()
|
2018-02-23 21:39:33 +01:00
|
|
|
{
|
2018-05-30 22:19:18 +02:00
|
|
|
return $this->file_scanner->getRootFileName();
|
2018-02-23 21:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Aliases
|
|
|
|
*/
|
|
|
|
public function getAliases()
|
|
|
|
{
|
|
|
|
return $this->aliases;
|
|
|
|
}
|
2018-09-24 19:08:23 +02:00
|
|
|
|
|
|
|
public function afterTraverse(array $nodes)
|
|
|
|
{
|
|
|
|
$this->file_storage->type_aliases = $this->type_aliases;
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|