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-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use PhpParser\Node\Stmt\Function_;
|
|
|
|
use PhpParser;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\EffectsAnalyser;
|
|
|
|
use Psalm\FunctionLikeParameter;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
|
|
|
use Psalm\Issue\InvalidReturnType;
|
2016-10-16 00:01:04 +02:00
|
|
|
use Psalm\Issue\MethodSignatureMismatch;
|
2016-11-07 23:07:59 +01:00
|
|
|
use Psalm\Issue\MissingReturnType;
|
2016-11-05 22:53:30 +01:00
|
|
|
use Psalm\Issue\MixedInferredReturnType;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\StatementsSource;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
abstract class FunctionLikeChecker extends SourceChecker implements StatementsSource
|
2016-08-14 05:26:45 +02: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
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $namespace;
|
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 string
|
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
protected $fq_class_name;
|
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
|
|
|
/**
|
|
|
|
* @var array<string,array<string,Psalm\Type\Union>>
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $return_vars_in_scope = [];
|
2016-10-15 06:12:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string,array<string,bool>>
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $return_vars_possibly_in_scope = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $class_name;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $class_extends;
|
|
|
|
|
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->namespace = $source->getNamespace();
|
|
|
|
$this->class_name = $source->getClassName();
|
|
|
|
$this->class_extends = $source->getParentClass();
|
|
|
|
$this->file_name = $source->getFileName();
|
2016-12-04 01:11:30 +01:00
|
|
|
$this->file_path = $source->getFilePath();
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->include_file_name = $source->getIncludeFileName();
|
2016-12-04 01:11:30 +01:00
|
|
|
$this->include_file_path = $source->getIncludeFilePath();
|
2016-11-08 01:16:51 +01:00
|
|
|
$this->fq_class_name = $source->getFQCLN();
|
2016-08-14 05:26:45 +02:00
|
|
|
$this->source = $source;
|
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
2016-11-21 03:49:06 +01:00
|
|
|
$this->aliased_classes = $source->getAliasedClasses();
|
|
|
|
$this->aliased_constants = $source->getAliasedConstants();
|
|
|
|
$this->aliased_functions = $source->getAliasedFunctions();
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-11-05 22:53:30 +01:00
|
|
|
* @param Context $context
|
|
|
|
* @param Context|null $global_context
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-11-05 22:53:30 +01:00
|
|
|
public function check(Context $context, Context $global_context = null)
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2016-10-14 06:53:43 +02:00
|
|
|
if ($function_stmts = $this->function->getStmts()) {
|
2016-10-18 22:28:51 +02:00
|
|
|
$statements_checker = new StatementsChecker($this);
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2016-10-03 17:38:59 +02:00
|
|
|
$hash = null;
|
|
|
|
|
2016-08-15 21:14:28 +02:00
|
|
|
if ($this instanceof MethodChecker) {
|
2016-08-14 05:26:45 +02:00
|
|
|
if (ClassLikeChecker::getThisClass()) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$hash = $this->getMethodId() . json_encode([
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
]);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
|
|
// if we know that the function has no effects on vars, we don't bother rechecking
|
|
|
|
if (isset(self::$no_effects_hashes[$hash])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
list(
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
) = self::$no_effects_hashes[$hash];
|
|
|
|
|
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($context->self) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic($context->self)]);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
$function_params = MethodChecker::getMethodParams((string)$this->getMethodId());
|
2016-10-16 00:01:04 +02:00
|
|
|
|
2016-11-07 05:29:54 +01:00
|
|
|
if ($function_params === null) {
|
|
|
|
throw new \InvalidArgumentException('Cannot get params for own method');
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:01:04 +02:00
|
|
|
$implemented_method_ids = MethodChecker::getOverriddenMethodIds((string)$this->getMethodId());
|
|
|
|
|
|
|
|
if ($implemented_method_ids) {
|
2016-10-18 22:00:03 +02:00
|
|
|
$have_emitted = false;
|
2016-11-05 03:04:55 +01:00
|
|
|
|
2016-10-16 00:01:04 +02:00
|
|
|
foreach ($implemented_method_ids as $implemented_method_id) {
|
2016-10-18 22:00:03 +02:00
|
|
|
if ($have_emitted) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:45:31 +02:00
|
|
|
if ($implemented_method_id === 'ArrayObject::__construct') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:01:04 +02:00
|
|
|
$implemented_params = MethodChecker::getMethodParams($implemented_method_id);
|
|
|
|
|
2016-11-07 05:29:54 +01:00
|
|
|
if ($implemented_params === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:01:04 +02:00
|
|
|
foreach ($implemented_params as $i => $implemented_param) {
|
|
|
|
if (!isset($function_params[$i])) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId((string)$this->getMethodId());
|
|
|
|
$parent_method_id = MethodChecker::getCasedMethodId($implemented_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
2016-11-02 07:29:00 +01:00
|
|
|
'Method ' . $cased_method_id .' has fewer arguments than parent method ' .
|
|
|
|
$parent_method_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->function)
|
2016-10-16 00:01:04 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-18 22:00:03 +02:00
|
|
|
$have_emitted = true;
|
2016-10-16 00:01:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ((string)$function_params[$i]->signature_type !==
|
|
|
|
(string)$implemented_param->signature_type
|
|
|
|
) {
|
2016-10-16 00:01:04 +02:00
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId((string)$this->getMethodId());
|
|
|
|
$parent_method_id = MethodChecker::getCasedMethodId($implemented_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
2016-11-02 07:29:00 +01:00
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_method_id .' has wrong type \'' .
|
|
|
|
$function_params[$i]->signature_type . '\', expecting \'' .
|
|
|
|
$implemented_param->signature_type . '\' as defined by ' .
|
|
|
|
$parent_method_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->function)
|
2016-10-16 00:01:04 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-18 22:00:03 +02:00
|
|
|
$have_emitted = true;
|
2016-10-16 00:01:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($this instanceof FunctionChecker) {
|
2016-11-21 03:49:06 +01:00
|
|
|
$function_params = FunctionChecker::getParams(strtolower((string)$this->getMethodId()), $this->file_name);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else { // Closure
|
2016-09-02 00:02:09 +02:00
|
|
|
$function_params = [];
|
|
|
|
|
2016-10-16 00:01:04 +02:00
|
|
|
foreach ($this->function->getParams() as $param) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$function_params[] = self::getTranslatedParam(
|
|
|
|
$param,
|
2016-12-04 01:11:30 +01:00
|
|
|
$this
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-02 00:02:09 +02:00
|
|
|
|
|
|
|
foreach ($function_params as $function_param) {
|
2016-10-22 19:23:18 +02:00
|
|
|
$param_type = ExpressionChecker::fleshOutTypes(
|
2016-10-09 23:54:58 +02:00
|
|
|
clone $function_param->type,
|
2016-09-02 00:02:09 +02:00
|
|
|
[],
|
|
|
|
$context->self,
|
|
|
|
$this->getMethodId()
|
|
|
|
);
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
if (!$function_param->code_location) {
|
|
|
|
throw new \UnexpectedValueException('We should know where this code is');
|
|
|
|
}
|
|
|
|
|
2016-09-02 00:02:09 +02:00
|
|
|
foreach ($param_type->types as $atomic_type) {
|
|
|
|
if ($atomic_type->isObjectType()
|
2016-09-02 02:27:16 +02:00
|
|
|
&& !$atomic_type->isObject()
|
2016-10-15 06:12:57 +02:00
|
|
|
&& $this->function instanceof PhpParser\Node
|
2016-11-08 01:16:51 +01:00
|
|
|
&& ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2016-09-02 00:02:09 +02:00
|
|
|
$atomic_type->value,
|
2016-12-04 01:11:30 +01:00
|
|
|
$function_param->code_location,
|
2016-09-02 00:02:09 +02:00
|
|
|
$this->suppressed_issues
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$' . $function_param->name] = $param_type;
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
$statements_checker->registerVariable($function_param->name, $function_param->code_location->line_number);
|
2016-09-02 00:02:09 +02:00
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-11-05 22:53:30 +01:00
|
|
|
$statements_checker->check($function_stmts, $context, null, $global_context);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
|
|
if (isset($this->return_vars_in_scope[''])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$context->vars_in_scope = TypeChecker::combineKeyedTypes(
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$this->return_vars_in_scope['']
|
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->return_vars_possibly_in_scope[''])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$this->return_vars_possibly_in_scope['']
|
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if (strpos($var, '$this->') !== 0) {
|
2016-08-14 05:26:45 +02:00
|
|
|
unset($context->vars_in_scope[$var]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $type) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if (strpos($var, '$this->') !== 0) {
|
2016-08-14 05:26:45 +02:00
|
|
|
unset($context->vars_possibly_in_scope[$var]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:38:59 +02:00
|
|
|
if ($hash && ClassLikeChecker::getThisClass() && $this instanceof MethodChecker) {
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$no_effects_hashes[$hash] = [
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getMethodId()
|
|
|
|
{
|
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
|
|
|
|
2016-11-21 22:44:35 +01:00
|
|
|
if (strtolower($function_name) === strtolower((string)$this->class_name)) {
|
2016-11-21 20:36:06 +01:00
|
|
|
$function_name = '__construct';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fq_class_name . '::' . strtolower($function_name);
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->function instanceof Function_) {
|
|
|
|
return ($this->namespace ? $this->namespace . '\\' : '') . 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-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return $this->namespace;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public function getFQCLN()
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2016-11-07 23:29:51 +01:00
|
|
|
return $this->fq_class_name;
|
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()
|
|
|
|
{
|
|
|
|
return $this->class_name;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public function getClassLikeChecker()
|
|
|
|
{
|
|
|
|
return $this->source->getClassLikeChecker();
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
public function getParentClass()
|
|
|
|
{
|
|
|
|
return $this->class_extends;
|
|
|
|
}
|
|
|
|
|
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-11-13 00:51:48 +01:00
|
|
|
* @param bool $update_docblock
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-08-14 05:26:45 +02:00
|
|
|
*/
|
2016-11-13 00:51:48 +01:00
|
|
|
public function checkReturnTypes($update_docblock = false)
|
2016-08-14 05:26:45 +02:00
|
|
|
{
|
2016-10-15 06:12:57 +02:00
|
|
|
if (!$this->function->getStmts()) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:07:59 +01:00
|
|
|
if ($this->function instanceof ClassMethod && substr($this->function->name, 0, 2) === '__') {
|
|
|
|
// 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-11-06 01:17:22 +01:00
|
|
|
$cased_method_id = $this instanceof MethodChecker ? MethodChecker::getCasedMethodId($method_id) : $method_id;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-08-15 21:14:28 +02:00
|
|
|
if ($this instanceof MethodChecker) {
|
|
|
|
$method_return_types = MethodChecker::getMethodReturnTypes($method_id);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-20 20:36:15 +02:00
|
|
|
try {
|
|
|
|
$method_return_types = FunctionChecker::getFunctionReturnTypes($method_id, $this->file_name);
|
2016-11-02 07:29:00 +01:00
|
|
|
} catch (\Exception $e) {
|
2016-10-20 20:36:15 +02:00
|
|
|
$method_return_types = null;
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
if (!$method_return_types && !$update_docblock) {
|
2016-11-07 23:07:59 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingReturnType(
|
|
|
|
'Method ' . $cased_method_id . ' does not have a 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-11-13 00:51:48 +01:00
|
|
|
$inferred_yield_types = [];
|
|
|
|
$inferred_return_types = EffectsAnalyser::getReturnTypes(
|
|
|
|
$this->function->getStmts(),
|
|
|
|
$inferred_yield_types,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
$inferred_return_type = $inferred_return_types ? Type::combineTypes($inferred_return_types) : Type::getVoid();
|
2016-11-13 00:51:48 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-11-13 17:24:46 +01:00
|
|
|
if (!$method_return_types) {
|
2016-11-13 05:59:31 +01:00
|
|
|
if ($inferred_return_type && !$inferred_return_type->isMixed()) {
|
2016-11-13 00:51:48 +01:00
|
|
|
FileChecker::addDocblockReturnType(
|
|
|
|
$this->file_name,
|
|
|
|
$this->function->getLine(),
|
2016-11-13 05:59:31 +01:00
|
|
|
(string)$this->function->getDocComment(),
|
2016-11-14 02:32:09 +01:00
|
|
|
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), false),
|
|
|
|
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), true)
|
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-08-14 05:26:45 +02:00
|
|
|
$method_return_types,
|
|
|
|
[],
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name,
|
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-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->function, true)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($inferred_return_type->isMixed()) {
|
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-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->function, true)
|
2016-11-13 00:51:48 +01:00
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
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
|
|
|
}
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
if (!TypeChecker::hasIdenticalTypes(
|
|
|
|
$declared_return_type,
|
|
|
|
$inferred_return_type,
|
|
|
|
$this->fq_class_name
|
|
|
|
)) {
|
|
|
|
if ($update_docblock) {
|
2016-11-13 07:43:51 +01:00
|
|
|
if (!in_array('InvalidReturnType', $this->getSuppressedIssues())) {
|
|
|
|
FileChecker::addDocblockReturnType(
|
|
|
|
$this->file_name,
|
|
|
|
$this->function->getLine(),
|
|
|
|
(string)$this->function->getDocComment(),
|
2016-11-14 02:32:09 +01:00
|
|
|
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), false),
|
|
|
|
$inferred_return_type->toNamespacedString($this->getAliasedClassesFlipped(), $this->getFQCLN(), true)
|
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
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
|
|
|
'The given return type \'' . $declared_return_type . '\' for ' . $cased_method_id .
|
|
|
|
' is incorrect, got \'' . $inferred_return_type . '\'',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->function, true)
|
2016-11-13 00:51:48 +01:00
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
2016-11-02 07:29:00 +01:00
|
|
|
)) {
|
2016-11-13 00:51:48 +01:00
|
|
|
return false;
|
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-10-15 06:12:57 +02:00
|
|
|
/**
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param array<array{type:string,name:string,line_number:int}> $docblock_params
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param array<string,Type\Union> $function_param_names
|
|
|
|
* @param array<\Psalm\FunctionLikeParameter> &$function_signature
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param CodeLocation $code_location
|
2016-10-15 06:12:57 +02:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
protected function improveParamsFromDocblock(
|
|
|
|
array $docblock_params,
|
|
|
|
array $function_param_names,
|
|
|
|
array &$function_signature,
|
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 = [];
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
foreach ($docblock_params as $docblock_param) {
|
|
|
|
$param_name = $docblock_param['name'];
|
2016-12-04 01:11:30 +01:00
|
|
|
$line_number = $docblock_param['line_number'];
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
|
|
if (!array_key_exists($param_name, $function_param_names)) {
|
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 .' does not appear in the argument list for ' .
|
|
|
|
$this->getMethodId(),
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-08-14 05:26:45 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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'],
|
|
|
|
null,
|
|
|
|
$this->namespace,
|
|
|
|
$this->getAliasedClasses()
|
|
|
|
)
|
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
|
|
if ($function_param_names[$param_name] && !$function_param_names[$param_name]->isMixed()) {
|
|
|
|
if (!$new_param_type->isIn($function_param_names[$param_name])) {
|
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 \'' .
|
|
|
|
$function_param_names[$param_name] . '\'',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-08-14 05:26:45 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($function_signature as &$function_signature_param) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($function_signature_param->name === $param_name) {
|
|
|
|
$existing_param_type_nullable = $function_signature_param->is_nullable;
|
2016-08-14 05:26:45 +02:00
|
|
|
|
|
|
|
if ($existing_param_type_nullable && !$new_param_type->isNullable()) {
|
2016-09-09 22:21:49 +02:00
|
|
|
$new_param_type->types['null'] = new Type\Atomic('null');
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2016-10-16 00:01:04 +02:00
|
|
|
$function_signature_param->signature_type = $function_signature_param->type;
|
2016-10-09 23:54:58 +02:00
|
|
|
$function_signature_param->type = $new_param_type;
|
2016-08-14 05:26:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
foreach ($function_signature as &$function_signature_param) {
|
|
|
|
if (!isset($docblock_param_vars[$function_signature_param->name]) && $function_signature_param->code_location) {
|
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-11-21 19:37:02 +01:00
|
|
|
$this->getMethodId(),
|
2016-12-04 01:11:30 +01:00
|
|
|
$function_signature_param->code_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),
|
2016-12-04 01:11:30 +01:00
|
|
|
$source->getNamespace(),
|
|
|
|
$source->getAliasedClasses()
|
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([
|
|
|
|
new Type\GenericArray(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
Type::getInt(),
|
|
|
|
$param_type
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
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-04 01:11:30 +01:00
|
|
|
new CodeLocation($source, $param),
|
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
|
|
|
|
*/
|
2016-08-14 19:13:53 +02:00
|
|
|
protected static function getReflectionParamArray(\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,
|
|
|
|
$is_nullable
|
2016-10-09 23:54:58 +02:00
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2016-10-15 06:12:57 +02:00
|
|
|
* @param string $return_type
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string|null $fq_class_name
|
2016-10-15 06:12:57 +02:00
|
|
|
* @param string $namespace
|
|
|
|
* @param array $aliased_classes
|
2016-10-14 06:53:43 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public static function fixUpLocalType($return_type, $fq_class_name, $namespace, array $aliased_classes)
|
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);
|
|
|
|
|
|
|
|
if ($return_type_token[0] === strtoupper($return_type_token[0])) {
|
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,
|
|
|
|
$namespace,
|
|
|
|
$aliased_classes
|
|
|
|
);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('', $return_type_tokens);
|
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the input param type match the given param type
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-08-22 21:00:12 +02:00
|
|
|
* @param Type\Union $input_type
|
|
|
|
* @param Type\Union $param_type
|
|
|
|
* @param bool &$has_scalar_match
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param bool &$type_coerced whether or not there was type coercion involved
|
2016-08-22 21:00:12 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
public static function doesParamMatch(
|
|
|
|
Type\Union $input_type,
|
|
|
|
Type\Union $param_type,
|
|
|
|
&$has_scalar_match = null,
|
|
|
|
&$type_coerced = null
|
|
|
|
) {
|
2016-08-22 21:00:12 +02:00
|
|
|
$has_scalar_match = true;
|
|
|
|
|
|
|
|
if ($param_type->isMixed()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type_match_found = false;
|
|
|
|
$has_type_mismatch = false;
|
|
|
|
|
|
|
|
foreach ($input_type->types as $input_type_part) {
|
|
|
|
if ($input_type_part->isNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type_match_found = false;
|
|
|
|
$scalar_type_match_found = false;
|
|
|
|
|
|
|
|
foreach ($param_type->types as $param_type_part) {
|
|
|
|
if ($param_type_part->isNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($input_type_part->value === $param_type_part->value ||
|
|
|
|
ClassChecker::classExtendsOrImplements($input_type_part->value, $param_type_part->value) ||
|
2016-10-22 19:23:18 +02:00
|
|
|
ExpressionChecker::isMock($input_type_part->value)
|
2016-08-22 21:00:12 +02:00
|
|
|
) {
|
|
|
|
$type_match_found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($input_type_part->value === 'false' && $param_type_part->value === 'bool') {
|
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($input_type_part->value === 'int' && $param_type_part->value === 'float') {
|
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:06:41 +02:00
|
|
|
if ($input_type_part->value === 'Closure' && $param_type_part->value === 'callable') {
|
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
if ($param_type_part->isNumeric() && $input_type_part->isNumericType()) {
|
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
2016-10-28 19:24:06 +02:00
|
|
|
if ($param_type_part->isGenericArray() && $input_type_part->isObjectLike()) {
|
2016-09-21 22:42:39 +02:00
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
2016-08-30 06:39:17 +02:00
|
|
|
if ($param_type_part->isScalar() && $input_type_part->isScalarType()) {
|
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($param_type_part->isCallable() &&
|
|
|
|
($input_type_part->value === 'string' || $input_type_part->value === 'array')
|
|
|
|
) {
|
2016-08-22 21:00:12 +02:00
|
|
|
// @todo add value checks if possible here
|
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
2016-08-30 06:39:17 +02:00
|
|
|
if ($input_type_part->isNumeric()) {
|
|
|
|
if ($param_type_part->isNumericType()) {
|
|
|
|
$scalar_type_match_found = true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2016-08-31 19:25:29 +02:00
|
|
|
if ($input_type_part->isScalarType() || $input_type_part->isScalar()) {
|
2016-08-22 21:00:12 +02:00
|
|
|
if ($param_type_part->isScalarType()) {
|
|
|
|
$scalar_type_match_found = true;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($param_type_part->isObject() &&
|
|
|
|
!$input_type_part->isArray() &&
|
|
|
|
!$input_type_part->isResource()
|
|
|
|
) {
|
2016-08-22 21:00:12 +02:00
|
|
|
$type_match_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassChecker::classExtendsOrImplements($param_type_part->value, $input_type_part->value)) {
|
2016-10-09 23:54:58 +02:00
|
|
|
$type_coerced = true;
|
2016-08-22 21:00:12 +02:00
|
|
|
$type_match_found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$type_match_found) {
|
|
|
|
if (!$scalar_type_match_found) {
|
|
|
|
$has_scalar_match = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param array<PhpParser\Node\Arg> $args
|
|
|
|
* @param string $file_name
|
|
|
|
* @return array<int,FunctionLikeParameter>
|
|
|
|
*/
|
2016-08-23 05:02:03 +02:00
|
|
|
public static function getParamsById($method_id, array $args, $file_name)
|
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-11-07 05:29:54 +01:00
|
|
|
/** @var array<\Psalm\FunctionLikeParameter> */
|
2016-10-25 17:40:09 +02:00
|
|
|
return MethodChecker::getMethodParams($method_id);
|
2016-11-07 23:29:51 +01:00
|
|
|
} elseif (!$fq_class_name && FunctionChecker::inCallMap($method_id)) {
|
2016-10-30 17:46:18 +01:00
|
|
|
/** @var array<array<FunctionLikeParameter>> */
|
2016-10-24 21:54:06 +02:00
|
|
|
$function_param_options = FunctionChecker::getParamsFromCallMap($method_id);
|
2016-11-07 23:29:51 +01:00
|
|
|
} elseif ($fq_class_name) {
|
2016-11-07 05:29:54 +01:00
|
|
|
if ($method_params = MethodChecker::getMethodParams($method_id)) {
|
|
|
|
// fall back to using reflected params anyway
|
|
|
|
return $method_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($method_id);
|
|
|
|
|
|
|
|
$method_id = $declaring_method_id ?: $method_id;
|
|
|
|
|
|
|
|
if (!FunctionChecker::inCallMap($method_id)) {
|
|
|
|
throw new \InvalidArgumentException('Cannot get params for ' . $method_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var array<array<FunctionLikeParameter>> */
|
|
|
|
$function_param_options = FunctionChecker::getParamsFromCallMap($method_id);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-08-22 21:42:05 +02:00
|
|
|
return FunctionChecker::getParams(strtolower($method_id), $file_name);
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$function_params = null;
|
|
|
|
|
|
|
|
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) {
|
2016-11-02 07:29:00 +01:00
|
|
|
if (count($possible_function_params) <= $argument_offset &&
|
|
|
|
(!$last_param || substr($last_param->name, 0, 3) !== '...')
|
|
|
|
) {
|
2016-10-18 22:43:50 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FunctionLikeChecker::doesParamMatch($arg->value->inferredType, $param_type)) {
|
|
|
|
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];
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|