2016-08-14 05:26:45 +02:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Analyzer;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
2017-05-19 06:48:26 +02:00
|
|
|
|
use PhpParser;
|
2019-09-01 06:56:46 +02:00
|
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
2016-10-15 06:12:57 +02:00
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use Psalm\CodeLocation;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Codebase;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use Psalm\Context;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\FileManipulation;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLike\ReturnTypeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLike\ReturnTypeCollector;
|
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
2021-11-25 22:51:35 +01:00
|
|
|
|
use Psalm\Internal\Codebase\TaintFlowGraph;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Internal\Codebase\VariableUseGraph;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use Psalm\Internal\DataFlow\DataFlowNode;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use Psalm\Internal\FileManipulation\FunctionDocblockManipulator;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Internal\MethodIdentifier;
|
|
|
|
|
use Psalm\Internal\PhpVisitor\NodeCounterVisitor;
|
|
|
|
|
use Psalm\Internal\Provider\NodeDataProvider;
|
2020-07-22 01:40:35 +02:00
|
|
|
|
use Psalm\Internal\Type\Comparator\TypeComparisonResult;
|
|
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Internal\Type\TypeExpander;
|
2019-02-27 22:00:44 +01:00
|
|
|
|
use Psalm\Issue\InvalidDocblockParamName;
|
2016-12-31 05:40:32 +01:00
|
|
|
|
use Psalm\Issue\InvalidParamDefault;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Issue\InvalidThrow;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use Psalm\Issue\MethodSignatureMismatch;
|
2018-01-05 18:11:12 +01:00
|
|
|
|
use Psalm\Issue\MismatchingDocblockParamType;
|
2018-01-29 02:03:47 +01:00
|
|
|
|
use Psalm\Issue\MissingClosureParamType;
|
|
|
|
|
use Psalm\Issue\MissingParamType;
|
2018-06-22 07:13:49 +02:00
|
|
|
|
use Psalm\Issue\MissingThrowsDocblock;
|
2019-03-03 21:11:09 +01:00
|
|
|
|
use Psalm\Issue\ReferenceConstraintViolation;
|
2018-01-10 06:07:47 +01:00
|
|
|
|
use Psalm\Issue\ReservedWord;
|
2019-03-05 21:45:09 +01:00
|
|
|
|
use Psalm\Issue\UnusedClosureParam;
|
2017-12-29 23:27:16 +01:00
|
|
|
|
use Psalm\Issue\UnusedParam;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
use Psalm\IssueBuffer;
|
2021-01-06 15:05:53 +01:00
|
|
|
|
use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2021-12-04 03:37:19 +01:00
|
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2017-09-04 02:52:54 +02:00
|
|
|
|
use Psalm\Storage\FunctionLikeStorage;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use Psalm\Storage\FunctionStorage;
|
2017-01-06 07:07:11 +01:00
|
|
|
|
use Psalm\Storage\MethodStorage;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2021-12-03 21:40:18 +01:00
|
|
|
|
use UnexpectedValueException;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
|
use function array_combine;
|
|
|
|
|
use function array_diff_key;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function array_key_exists;
|
|
|
|
|
use function array_keys;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use function array_merge;
|
|
|
|
|
use function array_search;
|
2021-12-03 21:07:25 +01:00
|
|
|
|
use function array_values;
|
|
|
|
|
use function count;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function end;
|
2021-12-11 20:21:45 +01:00
|
|
|
|
use function in_array;
|
2021-12-03 21:07:25 +01:00
|
|
|
|
use function is_string;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use function md5;
|
2021-12-03 21:07:25 +01:00
|
|
|
|
use function microtime;
|
|
|
|
|
use function reset;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
use function strpos;
|
|
|
|
|
use function strtolower;
|
|
|
|
|
use function substr;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
|
/**
|
|
|
|
|
* @internal
|
2021-02-09 17:40:52 +01:00
|
|
|
|
* @template-covariant TFunction as Closure|Function_|ClassMethod|ArrowFunction
|
2018-12-02 00:37:49 +01:00
|
|
|
|
*/
|
2019-11-25 17:44:54 +01:00
|
|
|
|
abstract class FunctionLikeAnalyzer extends SourceAnalyzer
|
2016-08-14 05:26:45 +02:00
|
|
|
|
{
|
2016-10-14 06:53:43 +02:00
|
|
|
|
/**
|
2021-02-09 17:40:52 +01:00
|
|
|
|
* @var TFunction
|
2016-10-14 06:53:43 +02:00
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
|
protected $function;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
/**
|
|
|
|
|
* @var Codebase
|
|
|
|
|
*/
|
|
|
|
|
protected $codebase;
|
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
|
/**
|
2017-01-07 20:35:07 +01:00
|
|
|
|
* @var array<string>
|
2016-10-14 06:53:43 +02:00
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
|
protected $suppressed_issues;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
|
protected $is_static = false;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
|
/**
|
2019-03-06 17:12:36 +01:00
|
|
|
|
* @var ?array<string, Type\Union>
|
2016-10-15 06:12:57 +02:00
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
|
protected $return_vars_in_scope = [];
|
2016-10-15 06:12:57 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2019-03-06 17:12:36 +01:00
|
|
|
|
* @var ?array<string, bool>
|
2016-10-15 06:12:57 +02:00
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
|
protected $return_vars_possibly_in_scope = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
|
/**
|
|
|
|
|
* @var Type\Union|null
|
|
|
|
|
*/
|
|
|
|
|
private $local_return_type;
|
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
|
/**
|
2019-01-31 18:55:48 +01:00
|
|
|
|
* @var array<string, bool>
|
2016-11-01 05:39:41 +01:00
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
|
protected static $no_effects_hashes = [];
|
|
|
|
|
|
2020-08-28 18:42:55 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
public $track_mutations = false;
|
|
|
|
|
|
2020-08-23 16:28:26 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
public $inferred_impure = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
public $inferred_has_mutation = false;
|
|
|
|
|
|
2020-09-30 19:08:01 +02:00
|
|
|
|
/**
|
|
|
|
|
* Holds param nodes for functions with func_get_args calls
|
|
|
|
|
*
|
2020-10-13 22:49:03 +02:00
|
|
|
|
* @var array<string, DataFlowNode>
|
2020-09-30 19:08:01 +02:00
|
|
|
|
*/
|
|
|
|
|
public $param_nodes = [];
|
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
|
/**
|
2019-06-16 15:12:32 +02:00
|
|
|
|
* @var FunctionLikeStorage
|
2018-12-18 05:29:27 +01:00
|
|
|
|
*/
|
|
|
|
|
protected $storage;
|
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
|
/**
|
2021-02-09 17:40:52 +01:00
|
|
|
|
* @param TFunction $function
|
2016-10-15 06:12:57 +02:00
|
|
|
|
*/
|
2020-09-20 14:55:28 +02:00
|
|
|
|
public function __construct($function, SourceAnalyzer $source, FunctionLikeStorage $storage)
|
2016-08-14 05:26:45 +02:00
|
|
|
|
{
|
|
|
|
|
$this->function = $function;
|
|
|
|
|
$this->source = $source;
|
|
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$this->codebase = $source->getCodebase();
|
2018-12-18 05:29:27 +01:00
|
|
|
|
$this->storage = $storage;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
/**
|
2017-01-12 03:37:53 +01:00
|
|
|
|
* @param bool $add_mutations whether or not to add mutations to this method
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @return false|null
|
2021-10-11 17:23:21 +02:00
|
|
|
|
*
|
|
|
|
|
* @psalm-suppress PossiblyUnusedReturnValue unused but seems important
|
2016-11-02 07:29:00 +01:00
|
|
|
|
*/
|
2019-03-10 21:36:35 +01:00
|
|
|
|
public function analyze(
|
|
|
|
|
Context $context,
|
2021-12-03 20:11:20 +01:00
|
|
|
|
NodeDataProvider $type_provider,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?Context $global_context = null,
|
2020-11-13 19:13:29 +01:00
|
|
|
|
bool $add_mutations = false
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): ?bool {
|
2018-12-18 05:29:27 +01:00
|
|
|
|
$storage = $this->storage;
|
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
|
$function_stmts = $this->function->getStmts() ?: [];
|
2016-09-02 00:02:09 +02:00
|
|
|
|
|
2020-09-19 20:59:19 +02:00
|
|
|
|
if ($this->function instanceof ArrowFunction
|
|
|
|
|
&& isset($function_stmts[0])
|
|
|
|
|
&& $function_stmts[0] instanceof PhpParser\Node\Stmt\Return_
|
|
|
|
|
&& $function_stmts[0]->expr
|
|
|
|
|
) {
|
|
|
|
|
$function_stmts[0]->setAttributes($function_stmts[0]->expr->getAttributes());
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-15 21:58:40 +01:00
|
|
|
|
if ($global_context) {
|
|
|
|
|
foreach ($global_context->constants as $const_name => $var_type) {
|
2017-02-01 05:24:33 +01:00
|
|
|
|
if (!$context->hasVariable($const_name)) {
|
2017-01-15 21:58:40 +01:00
|
|
|
|
$context->vars_in_scope[$const_name] = clone $var_type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase = $this->codebase;
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$project_analyzer = $this->getProjectAnalyzer();
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
2019-08-18 23:18:03 +02:00
|
|
|
|
if ($codebase->track_unused_suppressions && !isset($storage->suppressed_issues[0])) {
|
2021-12-11 20:21:45 +01:00
|
|
|
|
if (count($storage->suppressed_issues) === 1
|
|
|
|
|
|| !in_array("UnusedPsalmSuppress", $storage->suppressed_issues)
|
|
|
|
|
) {
|
|
|
|
|
foreach ($storage->suppressed_issues as $offset => $issue_name) {
|
|
|
|
|
IssueBuffer::addUnusedSuppression($this->getFilePath(), $offset, $issue_name);
|
|
|
|
|
}
|
2019-08-18 20:44:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 00:54:55 +01:00
|
|
|
|
foreach ($storage->docblock_issues as $docblock_issue) {
|
|
|
|
|
IssueBuffer::add($docblock_issue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 23:50:23 +01:00
|
|
|
|
$function_information = $this->getFunctionInformation(
|
|
|
|
|
$context,
|
|
|
|
|
$codebase,
|
|
|
|
|
$type_provider,
|
|
|
|
|
$storage,
|
|
|
|
|
$add_mutations
|
|
|
|
|
);
|
2020-06-14 17:58:50 +02:00
|
|
|
|
|
2021-01-05 23:50:23 +01:00
|
|
|
|
if ($function_information === null) {
|
|
|
|
|
return null;
|
2016-12-07 20:13:39 +01:00
|
|
|
|
}
|
2016-09-02 00:02:09 +02:00
|
|
|
|
|
2021-01-05 23:50:23 +01:00
|
|
|
|
[
|
|
|
|
|
$real_method_id,
|
|
|
|
|
$method_id,
|
|
|
|
|
$appearing_class_storage,
|
|
|
|
|
$hash,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$overridden_method_ids
|
|
|
|
|
] = $function_information;
|
|
|
|
|
|
2019-08-18 18:25:48 +02:00
|
|
|
|
$this->suppressed_issues = $this->getSource()->getSuppressedIssues() + $storage->suppressed_issues;
|
2021-05-04 04:26:56 +02:00
|
|
|
|
if ($appearing_class_storage) {
|
|
|
|
|
$this->suppressed_issues += $appearing_class_storage->suppressed_issues;
|
|
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
|
|
|
|
if ($storage instanceof MethodStorage && $storage->is_static) {
|
|
|
|
|
$this->is_static = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer = new StatementsAnalyzer($this, $type_provider);
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
2021-02-09 17:40:52 +01:00
|
|
|
|
if ($this instanceof ClosureAnalyzer && $this->function instanceof Closure) {
|
2020-11-13 19:13:29 +01:00
|
|
|
|
$byref_uses = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->function->uses as $use) {
|
2021-12-03 21:07:25 +01:00
|
|
|
|
if (!is_string($use->var->name)) {
|
2020-11-13 19:13:29 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$use_var_id = '$' . $use->var->name;
|
|
|
|
|
|
2020-11-13 19:37:27 +01:00
|
|
|
|
$use_location = new CodeLocation($this, $use);
|
2020-11-13 19:13:29 +01:00
|
|
|
|
|
2020-11-13 19:37:27 +01:00
|
|
|
|
$use_assignment = null;
|
2020-11-13 19:13:29 +01:00
|
|
|
|
|
2020-11-13 19:37:27 +01:00
|
|
|
|
if ($statements_analyzer->data_flow_graph) {
|
|
|
|
|
$use_assignment = DataFlowNode::getForAssignment(
|
|
|
|
|
$use_var_id,
|
|
|
|
|
$use_location
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$statements_analyzer->data_flow_graph->addNode($use_assignment);
|
|
|
|
|
|
|
|
|
|
$context->vars_in_scope[$use_var_id]->parent_nodes += [$use_assignment->id => $use_assignment];
|
|
|
|
|
}
|
2020-11-13 19:13:29 +01:00
|
|
|
|
|
2020-11-13 19:37:27 +01:00
|
|
|
|
if ($use->byRef) {
|
|
|
|
|
$byref_uses[$use_var_id] = true;
|
2020-11-13 19:13:29 +01:00
|
|
|
|
|
2020-11-13 19:37:27 +01:00
|
|
|
|
if ($statements_analyzer->data_flow_graph && $use_assignment) {
|
|
|
|
|
$statements_analyzer->data_flow_graph->addPath(
|
|
|
|
|
$use_assignment,
|
|
|
|
|
new DataFlowNode('closure-use', 'closure use', null),
|
|
|
|
|
'closure-use'
|
|
|
|
|
);
|
2020-11-13 19:13:29 +01:00
|
|
|
|
}
|
2020-11-13 19:37:27 +01:00
|
|
|
|
} else {
|
|
|
|
|
$statements_analyzer->registerVariable($use_var_id, $use_location, null);
|
2020-11-13 19:13:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-10 21:36:35 +01:00
|
|
|
|
$statements_analyzer->setByRefUses($byref_uses);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 03:54:00 +01:00
|
|
|
|
if ($storage->template_types) {
|
|
|
|
|
foreach ($storage->template_types as $param_name => $_) {
|
|
|
|
|
$fq_classlike_name = Type::getFQCLNFromString(
|
|
|
|
|
$param_name,
|
|
|
|
|
$this->getAliases()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($codebase->classOrInterfaceExists($fq_classlike_name)) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-02-22 03:54:00 +01:00
|
|
|
|
new ReservedWord(
|
|
|
|
|
'Cannot use ' . $param_name . ' as template name since the class already exists',
|
|
|
|
|
new CodeLocation($this, $this->function),
|
|
|
|
|
'resource'
|
|
|
|
|
),
|
|
|
|
|
$this->getSuppressedIssues()
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2019-02-22 03:54:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
|
$template_types = $storage->template_types;
|
|
|
|
|
|
2019-12-18 15:43:15 +01:00
|
|
|
|
if ($appearing_class_storage && $appearing_class_storage->template_types) {
|
|
|
|
|
$template_types = array_merge($template_types ?: [], $appearing_class_storage->template_types);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 23:28:25 +01:00
|
|
|
|
$params = $storage->params;
|
|
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
|
if ($codebase->alter_code) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$this->alterParams($codebase, $storage, $params, $context);
|
|
|
|
|
}
|
2019-06-01 16:32:49 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
foreach ($codebase->methods_to_rename as $original_method_id => $new_method_name) {
|
2021-02-09 17:40:52 +01:00
|
|
|
|
if ($this instanceof MethodAnalyzer
|
2020-02-15 02:54:26 +01:00
|
|
|
|
&& strtolower((string) $this->getMethodId()) === $original_method_id
|
2019-08-22 16:29:21 +02:00
|
|
|
|
) {
|
|
|
|
|
$file_manipulations = [
|
2021-12-03 20:11:20 +01:00
|
|
|
|
new FileManipulation(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
(int) $this->function->name->getAttribute('startFilePos'),
|
|
|
|
|
(int) $this->function->name->getAttribute('endFilePos') + 1,
|
|
|
|
|
$new_method_name
|
|
|
|
|
)
|
|
|
|
|
];
|
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
FileManipulationBuffer::add(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$this->getFilePath(),
|
|
|
|
|
$file_manipulations
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 17:27:45 +01:00
|
|
|
|
if ($storage instanceof MethodStorage
|
2021-12-03 20:11:20 +01:00
|
|
|
|
&& $method_id instanceof MethodIdentifier
|
2020-12-08 17:27:45 +01:00
|
|
|
|
&& $overridden_method_ids
|
|
|
|
|
) {
|
|
|
|
|
$params = $codebase->methods->getMethodParams(
|
|
|
|
|
$method_id,
|
|
|
|
|
$this
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$check_stmts = $this->processParams(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$storage,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$params,
|
|
|
|
|
$context,
|
|
|
|
|
(bool) $template_types
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($storage->pure) {
|
|
|
|
|
$context->pure = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 16:36:57 +02:00
|
|
|
|
if ($storage->mutation_free
|
|
|
|
|
&& $cased_method_id
|
|
|
|
|
&& !strpos($cased_method_id, '__construct')
|
|
|
|
|
&& !($storage instanceof MethodStorage && $storage->mutation_free_inferred)
|
|
|
|
|
) {
|
2019-08-30 18:36:35 +02:00
|
|
|
|
$context->mutation_free = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 16:36:57 +02:00
|
|
|
|
if ($storage instanceof MethodStorage
|
|
|
|
|
&& $storage->external_mutation_free
|
|
|
|
|
&& !$storage->mutation_free_inferred
|
|
|
|
|
) {
|
2019-08-30 18:36:35 +02:00
|
|
|
|
$context->external_mutation_free = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($storage->unused_docblock_params) {
|
|
|
|
|
foreach ($storage->unused_docblock_params as $param_name => $param_location) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidDocblockParamName(
|
|
|
|
|
'Incorrect param name $' . $param_name . ' in docblock for ' . $cased_method_id,
|
|
|
|
|
$param_location
|
|
|
|
|
)
|
|
|
|
|
)) {
|
2019-06-01 16:32:49 +02:00
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-01 16:32:49 +02:00
|
|
|
|
|
2020-07-11 23:14:39 +02:00
|
|
|
|
if ($storage->signature_return_type && $storage->signature_return_type_location) {
|
2020-09-02 06:17:41 +02:00
|
|
|
|
[$start, $end] = $storage->signature_return_type_location->getSelectionBounds();
|
2020-07-11 23:14:39 +02:00
|
|
|
|
|
|
|
|
|
$codebase->analyzer->addOffsetReference(
|
|
|
|
|
$this->getFilePath(),
|
|
|
|
|
$start,
|
|
|
|
|
$end,
|
|
|
|
|
(string) $storage->signature_return_type
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 12:55:13 +02:00
|
|
|
|
if ($storage instanceof MethodStorage && $storage->location && !$storage->allow_named_arg_calls) {
|
|
|
|
|
foreach ($overridden_method_ids as $overridden_method_id) {
|
|
|
|
|
$overridden_storage = $codebase->methods->getStorage($overridden_method_id);
|
|
|
|
|
if ($overridden_storage->allow_named_arg_calls) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(new MethodSignatureMismatch(
|
2021-04-15 12:55:13 +02:00
|
|
|
|
'Method ' . (string) $method_id . ' should accept named arguments '
|
|
|
|
|
. ' as ' . (string) $overridden_method_id . ' does',
|
|
|
|
|
$storage->location
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if (ReturnTypeAnalyzer::checkReturnType(
|
|
|
|
|
$this->function,
|
|
|
|
|
$project_analyzer,
|
|
|
|
|
$this,
|
|
|
|
|
$storage,
|
|
|
|
|
$context
|
|
|
|
|
) === false) {
|
|
|
|
|
$check_stmts = false;
|
|
|
|
|
}
|
2019-06-01 16:32:49 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if (!$check_stmts) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-06-01 16:32:49 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($context->collect_initializations || $context->collect_mutations) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues([
|
|
|
|
|
'DocblockTypeContradiction',
|
|
|
|
|
'InvalidReturnStatement',
|
|
|
|
|
'RedundantCondition',
|
|
|
|
|
'RedundantConditionGivenDocblockType',
|
|
|
|
|
'TypeDoesNotContainNull',
|
|
|
|
|
'TypeDoesNotContainType',
|
|
|
|
|
'LoopInvalidation',
|
|
|
|
|
]);
|
2019-06-01 16:32:49 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($context->collect_initializations) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues([
|
|
|
|
|
'UndefinedInterfaceMethod',
|
|
|
|
|
'UndefinedMethod',
|
|
|
|
|
'PossiblyUndefinedMethod',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-02-07 05:10:18 +01:00
|
|
|
|
} elseif ($cased_method_id && strpos($cased_method_id, '__destruct')) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues([
|
|
|
|
|
'InvalidPropertyAssignmentValue',
|
|
|
|
|
'PossiblyNullPropertyAssignmentValue',
|
|
|
|
|
]);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
|
$time = microtime(true);
|
2020-07-30 21:30:19 +02:00
|
|
|
|
|
|
|
|
|
$project_analyzer = $statements_analyzer->getProjectAnalyzer();
|
|
|
|
|
|
2020-08-28 18:42:55 +02:00
|
|
|
|
if ($codebase->alter_code
|
|
|
|
|
&& (isset($project_analyzer->getIssuesToFix()['MissingPureAnnotation'])
|
|
|
|
|
|| isset($project_analyzer->getIssuesToFix()['MissingImmutableAnnotation']))
|
|
|
|
|
) {
|
|
|
|
|
$this->track_mutations = true;
|
|
|
|
|
} elseif ($this->function instanceof Closure
|
|
|
|
|
|| $this->function instanceof ArrowFunction
|
|
|
|
|
) {
|
|
|
|
|
$this->track_mutations = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$statements_analyzer->analyze($function_stmts, $context, $global_context, true);
|
|
|
|
|
|
2020-08-23 16:28:26 +02:00
|
|
|
|
if ($codebase->alter_code
|
|
|
|
|
&& isset($project_analyzer->getIssuesToFix()['MissingPureAnnotation'])
|
|
|
|
|
&& !$this->inferred_impure
|
2020-08-23 19:34:32 +02:00
|
|
|
|
&& ($this->function instanceof Function_
|
2020-08-24 00:37:46 +02:00
|
|
|
|
|| $this->function instanceof ClassMethod)
|
2020-08-24 00:41:31 +02:00
|
|
|
|
&& $storage->params
|
2020-08-24 04:16:03 +02:00
|
|
|
|
&& !$overridden_method_ids
|
2020-08-23 16:28:26 +02:00
|
|
|
|
) {
|
|
|
|
|
$manipulator = FunctionDocblockManipulator::getForFunction(
|
|
|
|
|
$project_analyzer,
|
|
|
|
|
$this->source->getFilePath(),
|
|
|
|
|
$this->function
|
|
|
|
|
);
|
|
|
|
|
|
2020-08-23 19:58:34 +02:00
|
|
|
|
$yield_types = [];
|
|
|
|
|
|
|
|
|
|
$inferred_return_types = ReturnTypeCollector::getReturnTypes(
|
|
|
|
|
$codebase,
|
|
|
|
|
$type_provider,
|
|
|
|
|
$function_stmts,
|
|
|
|
|
$yield_types,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$inferred_return_type = $inferred_return_types
|
2021-12-03 20:11:20 +01:00
|
|
|
|
? Type::combineUnionTypeArray(
|
2020-08-23 19:58:34 +02:00
|
|
|
|
$inferred_return_types,
|
|
|
|
|
$codebase
|
|
|
|
|
)
|
2020-12-04 19:16:05 +01:00
|
|
|
|
: Type::getVoid();
|
2020-08-23 19:58:34 +02:00
|
|
|
|
|
2020-12-04 21:10:50 +01:00
|
|
|
|
if (!$inferred_return_type->isVoid()
|
2020-08-23 19:58:34 +02:00
|
|
|
|
&& !$inferred_return_type->isFalse()
|
2020-08-24 00:37:46 +02:00
|
|
|
|
&& !$inferred_return_type->isNull()
|
|
|
|
|
&& !$inferred_return_type->isSingleIntLiteral()
|
|
|
|
|
&& !$inferred_return_type->isSingleStringLiteral()
|
2020-08-23 19:58:34 +02:00
|
|
|
|
&& !$inferred_return_type->isTrue()
|
2020-08-23 20:07:19 +02:00
|
|
|
|
&& $inferred_return_type->getId() !== 'array<empty, empty>'
|
2020-08-23 19:58:34 +02:00
|
|
|
|
) {
|
|
|
|
|
$manipulator->makePure();
|
|
|
|
|
}
|
2020-08-23 16:28:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 01:24:27 +02:00
|
|
|
|
if ($this->inferred_has_mutation && $context->self) {
|
|
|
|
|
$this->codebase->analyzer->addMutableClass($context->self);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 21:30:19 +02:00
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
&& $project_analyzer->debug_performance
|
|
|
|
|
&& $cased_method_id
|
|
|
|
|
) {
|
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$node_counter = new NodeCounterVisitor();
|
2020-07-30 21:30:19 +02:00
|
|
|
|
$traverser->addVisitor($node_counter);
|
|
|
|
|
$traverser->traverse($function_stmts);
|
|
|
|
|
|
|
|
|
|
if ($node_counter->count > 5) {
|
2021-12-03 21:07:25 +01:00
|
|
|
|
$time_taken = microtime(true) - $time;
|
2020-07-30 21:30:19 +02:00
|
|
|
|
$codebase->analyzer->addFunctionTiming($cased_method_id, $time_taken / $node_counter->count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 14:50:15 +01:00
|
|
|
|
$final_actions = ScopeAnalyzer::getControlActions(
|
|
|
|
|
$this->function->getStmts() ?: [],
|
|
|
|
|
null,
|
|
|
|
|
$codebase->config->exit_functions,
|
2021-05-17 14:27:24 +02:00
|
|
|
|
[]
|
2021-03-27 14:50:15 +01:00
|
|
|
|
);
|
|
|
|
|
|
2021-10-15 23:20:38 +02:00
|
|
|
|
if ($final_actions !== [ScopeAnalyzer::ACTION_END]) {
|
|
|
|
|
$this->examineParamTypes($statements_analyzer, $context, $codebase);
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|
2020-12-08 17:27:45 +01:00
|
|
|
|
foreach ($params as $function_param) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
// only complain if there's no type defined by a parent type
|
|
|
|
|
if (!$function_param->type
|
|
|
|
|
&& $function_param->location
|
|
|
|
|
) {
|
2020-05-06 15:57:02 +02:00
|
|
|
|
if ($this->function instanceof Closure
|
|
|
|
|
|| $this->function instanceof ArrowFunction
|
|
|
|
|
) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
new MissingClosureParamType(
|
|
|
|
|
'Parameter $' . $function_param->name . ' has no provided type',
|
|
|
|
|
$function_param->location
|
|
|
|
|
),
|
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
new MissingParamType(
|
|
|
|
|
'Parameter $' . $function_param->name . ' has no provided type',
|
|
|
|
|
$function_param->location
|
|
|
|
|
),
|
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2019-06-01 16:32:49 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 06:56:46 +02:00
|
|
|
|
if ($this->function instanceof Closure
|
|
|
|
|
|| $this->function instanceof ArrowFunction
|
|
|
|
|
) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$this->verifyReturnType(
|
2019-09-01 06:56:46 +02:00
|
|
|
|
$function_stmts,
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$storage->return_type,
|
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
|
$storage->return_type_location,
|
2020-03-29 05:37:42 +02:00
|
|
|
|
$context->has_returned,
|
2021-06-10 20:18:15 +02:00
|
|
|
|
$global_context && ($global_context->inside_call || $global_context->inside_return)
|
2019-08-22 16:29:21 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$closure_yield_types = [];
|
2019-06-01 19:02:20 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$closure_return_types = ReturnTypeCollector::getReturnTypes(
|
2019-10-18 17:01:16 +02:00
|
|
|
|
$codebase,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$type_provider,
|
2019-09-01 06:56:46 +02:00
|
|
|
|
$function_stmts,
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$closure_yield_types,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
2020-05-20 01:48:11 +02:00
|
|
|
|
$closure_return_type = $closure_return_types
|
2021-12-03 20:11:20 +01:00
|
|
|
|
? Type::combineUnionTypeArray(
|
2020-01-13 16:23:58 +01:00
|
|
|
|
$closure_return_types,
|
|
|
|
|
$codebase
|
2020-05-20 01:48:11 +02:00
|
|
|
|
)
|
2020-12-04 19:16:05 +01:00
|
|
|
|
: Type::getVoid();
|
2019-06-01 19:02:20 +02:00
|
|
|
|
|
2020-05-20 01:48:11 +02:00
|
|
|
|
$closure_yield_type = $closure_yield_types
|
2021-12-03 20:11:20 +01:00
|
|
|
|
? Type::combineUnionTypeArray(
|
2020-05-20 01:48:11 +02:00
|
|
|
|
$closure_yield_types,
|
|
|
|
|
$codebase
|
|
|
|
|
)
|
|
|
|
|
: null;
|
2020-04-29 22:52:37 +02:00
|
|
|
|
|
2020-12-04 21:10:50 +01:00
|
|
|
|
if ($closure_yield_type) {
|
|
|
|
|
$closure_return_type = $closure_yield_type;
|
|
|
|
|
}
|
2020-04-29 22:52:37 +02:00
|
|
|
|
|
2020-12-04 21:10:50 +01:00
|
|
|
|
if ($function_type = $statements_analyzer->node_data->getType($this->function)) {
|
|
|
|
|
/**
|
|
|
|
|
* @var Type\Atomic\TClosure
|
|
|
|
|
*/
|
2021-12-03 21:07:25 +01:00
|
|
|
|
$closure_atomic = array_values($function_type->getAtomicTypes())[0];
|
2020-08-28 18:42:55 +02:00
|
|
|
|
|
2020-12-04 21:10:50 +01:00
|
|
|
|
if (($storage->return_type === $storage->signature_return_type)
|
|
|
|
|
&& (!$storage->return_type
|
|
|
|
|
|| $storage->return_type->hasMixed()
|
|
|
|
|
|| UnionTypeComparator::isContainedBy(
|
|
|
|
|
$codebase,
|
|
|
|
|
$closure_return_type,
|
|
|
|
|
$storage->return_type
|
|
|
|
|
))
|
|
|
|
|
) {
|
|
|
|
|
$closure_atomic->return_type = $closure_return_type;
|
2019-06-01 19:02:20 +02:00
|
|
|
|
}
|
2020-12-04 21:10:50 +01:00
|
|
|
|
|
|
|
|
|
$closure_atomic->is_pure = !$this->inferred_impure;
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-01 19:02:20 +02:00
|
|
|
|
|
2020-03-28 21:30:56 +01:00
|
|
|
|
if ($codebase->collect_references
|
2019-08-22 16:29:21 +02:00
|
|
|
|
&& !$context->collect_initializations
|
2020-03-31 21:49:43 +02:00
|
|
|
|
&& !$context->collect_mutations
|
2019-08-22 16:29:21 +02:00
|
|
|
|
&& $codebase->find_unused_variables
|
|
|
|
|
&& $context->check_variables
|
|
|
|
|
) {
|
|
|
|
|
$this->checkParamReferences(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$storage,
|
2019-12-18 15:43:15 +01:00
|
|
|
|
$appearing_class_storage,
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$context
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-06-01 19:02:20 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
foreach ($storage->throws as $expected_exception => $_) {
|
2020-01-31 00:00:31 +01:00
|
|
|
|
if (($expected_exception === 'self'
|
|
|
|
|
|| $expected_exception === 'static')
|
|
|
|
|
&& $context->self
|
|
|
|
|
) {
|
|
|
|
|
$expected_exception = $context->self;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if (isset($storage->throw_locations[$expected_exception])) {
|
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$expected_exception,
|
|
|
|
|
$storage->throw_locations[$expected_exception],
|
2020-02-11 22:39:33 +01:00
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2021-04-30 21:01:33 +02:00
|
|
|
|
new ClassLikeNameOptions(
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
true,
|
2021-05-03 23:54:09 +02:00
|
|
|
|
true,
|
2021-04-30 21:01:33 +02:00
|
|
|
|
true
|
|
|
|
|
)
|
2019-08-22 16:29:21 +02:00
|
|
|
|
)) {
|
|
|
|
|
$input_type = new Type\Union([new TNamedObject($expected_exception)]);
|
|
|
|
|
$container_type = new Type\Union([new TNamedObject('Exception'), new TNamedObject('Throwable')]);
|
2019-06-01 19:02:20 +02:00
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy($codebase, $input_type, $container_type)) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2021-12-03 20:11:20 +01:00
|
|
|
|
new InvalidThrow(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
'Class supplied for @throws ' . $expected_exception
|
|
|
|
|
. ' does not implement Throwable',
|
|
|
|
|
$storage->throw_locations[$expected_exception],
|
|
|
|
|
$expected_exception
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2019-06-01 19:02:20 +02:00
|
|
|
|
}
|
2019-11-15 21:34:25 +01:00
|
|
|
|
|
|
|
|
|
if ($codebase->alter_code) {
|
|
|
|
|
$codebase->classlikes->handleDocblockTypeInMigration(
|
|
|
|
|
$codebase,
|
|
|
|
|
$this,
|
|
|
|
|
$input_type,
|
|
|
|
|
$storage->throw_locations[$expected_exception],
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id
|
2019-11-15 21:34:25 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-01 19:02:20 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
foreach ($statements_analyzer->getUncaughtThrows($context) as $possibly_thrown_exception => $codelocations) {
|
|
|
|
|
$is_expected = false;
|
|
|
|
|
|
|
|
|
|
foreach ($storage->throws as $expected_exception => $_) {
|
|
|
|
|
if ($expected_exception === $possibly_thrown_exception
|
2019-10-11 04:44:21 +02:00
|
|
|
|
|| $codebase->classExtendsOrImplements($possibly_thrown_exception, $expected_exception)
|
2019-08-22 16:29:21 +02:00
|
|
|
|
) {
|
|
|
|
|
$is_expected = true;
|
|
|
|
|
break;
|
2019-06-01 19:02:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if (!$is_expected) {
|
|
|
|
|
foreach ($codelocations as $codelocation) {
|
|
|
|
|
// issues are suppressed in ThrowAnalyzer, CallAnalyzer, etc.
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
new MissingThrowsDocblock(
|
|
|
|
|
$possibly_thrown_exception . ' is thrown but not caught - please either catch'
|
|
|
|
|
. ' or add a @throws annotation',
|
|
|
|
|
$codelocation
|
|
|
|
|
)
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-25 06:37:40 +02:00
|
|
|
|
if ($codebase->taint_flow_graph
|
2020-06-19 07:22:51 +02:00
|
|
|
|
&& $this->function instanceof ClassMethod
|
|
|
|
|
&& $cased_method_id
|
2020-06-19 07:59:45 +02:00
|
|
|
|
&& $storage->specialize_call
|
2020-06-19 07:22:51 +02:00
|
|
|
|
&& isset($context->vars_in_scope['$this'])
|
|
|
|
|
&& $context->vars_in_scope['$this']->parent_nodes
|
|
|
|
|
) {
|
2020-10-13 22:49:03 +02:00
|
|
|
|
$method_source = DataFlowNode::getForMethodReturn(
|
2020-06-19 07:22:51 +02:00
|
|
|
|
(string) $method_id,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$storage->location
|
|
|
|
|
);
|
|
|
|
|
|
2020-09-25 06:37:40 +02:00
|
|
|
|
$codebase->taint_flow_graph->addNode($method_source);
|
2020-06-19 07:22:51 +02:00
|
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope['$this']->parent_nodes as $parent_node) {
|
2020-09-25 06:37:40 +02:00
|
|
|
|
$codebase->taint_flow_graph->addPath(
|
2020-06-19 07:22:51 +02:00
|
|
|
|
$parent_node,
|
|
|
|
|
$method_source,
|
|
|
|
|
'$this'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($add_mutations) {
|
|
|
|
|
if ($this->return_vars_in_scope !== null) {
|
|
|
|
|
$context->vars_in_scope = TypeAnalyzer::combineKeyedTypes(
|
|
|
|
|
$context->vars_in_scope,
|
|
|
|
|
$this->return_vars_in_scope
|
2019-06-01 19:02:20 +02:00
|
|
|
|
);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
2019-06-01 19:02:20 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($this->return_vars_possibly_in_scope !== null) {
|
|
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
|
$this->return_vars_possibly_in_scope
|
2019-06-01 19:02:20 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-06-01 22:57:33 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
foreach ($context->vars_in_scope as $var => $_) {
|
|
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
|
|
|
|
unset($context->vars_in_scope[$var]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $_) {
|
|
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
|
|
|
|
unset($context->vars_possibly_in_scope[$var]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 23:53:26 +01:00
|
|
|
|
if ($hash
|
|
|
|
|
&& $real_method_id
|
|
|
|
|
&& $this instanceof MethodAnalyzer
|
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
|
) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$new_hash = md5($real_method_id . '::' . $context->getScopeSummary());
|
|
|
|
|
|
|
|
|
|
if ($new_hash === $hash) {
|
|
|
|
|
self::$no_effects_hashes[$hash] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
|
$event = new AfterFunctionLikeAnalysisEvent(
|
|
|
|
|
$this->function,
|
|
|
|
|
$storage,
|
|
|
|
|
$this,
|
|
|
|
|
$codebase,
|
2021-01-07 15:02:05 +01:00
|
|
|
|
[],
|
|
|
|
|
$type_provider,
|
|
|
|
|
$context
|
2021-01-06 15:05:53 +01:00
|
|
|
|
);
|
2020-04-28 21:30:51 +02:00
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
|
if ($codebase->config->eventDispatcher->dispatchAfterFunctionLikeAnalysis($event) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-28 21:30:51 +02:00
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
|
$file_manipulations = $event->getFileReplacements();
|
2020-04-28 21:30:51 +02:00
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
|
if ($file_manipulations) {
|
2021-12-03 20:11:20 +01:00
|
|
|
|
FileManipulationBuffer::add(
|
2021-01-06 15:05:53 +01:00
|
|
|
|
$this->getFilePath(),
|
|
|
|
|
$file_manipulations
|
|
|
|
|
);
|
2020-04-28 21:30:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 06:10:22 +02:00
|
|
|
|
foreach ($storage->attributes as $attribute) {
|
|
|
|
|
AttributeAnalyzer::analyze(
|
|
|
|
|
$this,
|
|
|
|
|
$attribute,
|
2020-10-30 18:28:14 +01:00
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
|
|
|
|
$storage instanceof MethodStorage ? 4 : 2
|
2020-10-24 06:10:22 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function checkParamReferences(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
FunctionLikeStorage $storage,
|
2021-12-03 20:11:20 +01:00
|
|
|
|
?ClassLikeStorage $class_storage,
|
2019-08-22 16:29:21 +02:00
|
|
|
|
Context $context
|
2021-12-05 18:51:26 +01:00
|
|
|
|
): void {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
|
|
$unused_params = [];
|
|
|
|
|
|
2020-09-02 06:17:41 +02:00
|
|
|
|
foreach ($statements_analyzer->getUnusedVarLocations() as [$var_name, $original_location]) {
|
2020-06-24 17:48:27 +02:00
|
|
|
|
if (!array_key_exists(substr($var_name, 1), $storage->param_lookup)) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strpos($var_name, '$_') === 0 || (strpos($var_name, '$unused') === 0 && $var_name !== '$unused')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 17:48:27 +02:00
|
|
|
|
$position = array_search(substr($var_name, 1), array_keys($storage->param_lookup), true);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|
|
|
|
|
if ($position === false) {
|
2021-12-03 21:40:18 +01:00
|
|
|
|
throw new UnexpectedValueException('$position should not be false here');
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($storage->params[$position]->by_ref) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 18:41:43 +01:00
|
|
|
|
if ($storage->params[$position]->promoted_property) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 23:03:11 +02:00
|
|
|
|
$did_match_param = false;
|
|
|
|
|
|
|
|
|
|
foreach ($this->function->params as $param) {
|
|
|
|
|
if ($param->var->getAttribute('endFilePos') === $original_location->raw_file_end) {
|
|
|
|
|
$did_match_param = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$did_match_param) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
|
$assignment_node = DataFlowNode::getForAssignment($var_name, $original_location);
|
2020-09-30 18:28:13 +02:00
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph
|
2020-10-13 23:28:12 +02:00
|
|
|
|
&& $statements_analyzer->data_flow_graph->isVariableUsed($assignment_node)
|
2020-09-30 18:28:13 +02:00
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 17:40:52 +01:00
|
|
|
|
if (!$storage instanceof MethodStorage
|
2019-11-25 21:20:31 +01:00
|
|
|
|
|| !$storage->cased_name
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|| $storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
|
|
|
|
|
) {
|
2021-02-09 17:40:52 +01:00
|
|
|
|
if ($this instanceof ClosureAnalyzer) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
new UnusedClosureParam(
|
|
|
|
|
'Param ' . $var_name . ' is never referenced in this method',
|
|
|
|
|
$original_location
|
|
|
|
|
),
|
|
|
|
|
$this->getSuppressedIssues()
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
} else {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
new UnusedParam(
|
|
|
|
|
'Param ' . $var_name . ' is never referenced in this method',
|
|
|
|
|
$original_location
|
|
|
|
|
),
|
|
|
|
|
$this->getSuppressedIssues()
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$fq_class_name = (string)$context->self;
|
|
|
|
|
|
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
|
|
$method_name_lc = strtolower($storage->cased_name);
|
|
|
|
|
|
2020-03-31 21:49:43 +02:00
|
|
|
|
if ($storage->abstract) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 21:49:43 +02:00
|
|
|
|
if (isset($class_storage->overridden_method_ids[$method_name_lc])) {
|
|
|
|
|
$parent_method_id = end($class_storage->overridden_method_ids[$method_name_lc]);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|
2020-03-31 21:49:43 +02:00
|
|
|
|
if ($parent_method_id) {
|
|
|
|
|
$parent_method_storage = $codebase->methods->getStorage($parent_method_id);
|
2019-06-01 22:57:33 +02:00
|
|
|
|
|
2020-03-31 21:49:43 +02:00
|
|
|
|
// if the parent method has a param at that position and isn't abstract
|
|
|
|
|
if (!$parent_method_storage->abstract
|
|
|
|
|
&& isset($parent_method_storage->params[$position])
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
2019-06-01 22:57:33 +02:00
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|
|
|
|
|
$unused_params[$position] = $original_location;
|
2019-06-01 22:57:33 +02:00
|
|
|
|
}
|
2019-06-01 16:32:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($storage instanceof MethodStorage
|
2020-02-15 02:54:26 +01:00
|
|
|
|
&& $this instanceof MethodAnalyzer
|
2019-08-22 16:29:21 +02:00
|
|
|
|
&& $class_storage
|
2019-11-25 21:20:31 +01:00
|
|
|
|
&& $storage->cased_name
|
2019-08-22 16:29:21 +02:00
|
|
|
|
&& $storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PRIVATE
|
|
|
|
|
) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$method_id_lc = strtolower((string) $this->getMethodId());
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|
|
|
|
|
foreach ($storage->params as $i => $_) {
|
|
|
|
|
if (!isset($unused_params[$i])) {
|
|
|
|
|
$codebase->file_reference_provider->addMethodParamUse(
|
|
|
|
|
$method_id_lc,
|
|
|
|
|
$i,
|
|
|
|
|
$method_id_lc
|
|
|
|
|
);
|
2019-06-02 18:02:32 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$method_name_lc = strtolower($storage->cased_name);
|
|
|
|
|
|
|
|
|
|
if (!isset($class_storage->overridden_method_ids[$method_name_lc])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($class_storage->overridden_method_ids[$method_name_lc] as $parent_method_id) {
|
|
|
|
|
$codebase->file_reference_provider->addMethodParamUse(
|
2020-02-15 02:54:26 +01:00
|
|
|
|
strtolower((string) $parent_method_id),
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$i,
|
|
|
|
|
$method_id_lc
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-02 18:02:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-04 03:37:19 +01:00
|
|
|
|
* @param array<int, FunctionLikeParameter> $params
|
2019-08-22 16:29:21 +02:00
|
|
|
|
*/
|
|
|
|
|
private function processParams(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
FunctionLikeStorage $storage,
|
|
|
|
|
?string $cased_method_id,
|
|
|
|
|
array $params,
|
|
|
|
|
Context $context,
|
|
|
|
|
bool $has_template_types
|
2021-12-05 18:51:26 +01:00
|
|
|
|
): bool {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$check_stmts = true;
|
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
$project_analyzer = $statements_analyzer->getProjectAnalyzer();
|
2019-06-02 18:02:32 +02:00
|
|
|
|
|
2019-03-21 23:28:25 +01:00
|
|
|
|
foreach ($params as $offset => $function_param) {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
$signature_type = $function_param->signature_type;
|
2018-10-26 22:17:15 +02:00
|
|
|
|
$signature_type_location = $function_param->signature_type_location;
|
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
|
if ($signature_type && $signature_type_location && $signature_type->hasObjectType()) {
|
2020-07-11 23:12:03 +02:00
|
|
|
|
$referenced_type = $signature_type;
|
|
|
|
|
if ($referenced_type->isNullable()) {
|
|
|
|
|
$referenced_type = clone $referenced_type;
|
|
|
|
|
$referenced_type->removeType('null');
|
|
|
|
|
}
|
2020-09-02 06:17:41 +02:00
|
|
|
|
[$start, $end] = $signature_type_location->getSelectionBounds();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
$codebase->analyzer->addOffsetReference(
|
|
|
|
|
$this->getFilePath(),
|
|
|
|
|
$start,
|
|
|
|
|
$end,
|
2020-07-11 23:12:03 +02:00
|
|
|
|
(string) $referenced_type
|
2018-10-26 22:17:15 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2018-02-01 05:27:25 +01:00
|
|
|
|
|
2019-03-17 16:31:04 +01:00
|
|
|
|
if ($signature_type) {
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$signature_type = TypeExpander::expandUnion(
|
2019-03-17 16:31:04 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$signature_type,
|
|
|
|
|
$context->self,
|
2019-05-25 17:51:09 +02:00
|
|
|
|
$context->self,
|
|
|
|
|
$this->getParentFQCLN()
|
2019-03-17 16:31:04 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2018-05-21 06:46:56 +02:00
|
|
|
|
|
2019-03-17 16:31:04 +01:00
|
|
|
|
if ($function_param->type) {
|
2020-12-08 17:27:45 +01:00
|
|
|
|
$param_type = clone $function_param->type;
|
2017-09-02 17:18:56 +02:00
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$param_type = TypeExpander::expandUnion(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2017-09-02 17:18:56 +02:00
|
|
|
|
$param_type,
|
|
|
|
|
$context->self,
|
2019-05-25 17:51:09 +02:00
|
|
|
|
$context->self,
|
2021-02-07 17:07:22 +01:00
|
|
|
|
$this->getParentFQCLN(),
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
true
|
2017-09-02 17:18:56 +02:00
|
|
|
|
);
|
2019-03-17 16:31:04 +01:00
|
|
|
|
|
|
|
|
|
if ($function_param->type_location) {
|
|
|
|
|
if ($param_type->check(
|
|
|
|
|
$this,
|
|
|
|
|
$function_param->type_location,
|
|
|
|
|
$storage->suppressed_issues,
|
|
|
|
|
[],
|
2019-05-15 00:17:38 +02:00
|
|
|
|
false,
|
2020-02-22 07:12:19 +01:00
|
|
|
|
false,
|
2019-05-15 00:17:38 +02:00
|
|
|
|
$this->function instanceof ClassMethod
|
2021-02-14 18:07:09 +01:00
|
|
|
|
&& strtolower($this->function->name->name) !== '__construct',
|
|
|
|
|
$context->calling_method_id
|
2019-03-17 16:31:04 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
$check_stmts = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-08 17:27:45 +01:00
|
|
|
|
} else {
|
|
|
|
|
$param_type = Type::getMixed();
|
2017-09-02 17:18:56 +02:00
|
|
|
|
}
|
2016-12-04 01:11:30 +01:00
|
|
|
|
|
2019-04-09 19:58:49 +02:00
|
|
|
|
$var_type = $param_type;
|
|
|
|
|
|
|
|
|
|
if ($function_param->is_variadic) {
|
2021-03-23 00:58:22 +01:00
|
|
|
|
if ($storage->allow_named_arg_calls) {
|
|
|
|
|
$var_type = new Type\Union([
|
|
|
|
|
new Type\Atomic\TArray([Type::getArrayKey(), $param_type]),
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$var_type = new Type\Union([
|
|
|
|
|
new Type\Atomic\TList($param_type),
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-04-09 19:58:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
|
if ($statements_analyzer->data_flow_graph
|
2020-09-30 18:28:13 +02:00
|
|
|
|
&& $function_param->location
|
|
|
|
|
) {
|
2021-11-25 22:51:35 +01:00
|
|
|
|
//don't add to taint flow graph if the type can't transmit taints
|
|
|
|
|
if (!$statements_analyzer->data_flow_graph instanceof TaintFlowGraph
|
|
|
|
|
|| $function_param->type === null
|
2021-11-25 22:40:01 +01:00
|
|
|
|
|| !$function_param->type->isSingle()
|
|
|
|
|
|| (!$function_param->type->isInt()
|
|
|
|
|
&& !$function_param->type->isFloat()
|
|
|
|
|
&& !$function_param->type->isBool())
|
|
|
|
|
) {
|
|
|
|
|
$param_assignment = DataFlowNode::getForAssignment(
|
|
|
|
|
'$' . $function_param->name,
|
|
|
|
|
$function_param->location
|
|
|
|
|
);
|
2020-09-30 18:28:13 +02:00
|
|
|
|
|
2021-11-25 22:40:01 +01:00
|
|
|
|
$statements_analyzer->data_flow_graph->addNode($param_assignment);
|
2020-09-30 18:28:13 +02:00
|
|
|
|
|
2021-11-25 22:40:01 +01:00
|
|
|
|
if ($cased_method_id) {
|
|
|
|
|
$type_source = DataFlowNode::getForMethodArgument(
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$offset,
|
|
|
|
|
$function_param->location,
|
|
|
|
|
null
|
|
|
|
|
);
|
2020-09-30 18:28:13 +02:00
|
|
|
|
|
2021-11-25 22:40:01 +01:00
|
|
|
|
$statements_analyzer->data_flow_graph->addPath($type_source, $param_assignment, 'param');
|
|
|
|
|
}
|
2020-09-30 18:28:13 +02:00
|
|
|
|
|
2021-11-25 22:40:01 +01:00
|
|
|
|
if ($function_param->by_ref
|
|
|
|
|
&& $codebase->find_unused_variables
|
|
|
|
|
) {
|
|
|
|
|
$statements_analyzer->data_flow_graph->addPath(
|
|
|
|
|
$param_assignment,
|
|
|
|
|
new DataFlowNode('variable-use', 'variable use', null),
|
|
|
|
|
'variable-use'
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-09-30 18:28:13 +02:00
|
|
|
|
|
2021-11-25 22:40:01 +01:00
|
|
|
|
if ($storage->variadic) {
|
|
|
|
|
$this->param_nodes += [$param_assignment->id => $param_assignment];
|
|
|
|
|
}
|
2020-09-30 19:08:01 +02:00
|
|
|
|
|
2021-11-25 22:40:01 +01:00
|
|
|
|
$var_type->parent_nodes += [$param_assignment->id => $param_assignment];
|
|
|
|
|
}
|
2019-08-04 16:37:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 19:58:49 +02:00
|
|
|
|
$context->vars_in_scope['$' . $function_param->name] = $var_type;
|
2017-07-09 03:19:16 +02:00
|
|
|
|
$context->vars_possibly_in_scope['$' . $function_param->name] = true;
|
|
|
|
|
|
2020-01-23 20:21:34 +01:00
|
|
|
|
if ($function_param->by_ref) {
|
|
|
|
|
$context->vars_in_scope['$' . $function_param->name]->by_ref = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 18:40:16 +01:00
|
|
|
|
$parser_param = $this->function->getParams()[$offset] ?? null;
|
2016-12-31 05:40:32 +01:00
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
|
if ($function_param->location) {
|
|
|
|
|
$statements_analyzer->registerVariable(
|
|
|
|
|
'$' . $function_param->name,
|
|
|
|
|
$function_param->location,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 20:24:15 +02:00
|
|
|
|
if (!$function_param->type_location || !$function_param->location) {
|
2021-03-16 18:40:16 +01:00
|
|
|
|
if ($parser_param && $parser_param->default) {
|
2019-06-07 20:24:15 +02:00
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $parser_param->default, $context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
|
if ($signature_type) {
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_result = new TypeComparisonResult();
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2018-06-09 05:54:07 +02:00
|
|
|
|
$codebase,
|
|
|
|
|
$param_type,
|
|
|
|
|
$signature_type,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
2019-07-10 07:35:57 +02:00
|
|
|
|
$union_comparison_result
|
|
|
|
|
) && !$union_comparison_result->type_coerced_from_mixed
|
2017-07-25 22:11:02 +02:00
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase->alter_code
|
2018-11-11 18:01:14 +01:00
|
|
|
|
&& isset($project_analyzer->getIssuesToFix()['MismatchingDocblockParamType'])
|
2018-01-07 16:23:02 +01:00
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$this->addOrUpdateParamType($project_analyzer, $function_param->name, $signature_type, true);
|
2018-01-07 16:23:02 +01:00
|
|
|
|
|
2018-01-07 18:53:25 +01:00
|
|
|
|
continue;
|
2018-01-07 16:23:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2018-01-05 18:11:12 +01:00
|
|
|
|
new MismatchingDocblockParamType(
|
2017-07-25 22:11:02 +02:00
|
|
|
|
'Parameter $' . $function_param->name . ' has wrong type \'' . $param_type .
|
|
|
|
|
'\', should be \'' . $signature_type . '\'',
|
2018-01-07 06:11:23 +01:00
|
|
|
|
$function_param->type_location
|
2017-07-25 22:11:02 +02:00
|
|
|
|
),
|
2019-12-02 21:24:01 +01:00
|
|
|
|
$storage->suppressed_issues,
|
|
|
|
|
true
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
2019-01-05 18:59:12 +01:00
|
|
|
|
if ($signature_type->check(
|
2018-01-10 04:46:55 +01:00
|
|
|
|
$this,
|
|
|
|
|
$function_param->type_location,
|
|
|
|
|
$storage->suppressed_issues,
|
|
|
|
|
[],
|
|
|
|
|
false
|
2019-01-05 18:59:12 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
$check_stmts = false;
|
|
|
|
|
}
|
2018-01-10 04:46:55 +01:00
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
2018-04-10 19:05:31 +02:00
|
|
|
|
|
2021-03-16 18:40:16 +01:00
|
|
|
|
if ($parser_param && $parser_param->default) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $parser_param->default, $context);
|
2018-04-10 19:05:31 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$default_type = $statements_analyzer->node_data->getType($parser_param->default);
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|
|
|
|
|
if ($default_type
|
|
|
|
|
&& !$default_type->hasMixed()
|
2020-07-22 01:40:35 +02:00
|
|
|
|
&& !UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$default_type,
|
|
|
|
|
$param_type,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
null,
|
|
|
|
|
true
|
2018-04-10 04:00:36 +02:00
|
|
|
|
)
|
2018-04-10 19:05:31 +02:00
|
|
|
|
) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
new InvalidParamDefault(
|
2019-12-14 03:40:09 +01:00
|
|
|
|
'Default value type ' . $default_type->getId() . ' for argument ' . ($offset + 1)
|
2019-08-22 16:29:21 +02:00
|
|
|
|
. ' of method ' . $cased_method_id
|
2019-12-14 03:40:09 +01:00
|
|
|
|
. ' does not match the given type ' . $param_type->getId(),
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$function_param->type_location
|
|
|
|
|
)
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-25 07:04:26 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($has_template_types) {
|
|
|
|
|
$substituted_type = clone $param_type;
|
|
|
|
|
if ($substituted_type->check(
|
|
|
|
|
$this->source,
|
|
|
|
|
$function_param->type_location,
|
|
|
|
|
$this->suppressed_issues,
|
|
|
|
|
[],
|
|
|
|
|
false
|
|
|
|
|
) === false) {
|
|
|
|
|
$check_stmts = false;
|
2018-02-27 17:39:26 +01:00
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
} else {
|
|
|
|
|
if ($param_type->isVoid()) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
new ReservedWord(
|
|
|
|
|
'Parameter cannot be void',
|
|
|
|
|
$function_param->type_location,
|
|
|
|
|
'void'
|
|
|
|
|
),
|
|
|
|
|
$this->suppressed_issues
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2018-01-28 18:01:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($param_type->check(
|
|
|
|
|
$this->source,
|
|
|
|
|
$function_param->type_location,
|
|
|
|
|
$this->suppressed_issues,
|
|
|
|
|
[],
|
|
|
|
|
false
|
|
|
|
|
) === false) {
|
|
|
|
|
$check_stmts = false;
|
2018-01-28 18:01:51 +01:00
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
2018-01-28 18:01:51 +01:00
|
|
|
|
|
2020-03-28 21:30:56 +01:00
|
|
|
|
if ($codebase->collect_locations) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($function_param->type_location !== $function_param->signature_type_location &&
|
|
|
|
|
$function_param->signature_type_location &&
|
|
|
|
|
$function_param->signature_type
|
2017-02-01 05:24:33 +01:00
|
|
|
|
) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($function_param->signature_type->check(
|
|
|
|
|
$this->source,
|
|
|
|
|
$function_param->signature_type_location,
|
|
|
|
|
$this->suppressed_issues,
|
|
|
|
|
[],
|
|
|
|
|
false
|
|
|
|
|
) === false) {
|
|
|
|
|
$check_stmts = false;
|
2018-01-25 07:04:26 +01:00
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-25 07:04:26 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($function_param->by_ref) {
|
|
|
|
|
// register by ref params as having been used, to avoid false positives
|
|
|
|
|
// @todo change the assignment analysis *just* for byref params
|
|
|
|
|
// so that we don't have to do this
|
|
|
|
|
$context->hasVariable('$' . $function_param->name);
|
|
|
|
|
}
|
2020-10-24 06:10:22 +02:00
|
|
|
|
|
|
|
|
|
foreach ($function_param->attributes as $attribute) {
|
|
|
|
|
AttributeAnalyzer::analyze(
|
|
|
|
|
$this,
|
|
|
|
|
$attribute,
|
2020-10-30 18:28:14 +01:00
|
|
|
|
$storage->suppressed_issues,
|
|
|
|
|
$function_param->promoted_property ? 8 : 32
|
2020-10-24 06:10:22 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
2018-01-25 07:04:26 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
return $check_stmts;
|
|
|
|
|
}
|
2017-12-30 14:47:00 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
/**
|
2021-12-04 03:37:19 +01:00
|
|
|
|
* @param FunctionLikeParameter[] $params
|
2019-08-22 16:29:21 +02:00
|
|
|
|
*/
|
|
|
|
|
private function alterParams(
|
|
|
|
|
Codebase $codebase,
|
|
|
|
|
FunctionLikeStorage $storage,
|
|
|
|
|
array $params,
|
|
|
|
|
Context $context
|
2021-12-05 18:51:26 +01:00
|
|
|
|
): void {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
foreach ($this->function->params as $param) {
|
|
|
|
|
$param_name_node = null;
|
|
|
|
|
|
|
|
|
|
if ($param->type instanceof PhpParser\Node\Name) {
|
|
|
|
|
$param_name_node = $param->type;
|
|
|
|
|
} elseif ($param->type instanceof PhpParser\Node\NullableType
|
|
|
|
|
&& $param->type->type instanceof PhpParser\Node\Name
|
2019-04-29 23:29:38 +02:00
|
|
|
|
) {
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$param_name_node = $param->type->type;
|
|
|
|
|
}
|
2017-12-30 14:47:00 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($param_name_node) {
|
|
|
|
|
$resolved_name = ClassLikeAnalyzer::getFQCLNFromNameObject($param_name_node, $this->getAliases());
|
2017-12-30 14:47:00 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$parent_fqcln = $this->getParentFQCLN();
|
2017-12-30 14:47:00 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($resolved_name === 'self' && $context->self) {
|
2020-11-25 18:04:48 +01:00
|
|
|
|
$resolved_name = $context->self;
|
2019-08-22 16:29:21 +02:00
|
|
|
|
} elseif ($resolved_name === 'parent' && $parent_fqcln) {
|
|
|
|
|
$resolved_name = $parent_fqcln;
|
2017-02-01 05:24:33 +01:00
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
|
|
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
|
$codebase,
|
|
|
|
|
$this,
|
|
|
|
|
$param_name_node,
|
|
|
|
|
$resolved_name,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2019-11-17 01:59:08 +01:00
|
|
|
|
false,
|
|
|
|
|
true
|
2019-08-22 16:29:21 +02:00
|
|
|
|
);
|
2017-02-01 05:24:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($this->function->returnType) {
|
|
|
|
|
$return_name_node = null;
|
2019-08-13 05:42:51 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($this->function->returnType instanceof PhpParser\Node\Name) {
|
|
|
|
|
$return_name_node = $this->function->returnType;
|
|
|
|
|
} elseif ($this->function->returnType instanceof PhpParser\Node\NullableType
|
|
|
|
|
&& $this->function->returnType->type instanceof PhpParser\Node\Name
|
|
|
|
|
) {
|
|
|
|
|
$return_name_node = $this->function->returnType->type;
|
2019-08-13 05:42:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($return_name_node) {
|
|
|
|
|
$resolved_name = ClassLikeAnalyzer::getFQCLNFromNameObject($return_name_node, $this->getAliases());
|
2019-02-01 00:40:40 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$parent_fqcln = $this->getParentFQCLN();
|
2018-06-22 07:13:49 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($resolved_name === 'self' && $context->self) {
|
2020-11-25 18:04:48 +01:00
|
|
|
|
$resolved_name = $context->self;
|
2019-08-22 16:29:21 +02:00
|
|
|
|
} elseif ($resolved_name === 'parent' && $parent_fqcln) {
|
|
|
|
|
$resolved_name = $parent_fqcln;
|
2018-06-22 07:13:49 +02:00
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
|
$codebase,
|
|
|
|
|
$this,
|
|
|
|
|
$return_name_node,
|
|
|
|
|
$resolved_name,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id,
|
2019-11-17 01:59:08 +01:00
|
|
|
|
false,
|
|
|
|
|
true
|
2017-01-12 03:37:53 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-08-22 16:29:21 +02:00
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
if ($storage->return_type
|
|
|
|
|
&& $storage->return_type_location
|
|
|
|
|
&& $storage->return_type_location !== $storage->signature_return_type_location
|
|
|
|
|
) {
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$replace_type = TypeExpander::expandUnion(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$codebase,
|
|
|
|
|
$storage->return_type,
|
|
|
|
|
$context->self,
|
2019-11-15 22:50:43 +01:00
|
|
|
|
'static',
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$this->getParentFQCLN(),
|
|
|
|
|
false
|
|
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$codebase->classlikes->handleDocblockTypeInMigration(
|
|
|
|
|
$codebase,
|
|
|
|
|
$this,
|
|
|
|
|
$replace_type,
|
|
|
|
|
$storage->return_type_location,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id
|
2019-08-22 16:29:21 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
foreach ($params as $function_param) {
|
|
|
|
|
if ($function_param->type
|
|
|
|
|
&& $function_param->type_location
|
|
|
|
|
&& $function_param->type_location !== $function_param->signature_type_location
|
|
|
|
|
&& $function_param->type_location->file_path === $this->getFilePath()
|
|
|
|
|
) {
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$replace_type = TypeExpander::expandUnion(
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$codebase,
|
|
|
|
|
$function_param->type,
|
|
|
|
|
$context->self,
|
2019-11-15 22:50:43 +01:00
|
|
|
|
'static',
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$this->getParentFQCLN(),
|
|
|
|
|
false
|
|
|
|
|
);
|
2018-04-25 20:33:39 +02:00
|
|
|
|
|
2019-08-22 16:29:21 +02:00
|
|
|
|
$codebase->classlikes->handleDocblockTypeInMigration(
|
|
|
|
|
$codebase,
|
|
|
|
|
$this,
|
|
|
|
|
$replace_type,
|
|
|
|
|
$function_param->type_location,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
$context->calling_method_id
|
2019-08-22 16:29:21 +02:00
|
|
|
|
);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
|
/**
|
2019-09-01 06:56:46 +02:00
|
|
|
|
* @param array<PhpParser\Node\Stmt> $function_stmts
|
2017-11-26 22:03:17 +01:00
|
|
|
|
*/
|
2018-02-22 00:59:31 +01:00
|
|
|
|
public function verifyReturnType(
|
2019-09-01 06:56:46 +02:00
|
|
|
|
array $function_stmts,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?Type\Union $return_type = null,
|
|
|
|
|
?string $fq_class_name = null,
|
|
|
|
|
?CodeLocation $return_type_location = null,
|
2020-03-29 05:37:42 +02:00
|
|
|
|
bool $did_explicitly_return = false,
|
2019-05-07 02:47:55 +02:00
|
|
|
|
bool $closure_inside_call = false
|
2020-09-16 23:35:55 +02:00
|
|
|
|
): void {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
ReturnTypeAnalyzer::verifyReturnType(
|
2018-02-22 00:59:31 +01:00
|
|
|
|
$this->function,
|
2019-09-01 06:56:46 +02:00
|
|
|
|
$function_stmts,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data,
|
2018-02-22 00:59:31 +01:00
|
|
|
|
$this,
|
|
|
|
|
$return_type,
|
|
|
|
|
$fq_class_name,
|
2019-05-07 02:47:55 +02:00
|
|
|
|
$return_type_location,
|
|
|
|
|
[],
|
2020-03-29 05:37:42 +02:00
|
|
|
|
$did_explicitly_return,
|
2019-05-07 02:47:55 +02:00
|
|
|
|
$closure_inside_call
|
2018-02-22 00:59:31 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2017-11-26 22:03:17 +01:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
public function addOrUpdateParamType(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
ProjectAnalyzer $project_analyzer,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
string $param_name,
|
2018-02-22 00:59:31 +01:00
|
|
|
|
Type\Union $inferred_return_type,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
bool $docblock_only = false
|
2020-09-12 17:24:05 +02:00
|
|
|
|
): void {
|
2018-02-22 00:59:31 +01:00
|
|
|
|
$manipulator = FunctionDocblockManipulator::getForFunction(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$project_analyzer,
|
2018-02-22 00:59:31 +01:00
|
|
|
|
$this->source->getFilePath(),
|
|
|
|
|
$this->function
|
|
|
|
|
);
|
2019-12-03 07:49:43 +01:00
|
|
|
|
|
|
|
|
|
$codebase = $project_analyzer->getCodebase();
|
|
|
|
|
$is_final = true;
|
|
|
|
|
$fqcln = $this->source->getFQCLN();
|
|
|
|
|
|
2021-02-09 17:40:52 +01:00
|
|
|
|
if ($fqcln !== null && $this instanceof MethodAnalyzer) {
|
2019-12-03 07:49:43 +01:00
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fqcln);
|
|
|
|
|
$is_final = $this->function->isFinal() || $class_storage->final;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allow_native_type = !$docblock_only
|
|
|
|
|
&& $codebase->php_major_version >= 7
|
|
|
|
|
&& (
|
|
|
|
|
$codebase->allow_backwards_incompatible_changes
|
|
|
|
|
|| $is_final
|
2021-02-09 17:40:52 +01:00
|
|
|
|
|| !$this instanceof MethodAnalyzer
|
2019-12-03 07:49:43 +01:00
|
|
|
|
);
|
|
|
|
|
|
2018-02-22 00:59:31 +01:00
|
|
|
|
$manipulator->setParamType(
|
|
|
|
|
$param_name,
|
2019-12-03 07:49:43 +01:00
|
|
|
|
$allow_native_type
|
2018-02-22 00:59:31 +01:00
|
|
|
|
? $inferred_return_type->toPhpString(
|
|
|
|
|
$this->source->getNamespace(),
|
|
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
|
$this->source->getFQCLN(),
|
2019-02-07 18:25:57 +01:00
|
|
|
|
$project_analyzer->getCodebase()->php_major_version,
|
|
|
|
|
$project_analyzer->getCodebase()->php_minor_version
|
2018-02-22 00:59:31 +01:00
|
|
|
|
) : null,
|
|
|
|
|
$inferred_return_type->toNamespacedString(
|
|
|
|
|
$this->source->getNamespace(),
|
|
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
|
false
|
|
|
|
|
),
|
|
|
|
|
$inferred_return_type->toNamespacedString(
|
|
|
|
|
$this->source->getNamespace(),
|
|
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
|
true
|
2019-12-03 07:49:43 +01:00
|
|
|
|
)
|
2018-02-22 00:59:31 +01:00
|
|
|
|
);
|
2017-11-26 22:03:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
|
/**
|
|
|
|
|
* Adds return types for the given function
|
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
|
public function addReturnTypes(Context $context): void
|
2016-08-14 05:26:45 +02:00
|
|
|
|
{
|
2019-03-06 17:12:36 +01:00
|
|
|
|
if ($this->return_vars_in_scope !== null) {
|
|
|
|
|
$this->return_vars_in_scope = TypeAnalyzer::combineKeyedTypes(
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$context->vars_in_scope,
|
2019-03-06 17:12:36 +01:00
|
|
|
|
$this->return_vars_in_scope
|
2016-11-02 07:29:00 +01:00
|
|
|
|
);
|
|
|
|
|
} else {
|
2019-03-06 17:12:36 +01:00
|
|
|
|
$this->return_vars_in_scope = $context->vars_in_scope;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 17:12:36 +01:00
|
|
|
|
if ($this->return_vars_possibly_in_scope !== null) {
|
|
|
|
|
$this->return_vars_possibly_in_scope = array_merge(
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$context->vars_possibly_in_scope,
|
2019-03-06 17:12:36 +01:00
|
|
|
|
$this->return_vars_possibly_in_scope
|
2016-11-02 07:29:00 +01:00
|
|
|
|
);
|
|
|
|
|
} else {
|
2019-03-06 17:12:36 +01:00
|
|
|
|
$this->return_vars_possibly_in_scope = $context->vars_possibly_in_scope;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 00:21:12 +01:00
|
|
|
|
public function examineParamTypes(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
Context $context,
|
|
|
|
|
Codebase $codebase,
|
|
|
|
|
PhpParser\Node $stmt = null
|
2020-09-12 17:24:05 +02:00
|
|
|
|
): void {
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$storage = $this->getFunctionLikeStorage($statements_analyzer);
|
2019-03-03 21:11:09 +01:00
|
|
|
|
|
2020-06-24 17:51:24 +02:00
|
|
|
|
foreach ($storage->params as $param) {
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if ($param->by_ref && isset($context->vars_in_scope['$' . $param->name]) && !$param->is_variadic) {
|
|
|
|
|
$actual_type = $context->vars_in_scope['$' . $param->name];
|
2020-06-24 17:48:27 +02:00
|
|
|
|
$param_out_type = $param->out_type ?: $param->type;
|
2019-03-03 21:11:09 +01:00
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
|
if ($param_out_type && !$actual_type->hasMixed() && $param->location) {
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2019-05-31 07:47:35 +02:00
|
|
|
|
$codebase,
|
|
|
|
|
$actual_type,
|
|
|
|
|
$param_out_type,
|
|
|
|
|
$actual_type->ignore_nullable_issues,
|
|
|
|
|
$actual_type->ignore_falsable_issues
|
|
|
|
|
)
|
|
|
|
|
) {
|
2021-11-29 20:54:17 +01:00
|
|
|
|
IssueBuffer::maybeAdd(
|
2019-05-31 07:47:35 +02:00
|
|
|
|
new ReferenceConstraintViolation(
|
|
|
|
|
'Variable ' . '$' . $param->name . ' is limited to values of type '
|
|
|
|
|
. $param_out_type->getId()
|
|
|
|
|
. ' because it is passed by reference, '
|
|
|
|
|
. $actual_type->getId() . ' type found. Use @param-out to specify '
|
|
|
|
|
. 'a different output type',
|
|
|
|
|
$stmt
|
|
|
|
|
? new CodeLocation($this, $stmt)
|
|
|
|
|
: $param->location
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2021-11-29 20:54:17 +01:00
|
|
|
|
);
|
2019-03-03 21:11:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-02 20:16:49 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
|
public function getMethodName(): ?string
|
2017-02-22 06:51:34 +01:00
|
|
|
|
{
|
|
|
|
|
if ($this->function instanceof ClassMethod) {
|
|
|
|
|
return (string)$this->function->name;
|
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
|
|
return null;
|
2017-02-22 06:51:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
|
public function getCorrectlyCasedMethodId(?string $context_self = null): string
|
2017-12-07 21:50:25 +01:00
|
|
|
|
{
|
|
|
|
|
if ($this->function instanceof ClassMethod) {
|
|
|
|
|
$function_name = (string)$this->function->name;
|
|
|
|
|
|
|
|
|
|
return ($context_self ?: $this->source->getFQCLN()) . '::' . $function_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->function instanceof Function_) {
|
|
|
|
|
$namespace = $this->source->getNamespace();
|
|
|
|
|
|
|
|
|
|
return ($namespace ? $namespace . '\\' : '') . $this->function->name;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if (!$this instanceof ClosureAnalyzer) {
|
2021-12-03 21:40:18 +01:00
|
|
|
|
throw new UnexpectedValueException('This is weird');
|
2020-02-15 02:54:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->getClosureId();
|
2017-12-07 21:50:25 +01:00
|
|
|
|
}
|
2020-09-12 23:03:11 +02:00
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
|
public function getFunctionLikeStorage(?StatementsAnalyzer $statements_analyzer = null): FunctionLikeStorage
|
2017-09-04 02:52:54 +02:00
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase = $this->codebase;
|
2017-09-04 02:52:54 +02:00
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ($this->function instanceof ClassMethod && $this instanceof MethodAnalyzer) {
|
|
|
|
|
$method_id = $this->getMethodId();
|
2018-02-04 00:52:35 +01:00
|
|
|
|
$codebase_methods = $codebase->methods;
|
2018-01-25 07:04:26 +01:00
|
|
|
|
|
|
|
|
|
try {
|
2018-02-04 00:52:35 +01:00
|
|
|
|
return $codebase_methods->getStorage($method_id);
|
2021-12-03 21:40:18 +01:00
|
|
|
|
} catch (UnexpectedValueException $e) {
|
2018-03-03 22:52:48 +01:00
|
|
|
|
$declaring_method_id = $codebase_methods->getDeclaringMethodId($method_id);
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ($declaring_method_id === null) {
|
2021-12-03 21:40:18 +01:00
|
|
|
|
throw new UnexpectedValueException('Cannot get storage for function that doesn‘t exist');
|
2018-03-03 22:52:48 +01:00
|
|
|
|
}
|
2017-09-04 02:52:54 +02:00
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
|
// happens for fake constructors
|
2018-02-04 00:52:35 +01:00
|
|
|
|
return $codebase_methods->getStorage($declaring_method_id);
|
2017-09-04 02:52:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
|
if ($this instanceof FunctionAnalyzer) {
|
|
|
|
|
$function_id = $this->getFunctionId();
|
|
|
|
|
} elseif ($this instanceof ClosureAnalyzer) {
|
|
|
|
|
$function_id = $this->getClosureId();
|
|
|
|
|
} else {
|
2021-12-03 21:40:18 +01:00
|
|
|
|
throw new UnexpectedValueException('This is weird');
|
2020-02-15 02:54:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 16:18:05 +02:00
|
|
|
|
return $codebase->functions->getStorage($statements_analyzer, $function_id);
|
2020-02-15 02:54:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 16:18:05 +02:00
|
|
|
|
/** @return non-empty-string */
|
2021-12-05 18:51:26 +01:00
|
|
|
|
public function getId(): string
|
2020-02-15 02:54:26 +01:00
|
|
|
|
{
|
|
|
|
|
if ($this instanceof MethodAnalyzer) {
|
|
|
|
|
return (string) $this->getMethodId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this instanceof FunctionAnalyzer) {
|
|
|
|
|
return $this->getFunctionId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this instanceof ClosureAnalyzer) {
|
|
|
|
|
return $this->getClosureId();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 21:40:18 +01:00
|
|
|
|
throw new UnexpectedValueException('This is weird');
|
2017-09-04 02:52:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
|
/**
|
2020-12-29 12:42:12 +01:00
|
|
|
|
* @return array<lowercase-string, string>
|
2016-11-13 00:51:48 +01:00
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public function getAliasedClassesFlipped(): array
|
2016-11-13 00:51:48 +01:00
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($this->source instanceof NamespaceAnalyzer ||
|
|
|
|
|
$this->source instanceof FileAnalyzer ||
|
|
|
|
|
$this->source instanceof ClassLikeAnalyzer
|
2017-04-28 06:31:55 +02:00
|
|
|
|
) {
|
2016-11-13 17:24:46 +01:00
|
|
|
|
return $this->source->getAliasedClassesFlipped();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
2016-11-13 00:51:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-06 04:13:33 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public function getAliasedClassesFlippedReplaceable(): array
|
2019-06-06 04:13:33 +02:00
|
|
|
|
{
|
|
|
|
|
if ($this->source instanceof NamespaceAnalyzer ||
|
|
|
|
|
$this->source instanceof FileAnalyzer ||
|
|
|
|
|
$this->source instanceof ClassLikeAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
return $this->source->getAliasedClassesFlippedReplaceable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
|
/**
|
2020-11-27 17:43:23 +01:00
|
|
|
|
* @return array<string, array<string, Type\Union>>|null
|
2018-12-18 05:29:27 +01:00
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public function getTemplateTypeMap(): ?array
|
2018-12-18 05:29:27 +01:00
|
|
|
|
{
|
|
|
|
|
if ($this->source instanceof ClassLikeAnalyzer) {
|
|
|
|
|
return ($this->source->getTemplateTypeMap() ?: [])
|
|
|
|
|
+ ($this->storage->template_types ?: []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->storage->template_types;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public function isStatic(): bool
|
2016-08-14 05:26:45 +02:00
|
|
|
|
{
|
|
|
|
|
return $this->is_static;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
|
public function getCodebase(): Codebase
|
2018-11-06 03:57:36 +01:00
|
|
|
|
{
|
|
|
|
|
return $this->codebase;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
|
/**
|
|
|
|
|
* Get a list of suppressed issues
|
|
|
|
|
*
|
|
|
|
|
* @return array<string>
|
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public function getSuppressedIssues(): array
|
2017-01-07 20:35:07 +01:00
|
|
|
|
{
|
|
|
|
|
return $this->suppressed_issues;
|
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param array<int, string> $new_issues
|
|
|
|
|
*/
|
2020-10-12 21:02:52 +02:00
|
|
|
|
public function addSuppressedIssues(array $new_issues): void
|
2017-10-27 00:19:19 +02:00
|
|
|
|
{
|
2019-08-18 18:25:48 +02:00
|
|
|
|
if (isset($new_issues[0])) {
|
2021-12-03 21:07:25 +01:00
|
|
|
|
$new_issues = array_combine($new_issues, $new_issues);
|
2019-08-18 18:25:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->suppressed_issues = $new_issues + $this->suppressed_issues;
|
2017-10-27 00:19:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<int, string> $new_issues
|
|
|
|
|
*/
|
2020-10-12 21:02:52 +02:00
|
|
|
|
public function removeSuppressedIssues(array $new_issues): void
|
2017-10-27 00:19:19 +02:00
|
|
|
|
{
|
2019-08-18 18:25:48 +02:00
|
|
|
|
if (isset($new_issues[0])) {
|
2021-12-03 21:07:25 +01:00
|
|
|
|
$new_issues = array_combine($new_issues, $new_issues);
|
2019-08-18 18:25:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
|
$this->suppressed_issues = array_diff_key($this->suppressed_issues, $new_issues);
|
2017-10-27 00:19:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:09:09 +02:00
|
|
|
|
/**
|
|
|
|
|
* Adds a suppressed issue, useful when creating a method checker from scratch
|
|
|
|
|
*
|
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
|
public function addSuppressedIssue(string $issue_name): void
|
2017-07-26 22:09:09 +02:00
|
|
|
|
{
|
|
|
|
|
$this->suppressed_issues[] = $issue_name;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
|
public static function clearCache(): void
|
2017-01-27 07:23:12 +01:00
|
|
|
|
{
|
|
|
|
|
self::$no_effects_hashes = [];
|
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public function getLocalReturnType(Type\Union $storage_return_type, bool $final = false): Type\Union
|
2018-01-14 18:09:40 +01:00
|
|
|
|
{
|
|
|
|
|
if ($this->local_return_type) {
|
|
|
|
|
return $this->local_return_type;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$this->local_return_type = TypeExpander::expandUnion(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$this->codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
|
$storage_return_type,
|
|
|
|
|
$this->getFQCLN(),
|
2019-05-25 17:51:09 +02:00
|
|
|
|
$this->getFQCLN(),
|
2020-03-24 23:14:10 +01:00
|
|
|
|
$this->getParentFQCLN(),
|
|
|
|
|
true,
|
2020-03-29 19:37:37 +02:00
|
|
|
|
true,
|
2020-03-24 23:14:10 +01:00
|
|
|
|
$final
|
2018-01-14 18:09:40 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $this->local_return_type;
|
|
|
|
|
}
|
2021-01-05 23:50:23 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array{
|
2021-12-04 03:37:19 +01:00
|
|
|
|
* MethodIdentifier|null,
|
|
|
|
|
* MethodIdentifier|null,
|
|
|
|
|
* ClassLikeStorage|null,
|
2021-01-05 23:50:23 +01:00
|
|
|
|
* ?string,
|
|
|
|
|
* ?string,
|
2021-12-04 03:37:19 +01:00
|
|
|
|
* array<string, MethodIdentifier>
|
2021-01-05 23:50:23 +01:00
|
|
|
|
* }|null
|
|
|
|
|
*/
|
|
|
|
|
private function getFunctionInformation(
|
|
|
|
|
Context $context,
|
|
|
|
|
Codebase $codebase,
|
2021-12-03 20:11:20 +01:00
|
|
|
|
NodeDataProvider $type_provider,
|
|
|
|
|
FunctionLikeStorage $storage,
|
2021-01-05 23:50:23 +01:00
|
|
|
|
bool $add_mutations
|
|
|
|
|
): ?array {
|
|
|
|
|
$classlike_storage_provider = $codebase->classlike_storage_provider;
|
|
|
|
|
$real_method_id = null;
|
|
|
|
|
$method_id = null;
|
|
|
|
|
|
|
|
|
|
$cased_method_id = null;
|
|
|
|
|
$hash = null;
|
|
|
|
|
$appearing_class_storage = null;
|
|
|
|
|
$overridden_method_ids = [];
|
|
|
|
|
|
2021-02-09 17:40:52 +01:00
|
|
|
|
if ($this instanceof MethodAnalyzer) {
|
|
|
|
|
if (!$storage instanceof MethodStorage) {
|
2021-12-03 21:40:18 +01:00
|
|
|
|
throw new UnexpectedValueException('$storage must be MethodStorage');
|
2021-01-05 23:50:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$real_method_id = $this->getMethodId();
|
|
|
|
|
|
|
|
|
|
$method_id = $this->getMethodId($context->self);
|
|
|
|
|
|
|
|
|
|
$fq_class_name = (string)$context->self;
|
|
|
|
|
$appearing_class_storage = $classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
|
|
if ($add_mutations) {
|
|
|
|
|
if (!$context->collect_initializations) {
|
|
|
|
|
$hash = md5($real_method_id . '::' . $context->getScopeSummary());
|
|
|
|
|
|
|
|
|
|
// if we know that the function has no effects on vars, we don't bother rechecking
|
|
|
|
|
if (isset(self::$no_effects_hashes[$hash])) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($context->self) {
|
|
|
|
|
if ($appearing_class_storage->template_types) {
|
|
|
|
|
$template_params = [];
|
|
|
|
|
|
|
|
|
|
foreach ($appearing_class_storage->template_types as $param_name => $template_map) {
|
|
|
|
|
$key = array_keys($template_map)[0];
|
|
|
|
|
|
|
|
|
|
$template_params[] = new Type\Union([
|
|
|
|
|
new Type\Atomic\TTemplateParam(
|
|
|
|
|
$param_name,
|
2021-12-03 21:07:25 +01:00
|
|
|
|
reset($template_map),
|
2021-01-05 23:50:23 +01:00
|
|
|
|
$key
|
|
|
|
|
)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this_object_type = new Type\Atomic\TGenericObject(
|
|
|
|
|
$context->self,
|
|
|
|
|
$template_params
|
|
|
|
|
);
|
|
|
|
|
$this_object_type->was_static = !$storage->final;
|
|
|
|
|
} else {
|
|
|
|
|
$this_object_type = new TNamedObject($context->self);
|
|
|
|
|
$this_object_type->was_static = !$storage->final;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context->vars_in_scope['$this'] = new Type\Union([$this_object_type]);
|
|
|
|
|
|
|
|
|
|
if ($codebase->taint_flow_graph
|
|
|
|
|
&& $storage->specialize_call
|
|
|
|
|
&& $storage->location
|
|
|
|
|
) {
|
|
|
|
|
$new_parent_node = DataFlowNode::getForAssignment('$this in ' . $method_id, $storage->location);
|
|
|
|
|
|
|
|
|
|
$codebase->taint_flow_graph->addNode($new_parent_node);
|
|
|
|
|
$context->vars_in_scope['$this']->parent_nodes += [$new_parent_node->id => $new_parent_node];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($storage->external_mutation_free
|
|
|
|
|
&& !$storage->mutation_free_inferred
|
|
|
|
|
) {
|
|
|
|
|
$context->vars_in_scope['$this']->reference_free = true;
|
|
|
|
|
|
|
|
|
|
if ($this->function->name->name !== '__construct') {
|
|
|
|
|
$context->vars_in_scope['$this']->allow_mutations = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context->vars_possibly_in_scope['$this'] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($appearing_class_storage->has_visitor_issues) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cased_method_id = $fq_class_name . '::' . $storage->cased_name;
|
|
|
|
|
|
|
|
|
|
$overridden_method_ids = $codebase->methods->getOverriddenMethodIds($method_id);
|
|
|
|
|
|
|
|
|
|
$codeLocation = new CodeLocation(
|
|
|
|
|
$this,
|
|
|
|
|
$this->function,
|
|
|
|
|
null,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($overridden_method_ids
|
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
|
) {
|
|
|
|
|
foreach ($overridden_method_ids as $overridden_method_id) {
|
|
|
|
|
$parent_method_storage = $codebase->methods->getStorage($overridden_method_id);
|
|
|
|
|
|
|
|
|
|
$overridden_fq_class_name = $overridden_method_id->fq_class_name;
|
|
|
|
|
|
|
|
|
|
$parent_storage = $classlike_storage_provider->get($overridden_fq_class_name);
|
|
|
|
|
|
|
|
|
|
if ($this->function->name->name === '__construct'
|
|
|
|
|
&& !$parent_storage->preserve_constructor_signature
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$implementer_visibility = $storage->visibility;
|
|
|
|
|
|
|
|
|
|
$implementer_appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
|
|
|
|
|
$implementer_declaring_method_id = $real_method_id;
|
|
|
|
|
|
|
|
|
|
$declaring_class_storage = $appearing_class_storage;
|
|
|
|
|
|
|
|
|
|
if ($implementer_appearing_method_id
|
|
|
|
|
&& $implementer_appearing_method_id !== $implementer_declaring_method_id
|
|
|
|
|
) {
|
|
|
|
|
$appearing_fq_class_name = $implementer_appearing_method_id->fq_class_name;
|
|
|
|
|
$appearing_method_name = $implementer_appearing_method_id->method_name;
|
|
|
|
|
|
|
|
|
|
$declaring_fq_class_name = $implementer_declaring_method_id->fq_class_name;
|
|
|
|
|
|
|
|
|
|
$appearing_class_storage = $classlike_storage_provider->get(
|
|
|
|
|
$appearing_fq_class_name
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$declaring_class_storage = $classlike_storage_provider->get(
|
|
|
|
|
$declaring_fq_class_name
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isset($appearing_class_storage->trait_visibility_map[$appearing_method_name])) {
|
|
|
|
|
$implementer_visibility
|
|
|
|
|
= $appearing_class_storage->trait_visibility_map[$appearing_method_name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we've already checked this in the class checker
|
|
|
|
|
if (!isset($appearing_class_storage->class_implements[strtolower($overridden_fq_class_name)])) {
|
|
|
|
|
MethodComparator::compare(
|
|
|
|
|
$codebase,
|
2021-12-03 21:07:25 +01:00
|
|
|
|
count($overridden_method_ids) === 1 ? $this->function : null,
|
2021-01-05 23:50:23 +01:00
|
|
|
|
$declaring_class_storage,
|
|
|
|
|
$parent_storage,
|
|
|
|
|
$storage,
|
|
|
|
|
$parent_method_storage,
|
|
|
|
|
$fq_class_name,
|
|
|
|
|
$implementer_visibility,
|
|
|
|
|
$codeLocation,
|
|
|
|
|
$storage->suppressed_issues
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MethodAnalyzer::checkMethodSignatureMustOmitReturnType($storage, $codeLocation);
|
|
|
|
|
|
|
|
|
|
if (!$context->calling_method_id || !$context->collect_initializations) {
|
|
|
|
|
$context->calling_method_id = strtolower((string)$method_id);
|
|
|
|
|
}
|
2021-02-09 17:40:52 +01:00
|
|
|
|
} elseif ($this instanceof FunctionAnalyzer) {
|
2021-01-05 23:50:23 +01:00
|
|
|
|
$function_name = $this->function->name->name;
|
|
|
|
|
$namespace_prefix = $this->getNamespace();
|
|
|
|
|
$cased_method_id = ($namespace_prefix !== null ? $namespace_prefix . '\\' : '') . $function_name;
|
|
|
|
|
$context->calling_function_id = strtolower($cased_method_id);
|
2021-02-09 17:40:52 +01:00
|
|
|
|
} elseif ($this instanceof ClosureAnalyzer) {
|
2021-01-05 23:50:23 +01:00
|
|
|
|
if ($storage->return_type) {
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$closure_return_type = TypeExpander::expandUnion(
|
2021-01-05 23:50:23 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$storage->return_type,
|
|
|
|
|
$context->self,
|
|
|
|
|
$context->self,
|
|
|
|
|
$this->getParentFQCLN()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$closure_return_type = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$closure_type = new Type\Atomic\TClosure(
|
|
|
|
|
'Closure',
|
|
|
|
|
$storage->params,
|
|
|
|
|
$closure_return_type
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($storage instanceof FunctionStorage) {
|
|
|
|
|
$closure_type->byref_uses = $storage->byref_uses;
|
|
|
|
|
$closure_type->is_pure = $storage->pure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$type_provider->setType(
|
|
|
|
|
$this->function,
|
|
|
|
|
new Type\Union([
|
|
|
|
|
$closure_type,
|
|
|
|
|
])
|
|
|
|
|
);
|
2021-02-09 17:40:52 +01:00
|
|
|
|
} else {
|
2021-12-03 21:40:18 +01:00
|
|
|
|
throw new UnexpectedValueException('Impossible');
|
2021-01-05 23:50:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
$real_method_id,
|
|
|
|
|
$method_id,
|
|
|
|
|
$appearing_class_storage,
|
|
|
|
|
$hash,
|
|
|
|
|
$cased_method_id,
|
|
|
|
|
$overridden_method_ids
|
|
|
|
|
];
|
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
}
|