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-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;
|
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-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;
|
|
|
|
use function array_map;
|
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_key_exists;
|
|
|
|
use function array_change_key_case;
|
2019-07-24 22:48:54 +02:00
|
|
|
use function array_reverse;
|
|
|
|
use function trim;
|
|
|
|
use function array_column;
|
|
|
|
use function array_combine;
|
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>
|
|
|
|
*/
|
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;
|
|
|
|
|
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
|
2020-05-19 18:56:23 +02:00
|
|
|
$this->hoistFunctions($stmts);
|
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
|
|
|
|
) {
|
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
|
|
|
}
|
|
|
|
|
2019-07-24 22:48:54 +02:00
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Stmt> $stmts
|
2018-12-20 22:03:21 +01:00
|
|
|
*/
|
2020-05-19 18:56:23 +02:00
|
|
|
private function hoistFunctions(array $stmts) : 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
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @psalm-return false|null
|
|
|
|
*/
|
|
|
|
private static function analyzeStatement(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Stmt $stmt,
|
|
|
|
Context $context,
|
|
|
|
?Context $global_context
|
|
|
|
) {
|
|
|
|
$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-05-19 18:56:23 +02:00
|
|
|
return;
|
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-05-19 18:56:23 +02:00
|
|
|
$suppressed = array_filter(
|
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @param string $line
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function ($line) {
|
|
|
|
return preg_split('/[\s]+/', $line)[0];
|
|
|
|
},
|
2020-05-29 04:14:41 +02:00
|
|
|
$statements_analyzer->parsed_docblock->tags['psalm-suppress']
|
2020-05-19 18:56:23 +02:00
|
|
|
)
|
|
|
|
);
|
2017-12-19 15:48:01 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($suppressed) {
|
|
|
|
$new_issues = [];
|
2020-01-14 18:13:41 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
foreach ($suppressed as $offset => $issue_type) {
|
|
|
|
$offset += $docblock->getFilePos();
|
|
|
|
$new_issues[$offset] = $issue_type;
|
2018-01-14 00:33:32 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($issue_type === 'InaccessibleMethod') {
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-19 15:48:01 +01:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ($codebase->track_unused_suppressions) {
|
|
|
|
IssueBuffer::addUnusedSuppression(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$offset,
|
|
|
|
$issue_type
|
2018-11-28 21:12:08 +01:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
} 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) {
|
|
|
|
Statements\NopAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
|
|
|
} 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-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
|
|
|
|
* @return void
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2020-05-19 18:56:23 +02:00
|
|
|
public function checkUnreferencedVars(array $stmts)
|
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;
|
|
|
|
if ($codebase->alter_code) {
|
|
|
|
// Reverse array to deal with chain of assignments
|
|
|
|
$this->unused_var_locations = array_reverse($this->unused_var_locations, true);
|
2016-08-11 23:36:22 +02:00
|
|
|
}
|
2020-05-19 18:56:23 +02:00
|
|
|
$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-05-19 18:56:23 +02:00
|
|
|
foreach ($this->unused_var_locations as $hash => list($var_id, $original_location)) {
|
|
|
|
if (substr($var_id, 0, 2) === '$_' || isset($this->used_var_locations[$hash])) {
|
|
|
|
continue;
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
2017-07-28 16:42:30 +02:00
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
if ((!$function_storage
|
2020-06-24 17:48:27 +02:00
|
|
|
|| !array_key_exists(substr($var_id, 1), $function_storage->param_lookup))
|
2020-05-19 18:56:23 +02:00
|
|
|
&& !isset($this->byref_uses[$var_id])
|
|
|
|
&& !VariableFetchAnalyzer::isSuperGlobal($var_id)
|
|
|
|
) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
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-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-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
|
|
|
|
|
|
|
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
|
|
|
}
|