2016-01-20 00:27:06 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
2019-02-22 00:19:12 +01:00
|
|
|
use Psalm\Aliases;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\DoAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\ForAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\IfAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\SwitchAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\TryAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\WhileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\PropertyAssignmentAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\BinaryOpAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ReturnAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ThrowAnalyzer;
|
2020-03-15 04:54:42 +01:00
|
|
|
use Psalm\Internal\PhpVisitor\CheckTrivialExprVisitor;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Codebase;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\DocComment;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2018-11-12 16:46:55 +01:00
|
|
|
use Psalm\FileManipulation;
|
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2016-10-17 22:42:23 +02:00
|
|
|
use Psalm\Issue\ContinueOutsideLoop;
|
2018-09-18 22:42:51 +02:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
2018-09-18 23:08:32 +02:00
|
|
|
use Psalm\Issue\ForbiddenEcho;
|
2020-02-24 20:38:05 +01:00
|
|
|
use Psalm\Issue\ImpureFunctionCall;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-11-05 22:53:30 +01:00
|
|
|
use Psalm\Issue\InvalidGlobal;
|
2017-02-12 22:49:32 +01:00
|
|
|
use Psalm\Issue\UnevaluatedCode;
|
2016-11-06 01:53:39 +01:00
|
|
|
use Psalm\Issue\UnrecognizedStatement;
|
2018-01-25 07:04:26 +01:00
|
|
|
use Psalm\Issue\UnusedVariable;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Type;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
|
|
|
use function fwrite;
|
|
|
|
use const STDERR;
|
|
|
|
use function array_filter;
|
|
|
|
use function array_map;
|
|
|
|
use function preg_split;
|
|
|
|
use function is_string;
|
|
|
|
use function get_class;
|
|
|
|
use function in_array;
|
|
|
|
use function strrpos;
|
|
|
|
use function strlen;
|
|
|
|
use function substr;
|
|
|
|
use function array_key_exists;
|
|
|
|
use function count;
|
|
|
|
use function array_shift;
|
|
|
|
use function explode;
|
|
|
|
use function array_pop;
|
|
|
|
use function implode;
|
|
|
|
use function array_change_key_case;
|
2019-07-24 22:48:54 +02:00
|
|
|
use function token_get_all;
|
|
|
|
use function array_slice;
|
|
|
|
use function array_reverse;
|
|
|
|
use function is_array;
|
|
|
|
use function trim;
|
|
|
|
use function is_null;
|
|
|
|
use function array_column;
|
|
|
|
use function array_combine;
|
2019-08-06 00:33:33 +02:00
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @var SourceAnalyzer
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $source;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @var FileAnalyzer
|
2017-07-29 21:05:06 +02:00
|
|
|
*/
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $file_analyzer;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
/**
|
|
|
|
* @var Codebase
|
|
|
|
*/
|
|
|
|
protected $codebase;
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
2017-02-08 00:09:12 +01:00
|
|
|
* @var array<string, CodeLocation>
|
2016-11-01 05:39:41 +01:00
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $all_vars = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
2018-01-21 22:24:20 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, int>
|
|
|
|
*/
|
|
|
|
private $var_branch_points = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Possibly undefined variables should be initialised if we're altering code
|
|
|
|
*
|
|
|
|
* @var array<string, int>|null
|
|
|
|
*/
|
|
|
|
private $vars_to_initialize;
|
|
|
|
|
2017-08-12 01:05:04 +02:00
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @var array<string, FunctionAnalyzer>
|
2017-08-12 01:05:04 +02:00
|
|
|
*/
|
2018-11-11 18:01:14 +01:00
|
|
|
private $function_analyzers = [];
|
2017-08-12 01:05:04 +02:00
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, array{0: string, 1: CodeLocation}>
|
|
|
|
*/
|
|
|
|
private $unused_var_locations = [];
|
|
|
|
|
2018-06-17 02:01:33 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
|
|
|
private $used_var_locations = [];
|
|
|
|
|
2019-03-10 21:36:35 +01:00
|
|
|
/**
|
|
|
|
* @var ?array<string, bool>
|
|
|
|
*/
|
|
|
|
private $byref_uses;
|
|
|
|
|
2019-06-26 22:26:14 +02:00
|
|
|
/**
|
|
|
|
* @var array{description:string, specials:array<string, array<int, string>>}|null
|
|
|
|
*/
|
|
|
|
private $parsed_docblock = null;
|
|
|
|
|
2019-07-24 22:48:54 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, CodeLocation>
|
|
|
|
*/
|
|
|
|
private $removed_unref_vars = [];
|
|
|
|
|
2019-08-18 17:18:02 +02:00
|
|
|
/**
|
|
|
|
* @var ?string
|
|
|
|
*/
|
|
|
|
private $fake_this_class = null;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
/** @var \Psalm\Internal\Provider\NodeDataProvider */
|
|
|
|
public $node_data;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @param SourceAnalyzer $source
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2019-11-25 17:44:54 +01:00
|
|
|
public function __construct(SourceAnalyzer $source, \Psalm\Internal\Provider\NodeDataProvider $node_data)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->source = $source;
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->file_analyzer = $source->getFileAnalyzer();
|
2018-11-06 03:57:36 +01:00
|
|
|
$this->codebase = $source->getCodebase();
|
2019-11-25 17:44:54 +01:00
|
|
|
$this->node_data = $node_data;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
/**
|
|
|
|
* Checks an array of statements for validity
|
|
|
|
*
|
2018-04-17 18:16:25 +02:00
|
|
|
* @param array<PhpParser\Node\Stmt> $stmts
|
2016-12-12 19:50:46 +01:00
|
|
|
* @param Context $context
|
|
|
|
* @param Context|null $global_context
|
2018-01-21 22:24:20 +01:00
|
|
|
* @param bool $root_scope
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-06-18 20:45:55 +02:00
|
|
|
* @return null|false
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public function analyze(
|
2017-01-07 20:35:07 +01:00
|
|
|
array $stmts,
|
|
|
|
Context $context,
|
2018-01-21 22:24:20 +01:00
|
|
|
Context $global_context = null,
|
|
|
|
$root_scope = false
|
2017-01-07 20:35:07 +01:00
|
|
|
) {
|
2016-08-15 06:58:30 +02:00
|
|
|
// hoist functions to the top
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
2019-06-28 16:48:30 +02:00
|
|
|
try {
|
|
|
|
$function_analyzer = new FunctionAnalyzer($stmt, $this->source);
|
|
|
|
$this->function_analyzers[strtolower($stmt->name->name)] = $function_analyzer;
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2016-08-15 06:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer = $this->getFileAnalyzer()->project_analyzer;
|
|
|
|
$codebase = $project_analyzer->getCodebase();
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2018-06-01 15:07:22 +02:00
|
|
|
if ($codebase->config->hoist_constants) {
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Const_) {
|
|
|
|
foreach ($stmt->consts as $const) {
|
|
|
|
$this->setConstType(
|
|
|
|
$const->name->name,
|
2019-11-25 17:44:54 +01:00
|
|
|
self::getSimpleType($codebase, $this->node_data, $const->value, $this->getAliases(), $this)
|
2018-06-28 03:53:25 +02:00
|
|
|
?: Type::getMixed(),
|
2018-06-01 15:07:22 +02:00
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Expression
|
|
|
|
&& $stmt->expr instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
&& $stmt->expr->name instanceof PhpParser\Node\Name
|
|
|
|
&& $stmt->expr->name->parts === ['define']
|
|
|
|
&& isset($stmt->expr->args[1])
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$const_name = static::getConstName(
|
|
|
|
$stmt->expr->args[0]->value,
|
|
|
|
$this->node_data,
|
|
|
|
$codebase,
|
|
|
|
$this->getAliases()
|
|
|
|
);
|
|
|
|
|
2019-02-18 19:17:08 +01:00
|
|
|
if ($const_name !== null) {
|
|
|
|
$this->setConstType(
|
|
|
|
$const_name,
|
2019-11-25 17:44:54 +01:00
|
|
|
self::getSimpleType(
|
|
|
|
$codebase,
|
|
|
|
$this->node_data,
|
|
|
|
$stmt->expr->args[1]->value,
|
|
|
|
$this->getAliases(),
|
|
|
|
$this
|
|
|
|
) ?: Type::getMixed(),
|
2019-02-18 19:17:08 +01:00
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2018-06-01 15:07:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
$original_context = null;
|
|
|
|
|
2018-06-17 03:54:44 +02:00
|
|
|
if ($context->loop_scope) {
|
2018-09-21 01:19:00 +02:00
|
|
|
$original_context = clone $context->loop_scope->loop_parent_context;
|
2017-12-03 00:28:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
$plugin_classes = $codebase->config->after_statement_checks;
|
2016-12-06 22:41:42 +01:00
|
|
|
|
2017-11-07 20:46:53 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
2019-04-20 23:49:49 +02:00
|
|
|
$ignore_variable_property = false;
|
|
|
|
$ignore_variable_method = false;
|
|
|
|
|
2020-03-15 19:31:41 +01:00
|
|
|
if ($context->has_returned
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& !($stmt instanceof PhpParser\Node\Stmt\Nop)
|
|
|
|
&& !($stmt instanceof PhpParser\Node\Stmt\InlineHTML)
|
2017-01-07 20:35:07 +01:00
|
|
|
) {
|
2017-02-27 05:09:18 +01:00
|
|
|
if ($context->collect_references) {
|
2017-02-12 22:49:32 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnevaluatedCode(
|
|
|
|
'Expressions after return/throw/continue',
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 16:57:56 +01:00
|
|
|
break;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if ($project_analyzer->debug_lines) {
|
2019-05-30 16:30:41 +02:00
|
|
|
fwrite(STDERR, $this->getFilePath() . ':' . $stmt->getLine() . "\n");
|
2018-03-26 15:08:55 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 01:09:31 +01:00
|
|
|
/*
|
2018-05-03 19:56:30 +02:00
|
|
|
if (isset($context->vars_in_scope['$array']) && !$stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
var_dump($stmt->getLine(), $context->vars_in_scope['$array']);
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
2017-01-26 01:09:31 +01:00
|
|
|
*/
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
$new_issues = null;
|
|
|
|
|
|
|
|
if ($docblock = $stmt->getDocComment()) {
|
2018-12-02 00:37:49 +01:00
|
|
|
try {
|
2019-06-26 22:26:14 +02:00
|
|
|
$this->parsed_docblock = DocComment::parsePreservingLength($docblock);
|
2018-12-02 00:37:49 +01:00
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->getSource(), $stmt, null, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
2019-06-26 22:26:14 +02:00
|
|
|
$this->parsed_docblock = null;
|
2018-12-02 00:37:49 +01:00
|
|
|
}
|
2019-04-20 23:49:49 +02:00
|
|
|
|
2019-06-26 22:26:14 +02:00
|
|
|
$comments = $this->parsed_docblock;
|
|
|
|
|
2019-08-18 17:18:02 +02:00
|
|
|
if (isset($comments['specials']['psalm-scope-this'])) {
|
2019-08-18 17:24:14 +02:00
|
|
|
$trimmed = trim(\reset($comments['specials']['psalm-scope-this']));
|
2019-08-26 06:24:56 +02:00
|
|
|
|
|
|
|
if (!$codebase->classExists($trimmed)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\UndefinedDocblockClass(
|
|
|
|
'Scope class ' . $trimmed . ' does not exist',
|
|
|
|
new CodeLocation($this->getSource(), $stmt, null, true),
|
|
|
|
$trimmed
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this_type = Type::parseString($trimmed);
|
|
|
|
$context->self = $trimmed;
|
|
|
|
$context->vars_in_scope['$this'] = $this_type;
|
|
|
|
$this->setFQCLN($trimmed);
|
|
|
|
}
|
2019-08-18 17:18:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
if (isset($comments['specials']['psalm-suppress'])) {
|
|
|
|
$suppressed = array_filter(
|
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @param string $line
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function ($line) {
|
2019-06-03 21:02:28 +02:00
|
|
|
return preg_split('/[\s]+/', $line)[0];
|
2017-10-27 00:19:19 +02:00
|
|
|
},
|
|
|
|
$comments['specials']['psalm-suppress']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($suppressed) {
|
2019-08-18 20:27:50 +02:00
|
|
|
$new_issues = [];
|
|
|
|
|
|
|
|
foreach ($suppressed as $offset => $issue_type) {
|
2019-08-18 20:44:45 +02:00
|
|
|
$offset += $docblock->getFilePos();
|
|
|
|
$new_issues[$offset] = $issue_type;
|
2019-08-18 20:56:47 +02:00
|
|
|
|
|
|
|
if ($issue_type === 'InaccessibleMethod') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-18 21:34:32 +02:00
|
|
|
if ($codebase->track_unused_suppressions) {
|
|
|
|
IssueBuffer::addUnusedSuppression($this->getFilePath(), $offset, $issue_type);
|
|
|
|
}
|
2019-08-18 20:27:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
$this->addSuppressedIssues($new_issues);
|
|
|
|
}
|
|
|
|
}
|
2019-04-20 23:49:49 +02:00
|
|
|
|
|
|
|
if (isset($comments['specials']['psalm-ignore-variable-method'])) {
|
|
|
|
$context->ignore_variable_method = $ignore_variable_method = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['psalm-ignore-variable-property'])) {
|
|
|
|
$context->ignore_variable_property = $ignore_variable_property = true;
|
|
|
|
}
|
2019-06-26 22:26:14 +02:00
|
|
|
} else {
|
|
|
|
$this->parsed_docblock = null;
|
2017-10-27 00:19:19 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (IfAnalyzer::analyze($this, $stmt, $context) === false) {
|
2018-03-03 19:20:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (TryAnalyzer::analyze($this, $stmt, $context) === false) {
|
2018-03-03 19:20:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ForAnalyzer::analyze($this, $stmt, $context) === false) {
|
2018-03-03 19:20:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ForeachAnalyzer::analyze($this, $stmt, $context) === false) {
|
2018-03-03 19:20:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\While_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (WhileAnalyzer::analyze($this, $stmt, $context) === false) {
|
2018-03-03 19:20:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
DoAnalyzer::analyze($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Const_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$this->analyzeConstAssignment($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Unset_) {
|
2018-12-20 22:03:21 +01:00
|
|
|
$this->analyzeUnset($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Return_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ReturnAnalyzer::analyze($this, $stmt, $context);
|
2020-03-15 19:31:41 +01:00
|
|
|
$context->has_returned = true;
|
2020-03-15 19:43:05 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Throw_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ThrowAnalyzer::analyze($this, $stmt, $context);
|
2020-03-15 19:43:05 +01:00
|
|
|
$context->has_returned = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
2018-11-06 03:57:36 +01:00
|
|
|
SwitchAnalyzer::analyze($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Break_) {
|
2018-06-17 03:54:44 +02:00
|
|
|
$loop_scope = $context->loop_scope;
|
2020-01-27 18:17:12 +01:00
|
|
|
|
|
|
|
$leaving_switch = true;
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
if ($loop_scope && $original_context) {
|
2020-01-27 18:17:12 +01:00
|
|
|
if ($context->break_types
|
|
|
|
&& \end($context->break_types) === 'switch'
|
|
|
|
&& (!$stmt->num
|
|
|
|
|| !$stmt->num instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|| $stmt->num->value < 2
|
|
|
|
)
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$loop_scope->final_actions[] = ScopeAnalyzer::ACTION_LEAVE_SWITCH;
|
2018-11-10 22:26:03 +01:00
|
|
|
} else {
|
2020-01-27 18:17:12 +01:00
|
|
|
$leaving_switch = false;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$loop_scope->final_actions[] = ScopeAnalyzer::ACTION_BREAK;
|
2018-11-10 22:26:03 +01:00
|
|
|
}
|
2017-12-03 00:28:18 +01:00
|
|
|
|
|
|
|
$redefined_vars = $context->getRedefinedVars($loop_scope->loop_parent_context->vars_in_scope);
|
|
|
|
|
|
|
|
if ($loop_scope->possibly_redefined_loop_parent_vars === null) {
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars = $redefined_vars;
|
|
|
|
} else {
|
|
|
|
foreach ($redefined_vars as $var => $type) {
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($type->hasMixed()) {
|
2017-12-03 00:28:18 +01:00
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var] = $type;
|
|
|
|
} elseif (isset($loop_scope->possibly_redefined_loop_parent_vars[$var])) {
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var] = Type::combineUnionTypes(
|
|
|
|
$type,
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 02:01:33 +02:00
|
|
|
|
2018-11-10 20:06:31 +01:00
|
|
|
if ($loop_scope->iteration_count === 0) {
|
|
|
|
foreach ($context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (!isset($loop_scope->loop_parent_context->vars_in_scope[$var_id])) {
|
|
|
|
if (isset($loop_scope->possibly_defined_loop_parent_vars[$var_id])) {
|
|
|
|
$loop_scope->possibly_defined_loop_parent_vars[$var_id] = Type::combineUnionTypes(
|
|
|
|
$type,
|
|
|
|
$loop_scope->possibly_defined_loop_parent_vars[$var_id]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$loop_scope->possibly_defined_loop_parent_vars[$var_id] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 18:17:12 +01:00
|
|
|
if ($context->collect_references && !$leaving_switch) {
|
2018-06-17 02:01:33 +02:00
|
|
|
foreach ($context->unreferenced_vars as $var_id => $locations) {
|
|
|
|
if (isset($loop_scope->unreferenced_vars[$var_id])) {
|
|
|
|
$loop_scope->unreferenced_vars[$var_id] += $locations;
|
|
|
|
} else {
|
|
|
|
$loop_scope->unreferenced_vars[$var_id] = $locations;
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 16:33:32 +01:00
|
|
|
|
2020-01-03 16:56:47 +01:00
|
|
|
$loop_scope->referenced_var_ids += $context->referenced_var_ids;
|
2018-06-17 02:01:33 +02:00
|
|
|
}
|
2017-12-03 00:28:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-18 17:39:14 +01:00
|
|
|
$case_scope = $context->case_scope;
|
2020-01-27 18:17:12 +01:00
|
|
|
if ($case_scope && $leaving_switch) {
|
2018-11-11 02:34:53 +01:00
|
|
|
foreach ($context->vars_in_scope as $var_id => $type) {
|
2018-11-18 17:39:14 +01:00
|
|
|
if ($case_scope->parent_context !== $context) {
|
|
|
|
if ($case_scope->break_vars === null) {
|
|
|
|
$case_scope->break_vars = [];
|
2018-11-11 02:34:53 +01:00
|
|
|
}
|
|
|
|
|
2019-05-13 05:13:27 +02:00
|
|
|
if (isset($case_scope->break_vars[$var_id])) {
|
|
|
|
$case_scope->break_vars[$var_id] = Type::combineUnionTypes(
|
|
|
|
$type,
|
|
|
|
$case_scope->break_vars[$var_id]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$case_scope->break_vars[$var_id] = $type;
|
2018-11-11 02:34:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($context->collect_references) {
|
|
|
|
foreach ($context->unreferenced_vars as $var_id => $locations) {
|
2018-11-18 17:39:14 +01:00
|
|
|
if (isset($case_scope->unreferenced_vars[$var_id])) {
|
|
|
|
$case_scope->unreferenced_vars[$var_id] += $locations;
|
2018-11-11 02:34:53 +01:00
|
|
|
} else {
|
2018-11-18 17:39:14 +01:00
|
|
|
$case_scope->unreferenced_vars[$var_id] = $locations;
|
2018-11-11 02:34:53 +01:00
|
|
|
}
|
2018-06-17 03:54:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 19:31:41 +01:00
|
|
|
$context->has_returned = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Continue_) {
|
2018-06-17 03:54:44 +02:00
|
|
|
$loop_scope = $context->loop_scope;
|
2020-01-27 18:17:12 +01:00
|
|
|
|
|
|
|
$leaving_switch = true;
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
if ($loop_scope === null) {
|
2020-01-27 18:17:12 +01:00
|
|
|
if (!$context->break_types) {
|
2018-01-24 06:01:08 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ContinueOutsideLoop(
|
|
|
|
'Continue call outside loop context',
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-17 22:42:23 +02:00
|
|
|
}
|
2017-12-03 00:28:18 +01:00
|
|
|
} elseif ($original_context) {
|
2020-01-27 18:17:12 +01:00
|
|
|
if ($context->break_types
|
|
|
|
&& \end($context->break_types) === 'switch'
|
|
|
|
&& (!$stmt->num
|
|
|
|
|| !$stmt->num instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|| $stmt->num->value < 2
|
|
|
|
)
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$loop_scope->final_actions[] = ScopeAnalyzer::ACTION_LEAVE_SWITCH;
|
2018-11-10 22:29:40 +01:00
|
|
|
} else {
|
2020-01-27 18:17:12 +01:00
|
|
|
$leaving_switch = false;
|
2018-11-06 03:57:36 +01:00
|
|
|
$loop_scope->final_actions[] = ScopeAnalyzer::ACTION_CONTINUE;
|
2018-11-10 22:29:40 +01:00
|
|
|
}
|
2017-12-03 00:28:18 +01:00
|
|
|
|
|
|
|
$redefined_vars = $context->getRedefinedVars($original_context->vars_in_scope);
|
|
|
|
|
|
|
|
if ($loop_scope->redefined_loop_vars === null) {
|
|
|
|
$loop_scope->redefined_loop_vars = $redefined_vars;
|
|
|
|
} else {
|
|
|
|
foreach ($loop_scope->redefined_loop_vars as $redefined_var => $type) {
|
|
|
|
if (!isset($redefined_vars[$redefined_var])) {
|
|
|
|
unset($loop_scope->redefined_loop_vars[$redefined_var]);
|
|
|
|
} else {
|
|
|
|
$loop_scope->redefined_loop_vars[$redefined_var] = Type::combineUnionTypes(
|
|
|
|
$redefined_vars[$redefined_var],
|
|
|
|
$type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 19:40:28 +01:00
|
|
|
foreach ($redefined_vars as $var => $type) {
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($type->hasMixed()) {
|
2018-02-06 19:40:28 +01:00
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var] = $type;
|
|
|
|
} elseif (isset($loop_scope->possibly_redefined_loop_vars[$var])) {
|
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var] = Type::combineUnionTypes(
|
|
|
|
$type,
|
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var] = $type;
|
2017-12-03 00:28:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-17 02:01:33 +02:00
|
|
|
|
2018-11-18 17:39:14 +01:00
|
|
|
if ($context->collect_references && (!$context->case_scope || $stmt->num)) {
|
2018-06-17 02:01:33 +02:00
|
|
|
foreach ($context->unreferenced_vars as $var_id => $locations) {
|
2019-07-31 23:35:56 +02:00
|
|
|
if (isset($loop_scope->unreferenced_vars[$var_id])) {
|
|
|
|
$loop_scope->unreferenced_vars[$var_id] += $locations;
|
|
|
|
} else {
|
|
|
|
$loop_scope->unreferenced_vars[$var_id] = $locations;
|
|
|
|
}
|
|
|
|
|
2018-06-17 02:01:33 +02:00
|
|
|
if (isset($loop_scope->possibly_unreferenced_vars[$var_id])) {
|
|
|
|
$loop_scope->possibly_unreferenced_vars[$var_id] += $locations;
|
|
|
|
} else {
|
|
|
|
$loop_scope->possibly_unreferenced_vars[$var_id] = $locations;
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 16:33:32 +01:00
|
|
|
|
2020-01-03 16:56:47 +01:00
|
|
|
$loop_scope->referenced_var_ids += $context->referenced_var_ids;
|
2018-06-17 02:01:33 +02:00
|
|
|
}
|
2016-10-17 22:42:23 +02:00
|
|
|
}
|
|
|
|
|
2018-11-18 17:39:14 +01:00
|
|
|
$case_scope = $context->case_scope;
|
2020-01-27 18:17:12 +01:00
|
|
|
if ($case_scope && $context->collect_references && $leaving_switch) {
|
2018-06-17 03:54:44 +02:00
|
|
|
foreach ($context->unreferenced_vars as $var_id => $locations) {
|
2018-11-18 17:39:14 +01:00
|
|
|
if (isset($case_scope->unreferenced_vars[$var_id])) {
|
|
|
|
$case_scope->unreferenced_vars[$var_id] += $locations;
|
2018-06-17 03:54:44 +02:00
|
|
|
} else {
|
2018-11-18 17:39:14 +01:00
|
|
|
$case_scope->unreferenced_vars[$var_id] = $locations;
|
2018-06-17 03:54:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 19:31:41 +01:00
|
|
|
$context->has_returned = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Static_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$this->analyzeStatic($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
|
2019-08-06 00:33:33 +02:00
|
|
|
$echo_param = new FunctionLikeParameter(
|
|
|
|
'var',
|
|
|
|
false
|
|
|
|
);
|
2019-08-13 05:16:05 +02:00
|
|
|
|
|
|
|
$echo_param->sink = Type\Union::TAINTED_INPUT_HTML
|
|
|
|
| Type\Union::TAINTED_USER_SECRET
|
|
|
|
| Type\Union::TAINTED_SYSTEM_SECRET;
|
2019-08-06 00:33:33 +02:00
|
|
|
|
2016-12-09 18:48:02 +01:00
|
|
|
foreach ($stmt->exprs as $i => $expr) {
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = true;
|
2018-11-06 03:57:36 +01:00
|
|
|
ExpressionAnalyzer::analyze($this, $expr, $context);
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = false;
|
2016-12-09 18:48:02 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($expr_type = $this->node_data->getType($expr)) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (CallAnalyzer::checkFunctionArgumentType(
|
2016-12-09 18:48:02 +01:00
|
|
|
$this,
|
2019-11-25 17:44:54 +01:00
|
|
|
$expr_type,
|
2016-12-09 18:48:02 +01:00
|
|
|
Type::getString(),
|
2019-05-21 17:51:41 +02:00
|
|
|
null,
|
2016-12-09 18:48:02 +01:00
|
|
|
'echo',
|
2016-12-09 19:27:59 +01:00
|
|
|
(int)$i,
|
2017-12-11 03:14:30 +01:00
|
|
|
new CodeLocation($this->getSource(), $expr),
|
|
|
|
$expr,
|
2019-08-04 16:37:36 +02:00
|
|
|
$context,
|
2019-08-06 00:33:33 +02:00
|
|
|
$echo_param,
|
2019-08-05 16:21:23 +02:00
|
|
|
false,
|
2020-03-17 22:34:45 +01:00
|
|
|
null,
|
2019-09-11 19:52:37 +02:00
|
|
|
false,
|
2019-08-06 00:33:33 +02:00
|
|
|
true,
|
|
|
|
new CodeLocation($this->source, $stmt)
|
2016-12-09 18:48:02 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2018-09-18 22:42:51 +02:00
|
|
|
|
2018-09-18 23:08:32 +02:00
|
|
|
if ($codebase->config->forbid_echo) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ForbiddenEcho(
|
|
|
|
'Use of echo',
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif (isset($codebase->config->forbidden_functions['echo'])) {
|
2018-09-18 22:42:51 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ForbiddenCode(
|
|
|
|
'Use of echo',
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
2019-03-23 15:58:26 +01:00
|
|
|
// continue
|
2018-09-18 22:42:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 20:38:05 +01:00
|
|
|
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& ($context->mutation_free
|
|
|
|
|| $context->external_mutation_free)
|
|
|
|
) {
|
2020-03-06 21:23:11 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpureFunctionCall(
|
|
|
|
'Cannot call echo from a mutation-free context',
|
|
|
|
new CodeLocation($this, $stmt)
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2020-02-24 20:38:05 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
2018-02-25 16:43:54 +01:00
|
|
|
foreach ($stmt->stmts as $function_stmt) {
|
|
|
|
if ($function_stmt instanceof PhpParser\Node\Stmt\Global_) {
|
|
|
|
foreach ($function_stmt->vars as $var) {
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (is_string($var->name)) {
|
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
// registers variable in global context
|
|
|
|
$context->hasVariable($var_id, $this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif (!$function_stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (!$codebase->register_stub_files
|
|
|
|
&& !$codebase->register_autoload_files
|
2018-06-30 21:29:37 +02:00
|
|
|
) {
|
2018-04-17 18:16:25 +02:00
|
|
|
$function_id = strtolower($stmt->name->name);
|
2017-03-19 20:41:24 +01:00
|
|
|
$function_context = new Context($context->self);
|
2019-09-11 19:52:37 +02:00
|
|
|
$function_context->strict_types = $context->strict_types;
|
2018-06-22 07:13:49 +02:00
|
|
|
$config = Config::getInstance();
|
2018-11-06 03:57:36 +01:00
|
|
|
$function_context->collect_references = $codebase->collect_references;
|
2018-06-22 07:13:49 +02:00
|
|
|
$function_context->collect_exceptions = $config->check_for_throws_docblock;
|
2016-12-29 01:57:18 +01:00
|
|
|
|
2019-06-28 16:48:30 +02:00
|
|
|
if (isset($this->function_analyzers[$function_id])) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$this->function_analyzers[$function_id]->analyze(
|
|
|
|
$function_context,
|
|
|
|
$this->node_data,
|
|
|
|
$context
|
|
|
|
);
|
2016-12-29 01:57:18 +01:00
|
|
|
|
2019-06-28 16:48:30 +02:00
|
|
|
if ($config->reportIssueInFile('InvalidReturnType', $this->getFilePath())) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$method_id = $this->function_analyzers[$function_id]->getId();
|
2019-06-28 16:48:30 +02:00
|
|
|
|
|
|
|
$function_storage = $codebase->functions->getStorage(
|
|
|
|
$this,
|
|
|
|
$method_id
|
|
|
|
);
|
2016-12-29 01:57:18 +01:00
|
|
|
|
2019-06-28 16:48:30 +02:00
|
|
|
$return_type = $function_storage->return_type;
|
|
|
|
$return_type_location = $function_storage->return_type_location;
|
2017-03-19 20:41:24 +01:00
|
|
|
|
2019-06-28 16:48:30 +02:00
|
|
|
$this->function_analyzers[$function_id]->verifyReturnType(
|
2019-09-01 06:56:46 +02:00
|
|
|
$stmt->getStmts(),
|
2019-06-28 16:48:30 +02:00
|
|
|
$this,
|
|
|
|
$return_type,
|
|
|
|
$this->getFQCLN(),
|
|
|
|
$return_type_location
|
|
|
|
);
|
|
|
|
}
|
2017-03-19 20:41:24 +01:00
|
|
|
}
|
2016-12-29 01:57:18 +01:00
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Expression) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
$this,
|
|
|
|
$stmt->expr,
|
|
|
|
$context,
|
|
|
|
false,
|
|
|
|
$global_context,
|
|
|
|
true
|
|
|
|
) === false) {
|
2017-10-13 02:24:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\InlineHTML) {
|
2016-01-20 00:27:06 +01:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Global_) {
|
2017-02-12 19:27:02 +01:00
|
|
|
if (!$context->collect_initializations && !$global_context) {
|
2016-11-05 22:53:30 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidGlobal(
|
|
|
|
'Cannot use global scope here',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this->source, $stmt)
|
2016-11-05 22:53:30 +01:00
|
|
|
),
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source->getSuppressedIssues()
|
2016-11-05 22:53:30 +01:00
|
|
|
)) {
|
2017-02-12 18:23:05 +01:00
|
|
|
// fall through
|
2016-11-05 22:53:30 +01:00
|
|
|
}
|
2017-02-12 19:27:02 +01:00
|
|
|
}
|
|
|
|
|
2018-06-08 15:31:21 +02:00
|
|
|
$source = $this->getSource();
|
2018-11-06 03:57:36 +01:00
|
|
|
$function_storage = $source instanceof FunctionLikeAnalyzer
|
2018-06-08 15:31:21 +02:00
|
|
|
? $source->getFunctionLikeStorage($this)
|
|
|
|
: null;
|
|
|
|
|
2017-02-12 19:27:02 +01:00
|
|
|
foreach ($stmt->vars as $var) {
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (is_string($var->name)) {
|
|
|
|
$var_id = '$' . $var->name;
|
2016-11-05 22:53:30 +01:00
|
|
|
|
2018-05-31 04:56:46 +02:00
|
|
|
if ($var->name === 'argv' || $var->name === 'argc') {
|
2019-03-14 01:15:29 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $this->getGlobalType($var_id);
|
2018-06-08 15:31:21 +02:00
|
|
|
} elseif (isset($function_storage->global_types[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = clone $function_storage->global_types[$var_id];
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2018-05-31 04:56:46 +02:00
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] =
|
|
|
|
$global_context && $global_context->hasVariable($var_id, $this)
|
|
|
|
? clone $global_context->vars_in_scope[$var_id]
|
2019-03-14 01:15:29 +01:00
|
|
|
: $this->getGlobalType($var_id);
|
2016-11-05 22:53:30 +01:00
|
|
|
|
2018-05-31 04:56:46 +02:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
2016-01-26 20:11:56 +01:00
|
|
|
foreach ($stmt->props as $prop) {
|
|
|
|
if ($prop->default) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ExpressionAnalyzer::analyze($this, $prop->default, $context);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($prop_default_type = $this->node_data->getType($prop->default)) {
|
2019-07-30 04:18:46 +02:00
|
|
|
if (PropertyAssignmentAnalyzer::analyzeInstance(
|
|
|
|
$this,
|
|
|
|
$prop,
|
|
|
|
$prop->name->name,
|
|
|
|
$prop->default,
|
2019-11-25 17:44:54 +01:00
|
|
|
$prop_default_type,
|
2019-07-30 04:18:46 +02:00
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
// fall through
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
2016-12-04 07:44:33 +01:00
|
|
|
$const_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
|
|
|
|
if ($stmt->isProtected()) {
|
|
|
|
$const_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->isPrivate()) {
|
|
|
|
$const_visibility = \ReflectionProperty::IS_PRIVATE;
|
|
|
|
}
|
2016-12-06 22:41:42 +01:00
|
|
|
|
2016-08-14 03:14:32 +02:00
|
|
|
foreach ($stmt->consts as $const) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ExpressionAnalyzer::analyze($this, $const->value, $context);
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (($const_type = $this->node_data->getType($const->value))
|
|
|
|
&& !$const_type->hasMixed()
|
|
|
|
) {
|
2018-02-09 00:14:28 +01:00
|
|
|
$codebase->classlikes->setConstantType(
|
2017-01-07 20:35:07 +01:00
|
|
|
(string)$this->getFQCLN(),
|
2018-04-17 18:16:25 +02:00
|
|
|
$const->name->name,
|
2019-11-25 17:44:54 +01:00
|
|
|
$const_type,
|
2016-12-04 07:44:33 +01:00
|
|
|
$const_visibility
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-08-14 03:14:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
2017-12-23 01:26:08 +01:00
|
|
|
try {
|
2018-11-11 18:01:14 +01:00
|
|
|
$class_analyzer = new ClassAnalyzer($stmt, $this->source, $stmt->name ? $stmt->name->name : null);
|
|
|
|
$class_analyzer->analyze(null, $global_context);
|
2017-12-23 01:26:08 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// disregard this exception, we'll likely see it elsewhere in the form
|
|
|
|
// of an issue
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
2019-06-26 22:26:14 +02:00
|
|
|
if (($doc_comment = $stmt->getDocComment()) && $this->parsed_docblock) {
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments = [];
|
2018-01-24 19:52:34 +01:00
|
|
|
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
2019-06-26 22:26:14 +02:00
|
|
|
$var_comments = CommentAnalyzer::arrayToDocblocks(
|
2019-06-01 17:53:32 +02:00
|
|
|
$doc_comment,
|
2019-06-26 22:26:14 +02:00
|
|
|
$this->parsed_docblock,
|
2017-11-15 03:43:31 +01:00
|
|
|
$this->getSource(),
|
2018-12-18 05:29:27 +01:00
|
|
|
$this->getSource()->getAliases(),
|
|
|
|
$this->getSource()->getTemplateTypeMap()
|
2017-11-15 03:43:31 +01:00
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->getSource(), $stmt, null, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +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->var_id || !$var_comment->type) {
|
2018-02-08 05:33:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$comment_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
2018-01-02 02:04:03 +01:00
|
|
|
$var_comment->type,
|
2019-05-25 17:51:09 +02:00
|
|
|
$context->self,
|
|
|
|
$context->self,
|
|
|
|
$this->getParentFQCLN()
|
2017-10-07 16:22:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $comment_type;
|
2017-05-25 07:32:34 +02:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Goto_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Label) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// do nothing
|
2016-11-20 08:52:34 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Declare_) {
|
2018-08-28 23:42:39 +02:00
|
|
|
foreach ($stmt->declares as $declaration) {
|
|
|
|
if ((string) $declaration->key === 'strict_types'
|
|
|
|
&& $declaration->value instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
&& $declaration->value->value === 1
|
|
|
|
) {
|
|
|
|
$context->strict_types = true;
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 15:18:41 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\HaltCompiler) {
|
2020-03-15 19:31:41 +01:00
|
|
|
$context->has_returned = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-06 01:53:39 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnrecognizedStatement(
|
|
|
|
'Psalm does not understand ' . get_class($stmt),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this->source, $stmt)
|
2016-11-06 01:53:39 +01:00
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2017-10-27 00:19:19 +02:00
|
|
|
|
2018-06-17 03:54:44 +02:00
|
|
|
if ($context->loop_scope
|
|
|
|
&& $context->loop_scope->final_actions
|
2018-11-06 03:57:36 +01:00
|
|
|
&& !in_array(ScopeAnalyzer::ACTION_NONE, $context->loop_scope->final_actions, true)
|
2017-12-03 00:28:18 +01:00
|
|
|
) {
|
2020-03-15 19:31:41 +01:00
|
|
|
//$context->has_returned = true;
|
2017-12-03 00:28:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
if ($plugin_classes) {
|
2017-11-07 20:46:53 +01:00
|
|
|
$file_manipulations = [];
|
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
foreach ($plugin_classes as $plugin_fq_class_name) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($plugin_fq_class_name::afterStatementAnalysis(
|
2017-11-07 20:46:53 +01:00
|
|
|
$stmt,
|
|
|
|
$context,
|
2019-11-25 17:44:54 +01:00
|
|
|
$this,
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2017-11-07 20:46:53 +01:00
|
|
|
$file_manipulations
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
FileManipulationBuffer::add($this->getFilePath(), $file_manipulations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
if ($new_issues) {
|
|
|
|
$this->removeSuppressedIssues($new_issues);
|
|
|
|
}
|
2019-04-20 23:49:49 +02:00
|
|
|
|
|
|
|
if ($ignore_variable_property) {
|
|
|
|
$context->ignore_variable_property = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ignore_variable_method) {
|
|
|
|
$context->ignore_variable_method = false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($root_scope
|
|
|
|
&& $context->collect_references
|
2018-02-17 18:02:31 +01:00
|
|
|
&& !$context->collect_initializations
|
2019-03-05 21:45:09 +01:00
|
|
|
&& $codebase->find_unused_variables
|
2018-01-25 07:04:26 +01:00
|
|
|
&& $context->check_variables
|
|
|
|
) {
|
2019-07-24 22:48:54 +02:00
|
|
|
$this->checkUnreferencedVars($stmts);
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($codebase->alter_code && $root_scope && $this->vars_to_initialize) {
|
|
|
|
$file_contents = $codebase->getFileContents($this->getFilePath());
|
2018-01-21 22:24:20 +01:00
|
|
|
|
|
|
|
foreach ($this->vars_to_initialize as $var_id => $branch_point) {
|
|
|
|
$newline_pos = (int)strrpos($file_contents, "\n", $branch_point - strlen($file_contents)) + 1;
|
|
|
|
$indentation = substr($file_contents, $newline_pos, $branch_point - $newline_pos);
|
|
|
|
FileManipulationBuffer::add($this->getFilePath(), [
|
2018-04-13 01:42:24 +02:00
|
|
|
new FileManipulation($branch_point, $branch_point, $var_id . ' = null;' . "\n" . $indentation),
|
2018-01-21 22:24:20 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
/**
|
2019-07-24 22:48:54 +02:00
|
|
|
* @param CodeLocation $var_loc
|
|
|
|
* @param int $end_bound
|
|
|
|
* @param bool $assign_ref
|
|
|
|
* @return FileManipulation
|
|
|
|
*/
|
|
|
|
private function getPartialRemovalBounds(
|
|
|
|
CodeLocation $var_loc,
|
|
|
|
int $end_bound,
|
|
|
|
bool $assign_ref = false
|
|
|
|
): FileManipulation {
|
|
|
|
$var_start_loc= $var_loc->raw_file_start;
|
|
|
|
$stmt_content = $this->getSource()->getCodebase()->file_provider->getContents($var_loc->file_path);
|
|
|
|
$str_for_token = "<?php\n" . substr($stmt_content, $var_start_loc, $end_bound - $var_start_loc + 1);
|
|
|
|
$token_list = array_slice(token_get_all($str_for_token), 1); //Ignore "<?php"
|
|
|
|
|
|
|
|
$offset_count = strlen($token_list[0][1]);
|
|
|
|
$iter = 1;
|
|
|
|
|
|
|
|
// Check if second token is just whitespace
|
|
|
|
if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) == 0) {
|
|
|
|
$offset_count += strlen($token_list[1][1]);
|
|
|
|
$iter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add offset for assignment operator
|
|
|
|
if (is_string($token_list[$iter])) {
|
|
|
|
$offset_count += 1;
|
|
|
|
} else {
|
|
|
|
$offset_count += strlen($token_list[$iter][1]);
|
|
|
|
}
|
|
|
|
$iter++;
|
|
|
|
|
|
|
|
// Remove any whitespace following assignment operator token (e.g "=", "+=")
|
|
|
|
if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) == 0) {
|
|
|
|
$offset_count += strlen($token_list[$iter][1]);
|
|
|
|
$iter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are dealing with assignment by reference, we need to handle "&" and any whitespace after
|
|
|
|
if ($assign_ref) {
|
|
|
|
$offset_count += 1;
|
|
|
|
$iter++;
|
|
|
|
// Handle any whitespace after "&"
|
|
|
|
if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) == 0) {
|
|
|
|
$offset_count += strlen($token_list[$iter][1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_man_start = $var_start_loc;
|
|
|
|
$file_man_end = $var_start_loc + $offset_count;
|
|
|
|
|
|
|
|
return new FileManipulation($file_man_start, $file_man_end, "", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\Assign|PhpParser\Node\Expr\AssignOp|PhpParser\Node\Expr\AssignRef $cur_assign
|
|
|
|
* @param array<string, CodeLocation> $var_loc_map
|
2018-01-25 07:04:26 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-07-24 22:48:54 +02:00
|
|
|
private function markRemovedChainAssignVar(PhpParser\Node\Expr $cur_assign, array $var_loc_map): void
|
|
|
|
{
|
|
|
|
$var = $cur_assign->var;
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) {
|
|
|
|
$var_name = "$" . $var->name;
|
|
|
|
$var_loc = $var_loc_map[$var_name];
|
|
|
|
$this->removed_unref_vars[$var_name] = $var_loc;
|
|
|
|
|
|
|
|
$rhs_exp = $cur_assign->expr;
|
|
|
|
if ($rhs_exp instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|| $rhs_exp instanceof PhpParser\Node\Expr\AssignOp
|
|
|
|
|| $rhs_exp instanceof PhpParser\Node\Expr\AssignRef
|
|
|
|
) {
|
|
|
|
$this->markRemovedChainAssignVar($rhs_exp, $var_loc_map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\Assign|PhpParser\Node\Expr\AssignOp|PhpParser\Node\Expr\AssignRef $cur_assign
|
|
|
|
* @param array<string, CodeLocation> $var_loc_map
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function checkRemovableChainAssignment(PhpParser\Node\Expr $cur_assign, array $var_loc_map): bool
|
|
|
|
{
|
|
|
|
// Check if current assignment expr's variable is removable
|
|
|
|
$var = $cur_assign->var;
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) {
|
|
|
|
$var_loc = $cur_assign->var->getStartFilePos();
|
|
|
|
$var_name = "$" . $var->name;
|
|
|
|
|
|
|
|
if (array_key_exists($var_name, $var_loc_map) &&
|
|
|
|
$var_loc_map[$var_name]->raw_file_start === $var_loc) {
|
|
|
|
$curr_removable = true;
|
|
|
|
} else {
|
|
|
|
$curr_removable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($curr_removable) {
|
|
|
|
$rhs_exp = $cur_assign->expr;
|
|
|
|
|
|
|
|
if ($rhs_exp instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|| $rhs_exp instanceof PhpParser\Node\Expr\AssignOp
|
|
|
|
|| $rhs_exp instanceof PhpParser\Node\Expr\AssignRef
|
|
|
|
) {
|
|
|
|
$rhs_removable = $this->checkRemovableChainAssignment($rhs_exp, $var_loc_map);
|
|
|
|
return $rhs_removable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $curr_removable;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Stmt> $stmts
|
|
|
|
* @param string $var_id
|
|
|
|
* @param CodeLocation $original_location
|
|
|
|
* @return array{
|
|
|
|
* 0: PhpParser\Node\Stmt|null,
|
|
|
|
* 1: PhpParser\Node\Expr\Assign|PhpParser\Node\Expr\AssignOp|PhpParser\Node\Expr\AssignRef|null
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
private function findAssignStmt(array $stmts, string $var_id, CodeLocation $original_location)
|
|
|
|
{
|
|
|
|
$assign_stmt = null;
|
|
|
|
$assign_exp = null;
|
|
|
|
$assign_exp_found = false;
|
2019-07-24 23:01:41 +02:00
|
|
|
|
2019-07-24 22:48:54 +02:00
|
|
|
$i = 0;
|
2019-11-13 21:05:21 +01:00
|
|
|
|
|
|
|
while ($i < count($stmts) && !$assign_exp_found) {
|
2019-07-24 22:48:54 +02:00
|
|
|
$stmt = $stmts[$i];
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Expression) {
|
|
|
|
$search_result = $this->findAssignExp($stmt->expr, $var_id, $original_location->raw_file_start);
|
2019-11-13 21:05:21 +01:00
|
|
|
|
2019-07-24 22:48:54 +02:00
|
|
|
$target_exp = $search_result[0];
|
|
|
|
$levels_taken = $search_result[1];
|
|
|
|
|
|
|
|
if (!is_null($target_exp)) {
|
|
|
|
$assign_exp_found = true;
|
|
|
|
$assign_exp = $target_exp;
|
|
|
|
$assign_stmt = $levels_taken === 1 ? $stmt : null;
|
|
|
|
}
|
2019-11-13 21:05:21 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
|
|
|
|
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->catches as $catch_stmt) {
|
|
|
|
$search_result = $this->findAssignStmt($catch_stmt->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\While_
|
|
|
|
) {
|
|
|
|
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
|
|
|
|
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
|
|
|
|
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
2019-11-14 17:03:16 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
|
|
|
$search_result = $this->findAssignStmt($stmt->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->elseifs as $elseif_stmt) {
|
|
|
|
$search_result = $this->findAssignStmt($elseif_stmt->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->else) {
|
|
|
|
$search_result = $this->findAssignStmt($stmt->else->stmts, $var_id, $original_location);
|
|
|
|
|
|
|
|
if ($search_result[0] && $search_result[1]) {
|
|
|
|
return $search_result;
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 22:48:54 +02:00
|
|
|
}
|
2019-11-13 21:05:21 +01:00
|
|
|
|
|
|
|
$i++;
|
2019-07-24 22:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return [$assign_stmt, $assign_exp];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr $current_node
|
|
|
|
* @param string $var_id
|
|
|
|
* @param int $var_start_loc
|
|
|
|
* @param int $search_level
|
|
|
|
* @return array{
|
|
|
|
* 0: PhpParser\Node\Expr\Assign|PhpParser\Node\Expr\AssignOp|PhpParser\Node\Expr\AssignRef|null,
|
|
|
|
* 1: int
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
private function findAssignExp(
|
|
|
|
PhpParser\Node\Expr $current_node,
|
|
|
|
string $var_id,
|
|
|
|
int $var_start_loc,
|
|
|
|
int $search_level = 1
|
|
|
|
) {
|
|
|
|
if ($current_node instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|| $current_node instanceof PhpPArser\Node\Expr\AssignOp
|
|
|
|
|| $current_node instanceof PhpParser\Node\Expr\AssignRef
|
|
|
|
) {
|
|
|
|
$var = $current_node->var;
|
|
|
|
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $var->name === substr($var_id, 1)
|
|
|
|
&& $var->getStartFilePos() === $var_start_loc
|
|
|
|
) {
|
|
|
|
return [$current_node, $search_level];
|
|
|
|
}
|
|
|
|
|
|
|
|
$rhs_exp = $current_node->expr;
|
|
|
|
$rhs_search_result = $this->findAssignExp($rhs_exp, $var_id, $var_start_loc, $search_level + 1);
|
|
|
|
return [$rhs_search_result[0], $rhs_search_result[1]];
|
|
|
|
} else {
|
|
|
|
return [null, $search_level];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CodeLocation $var_loc
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function checkIfVarRemoved(string $var_id, CodeLocation $var_loc): bool
|
|
|
|
{
|
|
|
|
return array_key_exists($var_id, $this->removed_unref_vars)
|
|
|
|
&& $this->removed_unref_vars[$var_id] === $var_loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Stmt> $stmts
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function checkUnreferencedVars(array $stmts)
|
2018-01-25 07:04:26 +01:00
|
|
|
{
|
|
|
|
$source = $this->getSource();
|
2019-07-24 22:48:54 +02:00
|
|
|
$codebase = $source->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
$function_storage = $source instanceof FunctionLikeAnalyzer ? $source->getFunctionLikeStorage($this) : null;
|
2019-07-24 22:48:54 +02:00
|
|
|
if ($codebase->alter_code) {
|
|
|
|
// Reverse array to deal with chain of assignments
|
2019-11-17 20:53:08 +01:00
|
|
|
$this->unused_var_locations = array_reverse($this->unused_var_locations, true);
|
2019-07-24 22:48:54 +02:00
|
|
|
}
|
|
|
|
$var_list = array_column($this->unused_var_locations, 0);
|
|
|
|
$loc_list = array_column($this->unused_var_locations, 1);
|
|
|
|
$var_loc_map = array_combine($var_list, $loc_list);
|
2018-01-25 07:04:26 +01:00
|
|
|
|
2019-07-24 23:01:41 +02:00
|
|
|
$project_analyzer = $this->getProjectAnalyzer();
|
|
|
|
|
2018-06-17 02:01:33 +02:00
|
|
|
foreach ($this->unused_var_locations as $hash => list($var_id, $original_location)) {
|
2019-11-12 16:20:34 +01:00
|
|
|
if (substr($var_id, 0, 2) === '$_' || isset($this->used_var_locations[$hash])) {
|
2018-01-25 07:04:26 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-10 21:36:35 +01:00
|
|
|
if ((!$function_storage
|
2019-07-24 22:48:54 +02:00
|
|
|
|| !array_key_exists(substr($var_id, 1), $function_storage->param_types))
|
2019-03-10 21:36:35 +01:00
|
|
|
&& !isset($this->byref_uses[$var_id])
|
2019-03-20 04:16:00 +01:00
|
|
|
&& !$this->isSuperGlobal($var_id)
|
2019-03-10 21:36:35 +01:00
|
|
|
) {
|
2019-07-24 23:01:41 +02:00
|
|
|
$issue = new UnusedVariable(
|
|
|
|
'Variable ' . $var_id . ' is never referenced',
|
|
|
|
$original_location
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($codebase->alter_code
|
|
|
|
&& !$this->checkIfVarRemoved($var_id, $original_location)
|
|
|
|
&& isset($project_analyzer->getIssuesToFix()['UnusedVariable'])
|
|
|
|
&& !IssueBuffer::isSuppressed($issue, $this->getSuppressedIssues())
|
|
|
|
) {
|
2019-07-24 22:48:54 +02:00
|
|
|
$search_result = $this->findAssignStmt($stmts, $var_id, $original_location);
|
|
|
|
$assign_stmt = $search_result[0];
|
|
|
|
$assign_exp = $search_result[1];
|
|
|
|
$chain_assignment = false;
|
|
|
|
|
|
|
|
if (!is_null($assign_stmt) && !is_null($assign_exp)) {
|
|
|
|
// Check if we have to remove assignment statemnt as expression (i.e. just "$var = ")
|
|
|
|
|
|
|
|
// Consider chain of assignments
|
|
|
|
$rhs_exp = $assign_exp->expr;
|
|
|
|
if ($rhs_exp instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|| $rhs_exp instanceof PhpParser\Node\Expr\AssignOp
|
|
|
|
|| $rhs_exp instanceof PhpParser\Node\Expr\AssignRef
|
|
|
|
) {
|
|
|
|
$chain_assignment = true;
|
|
|
|
$removable_stmt = $this->checkRemovableChainAssignment($assign_exp, $var_loc_map);
|
|
|
|
} else {
|
|
|
|
$removable_stmt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removable_stmt) {
|
|
|
|
$traverser = new PhpParser\NodeTraverser();
|
|
|
|
$visitor = new CheckTrivialExprVisitor();
|
|
|
|
$traverser->addVisitor($visitor);
|
|
|
|
$traverser->traverse([$rhs_exp]);
|
|
|
|
|
|
|
|
$rhs_exp_trivial = (count($visitor->getNonTrivialExpr()) == 0);
|
|
|
|
|
|
|
|
if ($rhs_exp_trivial) {
|
|
|
|
$treat_as_expr = false;
|
|
|
|
} else {
|
|
|
|
$treat_as_expr = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$treat_as_expr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($treat_as_expr) {
|
|
|
|
$is_assign_ref = $assign_exp instanceof PhpParser\Node\Expr\AssignRef;
|
|
|
|
$new_file_manipulation = $this->getPartialRemovalBounds(
|
|
|
|
$original_location,
|
|
|
|
$assign_stmt->getEndFilePos(),
|
|
|
|
$is_assign_ref
|
|
|
|
);
|
|
|
|
$this->removed_unref_vars[$var_id] = $original_location;
|
|
|
|
} else {
|
|
|
|
// Remove whole assignment statement
|
|
|
|
$new_file_manipulation = new FileManipulation(
|
|
|
|
$assign_stmt->getStartFilePos(),
|
|
|
|
$assign_stmt->getEndFilePos() + 1,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// If statement we are removing is a chain of assignments, mark other variables as removed
|
|
|
|
if ($chain_assignment) {
|
|
|
|
$this->markRemovedChainAssignVar($assign_exp, $var_loc_map);
|
|
|
|
} else {
|
|
|
|
$this->removed_unref_vars[$var_id] = $original_location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileManipulationBuffer::add($original_location->file_path, [$new_file_manipulation]);
|
|
|
|
} elseif (!is_null($assign_exp)) {
|
|
|
|
$is_assign_ref = $assign_exp instanceof PhpParser\Node\Expr\AssignRef;
|
|
|
|
$new_file_manipulation = $this->getPartialRemovalBounds(
|
|
|
|
$original_location,
|
|
|
|
$assign_exp->getEndFilePos(),
|
|
|
|
$is_assign_ref
|
|
|
|
);
|
|
|
|
|
|
|
|
FileManipulationBuffer::add($original_location->file_path, [$new_file_manipulation]);
|
|
|
|
$this->removed_unref_vars[$var_id] = $original_location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2019-07-24 23:01:41 +02:00
|
|
|
$issue,
|
2019-12-02 21:24:01 +01:00
|
|
|
$this->getSuppressedIssues(),
|
|
|
|
true
|
2018-01-25 07:04:26 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:03:21 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function analyzeUnset(PhpParser\Node\Stmt\Unset_ $stmt, Context $context)
|
|
|
|
{
|
|
|
|
$context->inside_unset = true;
|
|
|
|
|
|
|
|
foreach ($stmt->vars as $var) {
|
|
|
|
ExpressionAnalyzer::analyze($this, $var, $context);
|
|
|
|
|
|
|
|
$var_id = ExpressionAnalyzer::getArrayVarId(
|
|
|
|
$var,
|
|
|
|
$this->getFQCLN(),
|
|
|
|
$this
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->remove($var_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\ArrayDimFetch && $var->dim) {
|
|
|
|
$root_var_id = ExpressionAnalyzer::getArrayVarId(
|
|
|
|
$var->var,
|
|
|
|
$this->getFQCLN(),
|
|
|
|
$this
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($root_var_id && isset($context->vars_in_scope[$root_var_id])) {
|
|
|
|
$root_type = clone $context->vars_in_scope[$root_var_id];
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($root_type->getAtomicTypes() as $atomic_root_type) {
|
2018-12-20 22:03:21 +01:00
|
|
|
if ($atomic_root_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
if ($var->dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $var->dim instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
) {
|
|
|
|
if (isset($atomic_root_type->properties[$var->dim->value])) {
|
|
|
|
unset($atomic_root_type->properties[$var->dim->value]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$atomic_root_type->properties) {
|
2019-10-01 22:13:17 +02:00
|
|
|
if ($atomic_root_type->previous_value_type) {
|
2019-03-11 06:54:32 +01:00
|
|
|
$root_type->addType(
|
|
|
|
new Type\Atomic\TArray([
|
2019-10-02 01:31:08 +02:00
|
|
|
$atomic_root_type->previous_key_type
|
|
|
|
? clone $atomic_root_type->previous_key_type
|
|
|
|
: new Type\Union([new Type\Atomic\TArrayKey]),
|
2019-10-01 22:13:17 +02:00
|
|
|
clone $atomic_root_type->previous_value_type,
|
2019-03-11 06:54:32 +01:00
|
|
|
])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$root_type->addType(
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
new Type\Union([new Type\Atomic\TEmpty]),
|
|
|
|
new Type\Union([new Type\Atomic\TEmpty]),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
2018-12-20 22:03:21 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-13 07:35:29 +02:00
|
|
|
$atomic_root_type->sealed = false;
|
|
|
|
|
2018-12-20 22:03:21 +01:00
|
|
|
$root_type->addType(
|
|
|
|
$atomic_root_type->getGenericArrayType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} elseif ($atomic_root_type instanceof Type\Atomic\TNonEmptyArray) {
|
|
|
|
$root_type->addType(
|
|
|
|
new Type\Atomic\TArray($atomic_root_type->type_params)
|
|
|
|
);
|
2019-02-18 17:39:05 +01:00
|
|
|
} elseif ($atomic_root_type instanceof Type\Atomic\TNonEmptyMixed) {
|
|
|
|
$root_type->addType(
|
|
|
|
new Type\Atomic\TMixed()
|
|
|
|
);
|
2019-10-09 18:49:31 +02:00
|
|
|
} elseif ($atomic_root_type instanceof Type\Atomic\TList) {
|
|
|
|
$root_type->addType(
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
$atomic_root_type->type_param
|
|
|
|
])
|
|
|
|
);
|
2018-12-20 22:03:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$root_var_id] = $root_type;
|
|
|
|
|
|
|
|
$context->removeVarFromConflictingClauses(
|
|
|
|
$root_var_id,
|
|
|
|
$context->vars_in_scope[$root_var_id],
|
|
|
|
$this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->inside_unset = false;
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:52:12 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Stmt\Static_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-06-17 00:52:12 +02:00
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function analyzeStatic(PhpParser\Node\Stmt\Static_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2019-03-28 05:10:08 +01:00
|
|
|
$codebase = $this->getCodebase();
|
|
|
|
|
2019-09-01 03:31:53 +02:00
|
|
|
if ($context->mutation_free) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\ImpureStaticVariable(
|
|
|
|
'Cannot use a static variable in a mutation-free context',
|
|
|
|
new CodeLocation($this, $stmt)
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($stmt->vars as $var) {
|
2019-03-28 05:10:08 +01:00
|
|
|
if (!is_string($var->var->name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_id = '$' . $var->var->name;
|
|
|
|
|
|
|
|
$doc_comment = $stmt->getDocComment();
|
|
|
|
|
|
|
|
$comment_type = null;
|
|
|
|
|
2019-06-26 22:26:14 +02:00
|
|
|
if ($doc_comment && $this->parsed_docblock) {
|
2019-03-28 05:16:35 +01:00
|
|
|
$var_comments = [];
|
|
|
|
|
2019-03-28 05:10:08 +01:00
|
|
|
try {
|
2019-06-26 22:26:14 +02:00
|
|
|
$var_comments = CommentAnalyzer::arrayToDocblocks(
|
2019-06-01 17:53:32 +02:00
|
|
|
$doc_comment,
|
2019-06-26 22:26:14 +02:00
|
|
|
$this->parsed_docblock,
|
2019-03-28 05:10:08 +01:00
|
|
|
$this->getSource(),
|
2019-06-26 22:26:14 +02:00
|
|
|
$this->getSource()->getAliases(),
|
|
|
|
$this->getSource()->getTemplateTypeMap()
|
2019-03-28 05:10:08 +01:00
|
|
|
);
|
|
|
|
} catch (\Psalm\Exception\IncorrectDocblockException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\MissingDocblockType(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this, $var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->getSource(), $var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($var_comments as $var_comment) {
|
2019-08-21 17:25:08 +02:00
|
|
|
if (!$var_comment->type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-28 05:10:08 +01:00
|
|
|
try {
|
|
|
|
$var_comment_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$var_comment->type,
|
|
|
|
$context->self,
|
2019-05-25 17:51:09 +02:00
|
|
|
$context->self,
|
|
|
|
$this->getParentFQCLN()
|
2019-03-28 05:10:08 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$var_comment_type->setFromDocblock();
|
|
|
|
|
|
|
|
$var_comment_type->check(
|
|
|
|
$this,
|
|
|
|
new CodeLocation($this->getSource(), $var),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
2019-06-06 20:27:49 +02:00
|
|
|
if ($codebase->alter_code
|
|
|
|
&& $var_comment->type_start
|
2019-06-01 18:25:57 +02:00
|
|
|
&& $var_comment->type_end
|
|
|
|
&& $var_comment->line_number
|
|
|
|
) {
|
2019-06-04 22:36:32 +02:00
|
|
|
$type_location = new CodeLocation\DocblockTypeLocation(
|
|
|
|
$this,
|
|
|
|
$var_comment->type_start,
|
|
|
|
$var_comment->type_end,
|
|
|
|
$var_comment->line_number
|
|
|
|
);
|
2019-06-01 18:25:57 +02:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
$codebase->classlikes->handleDocblockTypeInMigration(
|
|
|
|
$codebase,
|
|
|
|
$this,
|
2019-06-01 18:25:57 +02:00
|
|
|
$var_comment_type,
|
2019-06-04 22:36:32 +02:00
|
|
|
$type_location,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id
|
2019-06-01 18:25:57 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-28 05:10:08 +01:00
|
|
|
if (!$var_comment->var_id || $var_comment->var_id === $var_id) {
|
|
|
|
$comment_type = $var_comment_type;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $var_comment_type;
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this, $var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($comment_type) {
|
|
|
|
$context->byref_constraints[$var_id] = new \Psalm\Internal\ReferenceConstraint($comment_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($var->default) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($this, $var->default, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2019-03-28 05:10:08 +01:00
|
|
|
if ($comment_type
|
2019-11-25 17:44:54 +01:00
|
|
|
&& ($var_default_type = $this->node_data->getType($var->default))
|
2019-03-28 05:10:08 +01:00
|
|
|
&& !TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$var_default_type,
|
2019-03-28 05:10:08 +01:00
|
|
|
$comment_type
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\ReferenceConstraintViolation(
|
|
|
|
$var_id . ' of type ' . $comment_type->getId() . ' cannot be assigned type '
|
2019-11-25 17:44:54 +01:00
|
|
|
. $var_default_type->getId(),
|
2019-03-28 05:10:08 +01:00
|
|
|
new CodeLocation($this, $var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
}
|
2019-03-28 05:10:08 +01:00
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
|
2019-03-28 05:10:08 +01:00
|
|
|
if ($context->check_variables) {
|
|
|
|
$context->vars_in_scope[$var_id] = $comment_type ? clone $comment_type : Type::getMixed();
|
2018-02-27 01:32:26 +01:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
2019-03-20 04:26:46 +01:00
|
|
|
$this->byref_uses[$var_id] = true;
|
2018-02-27 01:32:26 +01:00
|
|
|
|
2019-03-28 05:10:08 +01:00
|
|
|
$location = new CodeLocation($this, $var);
|
2018-02-27 01:32:26 +01:00
|
|
|
|
|
|
|
if ($context->collect_references) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$context->unreferenced_vars[$var_id] = [$location->getHash() => $location];
|
2018-02-27 01:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->registerVariable(
|
|
|
|
$var_id,
|
|
|
|
$location,
|
|
|
|
$context->branch_point
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
2018-06-28 03:53:25 +02:00
|
|
|
* @param ?array<string, Type\Union> $existing_class_constants
|
2018-05-10 20:12:50 +02:00
|
|
|
* @param string $fq_classlike_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return Type\Union|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
\Psalm\Codebase $codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
\Psalm\Internal\Provider\NodeDataProvider $nodes,
|
2017-07-25 22:11:02 +02:00
|
|
|
PhpParser\Node\Expr $stmt,
|
2018-06-28 03:53:25 +02:00
|
|
|
\Psalm\Aliases $aliases,
|
|
|
|
\Psalm\FileSource $file_source = null,
|
|
|
|
array $existing_class_constants = null,
|
2018-05-10 20:12:50 +02:00
|
|
|
$fq_classlike_name = null
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
2019-02-22 00:19:12 +01:00
|
|
|
$left = self::getSimpleType(
|
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2019-02-22 00:19:12 +01:00
|
|
|
$stmt->left,
|
|
|
|
$aliases,
|
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
$right = self::getSimpleType(
|
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2019-02-22 00:19:12 +01:00
|
|
|
$stmt->right,
|
|
|
|
$aliases,
|
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($left
|
|
|
|
&& $right
|
|
|
|
&& $left->isSingleStringLiteral()
|
|
|
|
&& $right->isSingleStringLiteral()
|
|
|
|
) {
|
|
|
|
$result = $left->getSingleStringLiteral()->value . $right->getSingleStringLiteral()->value;
|
|
|
|
|
|
|
|
if (strlen($result) < 50) {
|
|
|
|
return Type::getString($result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\NotEqual
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Smaller
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual
|
|
|
|
) {
|
|
|
|
return Type::getBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Coalesce) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Spaceship) {
|
|
|
|
return Type::getInt();
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_left_type = self::getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2017-07-25 22:11:02 +02:00
|
|
|
$stmt->left,
|
2018-06-28 03:53:25 +02:00
|
|
|
$aliases,
|
2018-05-10 20:12:50 +02:00
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
$stmt_right_type = self::getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2017-07-25 22:11:02 +02:00
|
|
|
$stmt->right,
|
2018-06-28 03:53:25 +02:00
|
|
|
$aliases,
|
2018-05-10 20:12:50 +02:00
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$stmt_left_type || !$stmt_right_type) {
|
2017-07-25 22:11:02 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes->setType(
|
|
|
|
$stmt->left,
|
|
|
|
$stmt_left_type
|
|
|
|
);
|
|
|
|
|
|
|
|
$nodes->setType(
|
|
|
|
$stmt->right,
|
|
|
|
$stmt_right_type
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Mod
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Pow
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
2019-03-25 02:22:52 +01:00
|
|
|
BinaryOpAnalyzer::analyzeNonDivArithmeticOp(
|
2018-05-10 20:12:50 +02:00
|
|
|
$file_source instanceof StatementsSource ? $file_source : null,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2017-07-25 22:11:02 +02:00
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type) {
|
|
|
|
return $result_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div
|
2019-11-25 17:44:54 +01:00
|
|
|
&& ($stmt_left_type->hasInt() || $stmt_left_type->hasFloat())
|
|
|
|
&& ($stmt_right_type->hasInt() || $stmt_right_type->hasFloat())
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
|
|
|
return Type::combineUnionTypes(Type::getFloat(), Type::getInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
2017-01-19 07:12:19 +01:00
|
|
|
if (strtolower($stmt->name->parts[0]) === 'false') {
|
|
|
|
return Type::getFalse();
|
|
|
|
} elseif (strtolower($stmt->name->parts[0]) === 'true') {
|
2019-01-21 22:55:50 +01:00
|
|
|
return Type::getTrue();
|
2017-01-13 18:26:10 +01:00
|
|
|
} elseif (strtolower($stmt->name->parts[0]) === 'null') {
|
|
|
|
return Type::getNull();
|
2019-02-22 00:19:12 +01:00
|
|
|
} elseif ($stmt->name->parts[0] === '__NAMESPACE__') {
|
|
|
|
return Type::getString($aliases->namespace);
|
2017-01-13 18:26:10 +01:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:19:12 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Namespace_) {
|
|
|
|
return Type::getString($aliases->namespace);
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name
|
2018-04-17 18:16:25 +02:00
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
2018-05-10 20:12:50 +02:00
|
|
|
&& $fq_classlike_name
|
|
|
|
&& $stmt->class->parts !== ['static']
|
|
|
|
&& $stmt->class->parts !== ['parent']
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
2018-06-28 03:53:25 +02:00
|
|
|
if (isset($existing_class_constants[$stmt->name->name])) {
|
|
|
|
if ($stmt->class->parts === ['self']) {
|
|
|
|
return clone $existing_class_constants[$stmt->name->name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:12:50 +02:00
|
|
|
if ($stmt->class->parts === ['self']) {
|
2018-06-28 03:53:25 +02:00
|
|
|
$const_fq_class_name = $fq_classlike_name;
|
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
$const_fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-06-28 03:53:25 +02:00
|
|
|
$stmt->class,
|
|
|
|
$aliases
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strtolower($const_fq_class_name) === strtolower($fq_classlike_name)
|
|
|
|
&& isset($existing_class_constants[$stmt->name->name])
|
|
|
|
) {
|
2018-05-10 20:12:50 +02:00
|
|
|
return clone $existing_class_constants[$stmt->name->name];
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if (strtolower($stmt->name->name) === 'class') {
|
2019-01-04 18:28:00 +01:00
|
|
|
return Type::getLiteralClassString($const_fq_class_name);
|
2018-06-28 03:53:25 +02:00
|
|
|
}
|
2018-05-10 20:12:50 +02:00
|
|
|
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($existing_class_constants === null
|
|
|
|
&& $file_source instanceof StatementsAnalyzer
|
|
|
|
) {
|
2018-06-28 03:53:25 +02:00
|
|
|
try {
|
2019-09-14 20:26:31 +02:00
|
|
|
$foreign_class_constant = $codebase->classlikes->getConstantForClass(
|
2018-06-28 03:53:25 +02:00
|
|
|
$const_fq_class_name,
|
2019-09-14 20:26:31 +02:00
|
|
|
$stmt->name->name,
|
|
|
|
\ReflectionProperty::IS_PRIVATE,
|
|
|
|
$file_source
|
2018-06-28 03:53:25 +02:00
|
|
|
);
|
|
|
|
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($foreign_class_constant) {
|
|
|
|
return clone $foreign_class_constant;
|
2018-06-28 03:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
return null;
|
2019-12-10 22:16:44 +01:00
|
|
|
} catch (\Psalm\Exception\CircularReferenceException $e) {
|
|
|
|
return null;
|
2018-06-28 03:53:25 +02:00
|
|
|
}
|
2018-05-10 20:12:50 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Identifier && strtolower($stmt->name->name) === 'class') {
|
2018-03-05 22:06:06 +01:00
|
|
|
return Type::getClassString();
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
return null;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
2018-05-18 17:02:50 +02:00
|
|
|
return Type::getString(strlen($stmt->value) < 30 ? $stmt->value : null);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
|
2018-05-18 17:02:50 +02:00
|
|
|
return Type::getInt(false, $stmt->value);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
|
2018-05-18 17:02:50 +02:00
|
|
|
return Type::getFloat($stmt->value);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Array_) {
|
2017-01-02 07:07:44 +01:00
|
|
|
if (count($stmt->items) === 0) {
|
|
|
|
return Type::getEmptyArray();
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:48:01 +01:00
|
|
|
$item_key_type = null;
|
|
|
|
$item_value_type = null;
|
|
|
|
|
|
|
|
$property_types = [];
|
2018-08-21 23:59:06 +02:00
|
|
|
$class_strings = [];
|
2017-12-19 15:48:01 +01:00
|
|
|
|
|
|
|
$can_create_objectlike = true;
|
|
|
|
|
2020-01-14 18:13:41 +01:00
|
|
|
$is_list = true;
|
|
|
|
|
2017-12-19 15:48:01 +01:00
|
|
|
foreach ($stmt->items as $int_offset => $item) {
|
2018-01-14 00:33:32 +01:00
|
|
|
if ($item === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:15:37 +01:00
|
|
|
$single_item_key_type = null;
|
|
|
|
|
2017-12-19 15:48:01 +01:00
|
|
|
if ($item->key) {
|
|
|
|
$single_item_key_type = self::getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2017-12-19 15:48:01 +01:00
|
|
|
$item->key,
|
2018-06-28 03:53:25 +02:00
|
|
|
$aliases,
|
2018-05-10 20:12:50 +02:00
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
2017-12-19 15:48:01 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($single_item_key_type) {
|
|
|
|
if ($item_key_type) {
|
2018-11-28 21:12:08 +01:00
|
|
|
$item_key_type = Type::combineUnionTypes(
|
|
|
|
$single_item_key_type,
|
|
|
|
$item_key_type,
|
2020-01-15 17:26:58 +01:00
|
|
|
null,
|
2018-11-28 21:12:08 +01:00
|
|
|
false,
|
2018-12-08 19:18:55 +01:00
|
|
|
true,
|
2018-11-28 21:12:08 +01:00
|
|
|
30
|
|
|
|
);
|
2017-12-19 15:48:01 +01:00
|
|
|
} else {
|
|
|
|
$item_key_type = $single_item_key_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$item_key_type = Type::getInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
$single_item_value_type = self::getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2017-12-19 15:48:01 +01:00
|
|
|
$item->value,
|
2018-06-28 03:53:25 +02:00
|
|
|
$aliases,
|
2018-05-10 20:12:50 +02:00
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
2017-12-19 15:48:01 +01:00
|
|
|
);
|
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if (!$single_item_value_type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $item->key instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|| !$item->key
|
|
|
|
) {
|
2018-11-10 23:15:37 +01:00
|
|
|
if (count($property_types) <= 50) {
|
|
|
|
$property_types[$item->key ? $item->key->value : $int_offset] = $single_item_value_type;
|
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
}
|
2020-01-14 18:13:41 +01:00
|
|
|
|
|
|
|
if ($item->key
|
|
|
|
&& (!$item->key instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|| $item->key->value !== $int_offset)
|
|
|
|
) {
|
|
|
|
$is_list = false;
|
|
|
|
}
|
2018-06-28 03:53:25 +02:00
|
|
|
} else {
|
2020-01-14 18:13:41 +01:00
|
|
|
$is_list = false;
|
2018-11-10 23:15:37 +01:00
|
|
|
$dim_type = $single_item_key_type;
|
2018-05-14 23:39:08 +02:00
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if (!$dim_type) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-14 23:39:08 +02:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
$dim_atomic_types = $dim_type->getAtomicTypes();
|
2018-05-14 23:39:08 +02:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if (count($dim_atomic_types) > 1 || $dim_type->hasMixed() || count($property_types) > 50) {
|
2018-06-28 03:53:25 +02:00
|
|
|
$can_create_objectlike = false;
|
|
|
|
} else {
|
|
|
|
$atomic_type = array_shift($dim_atomic_types);
|
|
|
|
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TLiteralInt
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TLiteralString
|
|
|
|
) {
|
2018-08-21 23:59:06 +02:00
|
|
|
if ($atomic_type instanceof Type\Atomic\TLiteralClassString) {
|
|
|
|
$class_strings[$atomic_type->value] = true;
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
$property_types[$atomic_type->value] = $single_item_value_type;
|
2018-05-14 23:39:08 +02:00
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
}
|
2017-12-19 15:48:01 +01:00
|
|
|
}
|
2018-06-28 03:53:25 +02:00
|
|
|
}
|
2017-12-19 15:48:01 +01:00
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if ($item_value_type) {
|
2018-11-28 21:12:08 +01:00
|
|
|
$item_value_type = Type::combineUnionTypes(
|
|
|
|
$single_item_value_type,
|
|
|
|
$item_value_type,
|
2020-01-15 17:26:58 +01:00
|
|
|
null,
|
2018-11-28 21:12:08 +01:00
|
|
|
false,
|
2018-12-08 19:18:55 +01:00
|
|
|
true,
|
2018-11-28 21:12:08 +01:00
|
|
|
30
|
|
|
|
);
|
2017-12-19 15:48:01 +01:00
|
|
|
} else {
|
2018-06-28 03:53:25 +02:00
|
|
|
$item_value_type = $single_item_value_type;
|
2017-12-19 15:48:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this array looks like an object-like array, let's return that instead
|
|
|
|
if ($item_value_type
|
|
|
|
&& $item_key_type
|
|
|
|
&& ($item_key_type->hasString() || $item_key_type->hasInt())
|
|
|
|
&& $can_create_objectlike
|
2020-01-30 05:41:17 +01:00
|
|
|
&& $property_types
|
2017-12-19 15:48:01 +01:00
|
|
|
) {
|
2018-11-10 23:15:37 +01:00
|
|
|
$objectlike = new Type\Atomic\ObjectLike($property_types, $class_strings);
|
|
|
|
$objectlike->sealed = true;
|
2020-01-14 18:13:41 +01:00
|
|
|
$objectlike->is_list = $is_list;
|
2018-11-10 23:15:37 +01:00
|
|
|
return new Type\Union([$objectlike]);
|
2017-12-19 15:48:01 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 03:53:25 +02:00
|
|
|
if (!$item_key_type || !$item_value_type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:48:01 +01:00
|
|
|
return new Type\Union([
|
2018-11-10 23:15:37 +01:00
|
|
|
new Type\Atomic\TNonEmptyArray([
|
2018-06-28 03:53:25 +02:00
|
|
|
$item_key_type,
|
|
|
|
$item_value_type,
|
2017-12-19 15:48:01 +01:00
|
|
|
]),
|
|
|
|
]);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getInt();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getFloat();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getBool();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getString();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getObject();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getArray();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\UnaryMinus || $stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
|
2018-05-13 00:46:47 +02:00
|
|
|
$type_to_invert = self::getSimpleType(
|
2018-06-28 03:53:25 +02:00
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
$nodes,
|
2018-05-13 00:46:47 +02:00
|
|
|
$stmt->expr,
|
2018-06-28 03:53:25 +02:00
|
|
|
$aliases,
|
2018-05-13 00:46:47 +02:00
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$type_to_invert) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($type_to_invert->getAtomicTypes() as $type_part) {
|
2018-05-13 00:46:47 +02:00
|
|
|
if ($type_part instanceof Type\Atomic\TLiteralInt
|
|
|
|
&& $stmt instanceof PhpParser\Node\Expr\UnaryMinus
|
|
|
|
) {
|
2018-05-18 17:02:50 +02:00
|
|
|
$type_part->value = -$type_part->value;
|
2018-05-13 00:46:47 +02:00
|
|
|
} elseif ($type_part instanceof Type\Atomic\TLiteralFloat
|
|
|
|
&& $stmt instanceof PhpParser\Node\Expr\UnaryMinus
|
|
|
|
) {
|
2018-05-18 17:02:50 +02:00
|
|
|
$type_part->value = -$type_part->value;
|
2018-05-13 00:46:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type_to_invert;
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
2016-10-30 17:46:18 +01:00
|
|
|
|
2020-02-21 21:56:30 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2020-02-21 22:04:18 +01:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $stmt->dim
|
|
|
|
) {
|
2020-02-21 21:56:30 +01:00
|
|
|
$array_type = self::getSimpleType(
|
|
|
|
$codebase,
|
|
|
|
$nodes,
|
|
|
|
$stmt->var,
|
|
|
|
$aliases,
|
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$dim_type = self::getSimpleType(
|
|
|
|
$codebase,
|
|
|
|
$nodes,
|
|
|
|
$stmt->dim,
|
|
|
|
$aliases,
|
|
|
|
$file_source,
|
|
|
|
$existing_class_constants,
|
|
|
|
$fq_classlike_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($array_type !== null && $dim_type !== null) {
|
|
|
|
if ($dim_type->isSingleStringLiteral()) {
|
|
|
|
$dim_value = $dim_type->getSingleStringLiteral()->value;
|
|
|
|
} elseif ($dim_type->isSingleIntLiteral()) {
|
|
|
|
$dim_value = $dim_type->getSingleIntLiteral()->value;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($array_type->getAtomicTypes() as $array_atomic_type) {
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
if (isset($array_atomic_type->properties[$dim_value])) {
|
|
|
|
return clone $array_atomic_type->properties[$dim_value];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 17:46:18 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Const_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function analyzeConstAssignment(PhpParser\Node\Stmt\Const_ $stmt, Context $context)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
|
|
|
foreach ($stmt->consts as $const) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ExpressionAnalyzer::analyze($this, $const->value, $context);
|
2016-10-19 03:54:08 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->setConstType(
|
2018-04-17 18:16:25 +02:00
|
|
|
$const->name->name,
|
2019-11-25 17:44:54 +01:00
|
|
|
$this->node_data->getType($const->value) ?: Type::getMixed(),
|
2017-01-02 21:31:18 +01:00
|
|
|
$context
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-08-11 23:36:22 +02:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-08-11 23:36:22 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $const_name
|
2016-11-21 03:49:06 +01:00
|
|
|
* @param bool $is_fully_qualified
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-05 02:14:04 +01:00
|
|
|
* @return Type\Union|null
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-07-28 16:42:30 +02:00
|
|
|
public function getConstType(
|
2019-09-14 20:26:31 +02:00
|
|
|
string $const_name,
|
|
|
|
bool $is_fully_qualified,
|
|
|
|
?Context $context
|
2017-07-28 16:42:30 +02:00
|
|
|
) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$aliased_constants = $this->getAliases()->constants;
|
2017-01-07 20:35:07 +01:00
|
|
|
|
|
|
|
if (isset($aliased_constants[$const_name])) {
|
|
|
|
$fq_const_name = $aliased_constants[$const_name];
|
|
|
|
} elseif ($is_fully_qualified) {
|
2016-11-21 03:49:06 +01:00
|
|
|
$fq_const_name = $const_name;
|
2019-01-20 15:52:26 +01:00
|
|
|
} else {
|
2018-02-04 18:23:32 +01:00
|
|
|
$fq_const_name = Type::getFQCLNFromString($const_name, $this->getAliases());
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($fq_const_name) {
|
|
|
|
$const_name_parts = explode('\\', $fq_const_name);
|
|
|
|
$const_name = array_pop($const_name_parts);
|
|
|
|
$namespace_name = implode('\\', $const_name_parts);
|
2018-11-06 03:57:36 +01:00
|
|
|
$namespace_constants = NamespaceAnalyzer::getConstantsForNamespace(
|
2017-01-07 20:35:07 +01:00
|
|
|
$namespace_name,
|
|
|
|
\ReflectionProperty::IS_PUBLIC
|
|
|
|
);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
|
|
|
if (isset($namespace_constants[$const_name])) {
|
|
|
|
return $namespace_constants[$const_name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($context && $context->hasVariable($fq_const_name, $this)) {
|
2019-03-03 22:43:24 +01:00
|
|
|
return $context->vars_in_scope[$fq_const_name];
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:45:35 +01:00
|
|
|
$file_path = $this->getRootFilePath();
|
|
|
|
$codebase = $this->getCodebase();
|
2017-07-28 16:42:30 +02:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$file_storage_provider = $codebase->file_storage_provider;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
|
|
$file_storage = $file_storage_provider->get($file_path);
|
2017-07-28 16:42:30 +02:00
|
|
|
|
|
|
|
if (isset($file_storage->declaring_constants[$const_name])) {
|
|
|
|
$constant_file_path = $file_storage->declaring_constants[$const_name];
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
return $file_storage_provider->get($constant_file_path)->constants[$const_name];
|
2017-07-28 16:42:30 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:19:12 +01:00
|
|
|
if (isset($file_storage->declaring_constants[$fq_const_name])) {
|
|
|
|
$constant_file_path = $file_storage->declaring_constants[$fq_const_name];
|
|
|
|
|
|
|
|
return $file_storage_provider->get($constant_file_path)->constants[$fq_const_name];
|
|
|
|
}
|
|
|
|
|
2019-03-25 05:07:32 +01:00
|
|
|
return ConstFetchAnalyzer::getGlobalConstType($codebase, $fq_const_name, $const_name)
|
|
|
|
?? ConstFetchAnalyzer::getGlobalConstType($codebase, $const_name, $const_name);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-08-10 00:10:46 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $const_name
|
|
|
|
* @param Type\Union $const_type
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public function setConstType($const_name, Type\Union $const_type, Context $context)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
2017-01-15 21:58:40 +01:00
|
|
|
$context->vars_in_scope[$const_name] = $const_type;
|
|
|
|
$context->constants[$const_name] = $const_type;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($this->source instanceof NamespaceAnalyzer) {
|
2016-11-21 03:49:06 +01:00
|
|
|
$this->source->setConstType($const_name, $const_type);
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2017-02-08 00:09:12 +01:00
|
|
|
* @param string $var_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-08 00:09:12 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasVariable($var_name)
|
|
|
|
{
|
|
|
|
return isset($this->all_vars[$var_name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-21 22:24:20 +01:00
|
|
|
* @param string $var_id
|
2017-02-08 00:09:12 +01:00
|
|
|
* @param CodeLocation $location
|
2018-01-21 22:24:20 +01:00
|
|
|
* @param int|null $branch_point
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-21 22:24:20 +01:00
|
|
|
public function registerVariable($var_id, CodeLocation $location, $branch_point)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2018-01-21 22:24:20 +01:00
|
|
|
$this->all_vars[$var_id] = $location;
|
|
|
|
|
|
|
|
if ($branch_point) {
|
|
|
|
$this->var_branch_points[$var_id] = $branch_point;
|
|
|
|
}
|
2018-01-28 23:28:34 +01:00
|
|
|
|
|
|
|
$this->registerVariableAssignment($var_id, $location);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $var_id
|
|
|
|
* @param CodeLocation $location
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function registerVariableAssignment($var_id, CodeLocation $location)
|
|
|
|
{
|
2018-06-17 02:01:33 +02:00
|
|
|
$this->unused_var_locations[$location->getHash()] = [$var_id, $location];
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-17 02:01:33 +02:00
|
|
|
* @param array<string, CodeLocation> $locations
|
2018-01-28 23:28:34 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-06-17 02:01:33 +02:00
|
|
|
public function registerVariableUses(array $locations)
|
2018-01-28 23:28:34 +01:00
|
|
|
{
|
2018-06-17 02:01:33 +02:00
|
|
|
foreach ($locations as $hash => $_) {
|
|
|
|
unset($this->unused_var_locations[$hash]);
|
|
|
|
$this->used_var_locations[$hash] = true;
|
|
|
|
}
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, array{0: string, 1: CodeLocation}>
|
|
|
|
*/
|
|
|
|
public function getUnusedVarLocations()
|
|
|
|
{
|
2020-03-14 01:28:32 +01:00
|
|
|
return \array_diff_key($this->unused_var_locations, $this->used_var_locations);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
|
|
|
* The first appearance of the variable in this set of statements being evaluated
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2018-01-21 22:24:20 +01:00
|
|
|
* @param string $var_id
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-08 00:09:12 +01:00
|
|
|
* @return CodeLocation|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2018-01-21 22:24:20 +01:00
|
|
|
public function getFirstAppearance($var_id)
|
|
|
|
{
|
|
|
|
return isset($this->all_vars[$var_id]) ? $this->all_vars[$var_id] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $var_id
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function getBranchPoint($var_id)
|
|
|
|
{
|
|
|
|
return isset($this->var_branch_points[$var_id]) ? $this->var_branch_points[$var_id] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $var_id
|
|
|
|
* @param int $branch_point
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addVariableInitialization($var_id, $branch_point)
|
2016-10-02 19:05:49 +02:00
|
|
|
{
|
2018-01-21 22:24:20 +01:00
|
|
|
$this->vars_to_initialize[$var_id] = $branch_point;
|
2016-10-02 19:05:49 +02:00
|
|
|
}
|
2016-10-21 00:16:17 +02:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
public function getFileAnalyzer() : FileAnalyzer
|
2017-07-29 21:05:06 +02:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
return $this->file_analyzer;
|
2017-07-29 21:05:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
public function getCodebase() : Codebase
|
|
|
|
{
|
|
|
|
return $this->codebase;
|
|
|
|
}
|
|
|
|
|
2017-08-12 01:05:04 +02:00
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @return array<string, FunctionAnalyzer>
|
2017-08-12 01:05:04 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function getFunctionAnalyzers()
|
2017-08-12 01:05:04 +02:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
return $this->function_analyzers;
|
2017-08-12 01:05:04 +02:00
|
|
|
}
|
2019-02-18 19:17:08 +01:00
|
|
|
|
|
|
|
/**
|
2019-02-18 19:51:27 +01:00
|
|
|
* @param PhpParser\Node\Expr $first_arg_value
|
2019-02-18 19:17:08 +01:00
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2019-11-25 17:44:54 +01:00
|
|
|
public static function getConstName(
|
|
|
|
$first_arg_value,
|
|
|
|
\Psalm\Internal\Provider\NodeDataProvider $type_provider,
|
|
|
|
Codebase $codebase,
|
|
|
|
Aliases $aliases
|
|
|
|
) {
|
2019-02-18 19:17:08 +01:00
|
|
|
$const_name = null;
|
|
|
|
|
|
|
|
if ($first_arg_value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$const_name = $first_arg_value->value;
|
2019-11-25 17:44:54 +01:00
|
|
|
} elseif ($first_arg_type = $type_provider->getType($first_arg_value)) {
|
|
|
|
if ($first_arg_type->isSingleStringLiteral()) {
|
|
|
|
$const_name = $first_arg_type->getSingleStringLiteral()->value;
|
2019-02-18 19:17:08 +01:00
|
|
|
}
|
2019-02-22 00:19:12 +01:00
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$simple_type = self::getSimpleType($codebase, $type_provider, $first_arg_value, $aliases);
|
2019-02-22 00:19:12 +01:00
|
|
|
if ($simple_type && $simple_type->isSingleStringLiteral()) {
|
|
|
|
$const_name = $simple_type->getSingleStringLiteral()->value;
|
2019-02-18 19:17:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $const_name;
|
|
|
|
}
|
2019-03-06 00:08:41 +01:00
|
|
|
|
2019-03-14 01:15:29 +01:00
|
|
|
public function isSuperGlobal(string $var_id) : bool
|
|
|
|
{
|
|
|
|
return in_array(
|
|
|
|
$var_id,
|
|
|
|
[
|
|
|
|
'$GLOBALS',
|
|
|
|
'$_SERVER',
|
|
|
|
'$_GET',
|
|
|
|
'$_POST',
|
|
|
|
'$_FILES',
|
|
|
|
'$_COOKIE',
|
|
|
|
'$_SESSION',
|
|
|
|
'$_REQUEST',
|
|
|
|
'$_ENV',
|
|
|
|
'$http_response_header'
|
|
|
|
],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGlobalType(string $var_id) : Type\Union
|
2019-03-06 00:08:41 +01:00
|
|
|
{
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2019-03-14 01:15:29 +01:00
|
|
|
if (isset($config->globals[$var_id])) {
|
|
|
|
return Type::parseString($config->globals[$var_id]);
|
2019-03-06 00:08:41 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 01:15:29 +01:00
|
|
|
if ($var_id === '$argv') {
|
2019-03-06 00:08:41 +01:00
|
|
|
return new Type\Union([
|
|
|
|
new Type\Atomic\TArray([Type::getInt(), Type::getString()]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-03-14 01:15:29 +01:00
|
|
|
if ($var_id === '$argc') {
|
2019-03-06 00:08:41 +01:00
|
|
|
return Type::getInt();
|
|
|
|
}
|
2019-03-14 01:15:29 +01:00
|
|
|
|
|
|
|
if ($this->isSuperGlobal($var_id)) {
|
2020-01-14 06:53:38 +01:00
|
|
|
$type = Type::getArray();
|
|
|
|
|
|
|
|
return $type;
|
2019-03-14 01:15:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Type::getMixed();
|
2019-03-06 00:08:41 +01:00
|
|
|
}
|
2019-03-10 21:36:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, bool> $byref_uses
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setByRefUses(array $byref_uses)
|
|
|
|
{
|
|
|
|
$this->byref_uses = $byref_uses;
|
|
|
|
}
|
2019-03-24 21:17:14 +01:00
|
|
|
|
|
|
|
/**
|
2019-04-03 01:42:23 +02:00
|
|
|
* @return array<string, array<array-key, CodeLocation>>
|
2019-03-24 21:17:14 +01:00
|
|
|
*/
|
|
|
|
public function getUncaughtThrows(Context $context)
|
|
|
|
{
|
|
|
|
$uncaught_throws = [];
|
|
|
|
|
|
|
|
if ($context->collect_exceptions) {
|
|
|
|
if ($context->possibly_thrown_exceptions) {
|
2019-04-10 16:49:27 +02:00
|
|
|
$config = $this->codebase->config;
|
2019-03-24 21:17:14 +01:00
|
|
|
$ignored_exceptions = array_change_key_case(
|
2019-04-10 16:49:27 +02:00
|
|
|
$context->is_global ?
|
|
|
|
$config->ignored_exceptions_in_global_scope :
|
|
|
|
$config->ignored_exceptions
|
2019-03-24 21:17:14 +01:00
|
|
|
);
|
|
|
|
$ignored_exceptions_and_descendants = array_change_key_case(
|
2019-04-10 16:49:27 +02:00
|
|
|
$context->is_global ?
|
|
|
|
$config->ignored_exceptions_and_descendants_in_global_scope :
|
|
|
|
$config->ignored_exceptions_and_descendants
|
2019-03-24 21:17:14 +01:00
|
|
|
);
|
|
|
|
|
2019-03-29 00:43:14 +01:00
|
|
|
foreach ($context->possibly_thrown_exceptions as $possibly_thrown_exception => $codelocations) {
|
2019-03-24 21:17:14 +01:00
|
|
|
if (isset($ignored_exceptions[strtolower($possibly_thrown_exception)])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_expected = false;
|
|
|
|
|
|
|
|
foreach ($ignored_exceptions_and_descendants as $expected_exception => $_) {
|
2020-01-12 17:33:38 +01:00
|
|
|
if ($expected_exception === strtolower($possibly_thrown_exception)
|
2019-03-24 21:17:14 +01:00
|
|
|
|| $this->codebase->classExtends($possibly_thrown_exception, $expected_exception)
|
|
|
|
) {
|
|
|
|
$is_expected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$is_expected) {
|
2019-03-29 00:43:14 +01:00
|
|
|
$uncaught_throws[$possibly_thrown_exception] = $codelocations;
|
2019-03-24 21:17:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uncaught_throws;
|
|
|
|
}
|
2019-06-26 22:26:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array{description:string, specials:array<string, array<int, string>>}|null
|
|
|
|
*/
|
|
|
|
public function getParsedDocblock() : ?array
|
|
|
|
{
|
|
|
|
return $this->parsed_docblock;
|
|
|
|
}
|
2019-08-18 17:18:02 +02:00
|
|
|
|
|
|
|
public function getFQCLN()
|
|
|
|
{
|
|
|
|
if ($this->fake_this_class) {
|
|
|
|
return $this->fake_this_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getFQCLN();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFQCLN(string $fake_this_class) : void
|
|
|
|
{
|
|
|
|
$this->fake_this_class = $fake_this_class;
|
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
public function getNodeTypeProvider() : \Psalm\NodeTypeProvider
|
|
|
|
{
|
|
|
|
return $this->node_data;
|
|
|
|
}
|
2016-01-20 16:57:56 +01:00
|
|
|
}
|