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;
|
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;
|
2020-07-26 19:23:21 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\AssignmentAnalyzer;
|
2020-06-19 07:22:51 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\InstancePropertyAssignmentAnalyzer;
|
2020-05-19 18:56:23 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ClassConstFetchAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
|
2020-05-19 18:56:23 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\VariableFetchAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\SimpleTypeInferer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ReturnAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ThrowAnalyzer;
|
2020-05-29 04:14:41 +02:00
|
|
|
use Psalm\Internal\Scanner\ParsedDocblock;
|
2020-10-13 22:49:03 +02:00
|
|
|
use Psalm\Internal\Codebase\DataFlowGraph;
|
2020-09-25 06:37:40 +02:00
|
|
|
use Psalm\Internal\Codebase\TaintFlowGraph;
|
2020-09-30 18:28:13 +02:00
|
|
|
use Psalm\Internal\Codebase\VariableUseGraph;
|
2020-10-13 22:49:03 +02:00
|
|
|
use Psalm\Internal\DataFlow\DataFlowNode;
|
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\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;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2020-07-26 19:23:21 +02:00
|
|
|
use Psalm\Issue\MissingDocblockType;
|
2020-04-09 03:03:05 +02:00
|
|
|
use Psalm\Issue\Trace;
|
|
|
|
use Psalm\Issue\UndefinedTrace;
|
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;
|
2020-04-06 18:42:22 +02:00
|
|
|
use function array_merge;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function preg_split;
|
|
|
|
use function get_class;
|
|
|
|
use function strrpos;
|
|
|
|
use function strlen;
|
|
|
|
use function substr;
|
|
|
|
use function array_change_key_case;
|
2019-07-24 22:48:54 +02:00
|
|
|
use function trim;
|
|
|
|
use function array_column;
|
|
|
|
use function array_combine;
|
2020-09-14 03:45:07 +02:00
|
|
|
use function array_keys;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-10-15 19:23:35 +02:00
|
|
|
class StatementsAnalyzer extends SourceAnalyzer
|
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 = [];
|
|
|
|
|
2019-03-10 21:36:35 +01:00
|
|
|
/**
|
|
|
|
* @var ?array<string, bool>
|
|
|
|
*/
|
2020-05-19 18:56:23 +02:00
|
|
|
public $byref_uses;
|
2019-03-10 21:36:35 +01:00
|
|
|
|
2019-06-26 22:26:14 +02:00
|
|
|
/**
|
2020-05-29 04:14:41 +02:00
|
|
|
* @var ParsedDocblock|null
|
2019-06-26 22:26:14 +02:00
|
|
|
*/
|
|
|
|
private $parsed_docblock = null;
|
|
|
|
|
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;
|
2020-09-12 23:03:11 +02:00
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
/** @var ?DataFlowGraph */
|
2020-10-13 23:28:12 +02:00
|
|
|
public $data_flow_graph;
|
2020-09-21 00:42:21 +02: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;
|
2020-09-25 06:37:40 +02:00
|
|
|
|
|
|
|
if ($this->codebase->taint_flow_graph) {
|
2020-10-13 23:28:12 +02:00
|
|
|
$this->data_flow_graph = new TaintFlowGraph();
|
2020-09-25 06:37:40 +02:00
|
|
|
} elseif ($this->codebase->find_unused_variables) {
|
2020-10-13 23:28:12 +02:00
|
|
|
$this->data_flow_graph = new VariableUseGraph();
|
2020-09-25 06:37:40 +02:00
|
|
|
}
|
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
|
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,
|
2020-09-07 01:36:47 +02:00
|
|
|
?Context $global_context = null,
|
|
|
|
bool $root_scope = false
|
2020-09-13 22:39:06 +02:00
|
|
|
): ?bool {
|
2020-07-30 21:30:19 +02:00
|
|
|
if (!$stmts) {
|
2020-09-13 22:39:06 +02:00
|
|
|
return null;
|
2020-07-30 21:30:19 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 06:58:30 +02:00
|
|
|
// hoist functions to the top
|
2020-09-30 18:28:13 +02:00
|
|
|
$this->hoistFunctions($stmts, $context);
|
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) {
|
2020-05-19 18:56:23 +02:00
|
|
|
self::hoistConstants($this, $stmts, $context);
|
2017-12-03 00:28:18 +01:00
|
|
|
}
|
|
|
|
|
2017-11-07 20:46:53 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
2020-05-19 18:56:23 +02:00
|
|
|
if (self::analyzeStatement($this, $stmt, $context, $global_context) === false) {
|
|
|
|
return false;
|
2020-04-06 18:42:22 +02:00
|
|
|
}
|
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
|
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
|
|
|
|
) {
|
2020-10-13 23:28:12 +02:00
|
|
|
//var_dump($this->data_flow_graph);
|
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
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 01:37:25 +02:00
|
|
|
if ($root_scope
|
2020-10-13 23:28:12 +02:00
|
|
|
&& $this->data_flow_graph instanceof TaintFlowGraph
|
2020-09-25 06:37:40 +02:00
|
|
|
&& $this->codebase->taint_flow_graph
|
2020-09-21 01:37:25 +02:00
|
|
|
&& $codebase->config->trackTaintsInPath($this->getFilePath())
|
|
|
|
) {
|
2020-10-13 23:28:12 +02:00
|
|
|
$this->codebase->taint_flow_graph->addGraph($this->data_flow_graph);
|
2020-09-21 01:26:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 22:48:54 +02:00
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Stmt> $stmts
|
2018-12-20 22:03:21 +01:00
|
|
|
*/
|
2020-09-30 18:28:13 +02:00
|
|
|
private function hoistFunctions(array $stmts, Context $context) : void
|
2018-12-20 22:03:21 +01:00
|
|
|
{
|
2020-05-19 18:56:23 +02:00
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
$function_name = strtolower($stmt->name->name);
|
2018-12-20 22:03:21 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($ns = $this->getNamespace()) {
|
|
|
|
$fq_function_name = strtolower($ns) . '\\' . $function_name;
|
|
|
|
} else {
|
|
|
|
$fq_function_name = $function_name;
|
2018-12-20 22:03:21 +01:00
|
|
|
}
|
2019-03-28 05:16:35 +01:00
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
if ($this->data_flow_graph
|
2020-09-30 18:28:13 +02:00
|
|
|
&& $this->codebase->find_unused_variables
|
|
|
|
) {
|
|
|
|
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
|
|
|
|
|| !\is_string($var->name)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
if ($var_id !== '$argv' && $var_id !== '$argc') {
|
|
|
|
$context->byref_constraints[$var_id] = new \Psalm\Internal\ReferenceConstraint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 05:10:08 +01:00
|
|
|
try {
|
2020-05-19 18:56:23 +02:00
|
|
|
$function_analyzer = new FunctionAnalyzer($stmt, $this->source);
|
|
|
|
$this->function_analyzers[$fq_function_name] = $function_analyzer;
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
// do nothing
|
2018-02-27 01:32:26 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2020-05-19 18:56:23 +02:00
|
|
|
* @param array<PhpParser\Node\Stmt> $stmts
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2020-05-19 18:56:23 +02:00
|
|
|
private static function hoistConstants(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
array $stmts,
|
|
|
|
Context $context
|
|
|
|
) : void {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2019-02-22 00:19:12 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Const_) {
|
|
|
|
foreach ($stmt->consts as $const) {
|
|
|
|
ConstFetchAnalyzer::setConstType(
|
|
|
|
$statements_analyzer,
|
|
|
|
$const->name->name,
|
|
|
|
SimpleTypeInferer::infer(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
$const->value,
|
|
|
|
$statements_analyzer->getAliases(),
|
|
|
|
$statements_analyzer
|
|
|
|
) ?: Type::getMixed(),
|
|
|
|
$context
|
|
|
|
);
|
2019-02-22 00:19:12 +01:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
} 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])
|
2017-07-25 22:11:02 +02:00
|
|
|
) {
|
2020-05-19 18:56:23 +02:00
|
|
|
$const_name = ConstFetchAnalyzer::getConstName(
|
|
|
|
$stmt->expr->args[0]->value,
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer->getAliases()
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($const_name !== null) {
|
|
|
|
ConstFetchAnalyzer::setConstType(
|
|
|
|
$statements_analyzer,
|
|
|
|
$const_name,
|
|
|
|
Statements\Expression\SimpleTypeInferer::infer(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
$stmt->expr->args[1]->value,
|
|
|
|
$statements_analyzer->getAliases(),
|
|
|
|
$statements_analyzer
|
|
|
|
) ?: Type::getMixed(),
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
/**
|
2020-10-17 18:36:44 +02:00
|
|
|
* @return false|null
|
2020-05-19 18:56:23 +02:00
|
|
|
*/
|
|
|
|
private static function analyzeStatement(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Stmt $stmt,
|
|
|
|
Context $context,
|
|
|
|
?Context $global_context
|
2020-09-13 22:39:06 +02:00
|
|
|
): ?bool {
|
2020-05-19 18:56:23 +02:00
|
|
|
$ignore_variable_property = false;
|
|
|
|
$ignore_variable_method = false;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($context->has_returned
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& !($stmt instanceof PhpParser\Node\Stmt\Nop)
|
|
|
|
&& !($stmt instanceof PhpParser\Node\Stmt\InlineHTML)
|
|
|
|
) {
|
|
|
|
if ($codebase->find_unused_variables) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnevaluatedCode(
|
|
|
|
'Expressions after return/throw/continue',
|
|
|
|
new CodeLocation($statements_analyzer->source, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
return null;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($statements_analyzer->getProjectAnalyzer()->debug_lines) {
|
|
|
|
fwrite(STDERR, $statements_analyzer->getFilePath() . ':' . $stmt->getLine() . "\n");
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
/*
|
|
|
|
if (isset($context->vars_in_scope['$array']) && !$stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
var_dump($stmt->getLine(), $context->vars_in_scope['$array']);
|
2019-02-22 00:19:12 +01:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
*/
|
2019-02-22 00:19:12 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$new_issues = null;
|
|
|
|
$traced_variables = [];
|
2018-05-10 20:12:50 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($docblock = $stmt->getDocComment()) {
|
|
|
|
$statements_analyzer->parseStatementDocblock($docblock, $stmt, $context);
|
2018-05-10 20:12:50 +02:00
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
if (isset($statements_analyzer->parsed_docblock->tags['psalm-trace'])) {
|
|
|
|
foreach ($statements_analyzer->parsed_docblock->tags['psalm-trace'] as $traced_variable_line) {
|
2020-05-19 18:56:23 +02:00
|
|
|
$possible_traced_variable_names = preg_split('/[\s]+/', $traced_variable_line);
|
|
|
|
if ($possible_traced_variable_names) {
|
|
|
|
$traced_variables = array_merge(
|
|
|
|
$traced_variables,
|
|
|
|
array_filter($possible_traced_variable_names)
|
2018-06-28 03:53:25 +02:00
|
|
|
);
|
|
|
|
}
|
2018-05-10 20:12:50 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
if (isset($statements_analyzer->parsed_docblock->tags['psalm-ignore-variable-method'])) {
|
2020-05-19 18:56:23 +02:00
|
|
|
$context->ignore_variable_method = $ignore_variable_method = true;
|
2018-03-05 22:06:06 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
if (isset($statements_analyzer->parsed_docblock->tags['psalm-ignore-variable-property'])) {
|
2020-05-19 18:56:23 +02:00
|
|
|
$context->ignore_variable_property = $ignore_variable_property = true;
|
2017-01-02 07:07:44 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
if (isset($statements_analyzer->parsed_docblock->tags['psalm-suppress'])) {
|
2020-09-13 22:41:14 +02:00
|
|
|
$suppressed = $statements_analyzer->parsed_docblock->tags['psalm-suppress'];
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($suppressed) {
|
|
|
|
$new_issues = [];
|
2020-01-14 18:13:41 +01:00
|
|
|
|
2020-09-13 22:41:14 +02:00
|
|
|
foreach ($suppressed as $offset => $suppress_entry) {
|
|
|
|
foreach (DocComment::parseSuppressList($suppress_entry) as $issue_offset => $issue_type) {
|
2020-09-20 14:56:49 +02:00
|
|
|
$new_issues[$issue_offset + $offset + $docblock->getStartFilePos()] = $issue_type;
|
2020-09-13 22:41:14 +02:00
|
|
|
|
|
|
|
if ($issue_type === 'InaccessibleMethod') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($codebase->track_unused_suppressions) {
|
|
|
|
IssueBuffer::addUnusedSuppression(
|
|
|
|
$statements_analyzer->getFilePath(),
|
2020-09-20 14:56:49 +02:00
|
|
|
$issue_offset + $offset + $docblock->getStartFilePos(),
|
2020-09-13 22:41:14 +02:00
|
|
|
$issue_type
|
|
|
|
);
|
|
|
|
}
|
2017-12-19 15:48:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$statements_analyzer->addSuppressedIssues($new_issues);
|
2018-06-28 03:53:25 +02:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
}
|
2020-07-26 19:23:21 +02:00
|
|
|
|
|
|
|
if (isset($statements_analyzer->parsed_docblock->combined_tags['var'])
|
|
|
|
&& !($stmt instanceof PhpParser\Node\Stmt\Expression
|
|
|
|
&& $stmt->expr instanceof PhpParser\Node\Expr\Assign)
|
|
|
|
&& !$stmt instanceof PhpParser\Node\Stmt\Foreach_
|
|
|
|
&& !$stmt instanceof PhpParser\Node\Stmt\Return_
|
|
|
|
) {
|
|
|
|
$file_path = $statements_analyzer->getRootFilePath();
|
|
|
|
|
|
|
|
$file_storage_provider = $codebase->file_storage_provider;
|
|
|
|
|
|
|
|
$file_storage = $file_storage_provider->get($file_path);
|
|
|
|
|
|
|
|
$template_type_map = $statements_analyzer->getTemplateTypeMap();
|
|
|
|
|
|
|
|
$var_comments = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$var_comments = CommentAnalyzer::arrayToDocblocks(
|
|
|
|
$docblock,
|
|
|
|
$statements_analyzer->parsed_docblock,
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
$statements_analyzer->getAliases(),
|
|
|
|
$template_type_map,
|
|
|
|
$file_storage->type_aliases
|
|
|
|
);
|
|
|
|
} catch (\Psalm\Exception\IncorrectDocblockException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingDocblockType(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} catch (\Psalm\Exception\DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($var_comments as $var_comment) {
|
|
|
|
AssignmentAnalyzer::assignTypeFromVarDocblock(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$var_comment,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
} else {
|
|
|
|
$statements_analyzer->parsed_docblock = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
|
|
|
if (IfAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
|
|
|
|
if (TryAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
|
|
|
|
if (ForAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
|
|
|
|
if (ForeachAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\While_) {
|
|
|
|
if (WhileAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_) {
|
|
|
|
DoAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Const_) {
|
|
|
|
ConstFetchAnalyzer::analyzeConstAssignment($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Unset_) {
|
|
|
|
Statements\UnsetAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Return_) {
|
|
|
|
ReturnAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
$context->has_returned = true;
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Throw_) {
|
|
|
|
ThrowAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
$context->has_returned = true;
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
|
|
|
SwitchAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Break_) {
|
|
|
|
Statements\BreakAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Continue_) {
|
|
|
|
Statements\ContinueAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Static_) {
|
|
|
|
Statements\StaticAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
|
|
|
|
if (Statements\EchoAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
FunctionAnalyzer::analyzeStatement($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Expression) {
|
|
|
|
if (ExpressionAnalyzer::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->expr,
|
|
|
|
$context,
|
|
|
|
false,
|
|
|
|
$global_context,
|
|
|
|
true
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\InlineHTML) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Global_) {
|
|
|
|
Statements\GlobalAnalyzer::analyze($statements_analyzer, $stmt, $context, $global_context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
2020-06-19 07:22:51 +02:00
|
|
|
InstancePropertyAssignmentAnalyzer::analyzeStatement($statements_analyzer, $stmt, $context);
|
2020-05-19 18:56:23 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
|
|
|
ClassConstFetchAnalyzer::analyzeClassConstAssignment($statements_analyzer, $stmt, $context);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
try {
|
|
|
|
$class_analyzer = new ClassAnalyzer(
|
|
|
|
$stmt,
|
|
|
|
$statements_analyzer->source,
|
|
|
|
$stmt->name ? $stmt->name->name : null
|
|
|
|
);
|
2018-06-28 03:53:25 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$class_analyzer->analyze(null, $global_context);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// disregard this exception, we'll likely see it elsewhere in the form
|
|
|
|
// of an issue
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
2020-07-26 19:23:21 +02:00
|
|
|
// do nothing
|
2020-05-19 18:56:23 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Goto_) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Label) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Declare_) {
|
|
|
|
foreach ($stmt->declares as $declaration) {
|
|
|
|
if ((string) $declaration->key === 'strict_types'
|
|
|
|
&& $declaration->value instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
&& $declaration->value->value === 1
|
2018-06-28 03:53:25 +02:00
|
|
|
) {
|
2020-05-19 18:56:23 +02:00
|
|
|
$context->strict_types = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\HaltCompiler) {
|
|
|
|
$context->has_returned = true;
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnrecognizedStatement(
|
|
|
|
'Psalm does not understand ' . get_class($stmt),
|
|
|
|
new CodeLocation($statements_analyzer->source, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 23:39:08 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-06-28 03:53:25 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$plugin_classes = $codebase->config->after_statement_checks;
|
2018-08-21 23:59:06 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($plugin_classes) {
|
|
|
|
$file_manipulations = [];
|
2017-12-19 15:48:01 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
foreach ($plugin_classes as $plugin_fq_class_name) {
|
|
|
|
if ($plugin_fq_class_name::afterStatementAnalysis(
|
|
|
|
$stmt,
|
|
|
|
$context,
|
|
|
|
$statements_analyzer,
|
|
|
|
$codebase,
|
|
|
|
$file_manipulations
|
|
|
|
) === false) {
|
|
|
|
return false;
|
2017-12-19 15:48:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($file_manipulations) {
|
|
|
|
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
|
2018-06-28 03:53:25 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($new_issues) {
|
|
|
|
$statements_analyzer->removeSuppressedIssues($new_issues);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($ignore_variable_property) {
|
|
|
|
$context->ignore_variable_property = false;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($ignore_variable_method) {
|
|
|
|
$context->ignore_variable_method = false;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
foreach ($traced_variables as $traced_variable) {
|
|
|
|
if (isset($context->vars_in_scope[$traced_variable])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new Trace(
|
|
|
|
$traced_variable . ': ' . $context->vars_in_scope[$traced_variable]->getId(),
|
|
|
|
new CodeLocation($statements_analyzer->source, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedTrace(
|
|
|
|
'Attempt to trace undefined variable ' . $traced_variable,
|
|
|
|
new CodeLocation($statements_analyzer->source, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2020-05-19 18:56:23 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
private function parseStatementDocblock(
|
|
|
|
PhpParser\Comment\Doc $docblock,
|
|
|
|
PhpParser\Node\Stmt $stmt,
|
|
|
|
Context $context
|
|
|
|
) : void {
|
|
|
|
$codebase = $this->getCodebase();
|
2018-05-13 00:46:47 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
try {
|
|
|
|
$this->parsed_docblock = DocComment::parsePreservingLength($docblock);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->getSource(), $stmt, null, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
2018-05-13 00:46:47 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$this->parsed_docblock = null;
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
2016-10-30 17:46:18 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$comments = $this->parsed_docblock;
|
2020-02-21 21:56:30 +01:00
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
if (isset($comments->tags['psalm-scope-this'])) {
|
|
|
|
$trimmed = trim(\reset($comments->tags['psalm-scope-this']));
|
2020-02-21 21:56:30 +01:00
|
|
|
|
2020-05-19 18:56:23 +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
|
2020-02-21 21:56:30 +01:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
} else {
|
|
|
|
$this_type = Type::parseString($trimmed);
|
|
|
|
$context->self = $trimmed;
|
|
|
|
$context->vars_in_scope['$this'] = $this_type;
|
|
|
|
$this->setFQCLN($trimmed);
|
2020-02-21 21:56:30 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2020-05-19 18:56:23 +02:00
|
|
|
* @param array<PhpParser\Node\Stmt> $stmts
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function checkUnreferencedVars(array $stmts): void
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
2020-05-19 18:56:23 +02:00
|
|
|
$source = $this->getSource();
|
|
|
|
$codebase = $source->getCodebase();
|
|
|
|
$function_storage = $source instanceof FunctionLikeAnalyzer ? $source->getFunctionLikeStorage($this) : null;
|
|
|
|
$var_list = array_column($this->unused_var_locations, 0);
|
|
|
|
$loc_list = array_column($this->unused_var_locations, 1);
|
2016-08-11 23:36:22 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$project_analyzer = $this->getProjectAnalyzer();
|
2017-01-07 20:35:07 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
$unused_var_remover = new Statements\UnusedAssignmentRemover();
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
foreach ($this->unused_var_locations as [$var_id, $original_location]) {
|
|
|
|
if (substr($var_id, 0, 2) === '$_') {
|
2020-05-19 18:56:23 +02:00
|
|
|
continue;
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
2017-07-28 16:42:30 +02:00
|
|
|
|
2020-09-12 23:03:11 +02:00
|
|
|
if ($function_storage) {
|
|
|
|
$param_index = \array_search(substr($var_id, 1), array_keys($function_storage->param_lookup));
|
|
|
|
if ($param_index !== false) {
|
|
|
|
$param = $function_storage->params[$param_index];
|
|
|
|
|
2020-09-13 22:38:32 +02:00
|
|
|
if ($param->location
|
|
|
|
&& ($original_location->raw_file_end === $param->location->raw_file_end
|
|
|
|
|| $param->by_ref)
|
|
|
|
) {
|
2020-09-12 23:03:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
$assignment_node = DataFlowNode::getForAssignment($var_id, $original_location);
|
2020-09-30 18:28:13 +02:00
|
|
|
|
2020-09-12 23:03:11 +02:00
|
|
|
if (!isset($this->byref_uses[$var_id])
|
2020-05-19 18:56:23 +02:00
|
|
|
&& !VariableFetchAnalyzer::isSuperGlobal($var_id)
|
2020-10-13 23:28:12 +02:00
|
|
|
&& $this->data_flow_graph instanceof VariableUseGraph
|
|
|
|
&& !$this->data_flow_graph->isVariableUsed($assignment_node)
|
2020-05-19 18:56:23 +02:00
|
|
|
) {
|
|
|
|
$issue = new UnusedVariable(
|
|
|
|
'Variable ' . $var_id . ' is never referenced',
|
|
|
|
$original_location
|
|
|
|
);
|
2016-08-10 00:10:46 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($codebase->alter_code
|
|
|
|
&& !$unused_var_remover->checkIfVarRemoved($var_id, $original_location)
|
|
|
|
&& isset($project_analyzer->getIssuesToFix()['UnusedVariable'])
|
|
|
|
&& !IssueBuffer::isSuppressed($issue, $this->getSuppressedIssues())
|
|
|
|
) {
|
|
|
|
$unused_var_remover->findUnusedAssignment(
|
|
|
|
$this->getCodebase(),
|
|
|
|
$stmts,
|
|
|
|
array_combine($var_list, $loc_list),
|
|
|
|
$var_id,
|
|
|
|
$original_location
|
|
|
|
);
|
|
|
|
}
|
2017-01-15 21:58:40 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
$issue,
|
|
|
|
$this->getSuppressedIssues(),
|
|
|
|
true
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function hasVariable(string $var_name): bool
|
2017-02-08 00:09:12 +01:00
|
|
|
{
|
|
|
|
return isset($this->all_vars[$var_name]);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function registerVariable(string $var_id, CodeLocation $location, ?int $branch_point): void
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function registerVariableAssignment(string $var_id, CodeLocation $location): void
|
2018-01-28 23:28:34 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-30 18:28:13 +02:00
|
|
|
* @return array<string, array{0: string, 1: CodeLocation}>
|
2018-01-28 23:28:34 +01:00
|
|
|
*/
|
2020-09-30 18:28:13 +02:00
|
|
|
public function getUnusedVarLocations(): array
|
2018-01-28 23:28:34 +01:00
|
|
|
{
|
2020-09-30 18:28:13 +02:00
|
|
|
return $this->unused_var_locations;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerPossiblyUndefinedVariable(
|
|
|
|
string $undefined_var_id,
|
|
|
|
PhpParser\Node\Expr\Variable $stmt
|
|
|
|
) : void {
|
2020-10-13 23:28:12 +02:00
|
|
|
if (!$this->data_flow_graph) {
|
2020-09-30 18:28:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$use_location = new CodeLocation($this->getSource(), $stmt);
|
2020-10-13 22:49:03 +02:00
|
|
|
$use_node = DataFlowNode::getForAssignment($undefined_var_id, $use_location);
|
2020-09-30 18:28:13 +02:00
|
|
|
|
|
|
|
$stmt_type = $this->node_data->getType($stmt);
|
|
|
|
|
|
|
|
if ($stmt_type) {
|
|
|
|
$stmt_type->parent_nodes[$use_node->id] = $use_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->unused_var_locations as [$var_id, $original_location]) {
|
|
|
|
if ($var_id === $undefined_var_id) {
|
2020-10-13 22:49:03 +02:00
|
|
|
$parent_node = DataFlowNode::getForAssignment($var_id, $original_location);
|
2020-09-30 18:28:13 +02:00
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
$this->data_flow_graph->addPath($parent_node, $use_node, '=');
|
2020-09-30 18:28:13 +02:00
|
|
|
}
|
2018-06-17 02:01:33 +02:00
|
|
|
}
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-13 22:49:03 +02:00
|
|
|
* @return array<string, DataFlowNode>
|
2018-01-28 23:28:34 +01:00
|
|
|
*/
|
2020-09-30 18:28:13 +02:00
|
|
|
public function getParentNodesForPossiblyUndefinedVariable(string $undefined_var_id) : array
|
2018-01-28 23:28:34 +01:00
|
|
|
{
|
2020-10-13 23:28:12 +02:00
|
|
|
if (!$this->data_flow_graph) {
|
2020-09-30 18:28:13 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$parent_nodes = [];
|
|
|
|
|
|
|
|
foreach ($this->unused_var_locations as [$var_id, $original_location]) {
|
|
|
|
if ($var_id === $undefined_var_id) {
|
2020-10-13 22:49:03 +02:00
|
|
|
$assignment_node = DataFlowNode::getForAssignment($var_id, $original_location);
|
2020-09-30 18:28:13 +02:00
|
|
|
$parent_nodes[$assignment_node->id] = $assignment_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parent_nodes;
|
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
|
|
|
|
*/
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getFirstAppearance(string $var_id): ?CodeLocation
|
2018-01-21 22:24:20 +01:00
|
|
|
{
|
|
|
|
return isset($this->all_vars[$var_id]) ? $this->all_vars[$var_id] : null;
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getBranchPoint(string $var_id): ?int
|
2018-01-21 22:24:20 +01:00
|
|
|
{
|
|
|
|
return isset($this->var_branch_points[$var_id]) ? $this->var_branch_points[$var_id] : null;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function addVariableInitialization(string $var_id, int $branch_point): void
|
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
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getFunctionAnalyzers(): array
|
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-03-10 21:36:35 +01:00
|
|
|
/**
|
|
|
|
* @param array<string, bool> $byref_uses
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function setByRefUses(array $byref_uses): void
|
2019-03-10 21:36:35 +01:00
|
|
|
{
|
|
|
|
$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
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getUncaughtThrows(Context $context): array
|
2019-03-24 21:17:14 +01:00
|
|
|
{
|
|
|
|
$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-07-07 06:29:46 +02:00
|
|
|
try {
|
|
|
|
if ($expected_exception === strtolower($possibly_thrown_exception)
|
|
|
|
|| $this->codebase->classExtends($possibly_thrown_exception, $expected_exception)
|
|
|
|
) {
|
|
|
|
$is_expected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
2019-03-24 21:17:14 +01:00
|
|
|
$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
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
public function getFunctionAnalyzer(string $function_id) : ?FunctionAnalyzer
|
|
|
|
{
|
|
|
|
return $this->function_analyzers[$function_id] ?? null;
|
|
|
|
}
|
|
|
|
|
2020-05-29 04:14:41 +02:00
|
|
|
public function getParsedDocblock() : ?ParsedDocblock
|
2019-06-26 22:26:14 +02:00
|
|
|
{
|
|
|
|
return $this->parsed_docblock;
|
|
|
|
}
|
2019-08-18 17:18:02 +02:00
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getFQCLN(): ?string
|
2019-08-18 17:18:02 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-10-17 18:36:44 +02:00
|
|
|
/**
|
|
|
|
* @return \Psalm\Internal\Provider\NodeDataProvider
|
|
|
|
*/
|
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
|
|
|
}
|