2016-08-14 05:26:45 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker;
|
|
|
|
|
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_;
|
|
|
|
use PhpParser;
|
2016-12-07 01:41:52 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-12-11 19:48:11 +01:00
|
|
|
use Psalm\Checker\TypeChecker;
|
2017-01-06 07:07:11 +01:00
|
|
|
use Psalm\Config;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\EffectsAnalyser;
|
2016-12-07 01:41:52 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\FunctionLikeParameter;
|
2017-01-02 07:20:47 +01:00
|
|
|
use Psalm\Issue\DuplicateParam;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-12-31 05:40:32 +01:00
|
|
|
use Psalm\Issue\InvalidParamDefault;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\Issue\InvalidReturnType;
|
2016-12-09 18:48:02 +01:00
|
|
|
use Psalm\Issue\InvalidToString;
|
2017-03-18 17:18:17 +01:00
|
|
|
use Psalm\Issue\LessSpecificReturnType;
|
2016-10-16 00:01:04 +02:00
|
|
|
use Psalm\Issue\MethodSignatureMismatch;
|
2017-01-13 19:40:20 +01:00
|
|
|
use Psalm\Issue\MisplacedRequiredParam;
|
2017-01-16 07:22:36 +01:00
|
|
|
use Psalm\Issue\MissingClosureReturnType;
|
2016-11-07 23:07:59 +01:00
|
|
|
use Psalm\Issue\MissingReturnType;
|
2016-11-05 22:53:30 +01:00
|
|
|
use Psalm\Issue\MixedInferredReturnType;
|
2017-01-17 17:17:49 +01:00
|
|
|
use Psalm\Issue\MoreSpecificReturnType;
|
2017-01-13 18:03:22 +01:00
|
|
|
use Psalm\Issue\OverriddenMethodAccess;
|
2017-02-12 00:56:38 +01:00
|
|
|
use Psalm\Issue\PossiblyUnusedVariable;
|
|
|
|
use Psalm\Issue\UnusedVariable;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2017-02-18 19:41:27 +01:00
|
|
|
use Psalm\Mutator\FileMutator;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\StatementsSource;
|
2017-01-06 07:07:11 +01:00
|
|
|
use Psalm\Storage\FunctionLikeStorage;
|
|
|
|
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;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
abstract class FunctionLikeChecker extends SourceChecker implements StatementsSource
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2017-01-28 06:24:25 +01:00
|
|
|
const RETURN_TYPE_REGEX = '/\\:\s+(\\??[A-Za-z0-9_\\\\\[\]]+)/';
|
|
|
|
const PARAM_TYPE_REGEX = '/^(\\??[A-Za-z0-9_\\\\\[\]]+)\s/';
|
2016-12-06 22:33:47 +01:00
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2016-10-15 06:12:57 +02:00
|
|
|
* @var Closure|Function_|ClassMethod
|
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
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StatementsChecker|null
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $statements_checker;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StatementsSource
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $source;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
2016-12-24 19:23:22 +01:00
|
|
|
* @var array<string, 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
|
|
|
|
|
|
|
/**
|
2017-02-24 01:36:51 +01:00
|
|
|
* @var array<string, array<string, bool|string>>
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, array>
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected static $no_effects_hashes = [];
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param Closure|Function_|ClassMethod $function
|
|
|
|
* @param StatementsSource $source
|
|
|
|
*/
|
|
|
|
public function __construct($function, StatementsSource $source)
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
|
|
|
$this->function = $function;
|
|
|
|
$this->source = $source;
|
2017-03-20 04:30:20 +01:00
|
|
|
$this->file_checker = $source->getFileChecker();
|
2016-08-14 05:26:45 +02:00
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-11-05 22:53:30 +01:00
|
|
|
* @param Context $context
|
|
|
|
* @param Context|null $global_context
|
2017-01-12 03:37:53 +01:00
|
|
|
* @param bool $add_mutations whether or not to add mutations to this method
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public function analyze(Context $context, Context $global_context = null, $add_mutations = false)
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
/** @var array<PhpParser\Node\Expr|PhpParser\Node\Stmt> */
|
2016-12-07 20:13:39 +01:00
|
|
|
$function_stmts = $this->function->getStmts() ?: [];
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
$hash = null;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$cased_method_id = null;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$class_storage = null;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
if ($this->function instanceof ClassMethod) {
|
2017-01-12 06:54:41 +01:00
|
|
|
$real_method_id = (string)$this->getMethodId();
|
|
|
|
|
|
|
|
$method_id = (string)$this->getMethodId($context->self);
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($add_mutations) {
|
2016-12-07 20:13:39 +01:00
|
|
|
$hash = $this->getMethodId() . json_encode([
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
]);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
// if we know that the function has no effects on vars, we don't bother rechecking
|
|
|
|
if (isset(self::$no_effects_hashes[$hash])) {
|
|
|
|
list(
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
) = self::$no_effects_hashes[$hash];
|
2016-10-16 00:01:04 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
return null;
|
2016-11-07 05:29:54 +01:00
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
} elseif ($context->self) {
|
2017-01-15 01:06:58 +01:00
|
|
|
$context->vars_in_scope['$this'] = new Type\Union([new TNamedObject($context->self)]);
|
2017-02-08 08:23:17 +01:00
|
|
|
$context->vars_possibly_in_scope['$this'] = true;
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
2016-11-07 05:29:54 +01:00
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
$declaring_method_id = (string)MethodChecker::getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
if ($declaring_method_id !== $real_method_id) {
|
|
|
|
// this trait method has been overridden, so we don't care about it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$fq_class_name = (string)$context->self;
|
2016-11-05 03:04:55 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$class_storage = ClassLikeChecker::$storage[strtolower($fq_class_name)];
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
$storage = MethodChecker::getStorage($declaring_method_id);
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
|
|
$cased_method_id = $fq_class_name . '::' . $storage->cased_name;
|
2016-12-30 18:41:14 +01:00
|
|
|
|
|
|
|
$implemented_method_ids = MethodChecker::getOverriddenMethodIds($method_id);
|
2016-10-18 22:00:03 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
if ($implemented_method_ids) {
|
|
|
|
$have_emitted = false;
|
2016-10-16 00:45:31 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
foreach ($implemented_method_ids as $implemented_method_id) {
|
2017-02-09 03:19:47 +01:00
|
|
|
if ($this->function->name === '__construct') {
|
2016-12-07 20:13:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-07 05:29:54 +01:00
|
|
|
|
2017-02-09 23:49:13 +01:00
|
|
|
list($implemented_fq_class_name) = explode('::', $implemented_method_id);
|
|
|
|
|
|
|
|
$class_storage = ClassLikeChecker::$storage[strtolower($implemented_fq_class_name)];
|
|
|
|
|
2017-01-13 18:03:22 +01:00
|
|
|
$implemented_storage = MethodChecker::getStorage($implemented_method_id);
|
|
|
|
|
|
|
|
if ($implemented_storage->visibility < $storage->visibility) {
|
|
|
|
$parent_method_id = MethodChecker::getCasedMethodId($implemented_method_id);
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new OverriddenMethodAccess(
|
|
|
|
'Method ' . $cased_method_id .' has different access level than ' . $parent_method_id,
|
|
|
|
new CodeLocation($this, $this->function, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
|
2017-01-13 18:03:22 +01:00
|
|
|
$implemented_params = $implemented_storage->params;
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
foreach ($implemented_params as $i => $implemented_param) {
|
2017-01-06 07:07:11 +01:00
|
|
|
if (!isset($storage->params[$i])) {
|
2016-12-07 20:13:39 +01:00
|
|
|
$parent_method_id = MethodChecker::getCasedMethodId($implemented_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Method ' . $cased_method_id .' has fewer arguments than parent method ' .
|
|
|
|
$parent_method_id,
|
2017-01-13 18:03:22 +01:00
|
|
|
new CodeLocation($this, $this->function, true)
|
2016-12-07 20:13:39 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-10-16 00:01:04 +02:00
|
|
|
}
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
$have_emitted = true;
|
2017-03-15 01:14:25 +01:00
|
|
|
break 2;
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
|
2017-02-09 23:49:13 +01:00
|
|
|
if ($class_storage->user_defined &&
|
|
|
|
(string)$storage->params[$i]->signature_type !== (string)$implemented_param->signature_type
|
2016-12-07 20:13:39 +01:00
|
|
|
) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId((string)$this->getMethodId());
|
|
|
|
$parent_method_id = MethodChecker::getCasedMethodId($implemented_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_method_id .' has wrong type \'' .
|
2017-01-06 07:07:11 +01:00
|
|
|
$storage->params[$i]->signature_type . '\', expecting \'' .
|
2016-12-07 20:13:39 +01:00
|
|
|
$implemented_param->signature_type . '\' as defined by ' .
|
|
|
|
$parent_method_id,
|
2017-03-02 00:36:04 +01:00
|
|
|
$storage->params[$i]->location ?: new CodeLocation($this, $this->function, true)
|
2016-12-07 20:13:39 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-10-16 00:01:04 +02:00
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
|
|
|
$have_emitted = true;
|
2017-03-15 01:14:25 +01:00
|
|
|
break 2;
|
2016-10-16 00:01:04 +02:00
|
|
|
}
|
2017-02-09 23:49:13 +01:00
|
|
|
|
|
|
|
if (!$class_storage->user_defined &&
|
|
|
|
!$implemented_param->type->isMixed() &&
|
|
|
|
(string)$storage->params[$i]->type !== (string)$implemented_param->type
|
|
|
|
) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId((string)$this->getMethodId());
|
|
|
|
$parent_method_id = MethodChecker::getCasedMethodId($implemented_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_method_id .' has wrong type \'' .
|
|
|
|
$storage->params[$i]->type . '\', expecting \'' .
|
|
|
|
$implemented_param->type . '\' as defined by ' .
|
|
|
|
$parent_method_id,
|
2017-03-02 00:36:04 +01:00
|
|
|
$storage->params[$i]->location ?: new CodeLocation($this, $this->function, true)
|
2017-02-09 23:49:13 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$have_emitted = true;
|
2017-03-15 01:14:25 +01:00
|
|
|
break 2;
|
2017-02-09 23:49:13 +01:00
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
}
|
2017-01-13 18:03:22 +01:00
|
|
|
|
|
|
|
if ($storage->cased_name !== '__construct' &&
|
|
|
|
$storage->required_param_count > $implemented_storage->required_param_count
|
|
|
|
) {
|
|
|
|
$parent_method_id = MethodChecker::getCasedMethodId($implemented_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Method ' . $cased_method_id .' has more arguments than parent method ' .
|
|
|
|
$parent_method_id,
|
|
|
|
new CodeLocation($this, $this->function, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$have_emitted = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
} elseif ($this->function instanceof Function_) {
|
2017-01-07 20:35:07 +01:00
|
|
|
$storage = FileChecker::$storage[$this->source->getFilePath()]->functions[(string)$this->getMethodId()];
|
2016-12-07 20:13:39 +01:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$cased_method_id = $this->function->name;
|
|
|
|
} else { // Closure
|
|
|
|
$storage = self::register($this->function, $this->getSource());
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2016-12-15 01:19:25 +01:00
|
|
|
/** @var PhpParser\Node\Expr\Closure $this->function */
|
2016-12-07 20:13:39 +01:00
|
|
|
$this->function->inferredType = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new Type\Atomic\Fn(
|
2016-12-07 20:13:39 +01:00
|
|
|
'Closure',
|
2017-01-06 07:07:11 +01:00
|
|
|
$storage->params,
|
|
|
|
$storage->return_type ?: Type::getMixed()
|
2016-12-07 20:13:39 +01:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2017-01-19 23:58:08 +01:00
|
|
|
$this->suppressed_issues = array_merge(
|
|
|
|
$this->getSource()->getSuppressedIssues(),
|
|
|
|
$storage->suppressed_issues
|
|
|
|
);
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
|
|
if ($storage instanceof MethodStorage && $storage->is_static) {
|
|
|
|
$this->is_static = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_checker = new StatementsChecker($this);
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$template_types = $storage->template_types;
|
|
|
|
|
|
|
|
if ($class_storage && $class_storage->template_types) {
|
|
|
|
$template_types = array_merge($template_types ?: [], $class_storage->template_types);
|
|
|
|
}
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
foreach ($storage->params as $offset => $function_param) {
|
2017-02-10 06:14:44 +01:00
|
|
|
$param_type = clone $function_param->type;
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
$param_type = ExpressionChecker::fleshOutTypes(
|
2017-02-10 06:14:44 +01:00
|
|
|
$param_type,
|
2016-12-07 20:13:39 +01:00
|
|
|
$context->self,
|
|
|
|
$this->getMethodId()
|
|
|
|
);
|
2016-12-04 01:11:30 +01:00
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
if (!$function_param->location) {
|
2016-12-07 20:13:39 +01:00
|
|
|
throw new \UnexpectedValueException('We should know where this code is');
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:40:32 +01:00
|
|
|
$parser_param = $this->function->getParams()[$offset];
|
|
|
|
|
|
|
|
if ($parser_param->default) {
|
|
|
|
$default_type = StatementsChecker::getSimpleType($parser_param->default);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($default_type &&
|
|
|
|
!TypeChecker::isContainedBy(
|
|
|
|
$default_type,
|
|
|
|
$param_type,
|
|
|
|
$statements_checker->getFileChecker()
|
|
|
|
)
|
|
|
|
) {
|
2017-01-02 07:07:44 +01:00
|
|
|
$method_id = $this->getMethodId();
|
|
|
|
|
2016-12-31 05:40:32 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidParamDefault(
|
2017-01-02 07:07:44 +01:00
|
|
|
'Default value for argument ' . ($offset + 1) . ' of method ' . $cased_method_id .
|
2016-12-31 05:40:32 +01:00
|
|
|
' does not match the given type ' . $param_type,
|
2017-03-02 00:36:04 +01:00
|
|
|
$function_param->location
|
2016-12-31 05:40:32 +01:00
|
|
|
)
|
|
|
|
)) {
|
2016-12-31 06:14:00 +01:00
|
|
|
// fall through
|
2016-12-31 05:40:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($template_types) {
|
2017-02-10 06:14:44 +01:00
|
|
|
$substituted_type = clone $param_type;
|
2017-02-10 04:57:23 +01:00
|
|
|
$generic_types = [];
|
2017-02-10 06:14:44 +01:00
|
|
|
$substituted_type->replaceTemplateTypes($template_types, $generic_types, null);
|
2017-03-02 18:19:18 +01:00
|
|
|
$substituted_type->check($this->source, $function_param->location, $this->suppressed_issues, [], false);
|
2017-02-10 02:35:17 +01:00
|
|
|
} else {
|
2017-03-02 18:19:18 +01:00
|
|
|
$param_type->check($this->source, $function_param->location, $this->suppressed_issues, [], false);
|
2017-03-02 00:36:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->getFileChecker()->project_checker->collect_references) {
|
|
|
|
if ($function_param->location !== $function_param->signature_location &&
|
|
|
|
$function_param->signature_location &&
|
|
|
|
$function_param->signature_type
|
|
|
|
) {
|
|
|
|
$function_param->signature_type->check(
|
|
|
|
$this->source,
|
|
|
|
$function_param->signature_location,
|
2017-03-02 18:19:18 +01:00
|
|
|
$this->suppressed_issues,
|
|
|
|
[],
|
|
|
|
false
|
2017-03-02 00:36:04 +01:00
|
|
|
);
|
|
|
|
}
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
$context->vars_in_scope['$' . $function_param->name] = $param_type;
|
2017-02-08 08:23:17 +01:00
|
|
|
$context->vars_possibly_in_scope['$' . $function_param->name] = true;
|
|
|
|
|
2017-02-23 06:25:28 +01:00
|
|
|
if ($function_param->by_ref && !$param_type->isMixed()) {
|
|
|
|
$context->byref_constraints['$' . $function_param->name] = new \Psalm\ReferenceConstraint($param_type);
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:23:17 +01: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);
|
|
|
|
}
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
$statements_checker->registerVariable(
|
2017-02-08 08:23:17 +01:00
|
|
|
'$' . $function_param->name,
|
2017-03-02 00:36:04 +01:00
|
|
|
$function_param->location
|
2016-12-07 20:13:39 +01:00
|
|
|
);
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
$statements_checker->analyze($function_stmts, $context, null, $global_context);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
if ($this->function instanceof Closure) {
|
|
|
|
$closure_yield_types = [];
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
$this->verifyReturnType(
|
2016-12-07 20:13:39 +01:00
|
|
|
false,
|
2017-01-06 07:07:11 +01:00
|
|
|
$storage->return_type,
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source->getFQCLN(),
|
2017-01-06 07:07:11 +01:00
|
|
|
$storage->return_type_location
|
2016-12-07 20:13:39 +01:00
|
|
|
);
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
if (!$storage->return_type || $storage->return_type->isMixed()) {
|
2016-12-07 20:13:39 +01:00
|
|
|
$closure_yield_types = [];
|
|
|
|
$closure_return_types = EffectsAnalyser::getReturnTypes(
|
|
|
|
$this->function->stmts,
|
|
|
|
$closure_yield_types,
|
|
|
|
true
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-12-15 01:19:25 +01:00
|
|
|
if ($closure_return_types && $this->function->inferredType) {
|
2017-01-15 01:06:58 +01:00
|
|
|
/** @var Type\Atomic\Fn */
|
2016-12-15 01:19:25 +01:00
|
|
|
$closure_atomic = $this->function->inferredType->types['Closure'];
|
|
|
|
$closure_atomic->return_type = new Type\Union($closure_return_types);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2017-02-27 07:30:44 +01:00
|
|
|
if ($context->collect_references &&
|
|
|
|
!$this->getFileChecker()->project_checker->find_references_to &&
|
|
|
|
$context->check_variables
|
|
|
|
) {
|
2017-02-02 06:45:23 +01:00
|
|
|
foreach ($context->vars_possibly_in_scope as $var_name => $_) {
|
2017-02-01 05:24:33 +01:00
|
|
|
if (strpos($var_name, '->') === false &&
|
|
|
|
$var_name !== '$this' &&
|
|
|
|
strpos($var_name, '::$') === false &&
|
2017-02-02 06:45:23 +01:00
|
|
|
strpos($var_name, '[') === false &&
|
2017-02-12 00:56:38 +01:00
|
|
|
$var_name !== '$_'
|
2017-02-01 05:24:33 +01:00
|
|
|
) {
|
2017-02-08 00:18:33 +01:00
|
|
|
$original_location = $statements_checker->getFirstAppearance($var_name);
|
|
|
|
|
|
|
|
if (!isset($context->referenced_vars[$var_name]) && $original_location) {
|
2017-02-12 00:56:38 +01:00
|
|
|
if (!isset($storage->param_types[substr($var_name, 1)]) ||
|
|
|
|
!$storage instanceof MethodStorage ||
|
|
|
|
$storage->visibility === ClassLikeChecker::VISIBILITY_PRIVATE
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnusedVariable(
|
|
|
|
'Variable ' . $var_name . ' is never referenced',
|
|
|
|
$original_location
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUnusedVariable(
|
|
|
|
'Variable ' . $var_name . ' is never referenced in this method',
|
|
|
|
$original_location
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-02-08 00:18:33 +01:00
|
|
|
}
|
2017-02-01 05:24:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($add_mutations) {
|
|
|
|
if (isset($this->return_vars_in_scope[''])) {
|
|
|
|
$context->vars_in_scope = TypeChecker::combineKeyedTypes(
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$this->return_vars_in_scope['']
|
|
|
|
);
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if (isset($this->return_vars_possibly_in_scope[''])) {
|
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$this->return_vars_possibly_in_scope['']
|
|
|
|
);
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
2017-02-11 01:10:13 +01:00
|
|
|
foreach ($context->vars_in_scope as $var => $_) {
|
2017-01-28 02:54:27 +01:00
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
2017-01-12 03:37:53 +01:00
|
|
|
unset($context->vars_in_scope[$var]);
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 01:10:13 +01:00
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $_) {
|
2017-01-28 02:54:27 +01:00
|
|
|
if (strpos($var, '$this->') !== 0 && $var !== '$this') {
|
2017-01-12 03:37:53 +01:00
|
|
|
unset($context->vars_possibly_in_scope[$var]);
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($hash && $this instanceof MethodChecker) {
|
|
|
|
self::$no_effects_hashes[$hash] = [
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
];
|
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
/**
|
|
|
|
* @param Closure|Function_|ClassMethod $function
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @return FunctionLikeStorage
|
|
|
|
*/
|
|
|
|
public static function register(
|
|
|
|
$function,
|
|
|
|
StatementsSource $source
|
|
|
|
) {
|
|
|
|
$namespace = $source->getNamespace();
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
$class_storage = null;
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
if ($function instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
$cased_function_id = ($namespace ? $namespace . '\\' : '') . $function->name;
|
|
|
|
$function_id = strtolower($cased_function_id);
|
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
$project_checker = $source->getFileChecker()->project_checker;
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
if ($project_checker->register_global_functions) {
|
|
|
|
$storage = FunctionChecker::$stubbed_functions[$function_id] = new FunctionLikeStorage();
|
|
|
|
} else {
|
|
|
|
$file_storage = FileChecker::$storage[$source->getFilePath()];
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
if (isset($file_storage->functions[$function_id])) {
|
|
|
|
return $file_storage->functions[$function_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $file_storage->functions[$function_id] = new FunctionLikeStorage();
|
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
} elseif ($function instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2017-01-07 20:35:07 +01:00
|
|
|
$fq_class_name = (string)$source->getFQCLN();
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
|
|
$function_id = $fq_class_name . '::' . strtolower($function->name);
|
|
|
|
$cased_function_id = $fq_class_name . '::' . $function->name;
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$fq_class_name_lower = strtolower($fq_class_name);
|
|
|
|
|
|
|
|
if (!isset(ClassLikeChecker::$storage[$fq_class_name_lower])) {
|
2017-01-06 07:07:11 +01:00
|
|
|
throw new \UnexpectedValueException('$class_storage cannot be empty for ' . $function_id);
|
|
|
|
}
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_storage = ClassLikeChecker::$storage[$fq_class_name_lower];
|
2017-01-06 07:07:11 +01:00
|
|
|
|
|
|
|
if (isset($class_storage->methods[strtolower($function->name)])) {
|
|
|
|
throw new \InvalidArgumentException('Cannot re-register ' . $function_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $class_storage->methods[strtolower($function->name)] = new MethodStorage();
|
|
|
|
|
|
|
|
$class_name_parts = explode('\\', $fq_class_name);
|
|
|
|
$class_name = array_pop($class_name_parts);
|
|
|
|
|
|
|
|
if (strtolower((string)$function->name) === strtolower($class_name)) {
|
|
|
|
MethodChecker::setDeclaringMethodId($fq_class_name . '::__construct', $function_id);
|
|
|
|
MethodChecker::setAppearingMethodId($fq_class_name . '::__construct', $function_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_storage->declaring_method_ids[strtolower($function->name)] = $function_id;
|
|
|
|
$class_storage->appearing_method_ids[strtolower($function->name)] = $function_id;
|
|
|
|
|
|
|
|
if (!isset($class_storage->overridden_method_ids[strtolower($function->name)])) {
|
|
|
|
$class_storage->overridden_method_ids[strtolower($function->name)] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
$storage->is_static = $function->isStatic();
|
|
|
|
|
|
|
|
if ($function->isPrivate()) {
|
|
|
|
$storage->visibility = ClassLikeChecker::VISIBILITY_PRIVATE;
|
|
|
|
} elseif ($function->isProtected()) {
|
|
|
|
$storage->visibility = ClassLikeChecker::VISIBILITY_PROTECTED;
|
|
|
|
} else {
|
|
|
|
$storage->visibility = ClassLikeChecker::VISIBILITY_PUBLIC;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$function_id = $cased_function_id = 'closure';
|
|
|
|
|
|
|
|
$storage = new FunctionLikeStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($function instanceof ClassMethod || $function instanceof Function_) {
|
|
|
|
$storage->cased_name = $function->name;
|
|
|
|
}
|
|
|
|
|
2017-02-08 06:28:26 +01:00
|
|
|
$storage->location = new CodeLocation($source, $function, true);
|
2017-01-06 07:07:11 +01:00
|
|
|
$storage->namespace = $source->getNamespace();
|
|
|
|
|
2017-01-13 18:03:22 +01:00
|
|
|
$required_param_count = 0;
|
|
|
|
$i = 0;
|
2017-01-13 19:40:20 +01:00
|
|
|
$has_optional_param = false;
|
2017-01-13 18:03:22 +01:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
/** @var PhpParser\Node\Param $param */
|
|
|
|
foreach ($function->getParams() as $param) {
|
|
|
|
$param_array = self::getTranslatedParam($param, $source);
|
|
|
|
|
|
|
|
if (isset($storage->param_types[$param_array->name])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateParam(
|
|
|
|
'Duplicate param $' . $param->name . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($source, $param, true)
|
|
|
|
),
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->param_types[$param_array->name] = $param_array->type;
|
|
|
|
$storage->params[] = $param_array;
|
2017-01-13 18:03:22 +01:00
|
|
|
|
|
|
|
if (!$param_array->is_optional) {
|
|
|
|
$required_param_count = $i + 1;
|
2017-01-13 19:40:20 +01:00
|
|
|
|
|
|
|
if (!$param->variadic && $has_optional_param) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MisplacedRequiredParam(
|
2017-01-15 22:43:49 +01:00
|
|
|
'Required param $' . $param->name . ' should come before any optional params in ' .
|
|
|
|
$cased_function_id,
|
2017-01-13 19:40:20 +01:00
|
|
|
new CodeLocation($source, $param, true)
|
|
|
|
),
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$has_optional_param = true;
|
2017-01-13 18:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
2017-01-06 07:07:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 18:03:22 +01:00
|
|
|
$storage->required_param_count = $required_param_count;
|
|
|
|
|
2017-01-15 22:43:49 +01:00
|
|
|
if ($function->stmts) {
|
|
|
|
// look for constant declarations
|
|
|
|
foreach ($function->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\FuncCall &&
|
|
|
|
$stmt->name instanceof PhpParser\Node\Name &&
|
|
|
|
$stmt->name->parts === ['define']
|
|
|
|
) {
|
|
|
|
$first_arg_value = isset($stmt->args[0]) ? $stmt->args[0]->value : null;
|
|
|
|
$second_arg_value = isset($stmt->args[1]) ? $stmt->args[1]->value : null;
|
|
|
|
|
|
|
|
if ($first_arg_value instanceof PhpParser\Node\Scalar\String_ && $second_arg_value) {
|
|
|
|
$storage->defined_constants[$first_arg_value->value] =
|
|
|
|
StatementsChecker::getSimpleType($second_arg_value) ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
if ($parser_return_type = $function->getReturnType()) {
|
|
|
|
$suffix = '';
|
|
|
|
|
|
|
|
if ($parser_return_type instanceof PhpParser\Node\NullableType) {
|
|
|
|
$suffix = '|null';
|
|
|
|
$parser_return_type = $parser_return_type->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($parser_return_type)) {
|
|
|
|
$return_type_string = $parser_return_type . $suffix;
|
|
|
|
} else {
|
|
|
|
$return_type_fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
|
|
|
$parser_return_type,
|
2017-01-07 20:35:07 +01:00
|
|
|
$source
|
2017-01-06 07:07:11 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$return_type_string = $return_type_fq_class_name . $suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->return_type = Type::parseString($return_type_string);
|
|
|
|
$storage->return_type_location = new CodeLocation(
|
|
|
|
$source,
|
|
|
|
$function,
|
|
|
|
false,
|
|
|
|
self::RETURN_TYPE_REGEX
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
$docblock_info = null;
|
|
|
|
$doc_comment = $function->getDocComment();
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
if (!$doc_comment) {
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-02-10 02:35:17 +01:00
|
|
|
$docblock_info = CommentChecker::extractFunctionDocblockInfo(
|
|
|
|
(string)$doc_comment,
|
|
|
|
$doc_comment->getLine()
|
|
|
|
);
|
2017-01-06 07:07:11 +01:00
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
$e->getMessage() . ' in docblock for ' . $cased_function_id,
|
|
|
|
new CodeLocation($source, $function, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$docblock_info) {
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info->deprecated) {
|
|
|
|
$storage->deprecated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info->variadic) {
|
|
|
|
$storage->variadic = true;
|
|
|
|
}
|
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
$storage->suppressed_issues = $docblock_info->suppress;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
if (!$config->use_docblock_types) {
|
|
|
|
return $storage;
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
$template_types = $class_storage && $class_storage->template_types ? $class_storage->template_types : null;
|
2017-02-10 02:35:17 +01:00
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
if ($docblock_info) {
|
|
|
|
if ($docblock_info->template_types) {
|
|
|
|
$storage->template_types = [];
|
|
|
|
|
|
|
|
foreach ($docblock_info->template_types as $template_type) {
|
|
|
|
if (count($template_type) === 3) {
|
|
|
|
$as_type_string = ClassLikeChecker::getFQCLNFromString($template_type[2], $source);
|
|
|
|
$storage->template_types[$template_type[0]] = $as_type_string;
|
|
|
|
} else {
|
|
|
|
$storage->template_types[$template_type[0]] = 'mixed';
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 06:14:44 +01:00
|
|
|
|
|
|
|
$template_types = array_merge($template_types ?: [], $storage->template_types);
|
2017-02-10 02:35:17 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
if ($docblock_info->template_typeofs) {
|
|
|
|
$storage->template_typeof_params = [];
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
foreach ($docblock_info->template_typeofs as $template_typeof) {
|
|
|
|
foreach ($storage->params as $i => $param) {
|
|
|
|
if ($param->name === $template_typeof['param_name']) {
|
|
|
|
$storage->template_typeof_params[$i] = $template_typeof['template_type'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info->return_type) {
|
2017-02-10 02:35:17 +01:00
|
|
|
$storage->has_template_return_type =
|
|
|
|
$template_types !== null &&
|
|
|
|
count(array_intersect(Type::tokenize($docblock_info->return_type), array_keys($template_types))) > 0;
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$docblock_return_type = Type::parseString(
|
|
|
|
self::fixUpLocalType(
|
|
|
|
(string)$docblock_info->return_type,
|
2017-02-10 02:35:17 +01:00
|
|
|
$source,
|
|
|
|
$template_types
|
2017-01-06 07:07:11 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$storage->return_type_location) {
|
|
|
|
$storage->return_type_location = new CodeLocation($source, $function, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage->return_type &&
|
|
|
|
!TypeChecker::hasIdenticalTypes(
|
|
|
|
$storage->return_type,
|
|
|
|
$docblock_return_type,
|
|
|
|
$source->getFileChecker()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
'Docblock return type does not match method return type for ' . $cased_function_id,
|
|
|
|
new CodeLocation($source, $function, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$storage->return_type = $docblock_return_type;
|
|
|
|
}
|
|
|
|
|
2017-01-28 03:09:40 +01:00
|
|
|
if ($docblock_info->return_type_line_number) {
|
|
|
|
$storage->return_type_location->setCommentLine($docblock_info->return_type_line_number);
|
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info->params) {
|
|
|
|
self::improveParamsFromDocblock(
|
|
|
|
$storage,
|
|
|
|
$docblock_info->params,
|
2017-02-10 06:14:44 +01:00
|
|
|
$template_types,
|
2017-03-02 00:36:04 +01:00
|
|
|
$function,
|
2017-01-06 07:07:11 +01:00
|
|
|
$source,
|
|
|
|
$source->getFQCLN(),
|
|
|
|
new CodeLocation($source, $function, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
/**
|
|
|
|
* Adds return types for the given function
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
|
|
|
* @param string $return_type
|
|
|
|
* @param Context $context
|
|
|
|
* @return void
|
2016-08-14 05:26:45 +02:00
|
|
|
*/
|
|
|
|
public function addReturnTypes($return_type, Context $context)
|
|
|
|
{
|
|
|
|
if (isset($this->return_vars_in_scope[$return_type])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->return_vars_in_scope[$return_type] = TypeChecker::combineKeyedTypes(
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$this->return_vars_in_scope[$return_type]
|
|
|
|
);
|
|
|
|
} else {
|
2016-08-14 05:26:45 +02:00
|
|
|
$this->return_vars_in_scope[$return_type] = $context->vars_in_scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->return_vars_possibly_in_scope[$return_type])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->return_vars_possibly_in_scope[$return_type] = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$this->return_vars_possibly_in_scope[$return_type]
|
|
|
|
);
|
|
|
|
} else {
|
2016-08-14 05:26:45 +02:00
|
|
|
$this->return_vars_possibly_in_scope[$return_type] = $context->vars_possibly_in_scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 06:51:34 +01:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getMethodName()
|
|
|
|
{
|
|
|
|
if ($this->function instanceof ClassMethod) {
|
|
|
|
return (string)$this->function->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
/**
|
2016-12-30 18:41:14 +01:00
|
|
|
* @param string|null $context_self
|
2016-08-14 05:26:45 +02:00
|
|
|
* @return null|string
|
|
|
|
*/
|
2016-12-30 18:41:14 +01:00
|
|
|
public function getMethodId($context_self = null)
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2016-11-21 03:49:06 +01:00
|
|
|
if ($this->function instanceof ClassMethod) {
|
2016-11-21 22:44:35 +01:00
|
|
|
$function_name = (string)$this->function->name;
|
2016-11-21 20:36:06 +01:00
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
return ($context_self ?: $this->source->getFQCLN()) . '::' . strtolower($function_name);
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->function instanceof Function_) {
|
2017-01-07 20:35:07 +01:00
|
|
|
return ($this->source->getNamespace() ? strtolower($this->source->getNamespace()) . '\\' : '') . strtolower($this->function->name);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getAliasedClassesFlipped()
|
|
|
|
{
|
2016-11-13 17:24:46 +01:00
|
|
|
if ($this->source instanceof NamespaceChecker || $this->source instanceof FileChecker || $this->source instanceof ClassLikeChecker) {
|
|
|
|
return $this->source->getAliasedClassesFlipped();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-07 20:35:07 +01:00
|
|
|
* @return string|null
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public function getFQCLN()
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->source->getFQCLN();
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public function getClassName()
|
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->source->getClassName();
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
public function getParentFQCLN()
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->source->getParentFQCLN();
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return $this->is_static;
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @return StatementsSource
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return $this->source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-07 20:13:39 +01:00
|
|
|
* @param bool $update_docblock
|
|
|
|
* @param Type\Union|null $return_type
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $fq_class_name
|
2016-12-07 20:13:39 +01:00
|
|
|
* @param CodeLocation|null $return_type_location
|
|
|
|
* @param CodeLocation|null $secondary_return_type_location
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-08-14 05:26:45 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public function verifyReturnType(
|
2016-12-07 20:13:39 +01:00
|
|
|
$update_docblock = false,
|
|
|
|
Type\Union $return_type = null,
|
2017-01-02 21:31:18 +01:00
|
|
|
$fq_class_name = null,
|
2016-12-07 20:13:39 +01:00
|
|
|
CodeLocation $return_type_location = null,
|
|
|
|
CodeLocation $secondary_return_type_location = null
|
|
|
|
) {
|
|
|
|
if (!$this->function->getStmts() &&
|
|
|
|
($this->function instanceof ClassMethod &&
|
|
|
|
($this->getSource() instanceof InterfaceChecker || $this->function->isAbstract())
|
|
|
|
)
|
|
|
|
) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:48:02 +01:00
|
|
|
$is_to_string = $this->function instanceof ClassMethod && strtolower($this->function->name) === '__tostring';
|
|
|
|
|
|
|
|
if ($this->function instanceof ClassMethod &&
|
|
|
|
substr($this->function->name, 0, 2) === '__' &&
|
|
|
|
!$is_to_string
|
|
|
|
) {
|
2016-11-07 23:07:59 +01:00
|
|
|
// do not check __construct, __set, __get, __call etc.
|
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
$method_id = (string)$this->getMethodId();
|
2016-12-10 19:20:41 +01:00
|
|
|
|
2017-02-13 00:06:18 +01:00
|
|
|
$cased_method_id = null;
|
|
|
|
|
|
|
|
if ($this instanceof MethodChecker) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId($method_id);
|
|
|
|
} elseif ($this->function instanceof Function_) {
|
|
|
|
$cased_method_id = $this->function->name;
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-12-06 22:33:47 +01:00
|
|
|
if (!$return_type_location) {
|
|
|
|
$return_type_location = new CodeLocation($this, $this->function, true);
|
|
|
|
}
|
|
|
|
|
2017-01-26 01:01:01 +01:00
|
|
|
$inferred_yield_types = [];
|
|
|
|
$inferred_return_types = EffectsAnalyser::getReturnTypes(
|
|
|
|
$this->function->getStmts(),
|
|
|
|
$inferred_yield_types,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$inferred_return_type = $inferred_return_types ? Type::combineTypes($inferred_return_types) : Type::getVoid();
|
|
|
|
$inferred_yield_type = $inferred_yield_types ? Type::combineTypes($inferred_yield_types) : null;
|
|
|
|
|
|
|
|
$inferred_generator_return_type = null;
|
|
|
|
|
|
|
|
if ($inferred_yield_type) {
|
|
|
|
$inferred_generator_return_type = $inferred_return_type;
|
|
|
|
$inferred_return_type = $inferred_yield_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$return_type && !Config::getInstance()->add_void_docblocks && $inferred_return_type->isVoid()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inferred_return_type = TypeChecker::simplifyUnionType(
|
|
|
|
ExpressionChecker::fleshOutTypes(
|
|
|
|
$inferred_return_type,
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
''
|
|
|
|
),
|
|
|
|
$this->getFileChecker()
|
|
|
|
);
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2016-12-09 18:48:02 +01:00
|
|
|
if (!$return_type && !$update_docblock && !$is_to_string) {
|
2017-01-16 07:22:36 +01:00
|
|
|
if ($this->function instanceof Closure) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingClosureReturnType(
|
2017-01-24 08:28:54 +01:00
|
|
|
'Closure does not have a return type, expecting ' . $inferred_return_type,
|
2017-01-16 07:22:36 +01:00
|
|
|
new CodeLocation($this, $this->function, true)
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:07:59 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingReturnType(
|
2017-01-28 06:44:52 +01:00
|
|
|
'Method ' . $cased_method_id . ' does not have a return type' .
|
|
|
|
(!$inferred_return_type->isMixed() ? ', expecting ' . $inferred_return_type : ''),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->function, true)
|
2016-11-07 23:07:59 +01:00
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:48:02 +01:00
|
|
|
if ($is_to_string) {
|
|
|
|
if (!$inferred_return_type->isMixed() && (string)$inferred_return_type !== 'string') {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidToString(
|
2017-02-12 19:26:24 +01:00
|
|
|
'__toString methods must return a string, ' . $inferred_return_type . ' returned',
|
2016-12-09 18:48:02 +01:00
|
|
|
$secondary_return_type_location ?: $return_type_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$return_type && !$update_docblock) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 22:33:47 +01:00
|
|
|
if (!$return_type) {
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($inferred_return_type && !$inferred_return_type->isMixed()) {
|
2017-02-12 00:25:44 +01:00
|
|
|
$this->addDocblockReturnType($inferred_return_type);
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
// passing it through fleshOutTypes eradicates errant $ vars
|
2016-10-22 19:23:18 +02:00
|
|
|
$declared_return_type = ExpressionChecker::fleshOutTypes(
|
2016-12-06 22:33:47 +01:00
|
|
|
$return_type,
|
2017-01-07 20:35:07 +01:00
|
|
|
$fq_class_name ?: $this->source->getFQCLN(),
|
2016-08-14 05:26:45 +02:00
|
|
|
$method_id
|
|
|
|
);
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
if (!$inferred_return_types && !$inferred_yield_types) {
|
2016-11-13 00:51:48 +01:00
|
|
|
if ($declared_return_type->isVoid()) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
if (ScopeChecker::onlyThrows($this->function->getStmts())) {
|
|
|
|
// if there's a single throw statement, it's presumably an exception saying this method is not to be
|
|
|
|
// used
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
2016-12-04 05:03:18 +01:00
|
|
|
'No return statements were found for method ' . $cased_method_id .
|
2016-11-13 00:51:48 +01:00
|
|
|
' but return type \'' . $declared_return_type . '\' was expected',
|
2016-12-06 22:33:47 +01:00
|
|
|
$secondary_return_type_location ?: $return_type_location
|
2016-11-13 00:51:48 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:56:36 +01:00
|
|
|
if (!$declared_return_type->isMixed()) {
|
|
|
|
$declared_return_type->check(
|
|
|
|
$this,
|
|
|
|
$secondary_return_type_location ?: $return_type_location,
|
2017-03-02 18:19:18 +01:00
|
|
|
$this->getSuppressedIssues(),
|
|
|
|
[],
|
|
|
|
false
|
2017-03-01 17:56:36 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
if ($inferred_return_type && !$declared_return_type->isMixed()) {
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($inferred_return_type->isVoid() && $declared_return_type->isVoid()) {
|
2016-11-13 00:51:48 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
if ($inferred_return_type->isMixed() || $inferred_return_type->isEmpty()) {
|
2016-08-14 05:26:45 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-13 00:51:48 +01:00
|
|
|
new MixedInferredReturnType(
|
|
|
|
'Could not verify return type \'' . $declared_return_type . '\' for ' .
|
|
|
|
$cased_method_id,
|
2016-12-06 22:33:47 +01:00
|
|
|
$secondary_return_type_location ?: $return_type_location
|
2016-11-13 00:51:48 +01:00
|
|
|
),
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->suppressed_issues
|
2016-08-14 05:26:45 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-31 00:38:23 +01:00
|
|
|
if (!TypeChecker::isContainedBy(
|
|
|
|
$inferred_return_type,
|
|
|
|
$declared_return_type,
|
|
|
|
$this->getFileChecker(),
|
2017-03-18 17:18:17 +01:00
|
|
|
false,
|
2017-01-31 00:38:23 +01:00
|
|
|
$has_scalar_match,
|
|
|
|
$type_coerced
|
|
|
|
)) {
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($update_docblock) {
|
2017-01-07 20:35:07 +01:00
|
|
|
if (!in_array('InvalidReturnType', $this->suppressed_issues)) {
|
2017-02-12 00:25:44 +01:00
|
|
|
$this->addDocblockReturnType($inferred_return_type);
|
2016-11-13 07:43:51 +01:00
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-11-05 22:53:30 +01:00
|
|
|
|
2017-01-17 17:17:49 +01:00
|
|
|
// is the declared return type more specific than the inferred one?
|
2017-02-12 00:25:44 +01:00
|
|
|
if ($type_coerced) {
|
2017-01-17 17:17:49 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MoreSpecificReturnType(
|
2017-03-18 17:18:17 +01:00
|
|
|
'The declared return type \'' . $declared_return_type . '\' for ' . $cased_method_id .
|
2017-01-17 17:17:49 +01:00
|
|
|
' is more specific than the inferred return type \'' . $inferred_return_type . '\'',
|
|
|
|
$secondary_return_type_location ?: $return_type_location
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
2017-03-18 17:18:17 +01:00
|
|
|
'The declared return type \'' . $declared_return_type . '\' for ' . $cased_method_id .
|
2017-01-17 17:17:49 +01:00
|
|
|
' is incorrect, got \'' . $inferred_return_type . '\'',
|
|
|
|
$secondary_return_type_location ?: $return_type_location
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2017-03-18 17:18:17 +01:00
|
|
|
} elseif (!$inferred_return_type->isNullable() && $declared_return_type->isNullable()) {
|
2017-02-12 00:25:44 +01:00
|
|
|
if ($update_docblock) {
|
|
|
|
if (!in_array('InvalidReturnType', $this->suppressed_issues)) {
|
|
|
|
$this->addDocblockReturnType($inferred_return_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
2017-03-18 17:18:17 +01:00
|
|
|
new LessSpecificReturnType(
|
|
|
|
'The inferred return type \'' . $inferred_return_type . '\' for ' . $cased_method_id .
|
|
|
|
' is more specific than the declared return type \'' . $declared_return_type . '\'',
|
2017-02-12 00:25:44 +01:00
|
|
|
$secondary_return_type_location ?: $return_type_location
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-17 02:00:51 +01:00
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2017-02-12 00:25:44 +01:00
|
|
|
/**
|
|
|
|
* @param Type\Union $inferred_return_type
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function addDocblockReturnType(Type\Union $inferred_return_type)
|
2017-02-12 00:25:44 +01:00
|
|
|
{
|
2017-02-18 19:41:27 +01:00
|
|
|
FileMutator::addDocblockReturnType(
|
|
|
|
$this->source->getFilePath(),
|
2017-02-12 00:25:44 +01:00
|
|
|
$this->function->getLine(),
|
|
|
|
(string)$this->function->getDocComment(),
|
|
|
|
$inferred_return_type->toNamespacedString(
|
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
false
|
|
|
|
),
|
|
|
|
$inferred_return_type->toNamespacedString(
|
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
2017-01-17 06:26:40 +01:00
|
|
|
* @param array<int, array{type:string,name:string,line_number:int}> $docblock_params
|
2017-01-06 07:07:11 +01:00
|
|
|
* @param FunctionLikeStorage $storage
|
2017-02-10 06:14:44 +01:00
|
|
|
* @param array<string, string>|null $template_types
|
2017-03-02 00:36:04 +01:00
|
|
|
* @param Closure|Function_|ClassMethod $function
|
2017-01-06 07:07:11 +01:00
|
|
|
* @param StatementsSource $source
|
2017-01-07 20:35:07 +01:00
|
|
|
* @param string|null $fq_class_name
|
2017-01-06 07:07:11 +01:00
|
|
|
* @param CodeLocation $code_location
|
2016-10-15 06:12:57 +02:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-01-06 07:07:11 +01:00
|
|
|
protected static function improveParamsFromDocblock(
|
|
|
|
FunctionLikeStorage $storage,
|
2016-11-02 07:29:00 +01:00
|
|
|
array $docblock_params,
|
2017-02-10 06:14:44 +01:00
|
|
|
$template_types,
|
2017-03-02 00:36:04 +01:00
|
|
|
$function,
|
2017-01-06 07:07:11 +01:00
|
|
|
StatementsSource $source,
|
|
|
|
$fq_class_name,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-11-21 19:37:02 +01:00
|
|
|
$docblock_param_vars = [];
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$base = $fq_class_name ? $fq_class_name . '::' : '';
|
2016-12-19 23:02:00 +01:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$cased_method_id = $base . $storage->cased_name;
|
2016-12-19 23:02:00 +01:00
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
$file_checker = $source->getFileChecker();
|
|
|
|
|
2017-01-17 07:14:43 +01:00
|
|
|
foreach ($docblock_params as $docblock_param) {
|
2016-08-14 05:26:45 +02:00
|
|
|
$param_name = $docblock_param['name'];
|
2016-12-04 01:11:30 +01:00
|
|
|
$line_number = $docblock_param['line_number'];
|
2017-01-17 07:14:43 +01:00
|
|
|
$docblock_param_variadic = false;
|
|
|
|
|
|
|
|
if (substr($param_name, 0, 3) === '...') {
|
|
|
|
$docblock_param_variadic = true;
|
|
|
|
$param_name = substr($param_name, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_name = substr($param_name, 1);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
if (!isset($storage->param_types[$param_name])) {
|
2017-01-28 03:09:40 +01:00
|
|
|
if ($line_number) {
|
|
|
|
$code_location->setCommentLine($line_number);
|
|
|
|
}
|
|
|
|
|
2016-12-25 12:32:21 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
'Parameter $' . $param_name .' does not appear in the argument list for ' .
|
|
|
|
$cased_method_id,
|
|
|
|
$code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
$storage_param = null;
|
|
|
|
|
|
|
|
foreach ($storage->params as $function_signature_param) {
|
|
|
|
if ($function_signature_param->name === $param_name) {
|
|
|
|
$storage_param = $function_signature_param;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage_param === null) {
|
|
|
|
throw new \UnexpectedValueException('This should not be possible');
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage_param_type = $storage->param_types[$param_name];
|
|
|
|
|
2016-11-21 19:37:02 +01:00
|
|
|
$docblock_param_vars[$param_name] = true;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$new_param_type = Type::parseString(
|
|
|
|
self::fixUpLocalType(
|
|
|
|
$docblock_param['type'],
|
2017-02-10 06:14:44 +01:00
|
|
|
$source,
|
|
|
|
$template_types
|
2016-11-02 07:29:00 +01:00
|
|
|
)
|
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2017-01-17 07:14:43 +01:00
|
|
|
if ($docblock_param_variadic) {
|
|
|
|
$new_param_type = new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
$new_param_type
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-02-04 04:07:14 +01:00
|
|
|
$new_param_type->setFromDocblock();
|
2017-01-15 23:52:01 +01:00
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
if (!$storage_param_type->isMixed()) {
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!TypeChecker::isContainedBy(
|
|
|
|
$new_param_type,
|
2017-01-06 07:07:11 +01:00
|
|
|
$storage->param_types[$param_name],
|
|
|
|
$source->getFileChecker()
|
2017-01-02 21:31:18 +01:00
|
|
|
)
|
|
|
|
) {
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location->setCommentLine($line_number);
|
2016-08-14 05:26:45 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
2016-11-02 07:29:00 +01:00
|
|
|
'Parameter $' . $param_name .' has wrong type \'' . $new_param_type . '\', should be \'' .
|
2017-03-02 00:36:04 +01:00
|
|
|
$storage_param_type . '\'',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-08-14 05:26:45 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
$existing_param_type_nullable = $storage_param->is_nullable;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
if ($existing_param_type_nullable && !$new_param_type->isNullable()) {
|
|
|
|
$new_param_type->types['null'] = new Type\Atomic\TNull();
|
|
|
|
}
|
2017-01-15 23:52:01 +01:00
|
|
|
|
2017-03-30 17:44:38 +02:00
|
|
|
$new_param_type = TypeChecker::simplifyUnionType($new_param_type, $file_checker);
|
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
if ((string)$storage_param->type !== (string)$new_param_type) {
|
|
|
|
$storage_param->type = $new_param_type;
|
|
|
|
}
|
2017-01-15 23:52:01 +01:00
|
|
|
|
2017-03-02 00:36:04 +01:00
|
|
|
if ($file_checker->project_checker->collect_references) {
|
|
|
|
$storage_param->location = new CodeLocation($source, $function);
|
|
|
|
$storage_param->location->setCommentLine($line_number);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
foreach ($storage->params as $function_signature_param) {
|
|
|
|
if (!isset($docblock_param_vars[$function_signature_param->name]) &&
|
2017-03-02 00:36:04 +01:00
|
|
|
$function_signature_param->location
|
2017-01-06 07:07:11 +01:00
|
|
|
) {
|
2016-11-21 19:37:02 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
2016-12-04 01:11:30 +01:00
|
|
|
'Parameter $' . $function_signature_param->name .' does not appear in the docbock for ' .
|
2016-12-19 23:02:00 +01:00
|
|
|
$cased_method_id,
|
2017-03-02 00:36:04 +01:00
|
|
|
$function_signature_param->location
|
2016-11-21 19:37:02 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Param $param
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param StatementsSource $source
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return FunctionLikeParameter
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
public static function getTranslatedParam(
|
|
|
|
PhpParser\Node\Param $param,
|
2016-12-04 01:11:30 +01:00
|
|
|
StatementsSource $source
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-08-14 05:26:45 +02:00
|
|
|
$param_type = null;
|
|
|
|
|
|
|
|
$is_nullable = $param->default !== null &&
|
2016-11-02 07:29:00 +01:00
|
|
|
$param->default instanceof \PhpParser\Node\Expr\ConstFetch &&
|
|
|
|
$param->default->name instanceof PhpParser\Node\Name &&
|
2016-11-30 21:03:52 +01:00
|
|
|
strtolower($param->default->name->parts[0]) === 'null';
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-12-04 04:41:45 +01:00
|
|
|
$param_typehint = $param->type;
|
|
|
|
|
|
|
|
if ($param_typehint instanceof PhpParser\Node\NullableType) {
|
|
|
|
$is_nullable = true;
|
|
|
|
$param_typehint = $param_typehint->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_typehint) {
|
|
|
|
if (is_string($param_typehint)) {
|
|
|
|
$param_type_string = $param_typehint;
|
|
|
|
} elseif ($param_typehint instanceof PhpParser\Node\Name\FullyQualified) {
|
|
|
|
$param_type_string = implode('\\', $param_typehint->parts);
|
|
|
|
} elseif ($param_typehint->parts === ['self']) {
|
2016-12-04 01:11:30 +01:00
|
|
|
$param_type_string = $source->getFQCLN();
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-08 01:16:51 +01:00
|
|
|
$param_type_string = ClassLikeChecker::getFQCLNFromString(
|
2016-12-04 04:41:45 +01:00
|
|
|
implode('\\', $param_typehint->parts),
|
2017-01-07 20:35:07 +01:00
|
|
|
$source
|
2016-08-15 07:21:50 +02:00
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_type_string) {
|
|
|
|
if ($is_nullable) {
|
|
|
|
$param_type_string .= '|null';
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_type = Type::parseString($param_type_string);
|
2016-10-30 02:50:24 +02:00
|
|
|
|
|
|
|
if ($param->variadic) {
|
|
|
|
$param_type = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
$param_type
|
|
|
|
])
|
2016-10-30 02:50:24 +02:00
|
|
|
]);
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2017-02-08 04:02:46 +01:00
|
|
|
} elseif ($param->variadic) {
|
|
|
|
$param_type = new Type\Union([
|
|
|
|
new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
Type::getMixed(),
|
|
|
|
])
|
|
|
|
]);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$is_optional = $param->default !== null;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
return new FunctionLikeParameter(
|
|
|
|
$param->name,
|
|
|
|
$param->byRef,
|
|
|
|
$param_type ?: Type::getMixed(),
|
2016-12-08 22:23:07 +01:00
|
|
|
new CodeLocation($source, $param, false, self::PARAM_TYPE_REGEX),
|
2016-10-09 23:54:58 +02:00
|
|
|
$is_optional,
|
2016-10-15 06:12:57 +02:00
|
|
|
$is_nullable,
|
2016-10-19 06:31:32 +02:00
|
|
|
$param->variadic
|
2016-10-09 23:54:58 +02:00
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param \ReflectionParameter $param
|
|
|
|
* @return FunctionLikeParameter
|
|
|
|
*/
|
2017-01-06 07:07:11 +01:00
|
|
|
protected static function getReflectionParamData(\ReflectionParameter $param)
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
|
|
|
$param_type_string = null;
|
|
|
|
|
|
|
|
if ($param->isArray()) {
|
|
|
|
$param_type_string = 'array';
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-08-14 05:26:45 +02:00
|
|
|
$param_class = null;
|
|
|
|
|
|
|
|
try {
|
2016-10-15 06:12:57 +02:00
|
|
|
/** @var \ReflectionClass */
|
2016-08-14 05:26:45 +02:00
|
|
|
$param_class = $param->getClass();
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (\ReflectionException $e) {
|
2016-08-14 05:26:45 +02:00
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_class) {
|
2016-10-15 06:12:57 +02:00
|
|
|
$param_type_string = (string)$param_class->getName();
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_nullable = false;
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
$is_optional = (bool)$param->isOptional();
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$is_nullable = $param->getDefaultValue() === null;
|
|
|
|
|
|
|
|
if ($param_type_string && $is_nullable) {
|
|
|
|
$param_type_string .= '|null';
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (\ReflectionException $e) {
|
2016-08-14 05:26:45 +02:00
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
$param_name = (string)$param->getName();
|
2016-08-14 05:26:45 +02:00
|
|
|
$param_type = $param_type_string ? Type::parseString($param_type_string) : Type::getMixed();
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
return new FunctionLikeParameter(
|
|
|
|
$param_name,
|
2016-10-15 06:12:57 +02:00
|
|
|
(bool)$param->isPassedByReference(),
|
2016-10-09 23:54:58 +02:00
|
|
|
$param_type,
|
2016-12-04 01:11:30 +01:00
|
|
|
null,
|
2016-10-10 07:35:12 +02:00
|
|
|
$is_optional,
|
2017-03-19 23:31:19 +01:00
|
|
|
$is_nullable,
|
|
|
|
$param->isVariadic()
|
2016-10-09 23:54:58 +02:00
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2017-02-10 02:35:17 +01:00
|
|
|
* @param string $return_type
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param array<string, string>|null $template_types
|
2016-10-14 06:53:43 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-02-10 02:35:17 +01:00
|
|
|
public static function fixUpLocalType(
|
|
|
|
$return_type,
|
|
|
|
StatementsSource $source,
|
|
|
|
array $template_types = null
|
|
|
|
) {
|
2016-08-14 05:26:45 +02:00
|
|
|
if (strpos($return_type, '[') !== false) {
|
|
|
|
$return_type = Type::convertSquareBrackets($return_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_tokens = Type::tokenize($return_type);
|
|
|
|
|
2016-10-03 22:39:42 +02:00
|
|
|
foreach ($return_type_tokens as $i => &$return_type_token) {
|
2016-08-14 05:26:45 +02:00
|
|
|
if ($return_type_token[0] === '\\') {
|
|
|
|
$return_type_token = substr($return_type_token, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:39:42 +02:00
|
|
|
if (in_array($return_type_token, ['<', '>', '|', '?', ',', '{', '}', ':'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-13 07:43:51 +01:00
|
|
|
if (isset($return_type_tokens[$i + 1]) && $return_type_tokens[$i + 1] === ':') {
|
2016-08-14 05:26:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_token = Type::fixScalarTerms($return_type_token);
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($return_type_token[0] === strtoupper($return_type_token[0]) &&
|
|
|
|
!isset($template_types[$return_type_token])
|
|
|
|
) {
|
2016-09-02 19:47:11 +02:00
|
|
|
if ($return_type_token[0] === '$') {
|
|
|
|
if ($return_type === '$this') {
|
|
|
|
$return_type_token = 'static';
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-08 01:16:51 +01:00
|
|
|
$return_type_token = ClassLikeChecker::getFQCLNFromString(
|
2016-11-02 07:29:00 +01:00
|
|
|
$return_type_token,
|
2017-01-07 20:35:07 +01:00
|
|
|
$source
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('', $return_type_tokens);
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2016-12-17 06:48:31 +01:00
|
|
|
* @param string $method_id
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param FileChecker $file_checker
|
2016-12-31 16:51:42 +01:00
|
|
|
* @return array<int, FunctionLikeParameter>
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function getMethodParamsById($method_id, array $args, FileChecker $file_checker)
|
2016-08-22 21:00:12 +02:00
|
|
|
{
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = strpos($method_id, '::') !== false ? explode('::', $method_id)[0] : null;
|
2016-10-25 17:40:09 +02:00
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($fq_class_name && ClassLikeChecker::isUserDefined($fq_class_name)) {
|
2016-12-23 21:06:20 +01:00
|
|
|
$method_params = MethodChecker::getMethodParams($method_id);
|
2016-12-27 02:06:05 +01:00
|
|
|
|
2016-12-23 21:06:20 +01:00
|
|
|
return $method_params;
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
if (FunctionChecker::inCallMap($declaring_method_id ?: $method_id)) {
|
|
|
|
$function_param_options = FunctionChecker::getParamsFromCallMap($declaring_method_id ?: $method_id);
|
2016-12-27 02:06:05 +01:00
|
|
|
|
2016-12-23 21:06:20 +01:00
|
|
|
if ($function_param_options === null) {
|
|
|
|
throw new \UnexpectedValueException('Not expecting $function_param_options to be null');
|
|
|
|
}
|
2016-11-07 05:29:54 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
return self::getMatchingParamsFromCallMapOptions($function_param_options, $args, $file_checker);
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
2016-12-23 21:06:20 +01:00
|
|
|
|
2017-02-21 07:56:43 +01:00
|
|
|
return MethodChecker::getMethodParams($method_id);
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param FileChecker $file_checker
|
2016-12-31 16:51:42 +01:00
|
|
|
* @return array<int, FunctionLikeParameter>
|
|
|
|
*/
|
2017-02-10 02:35:17 +01:00
|
|
|
public static function getFunctionParamsFromCallMapById($method_id, array $args, FileChecker $file_checker)
|
2016-12-31 16:51:42 +01:00
|
|
|
{
|
2017-02-10 02:35:17 +01:00
|
|
|
$function_param_options = FunctionChecker::getParamsFromCallMap($method_id);
|
2016-12-31 16:51:42 +01:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($function_param_options === null) {
|
|
|
|
throw new \UnexpectedValueException('Not expecting $function_param_options to be null');
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
return self::getMatchingParamsFromCallMapOptions($function_param_options, $args, $file_checker);
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, array<int, FunctionLikeParameter>> $function_param_options
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param FileChecker $file_checker
|
2016-12-31 16:51:42 +01:00
|
|
|
* @return array<int, FunctionLikeParameter>
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
protected static function getMatchingParamsFromCallMapOptions(
|
|
|
|
array $function_param_options,
|
|
|
|
array $args,
|
|
|
|
FileChecker $file_checker
|
|
|
|
) {
|
2016-08-22 21:00:12 +02:00
|
|
|
if (count($function_param_options) === 1) {
|
|
|
|
return $function_param_options[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($function_param_options as $possible_function_params) {
|
|
|
|
$all_args_match = true;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$last_param = count($possible_function_params)
|
|
|
|
? $possible_function_params[count($possible_function_params) - 1]
|
|
|
|
: null;
|
2016-10-18 22:43:50 +02:00
|
|
|
|
2016-10-26 17:51:59 +02:00
|
|
|
$mandatory_param_count = count($possible_function_params);
|
|
|
|
|
|
|
|
foreach ($possible_function_params as $i => $possible_function_param) {
|
|
|
|
if ($possible_function_param->is_optional) {
|
|
|
|
$mandatory_param_count = $i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($mandatory_param_count > count($args)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
foreach ($args as $argument_offset => $arg) {
|
2017-01-15 19:16:36 +01:00
|
|
|
if ($argument_offset >= count($possible_function_params)) {
|
|
|
|
if (!$last_param || !$last_param->is_variadic) {
|
|
|
|
$all_args_match = false;
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$param_type = $possible_function_params[$argument_offset]->type;
|
2016-08-22 21:00:12 +02:00
|
|
|
|
|
|
|
if (!isset($arg->value->inferredType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-11 04:49:43 +02:00
|
|
|
if ($arg->value->inferredType->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (TypeChecker::isContainedBy($arg->value->inferredType, $param_type, $file_checker)) {
|
2016-10-11 04:49:43 +02:00
|
|
|
continue;
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$all_args_match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($all_args_match) {
|
|
|
|
return $possible_function_params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't succeed in finding a match, set to the first possible and wait for issues below
|
|
|
|
return $function_param_options[0];
|
|
|
|
}
|
2017-01-07 20:35:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of suppressed issues
|
|
|
|
*
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
|
|
|
return $this->suppressed_issues;
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$no_effects_hashes = [];
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|