2016-08-14 05:26:45 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker;
|
|
|
|
|
2017-05-19 06:48:26 +02:00
|
|
|
use PhpParser;
|
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_;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Aliases;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\EffectsAnalyser;
|
2017-09-16 18:45:11 +02:00
|
|
|
use Psalm\FileManipulation\FunctionDocblockManipulator;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\FunctionLikeParameter;
|
2017-11-30 06:01:41 +01:00
|
|
|
use Psalm\Issue\ImplementedReturnTypeMismatch;
|
2018-01-07 23:17:18 +01:00
|
|
|
use Psalm\Issue\InvalidFalsableReturnType;
|
|
|
|
use Psalm\Issue\InvalidNullableReturnType;
|
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;
|
2018-01-05 18:11:12 +01:00
|
|
|
use Psalm\Issue\MismatchingDocblockParamType;
|
|
|
|
use Psalm\Issue\MismatchingDocblockReturnType;
|
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;
|
2018-01-05 06:19:35 +01:00
|
|
|
use Psalm\Issue\MoreSpecificImplementedParamType;
|
2017-11-30 06:01:41 +01:00
|
|
|
use Psalm\Issue\MoreSpecificImplementedReturnType;
|
2017-01-17 17:17:49 +01:00
|
|
|
use Psalm\Issue\MoreSpecificReturnType;
|
2017-01-13 18:03:22 +01:00
|
|
|
use Psalm\Issue\OverriddenMethodAccess;
|
2018-01-10 06:07:47 +01:00
|
|
|
use Psalm\Issue\ReservedWord;
|
2017-09-02 17:18:56 +02:00
|
|
|
use Psalm\Issue\UntypedParam;
|
2017-12-29 23:27:16 +01:00
|
|
|
use Psalm\Issue\UnusedParam;
|
2017-02-12 00:56:38 +01:00
|
|
|
use Psalm\Issue\UnusedVariable;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\StatementsSource;
|
2017-11-26 22:03:17 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2017-09-04 02:52:54 +02:00
|
|
|
use Psalm\Storage\FunctionLikeStorage;
|
2017-01-06 07:07:11 +01:00
|
|
|
use Psalm\Storage\MethodStorage;
|
2016-08-14 05:26:45 +02:00
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
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
|
|
|
{
|
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 StatementsSource
|
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $source;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
/**
|
|
|
|
* @var FileChecker
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public $file_checker;
|
|
|
|
|
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-12-02 19:32:20 +01:00
|
|
|
* @var array<string, array<string, bool>>
|
2016-10-15 06:12:57 +02:00
|
|
|
*/
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $return_vars_possibly_in_scope = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
/**
|
|
|
|
* @var Type\Union|null
|
|
|
|
*/
|
|
|
|
private $local_return_type;
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
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-07-29 21:05:06 +02:00
|
|
|
$project_checker = $this->file_checker->project_checker;
|
|
|
|
|
|
|
|
$file_storage_provider = $project_checker->file_storage_provider;
|
|
|
|
|
2017-12-03 18:44:08 +01:00
|
|
|
$implemented_docblock_param_types = [];
|
|
|
|
|
2017-12-29 23:27:16 +01:00
|
|
|
$project_checker = $this->file_checker->project_checker;
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
2017-12-29 23:27:16 +01:00
|
|
|
|
|
|
|
$classlike_storage_provider = $project_checker->classlike_storage_provider;
|
|
|
|
|
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) {
|
2017-05-13 01:15:08 +02:00
|
|
|
$hash = $real_method_id . json_encode([
|
2016-12-07 20:13:39 +01:00
|
|
|
$context->vars_in_scope,
|
2017-05-27 02:05:57 +02:00
|
|
|
$context->vars_possibly_in_scope,
|
2016-12-07 20:13:39 +01:00
|
|
|
]);
|
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-07-29 21:05:06 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($project_checker, $method_id);
|
2017-06-05 22:46:04 +02:00
|
|
|
|
|
|
|
if (!is_string($declaring_method_id)) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException('The declaring method of ' . $method_id . ' should not be null');
|
2017-06-05 22:46:04 +02:00
|
|
|
}
|
2017-01-07 20:35:07 +01:00
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
$fq_class_name = (string)$context->self;
|
2016-11-05 03:04:55 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$storage = $codebase->getMethodStorage($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
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
$overridden_method_ids = MethodChecker::getOverriddenMethodIds($project_checker, $method_id);
|
2016-10-18 22:00:03 +02:00
|
|
|
|
2017-04-15 05:26:58 +02:00
|
|
|
if ($this->function->name === '__construct') {
|
|
|
|
$context->inside_constructor = true;
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
if ($overridden_method_ids && $this->function->name !== '__construct') {
|
|
|
|
foreach ($overridden_method_ids as $overridden_method_id) {
|
2018-01-21 19:38:51 +01:00
|
|
|
$parent_method_storage = $codebase->getMethodStorage($overridden_method_id);
|
2017-01-13 18:03:22 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
list($overridden_fq_class_name) = explode('::', $overridden_method_id);
|
2016-12-07 20:13:39 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
$parent_storage = $classlike_storage_provider->get($overridden_fq_class_name);
|
2016-12-07 20:13:39 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
self::compareMethods(
|
|
|
|
$project_checker,
|
|
|
|
$class_storage,
|
|
|
|
$parent_storage,
|
|
|
|
$storage,
|
|
|
|
$parent_method_storage,
|
|
|
|
new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$this->function,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues
|
|
|
|
);
|
2017-02-09 23:49:13 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
foreach ($parent_method_storage->params as $i => $guide_param) {
|
|
|
|
if ($guide_param->type && (!$guide_param->signature_type || !$class_storage->user_defined)) {
|
2017-09-02 17:18:56 +02:00
|
|
|
$implemented_docblock_param_types[$i] = true;
|
|
|
|
}
|
2017-01-13 18:03:22 +01:00
|
|
|
}
|
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-07-29 21:05:06 +02:00
|
|
|
$file_storage = $file_storage_provider->get($this->source->getFilePath());
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
$storage = $file_storage->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
|
2017-07-29 21:05:06 +02:00
|
|
|
$file_storage = $file_storage_provider->get($this->source->getFilePath());
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-11-09 05:27:51 +01:00
|
|
|
$function_id = $this->getMethodId();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-08-08 20:45:47 +02:00
|
|
|
if (!isset($file_storage->functions[$function_id])) {
|
|
|
|
throw new \UnexpectedValueException('Closure function ' . $function_id . ' should exist');
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage = $file_storage->functions[$function_id];
|
2016-09-02 00:02:09 +02:00
|
|
|
|
2017-11-22 03:50:39 +01:00
|
|
|
if ($storage->return_type) {
|
|
|
|
$closure_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$storage->return_type,
|
|
|
|
$context->self,
|
|
|
|
$this->getMethodId()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$closure_return_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
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,
|
2017-11-22 03:50:39 +01:00
|
|
|
$closure_return_type
|
2017-05-27 02:05:57 +02:00
|
|
|
),
|
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-07-25 22:11:02 +02:00
|
|
|
$signature_type = $function_param->signature_type;
|
2017-09-02 17:18:56 +02:00
|
|
|
if ($function_param->type) {
|
|
|
|
$param_type = clone $function_param->type;
|
|
|
|
|
|
|
|
$param_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$param_type,
|
|
|
|
$context->self,
|
|
|
|
$this->getMethodId()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$param_type = Type::getMixed();
|
|
|
|
}
|
2016-12-04 01:11:30 +01:00
|
|
|
|
2017-07-09 03:19:16 +02:00
|
|
|
$context->vars_in_scope['$' . $function_param->name] = $param_type;
|
|
|
|
$context->vars_possibly_in_scope['$' . $function_param->name] = true;
|
|
|
|
|
2017-12-30 03:28:21 +01:00
|
|
|
if (!$function_param->type_location || !$function_param->location) {
|
2017-07-09 03:19:16 +02:00
|
|
|
continue;
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
*
|
|
|
|
* @var PhpParser\Node\Param
|
|
|
|
*/
|
2016-12-31 05:40:32 +01:00
|
|
|
$parser_param = $this->function->getParams()[$offset];
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($signature_type) {
|
|
|
|
if (!TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-07-25 22:11:02 +02:00
|
|
|
$param_type,
|
2017-07-29 21:05:06 +02:00
|
|
|
$signature_type
|
2017-07-25 22:11:02 +02:00
|
|
|
)
|
|
|
|
) {
|
2018-01-07 16:23:02 +01:00
|
|
|
if ($project_checker->alter_code
|
|
|
|
&& isset($project_checker->getIssuesToFix()['MismatchingDocblockParamType'])
|
|
|
|
) {
|
|
|
|
$this->addOrUpdateParamType($project_checker, $function_param->name, $signature_type, true);
|
|
|
|
|
2018-01-07 18:53:25 +01:00
|
|
|
continue;
|
2018-01-07 16:23:02 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2018-01-05 18:11:12 +01:00
|
|
|
new MismatchingDocblockParamType(
|
2017-07-25 22:11:02 +02:00
|
|
|
'Parameter $' . $function_param->name . ' has wrong type \'' . $param_type .
|
|
|
|
'\', should be \'' . $signature_type . '\'',
|
2018-01-07 06:11:23 +01:00
|
|
|
$function_param->type_location
|
2017-07-25 22:11:02 +02:00
|
|
|
),
|
|
|
|
$storage->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-10 04:46:55 +01:00
|
|
|
$signature_type->check(
|
|
|
|
$this,
|
|
|
|
$function_param->type_location,
|
|
|
|
$storage->suppressed_issues,
|
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:40:32 +01:00
|
|
|
if ($parser_param->default) {
|
|
|
|
$default_type = StatementsChecker::getSimpleType($parser_param->default);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($default_type &&
|
|
|
|
!TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-02 21:31:18 +01:00
|
|
|
$default_type,
|
2017-07-29 21:05:06 +02:00
|
|
|
$param_type
|
2017-01-02 21:31:18 +01:00
|
|
|
)
|
|
|
|
) {
|
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-12-30 03:28:21 +01:00
|
|
|
$function_param->type_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-12-03 17:23:40 +01:00
|
|
|
$substituted_type->replaceTemplateTypesWithStandins($template_types, $generic_types, null);
|
2017-12-30 03:28:21 +01:00
|
|
|
$substituted_type->check(
|
|
|
|
$this->source,
|
|
|
|
$function_param->type_location,
|
|
|
|
$this->suppressed_issues,
|
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
2017-02-10 02:35:17 +01:00
|
|
|
} else {
|
2018-01-10 06:07:47 +01:00
|
|
|
if ($param_type->isVoid()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ReservedWord(
|
|
|
|
'Parameter cannot be void',
|
|
|
|
$function_param->type_location
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_type->check(
|
|
|
|
$this->source,
|
|
|
|
$function_param->type_location,
|
|
|
|
$this->suppressed_issues,
|
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
2017-03-02 00:36:04 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
if ($codebase->collect_references) {
|
2017-12-30 03:28:21 +01:00
|
|
|
if ($function_param->type_location !== $function_param->signature_type_location &&
|
|
|
|
$function_param->signature_type_location &&
|
2017-03-02 00:36:04 +01:00
|
|
|
$function_param->signature_type
|
|
|
|
) {
|
|
|
|
$function_param->signature_type->check(
|
|
|
|
$this->source,
|
2017-12-30 03:28:21 +01:00
|
|
|
$function_param->signature_type_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
|
|
|
|
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,
|
2018-01-21 22:24:20 +01:00
|
|
|
$function_param->location,
|
|
|
|
null
|
2016-12-07 20:13:39 +01:00
|
|
|
);
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2018-01-10 06:07:47 +01:00
|
|
|
if ($storage->return_type && $storage->return_type_location && !$storage->has_template_return_type) {
|
|
|
|
if (!$storage->signature_return_type || $storage->signature_return_type === $storage->return_type) {
|
|
|
|
$fleshed_out_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$storage->return_type,
|
|
|
|
$context->self
|
|
|
|
);
|
2018-01-10 04:46:55 +01:00
|
|
|
|
2018-01-10 06:07:47 +01:00
|
|
|
$fleshed_out_return_type->check(
|
2018-01-10 04:46:55 +01:00
|
|
|
$this,
|
|
|
|
$storage->return_type_location,
|
|
|
|
$storage->suppressed_issues,
|
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
2018-01-11 05:29:18 +01:00
|
|
|
} else {
|
2018-01-10 06:07:47 +01:00
|
|
|
$fleshed_out_signature_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$storage->signature_return_type,
|
|
|
|
$context->self,
|
|
|
|
$cased_method_id
|
|
|
|
);
|
|
|
|
|
2018-01-11 05:29:18 +01:00
|
|
|
$fleshed_out_signature_type->check(
|
|
|
|
$this,
|
|
|
|
$storage->signature_return_type_location ?: $storage->return_type_location,
|
|
|
|
$storage->suppressed_issues,
|
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->function instanceof Closure) {
|
|
|
|
$fleshed_out_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$storage->return_type,
|
|
|
|
$context->self,
|
|
|
|
$cased_method_id
|
|
|
|
);
|
|
|
|
|
|
|
|
$fleshed_out_signature_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$storage->signature_return_type,
|
|
|
|
$context->self,
|
|
|
|
$cased_method_id
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!TypeChecker::isContainedBy(
|
|
|
|
$project_checker,
|
|
|
|
$fleshed_out_return_type,
|
|
|
|
$fleshed_out_signature_type
|
|
|
|
)
|
2018-01-10 06:07:47 +01:00
|
|
|
) {
|
2018-01-11 05:29:18 +01:00
|
|
|
if ($project_checker->alter_code
|
|
|
|
&& isset($project_checker->getIssuesToFix()['MismatchingDocblockReturnType'])
|
|
|
|
) {
|
|
|
|
$this->addOrUpdateReturnType($project_checker, $storage->signature_return_type);
|
2018-01-10 06:07:47 +01:00
|
|
|
|
2018-01-11 05:29:18 +01:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-10 06:07:47 +01:00
|
|
|
|
2018-01-11 05:29:18 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MismatchingDocblockReturnType(
|
|
|
|
'Docblock has incorrect return type \'' . $storage->return_type .
|
|
|
|
'\', should be \'' . $storage->signature_return_type . '\'',
|
|
|
|
$storage->return_type_location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-10 06:07:47 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:24:20 +01:00
|
|
|
$statements_checker->analyze($function_stmts, $context, null, $global_context, true);
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2017-09-03 00:15:52 +02:00
|
|
|
foreach ($storage->params as $offset => $function_param) {
|
|
|
|
$signature_type = $function_param->signature_type;
|
|
|
|
|
|
|
|
// only complain if there's no type defined by a parent type
|
|
|
|
if (!$function_param->type
|
|
|
|
&& $function_param->location
|
|
|
|
&& !isset($implemented_docblock_param_types[$offset])
|
|
|
|
) {
|
|
|
|
$possible_type = null;
|
|
|
|
|
|
|
|
if (isset($context->possible_param_types[$function_param->name])) {
|
|
|
|
$possible_type = $context->possible_param_types[$function_param->name];
|
|
|
|
}
|
|
|
|
|
2017-09-03 00:26:19 +02:00
|
|
|
$infer_text = $project_checker->infer_types_from_usage
|
|
|
|
? ', ' . ($possible_type ? 'should be ' . $possible_type : 'could not infer type')
|
|
|
|
: '';
|
|
|
|
|
2017-09-03 00:15:52 +02:00
|
|
|
IssueBuffer::accepts(
|
|
|
|
new UntypedParam(
|
2017-09-03 00:26:19 +02:00
|
|
|
'Parameter $' . $function_param->name . ' has no provided type' . $infer_text,
|
2017-09-03 00:15:52 +02:00
|
|
|
$function_param->location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
2017-09-16 18:45:11 +02:00
|
|
|
$project_checker,
|
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,
|
2017-05-10 20:05:26 +02:00
|
|
|
$ignore_nullable_issues,
|
2016-12-07 20:13:39 +01:00
|
|
|
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 */
|
2018-01-09 21:05:48 +01:00
|
|
|
$closure_atomic = $this->function->inferredType->getTypes()['Closure'];
|
2016-12-15 01:19:25 +01:00
|
|
|
$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);
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
if (!isset($context->referenced_var_ids[$var_name]) && $original_location) {
|
2017-12-29 23:27:16 +01:00
|
|
|
if (!array_key_exists(substr($var_name, 1), $storage->param_types)) {
|
2017-02-12 00:56:38 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnusedVariable(
|
|
|
|
'Variable ' . $var_name . ' is never referenced',
|
|
|
|
$original_location
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-12-29 23:27:16 +01:00
|
|
|
} elseif (!$storage instanceof MethodStorage
|
|
|
|
|| $storage->visibility === ClassLikeChecker::VISIBILITY_PRIVATE
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnusedParam(
|
|
|
|
'Param ' . $var_name . ' is never referenced in this method',
|
|
|
|
$original_location
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-02-12 00:56:38 +01:00
|
|
|
} else {
|
2017-12-29 23:27:16 +01:00
|
|
|
if (!$class_storage || $storage->abstract) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var ClassMethod $this->function */
|
|
|
|
$method_name_lc = strtolower((string)$this->function->name);
|
|
|
|
$parent_method_id = end($class_storage->overridden_method_ids[$method_name_lc]);
|
|
|
|
|
2017-12-30 14:47:00 +01:00
|
|
|
$position = array_search(substr($var_name, 1), array_keys($storage->param_types), true);
|
|
|
|
|
|
|
|
if ($position === false) {
|
|
|
|
throw new \UnexpectedValueException('$position should not be false here');
|
|
|
|
}
|
2017-12-29 23:27:16 +01:00
|
|
|
|
2017-12-30 14:47:00 +01:00
|
|
|
if ($parent_method_id) {
|
2018-01-21 19:38:51 +01:00
|
|
|
$parent_method_storage = $codebase->getMethodStorage($parent_method_id);
|
2017-12-29 23:27:16 +01:00
|
|
|
|
2017-12-30 14:47:00 +01:00
|
|
|
// if the parent method has a param at that position and isn't abstract
|
|
|
|
if (!$parent_method_storage->abstract
|
|
|
|
&& isset($parent_method_storage->params[$position])
|
|
|
|
) {
|
2017-12-29 23:27:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-30 14:47:00 +01:00
|
|
|
$storage->unused_params[$position] = $original_location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage instanceof MethodStorage && $class_storage) {
|
|
|
|
foreach ($storage->params as $i => $_) {
|
|
|
|
if (!isset($storage->unused_params[$i])) {
|
|
|
|
$storage->used_params[$i] = true;
|
|
|
|
|
|
|
|
/** @var ClassMethod $this->function */
|
|
|
|
$method_name_lc = strtolower((string)$this->function->name);
|
|
|
|
|
|
|
|
if (!isset($class_storage->overridden_method_ids[$method_name_lc])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class_storage->overridden_method_ids[$method_name_lc] as $parent_method_id) {
|
2018-01-21 19:38:51 +01:00
|
|
|
$parent_method_storage = $codebase->getMethodStorage($parent_method_id);
|
2017-12-30 14:47:00 +01:00
|
|
|
|
|
|
|
$parent_method_storage->used_params[$i] = true;
|
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,
|
2017-05-27 02:05:57 +02:00
|
|
|
$context->vars_possibly_in_scope,
|
2017-01-12 03:37:53 +01:00
|
|
|
];
|
|
|
|
}
|
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-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @param ProjectChecker $project_checker
|
|
|
|
* @param ClassLikeStorage $implementer_classlike_storage
|
|
|
|
* @param ClassLikeStorage $guide_classlike_storage
|
|
|
|
* @param MethodStorage $implementer_method_storage
|
|
|
|
* @param MethodStorage $guide_method_storage
|
|
|
|
* @param CodeLocation $code_location
|
|
|
|
* @param array $suppressed_issues
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function compareMethods(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
ClassLikeStorage $implementer_classlike_storage,
|
|
|
|
ClassLikeStorage $guide_classlike_storage,
|
|
|
|
MethodStorage $implementer_method_storage,
|
|
|
|
MethodStorage $guide_method_storage,
|
|
|
|
CodeLocation $code_location,
|
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
|
|
|
$implementer_method_id = $implementer_classlike_storage->name . '::'
|
|
|
|
. strtolower($guide_method_storage->cased_name);
|
|
|
|
$implementer_declaring_method_id = MethodChecker::getDeclaringMethodId(
|
|
|
|
$project_checker,
|
|
|
|
$implementer_method_id
|
|
|
|
);
|
|
|
|
|
|
|
|
$cased_implementer_method_id = $implementer_classlike_storage->name . '::'
|
|
|
|
. $implementer_method_storage->cased_name;
|
|
|
|
|
|
|
|
$cased_guide_method_id = $guide_classlike_storage->name . '::' . $guide_method_storage->cased_name;
|
|
|
|
|
|
|
|
if ($implementer_method_storage->visibility > $guide_method_storage->visibility) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new OverriddenMethodAccess(
|
|
|
|
'Method ' . $cased_implementer_method_id . ' has different access level than '
|
|
|
|
. $cased_guide_method_id,
|
|
|
|
$code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($guide_method_storage->signature_return_type) {
|
|
|
|
$guide_signature_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$guide_method_storage->signature_return_type,
|
|
|
|
$guide_classlike_storage->name
|
|
|
|
);
|
|
|
|
|
|
|
|
$implementer_signature_return_type = $implementer_method_storage->signature_return_type
|
|
|
|
? ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$implementer_method_storage->signature_return_type,
|
|
|
|
$implementer_classlike_storage->name
|
|
|
|
) : null;
|
|
|
|
|
2017-11-29 05:09:09 +01:00
|
|
|
$or_null_implementer_return_type = $implementer_signature_return_type
|
|
|
|
? clone $implementer_signature_return_type
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ($or_null_implementer_return_type) {
|
2018-01-09 21:05:48 +01:00
|
|
|
$or_null_implementer_return_type->addType(new Type\Atomic\TNull);
|
2017-11-29 05:09:09 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 18:44:08 +01:00
|
|
|
if ((!$implementer_signature_return_type
|
|
|
|
|| $implementer_signature_return_type->getId() !== $guide_signature_return_type->getId())
|
|
|
|
&& (!$or_null_implementer_return_type
|
|
|
|
|| $or_null_implementer_return_type->getId() !== $guide_signature_return_type->getId())
|
2017-11-29 05:09:09 +01:00
|
|
|
) {
|
2017-11-26 22:03:17 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Method ' . $cased_implementer_method_id . ' with return type \''
|
|
|
|
. $implementer_signature_return_type . '\' is different to return type \''
|
|
|
|
. $guide_signature_return_type . '\' of inherited method ' . $cased_guide_method_id,
|
|
|
|
$code_location
|
2018-01-05 07:04:07 +01:00
|
|
|
)
|
2017-11-26 22:03:17 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-27 17:43:06 +01:00
|
|
|
} elseif ($guide_method_storage->return_type
|
|
|
|
&& $implementer_method_storage->return_type
|
|
|
|
&& $implementer_classlike_storage->user_defined
|
|
|
|
) {
|
2018-01-23 07:32:16 +01:00
|
|
|
$implementer_method_storage_return_type = ExpressionChecker::fleshOutType(
|
2017-11-26 22:03:17 +01:00
|
|
|
$project_checker,
|
|
|
|
$implementer_method_storage->return_type,
|
2018-01-23 07:32:16 +01:00
|
|
|
$implementer_classlike_storage->name,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$guide_method_storage_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
2017-11-26 22:03:17 +01:00
|
|
|
$guide_method_storage->return_type,
|
2018-01-23 07:32:16 +01:00
|
|
|
$guide_classlike_storage->name,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!TypeChecker::isContainedBy(
|
|
|
|
$project_checker,
|
|
|
|
$implementer_method_storage_return_type,
|
|
|
|
$guide_method_storage_return_type,
|
2017-11-26 22:03:17 +01:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
$has_scalar_match,
|
|
|
|
$type_coerced,
|
|
|
|
$type_coerced_from_mixed
|
|
|
|
)) {
|
|
|
|
// is the declared return type more specific than the inferred one?
|
|
|
|
if ($type_coerced) {
|
|
|
|
if (IssueBuffer::accepts(
|
2017-11-30 06:01:41 +01:00
|
|
|
new MoreSpecificImplementedReturnType(
|
2017-11-26 22:03:17 +01:00
|
|
|
'The return type \'' . $guide_method_storage->return_type
|
|
|
|
. '\' for ' . $cased_guide_method_id . ' is more specific than the implemented '
|
|
|
|
. 'return type for ' . $implementer_declaring_method_id . ' \''
|
|
|
|
. $implementer_method_storage->return_type . '\'',
|
|
|
|
$implementer_method_storage->location ?: $code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
2017-11-30 06:01:41 +01:00
|
|
|
new ImplementedReturnTypeMismatch(
|
2017-11-26 22:03:17 +01:00
|
|
|
'The return type \'' . $guide_method_storage->return_type
|
|
|
|
. '\' for ' . $cased_guide_method_id . ' is different to the implemented '
|
|
|
|
. 'return type for ' . $implementer_declaring_method_id . ' \''
|
|
|
|
. $implementer_method_storage->return_type . '\'',
|
|
|
|
$implementer_method_storage->location ?: $code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list($implemented_fq_class_name) = explode('::', $implementer_method_id);
|
|
|
|
|
|
|
|
foreach ($guide_method_storage->params as $i => $guide_param) {
|
|
|
|
if (!isset($implementer_method_storage->params[$i])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Method ' . $cased_implementer_method_id . ' has fewer arguments than parent method ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-04 20:01:17 +01:00
|
|
|
$implementer_param = $implementer_method_storage->params[$i];
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
$or_null_guide_type = $guide_param->signature_type
|
|
|
|
? clone $guide_param->signature_type
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ($or_null_guide_type) {
|
2018-01-09 21:05:48 +01:00
|
|
|
$or_null_guide_type->addType(new Type\Atomic\TNull);
|
2017-11-26 22:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($guide_classlike_storage->user_defined
|
2018-01-05 06:55:48 +01:00
|
|
|
&& $implementer_param->signature_type
|
|
|
|
&& (
|
|
|
|
!$guide_param->signature_type
|
|
|
|
|| (
|
|
|
|
$implementer_param->signature_type->getId() !== $guide_param->signature_type->getId()
|
|
|
|
&& (!$or_null_guide_type
|
|
|
|
|| $implementer_param->signature_type->getId() !== $or_null_guide_type->getId())
|
|
|
|
)
|
|
|
|
)
|
2017-11-26 22:03:17 +01:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' .
|
2018-01-04 20:01:17 +01:00
|
|
|
$implementer_param->signature_type . '\', expecting \'' .
|
2017-11-26 22:03:17 +01:00
|
|
|
$guide_param->signature_type . '\' as defined by ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$implementer_method_storage->params[$i]->location
|
2018-01-04 20:01:17 +01:00
|
|
|
?: $code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-05 06:19:35 +01:00
|
|
|
|
|
|
|
if ($guide_classlike_storage->user_defined
|
|
|
|
&& $implementer_param->type
|
|
|
|
&& $guide_param->type
|
|
|
|
&& $implementer_param->type->getId() !== $guide_param->type->getId()
|
|
|
|
) {
|
|
|
|
if (!TypeChecker::isContainedBy(
|
|
|
|
$project_checker,
|
|
|
|
$guide_param->type,
|
|
|
|
$implementer_param->type,
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MoreSpecificImplementedParamType(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' .
|
|
|
|
$implementer_param->type . '\', expecting \'' .
|
|
|
|
$guide_param->type . '\' as defined by ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$implementer_method_storage->params[$i]->location
|
|
|
|
?: $code_location
|
2018-01-05 07:04:07 +01:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
2018-01-05 06:19:35 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 20:01:17 +01:00
|
|
|
|
|
|
|
if ($guide_classlike_storage->user_defined && $implementer_param->by_ref !== $guide_param->by_ref) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' is' .
|
|
|
|
($implementer_param->by_ref ? '' : ' not') . ' passed by reference, but argument ' .
|
|
|
|
($i + 1) . ' of ' . $cased_guide_method_id . ' is' . ($guide_param->by_ref ? '' : ' not'),
|
|
|
|
$implementer_method_storage->params[$i]->location
|
2017-11-26 22:03:17 +01:00
|
|
|
?: $code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-03 18:44:08 +01:00
|
|
|
$implemeneter_param_type = $implementer_method_storage->params[$i]->type;
|
|
|
|
|
2018-01-05 00:58:03 +01:00
|
|
|
if (!$guide_classlike_storage->user_defined
|
|
|
|
&& $guide_param->type
|
|
|
|
&& !$guide_param->type->isMixed()
|
|
|
|
&& !$guide_param->type->from_docblock
|
2018-01-05 02:35:43 +01:00
|
|
|
&& (
|
|
|
|
!$implemeneter_param_type
|
|
|
|
|| (
|
|
|
|
$implemeneter_param_type->getId() !== $guide_param->type->getId()
|
|
|
|
&& (
|
|
|
|
!$or_null_guide_type
|
2018-01-05 01:31:12 +01:00
|
|
|
|| $implemeneter_param_type->getId() !== $or_null_guide_type->getId()
|
|
|
|
)
|
2018-01-05 00:58:03 +01:00
|
|
|
)
|
|
|
|
)
|
2017-11-26 22:03:17 +01:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' .
|
|
|
|
$implementer_method_storage->params[$i]->type . '\', expecting \'' .
|
|
|
|
$guide_param->type . '\' as defined by ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$implementer_method_storage->params[$i]->location
|
|
|
|
?: $code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 00:58:03 +01:00
|
|
|
if ($guide_classlike_storage->user_defined
|
|
|
|
&& $implementer_method_storage->cased_name !== '__construct'
|
|
|
|
&& $implementer_method_storage->required_param_count > $guide_method_storage->required_param_count
|
2017-11-26 22:03:17 +01:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Method ' . $cased_implementer_method_id . ' has more arguments than parent method ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-08-15 01:30:11 +02:00
|
|
|
* @return string
|
2016-08-14 05:26:45 +02:00
|
|
|
*/
|
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-11-26 22:03:17 +01:00
|
|
|
$namespace = $this->source->getNamespace();
|
|
|
|
|
|
|
|
return ($namespace ? strtolower($namespace) . '\\' : '') . strtolower($this->function->name);
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-12-05 07:47:49 +01:00
|
|
|
return $this->getFilePath() . ':' . $this->function->getLine() . ':-:closure';
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 21:50:25 +01:00
|
|
|
/**
|
|
|
|
* @param string|null $context_self
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCorrectlyCasedMethodId($context_self = null)
|
|
|
|
{
|
|
|
|
if ($this->function instanceof ClassMethod) {
|
|
|
|
$function_name = (string)$this->function->name;
|
|
|
|
|
|
|
|
return ($context_self ?: $this->source->getFQCLN()) . '::' . $function_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->function instanceof Function_) {
|
|
|
|
$namespace = $this->source->getNamespace();
|
|
|
|
|
|
|
|
return ($namespace ? $namespace . '\\' : '') . $this->function->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getFilePath() . ':' . $this->function->getLine() . ':-:closure';
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:52:54 +02:00
|
|
|
/**
|
|
|
|
* @return FunctionLikeStorage
|
|
|
|
*/
|
2017-12-07 21:50:25 +01:00
|
|
|
public function getFunctionLikeStorage(StatementsChecker $statements_checker)
|
2017-09-04 02:52:54 +02:00
|
|
|
{
|
|
|
|
$function_id = $this->getMethodId();
|
|
|
|
|
|
|
|
$project_checker = $this->getFileChecker()->project_checker;
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
2017-09-04 02:52:54 +02:00
|
|
|
|
|
|
|
if (strpos($function_id, '::')) {
|
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($project_checker, $function_id);
|
|
|
|
|
|
|
|
if (!$declaring_method_id) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException('The declaring method of ' . $function_id . ' should not be null');
|
2017-09-04 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
return $codebase->getMethodStorage($declaring_method_id);
|
2017-09-04 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
return $codebase->getFunctionStorage($statements_checker, $function_id);
|
2017-09-04 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getAliasedClassesFlipped()
|
|
|
|
{
|
2017-04-28 06:31:55 +02:00
|
|
|
if ($this->source instanceof NamespaceChecker ||
|
|
|
|
$this->source instanceof FileChecker ||
|
|
|
|
$this->source instanceof ClassLikeChecker
|
|
|
|
) {
|
2016-11-13 17:24:46 +01:00
|
|
|
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 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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
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(
|
2017-09-16 18:45:11 +02:00
|
|
|
ProjectChecker $project_checker,
|
2016-12-07 20:13:39 +01:00
|
|
|
Type\Union $return_type = null,
|
2017-01-02 21:31:18 +01:00
|
|
|
$fq_class_name = null,
|
2017-12-16 16:51:04 +01:00
|
|
|
CodeLocation $return_type_location = null
|
2016-12-07 20:13:39 +01:00
|
|
|
) {
|
|
|
|
if (!$this->function->getStmts() &&
|
2017-12-01 01:00:09 +01:00
|
|
|
(
|
|
|
|
$this->function instanceof ClassMethod &&
|
2016-12-07 20:13:39 +01:00
|
|
|
($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-12-07 21:50:25 +01:00
|
|
|
$cased_method_id = $this->getCorrectlyCasedMethodId();
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-12-06 22:33:47 +01:00
|
|
|
if (!$return_type_location) {
|
2017-06-21 20:22:52 +02:00
|
|
|
$return_type_location = new CodeLocation($this, $this->function, null, true);
|
2016-12-06 22:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 01:01:01 +01:00
|
|
|
$inferred_yield_types = [];
|
2017-11-26 22:03:17 +01:00
|
|
|
|
|
|
|
/** @var PhpParser\Node\Stmt[] */
|
|
|
|
$function_stmts = $this->function->getStmts();
|
|
|
|
|
2018-01-02 22:57:40 +01:00
|
|
|
$inferred_return_type_parts = EffectsAnalyser::getReturnTypes(
|
2017-11-26 22:03:17 +01:00
|
|
|
$function_stmts,
|
2017-01-26 01:01:01 +01:00
|
|
|
$inferred_yield_types,
|
2017-05-10 20:03:51 +02:00
|
|
|
$ignore_nullable_issues,
|
2017-01-26 01:01:01 +01:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-10 19:04:37 +01:00
|
|
|
if ((!$return_type || $return_type->from_docblock)
|
2018-01-02 22:57:40 +01:00
|
|
|
&& ScopeChecker::getFinalControlActions($function_stmts) !== [ScopeChecker::ACTION_END]
|
|
|
|
&& !$inferred_yield_types
|
|
|
|
&& count($inferred_return_type_parts)
|
|
|
|
) {
|
|
|
|
// only add null if we have a return statement elsewhere and it wasn't void
|
|
|
|
foreach ($inferred_return_type_parts as $inferred_return_type_part) {
|
|
|
|
if (!$inferred_return_type_part instanceof Type\Atomic\TVoid) {
|
2018-01-10 19:04:37 +01:00
|
|
|
$atomic_null = new Type\Atomic\TNull();
|
|
|
|
$atomic_null->from_docblock = true;
|
|
|
|
$inferred_return_type_parts[] = $atomic_null;
|
2018-01-02 22:57:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return_type
|
|
|
|
&& !$return_type->from_docblock
|
|
|
|
&& !$return_type->isVoid()
|
|
|
|
&& !$inferred_yield_types
|
|
|
|
&& ScopeChecker::getFinalControlActions($function_stmts) !== [ScopeChecker::ACTION_END]
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
2018-01-05 00:58:03 +01:00
|
|
|
'Not all code paths of ' . $cased_method_id . ' end in a return statement, return type '
|
|
|
|
. $return_type . ' expected',
|
2018-01-02 22:57:40 +01:00
|
|
|
$return_type_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inferred_return_type = $inferred_return_type_parts
|
|
|
|
? Type::combineTypes($inferred_return_type_parts)
|
|
|
|
: Type::getVoid();
|
2017-01-26 01:01:01 +01:00
|
|
|
$inferred_yield_type = $inferred_yield_types ? Type::combineTypes($inferred_yield_types) : null;
|
|
|
|
|
|
|
|
if ($inferred_yield_type) {
|
|
|
|
$inferred_return_type = $inferred_yield_type;
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$project_checker = $this->getFileChecker()->project_checker;
|
|
|
|
$codebase = $project_checker->codebase;
|
|
|
|
|
|
|
|
if (!$return_type && !$codebase->config->add_void_docblocks && $inferred_return_type->isVoid()) {
|
2017-01-26 01:01:01 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:03:18 +01:00
|
|
|
$unsafe_return_type = false;
|
|
|
|
|
|
|
|
// prevent any return types that do not return a value from being used in PHP typehints
|
|
|
|
if ($project_checker->alter_code
|
|
|
|
&& $inferred_return_type
|
|
|
|
&& $inferred_return_type->isNullable()
|
|
|
|
&& !$inferred_yield_types
|
|
|
|
) {
|
|
|
|
foreach ($inferred_return_type_parts as $inferred_return_type_part) {
|
|
|
|
if ($inferred_return_type_part instanceof Type\Atomic\TVoid) {
|
|
|
|
$unsafe_return_type = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 01:01:01 +01:00
|
|
|
$inferred_return_type = TypeChecker::simplifyUnionType(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
|
|
|
ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
2017-01-26 01:01:01 +01:00
|
|
|
$inferred_return_type,
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
''
|
2017-07-29 21:05:06 +02:00
|
|
|
)
|
2017-01-26 01:01:01 +01:00
|
|
|
);
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($is_to_string) {
|
|
|
|
if (!$inferred_return_type->isMixed() && (string)$inferred_return_type !== 'string') {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidToString(
|
|
|
|
'__toString methods must return a string, ' . $inferred_return_type . ' returned',
|
|
|
|
$return_type_location
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$return_type) {
|
2017-01-16 07:22:36 +01:00
|
|
|
if ($this->function instanceof Closure) {
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($project_checker->alter_code
|
|
|
|
&& isset($project_checker->getIssuesToFix()['MissingClosureReturnType'])
|
|
|
|
) {
|
2018-01-23 00:00:19 +01:00
|
|
|
if ($inferred_return_type->isMixed() || $inferred_return_type->isNull()) {
|
2018-01-07 06:11:23 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:11:51 +01:00
|
|
|
$this->addOrUpdateReturnType(
|
|
|
|
$project_checker,
|
|
|
|
$inferred_return_type,
|
2018-01-23 18:03:18 +01:00
|
|
|
($project_checker->only_replace_php_types_with_non_docblock_types
|
|
|
|
|| $unsafe_return_type)
|
2018-01-07 22:11:51 +01:00
|
|
|
&& $inferred_return_type->from_docblock
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-16 07:22:36 +01:00
|
|
|
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-06-21 20:22:52 +02:00
|
|
|
new CodeLocation($this, $this->function, null, true)
|
2017-01-16 07:22:36 +01:00
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($project_checker->alter_code
|
|
|
|
&& isset($project_checker->getIssuesToFix()['MissingReturnType'])
|
|
|
|
) {
|
2018-01-23 00:00:19 +01:00
|
|
|
if ($inferred_return_type->isMixed() || $inferred_return_type->isNull()) {
|
2018-01-07 06:11:23 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:11:51 +01:00
|
|
|
$this->addOrUpdateReturnType(
|
|
|
|
$project_checker,
|
|
|
|
$inferred_return_type,
|
2018-01-23 18:03:18 +01:00
|
|
|
($project_checker->only_replace_php_types_with_non_docblock_types
|
|
|
|
|| $unsafe_return_type)
|
2018-01-07 22:11:51 +01:00
|
|
|
&& $inferred_return_type->from_docblock
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
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 : ''),
|
2017-06-21 20:22:52 +02:00
|
|
|
new CodeLocation($this, $this->function, null, 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
|
|
|
}
|
|
|
|
|
|
|
|
// passing it through fleshOutTypes eradicates errant $ vars
|
2017-07-29 21:05:06 +02:00
|
|
|
$declared_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
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
|
|
|
|
);
|
|
|
|
|
2018-01-02 22:57:40 +01:00
|
|
|
if (!$inferred_return_type_parts && !$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
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
if (ScopeChecker::onlyThrows($function_stmts)) {
|
2016-11-13 00:51:48 +01:00
|
|
|
// 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
|
|
|
|
2018-01-08 05:48:26 +01:00
|
|
|
if ($project_checker->alter_code && isset($project_checker->getIssuesToFix()['InvalidReturnType'])) {
|
2018-01-07 06:11:23 +01:00
|
|
|
$this->addOrUpdateReturnType($project_checker, Type::getVoid());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-08 23:17:49 +01:00
|
|
|
if (!$declared_return_type->from_docblock || !$declared_return_type->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
|
|
|
'No return statements were found for method ' . $cased_method_id .
|
|
|
|
' but return type \'' . $declared_return_type . '\' was expected',
|
|
|
|
$return_type_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
if (!$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,
|
2017-12-06 06:11:05 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2018-01-05 03:36:16 +01:00
|
|
|
if (!$ignore_nullable_issues
|
|
|
|
&& $inferred_return_type->isNullable()
|
|
|
|
&& !$declared_return_type->isNullable()
|
|
|
|
&& !$declared_return_type->isVoid()
|
|
|
|
) {
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($project_checker->alter_code
|
2018-01-08 00:06:31 +01:00
|
|
|
&& isset($project_checker->getIssuesToFix()['InvalidNullableReturnType'])
|
2018-01-23 00:00:19 +01:00
|
|
|
&& !$inferred_return_type->isNull()
|
2018-01-07 06:11:23 +01:00
|
|
|
) {
|
2018-01-07 22:11:51 +01:00
|
|
|
$this->addOrUpdateReturnType(
|
|
|
|
$project_checker,
|
|
|
|
$inferred_return_type,
|
2018-01-23 18:03:18 +01:00
|
|
|
($project_checker->only_replace_php_types_with_non_docblock_types
|
|
|
|
|| $unsafe_return_type)
|
2018-01-07 22:11:51 +01:00
|
|
|
&& $inferred_return_type->from_docblock
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:36:16 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-01-07 23:17:18 +01:00
|
|
|
new InvalidNullableReturnType(
|
2018-01-05 03:36:16 +01:00
|
|
|
'The declared return type \'' . $declared_return_type . '\' for ' . $cased_method_id .
|
|
|
|
' is not nullable, but \'' . $inferred_return_type . '\' contains null',
|
2018-01-07 06:11:23 +01:00
|
|
|
$return_type_location
|
2018-01-05 03:36:16 +01:00
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($inferred_return_type->isFalsable()
|
|
|
|
&& !$declared_return_type->isFalsable()
|
|
|
|
&& !$declared_return_type->hasBool()
|
|
|
|
) {
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($project_checker->alter_code
|
2018-01-07 23:17:18 +01:00
|
|
|
&& isset($project_checker->getIssuesToFix()['InvalidFalsableReturnType'])
|
2018-01-07 06:11:23 +01:00
|
|
|
) {
|
2018-01-07 22:11:51 +01:00
|
|
|
$this->addOrUpdateReturnType(
|
|
|
|
$project_checker,
|
|
|
|
$inferred_return_type,
|
2018-01-23 18:03:18 +01:00
|
|
|
($project_checker->only_replace_php_types_with_non_docblock_types
|
|
|
|
|| $unsafe_return_type)
|
2018-01-07 22:11:51 +01:00
|
|
|
&& $inferred_return_type->from_docblock
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:36:16 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-01-07 23:17:18 +01:00
|
|
|
new InvalidFalsableReturnType(
|
2018-01-05 03:36:16 +01:00
|
|
|
'The declared return type \'' . $declared_return_type . '\' for ' . $cased_method_id .
|
|
|
|
' does not allow false, but \'' . $inferred_return_type . '\' contains false',
|
2018-01-07 06:11:23 +01:00
|
|
|
$return_type_location
|
2018-01-05 03:36:16 +01:00
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 00:38:23 +01:00
|
|
|
if (!TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$this->source->getFileChecker()->project_checker,
|
2017-01-31 00:38:23 +01:00
|
|
|
$inferred_return_type,
|
|
|
|
$declared_return_type,
|
2018-01-05 03:36:16 +01:00
|
|
|
true,
|
|
|
|
true,
|
2017-01-31 00:38:23 +01:00
|
|
|
$has_scalar_match,
|
2017-11-19 19:42:48 +01:00
|
|
|
$type_coerced,
|
|
|
|
$type_coerced_from_mixed
|
2017-01-31 00:38:23 +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 . '\'',
|
2017-12-06 06:11:05 +01:00
|
|
|
$return_type_location
|
2017-01-17 17:17:49 +01:00
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($project_checker->alter_code
|
|
|
|
&& isset($project_checker->getIssuesToFix()['InvalidReturnType'])
|
|
|
|
) {
|
2018-01-07 22:11:51 +01:00
|
|
|
$this->addOrUpdateReturnType(
|
|
|
|
$project_checker,
|
|
|
|
$inferred_return_type,
|
2018-01-23 18:03:18 +01:00
|
|
|
($project_checker->only_replace_php_types_with_non_docblock_types
|
|
|
|
|| $unsafe_return_type)
|
2018-01-07 22:11:51 +01:00
|
|
|
&& $inferred_return_type->from_docblock
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-17 17:17:49 +01:00
|
|
|
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 . '\'',
|
2018-01-07 06:11:23 +01:00
|
|
|
$return_type_location
|
2017-01-17 17:17:49 +01:00
|
|
|
),
|
|
|
|
$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()) {
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($project_checker->alter_code
|
|
|
|
&& isset($project_checker->getIssuesToFix()['LessSpecificReturnType'])
|
|
|
|
) {
|
2018-01-07 22:11:51 +01:00
|
|
|
$this->addOrUpdateReturnType(
|
|
|
|
$project_checker,
|
|
|
|
$inferred_return_type,
|
2018-01-23 18:03:18 +01:00
|
|
|
($project_checker->only_replace_php_types_with_non_docblock_types
|
|
|
|
|| $unsafe_return_type)
|
2018-01-07 22:11:51 +01:00
|
|
|
&& $inferred_return_type->from_docblock
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-12 00:25:44 +01:00
|
|
|
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-12-06 06:11:05 +01:00
|
|
|
$return_type_location
|
2017-02-12 00:25:44 +01:00
|
|
|
),
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2018-01-07 16:23:02 +01:00
|
|
|
/**
|
|
|
|
* @param string $param_name
|
|
|
|
* @param bool $docblock_only
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function addOrUpdateParamType(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
$param_name,
|
|
|
|
Type\Union $inferred_return_type,
|
|
|
|
$docblock_only = false
|
|
|
|
) {
|
|
|
|
$manipulator = FunctionDocblockManipulator::getForFunction(
|
|
|
|
$project_checker,
|
|
|
|
$this->source->getFilePath(),
|
|
|
|
$this->getMethodId(),
|
|
|
|
$this->function
|
|
|
|
);
|
|
|
|
$manipulator->setParamType(
|
|
|
|
$param_name,
|
|
|
|
!$docblock_only && $project_checker->php_major_version >= 7
|
|
|
|
? $inferred_return_type->toPhpString(
|
2018-01-07 18:38:01 +01:00
|
|
|
$this->source->getNamespace(),
|
2018-01-07 16:23:02 +01:00
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
$project_checker->php_major_version,
|
|
|
|
$project_checker->php_minor_version
|
|
|
|
) : null,
|
|
|
|
$inferred_return_type->toNamespacedString(
|
2018-01-07 18:38:01 +01:00
|
|
|
$this->source->getNamespace(),
|
2018-01-07 16:23:02 +01:00
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
false
|
|
|
|
),
|
|
|
|
$inferred_return_type->toNamespacedString(
|
2018-01-07 18:38:01 +01:00
|
|
|
$this->source->getNamespace(),
|
2018-01-07 16:23:02 +01:00
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
true
|
|
|
|
),
|
|
|
|
$inferred_return_type->canBeFullyExpressedInPhp()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-12 00:25:44 +01:00
|
|
|
/**
|
2018-01-07 06:11:23 +01:00
|
|
|
* @param bool $docblock_only
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-12 00:25:44 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-07 06:11:23 +01:00
|
|
|
private function addOrUpdateReturnType(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
Type\Union $inferred_return_type,
|
|
|
|
$docblock_only = false
|
|
|
|
) {
|
2017-09-16 18:45:11 +02:00
|
|
|
$manipulator = FunctionDocblockManipulator::getForFunction(
|
|
|
|
$project_checker,
|
2017-02-18 19:41:27 +01:00
|
|
|
$this->source->getFilePath(),
|
2017-09-16 18:45:11 +02:00
|
|
|
$this->getMethodId(),
|
|
|
|
$this->function
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
$manipulator->setReturnType(
|
|
|
|
!$docblock_only && $project_checker->php_major_version >= 7
|
|
|
|
? $inferred_return_type->toPhpString(
|
2018-01-07 18:38:01 +01:00
|
|
|
$this->source->getNamespace(),
|
2018-01-07 06:11:23 +01:00
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
$project_checker->php_major_version,
|
|
|
|
$project_checker->php_minor_version
|
|
|
|
) : null,
|
2017-02-12 00:25:44 +01:00
|
|
|
$inferred_return_type->toNamespacedString(
|
2018-01-07 18:38:01 +01:00
|
|
|
$this->source->getNamespace(),
|
2017-02-12 00:25:44 +01:00
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
false
|
|
|
|
),
|
|
|
|
$inferred_return_type->toNamespacedString(
|
2018-01-07 18:38:01 +01:00
|
|
|
$this->source->getNamespace(),
|
2017-02-12 00:25:44 +01:00
|
|
|
$this->source->getAliasedClassesFlipped(),
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
true
|
2018-01-07 06:11:23 +01:00
|
|
|
),
|
|
|
|
$inferred_return_type->canBeFullyExpressedInPhp()
|
2017-02-12 00:25:44 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2017-02-10 02:35:17 +01:00
|
|
|
* @param string $return_type
|
2017-11-09 05:32:22 +01:00
|
|
|
* @param Aliases $aliases
|
2017-02-10 02:35:17 +01:00
|
|
|
* @param array<string, string>|null $template_types
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-14 06:53:43 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-02-10 02:35:17 +01:00
|
|
|
public static function fixUpLocalType(
|
|
|
|
$return_type,
|
2017-07-25 22:11:02 +02:00
|
|
|
Aliases $aliases,
|
2017-02-10 02:35:17 +01:00
|
|
|
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) {
|
2017-05-27 02:05:57 +02:00
|
|
|
if (in_array($return_type_token, ['<', '>', '|', '?', ',', '{', '}', ':'], true)) {
|
2016-10-03 22:39:42 +02:00
|
|
|
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-07-25 22:11:02 +02:00
|
|
|
$aliases
|
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-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-31 16:51:42 +01:00
|
|
|
* @return array<int, FunctionLikeParameter>
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function getMethodParamsById(ProjectChecker $project_checker, $method_id, array $args)
|
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
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if ($fq_class_name && ClassLikeChecker::isUserDefined($project_checker, $fq_class_name)) {
|
|
|
|
$method_params = MethodChecker::getMethodParams($project_checker, $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
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($project_checker, $method_id);
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
|
|
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) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Not expecting $function_param_options to be null for ' . $method_id
|
|
|
|
);
|
2016-12-23 21:06:20 +01:00
|
|
|
}
|
2016-11-07 05:29:54 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
return self::getMatchingParamsFromCallMapOptions($project_checker, $function_param_options, $args);
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
2016-12-23 21:06:20 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
return MethodChecker::getMethodParams($project_checker, $method_id);
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-31 16:51:42 +01:00
|
|
|
* @return array<int, FunctionLikeParameter>
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function getFunctionParamsFromCallMapById(ProjectChecker $project_checker, $method_id, array $args)
|
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) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Not expecting $function_param_options to be null for ' . $method_id
|
|
|
|
);
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
return self::getMatchingParamsFromCallMapOptions($project_checker, $function_param_options, $args);
|
2016-12-31 16:51:42 +01:00
|
|
|
}
|
|
|
|
|
2017-05-25 04:07:49 +02:00
|
|
|
/**
|
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-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-31 16:51:42 +01:00
|
|
|
* @return array<int, FunctionLikeParameter>
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
protected static function getMatchingParamsFromCallMapOptions(
|
2017-07-29 21:05:06 +02:00
|
|
|
ProjectChecker $project_checker,
|
2017-01-02 21:31:18 +01:00
|
|
|
array $function_param_options,
|
2017-07-29 21:05:06 +02:00
|
|
|
array $args
|
2017-01-02 21:31:18 +01:00
|
|
|
) {
|
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
|
|
|
|
2017-09-02 17:18:56 +02:00
|
|
|
if (!$param_type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-07-29 21:05:06 +02:00
|
|
|
if (TypeChecker::isContainedBy($project_checker, $arg->value->inferredType, $param_type)) {
|
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
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
/**
|
|
|
|
* @param array<int, string> $new_issues
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addSuppressedIssues(array $new_issues)
|
|
|
|
{
|
|
|
|
$this->suppressed_issues = array_merge($new_issues, $this->suppressed_issues);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $new_issues
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function removeSuppressedIssues(array $new_issues)
|
|
|
|
{
|
|
|
|
$this->suppressed_issues = array_diff($this->suppressed_issues, $new_issues);
|
|
|
|
}
|
|
|
|
|
2017-07-26 22:09:09 +02:00
|
|
|
/**
|
|
|
|
* Adds a suppressed issue, useful when creating a method checker from scratch
|
|
|
|
*
|
2017-07-27 05:47:29 +02:00
|
|
|
* @param string $issue_name
|
|
|
|
*
|
2017-07-26 22:09:09 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-27 05:47:29 +02:00
|
|
|
public function addSuppressedIssue($issue_name)
|
2017-07-26 22:09:09 +02:00
|
|
|
{
|
|
|
|
$this->suppressed_issues[] = $issue_name;
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$no_effects_hashes = [];
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @return FileChecker
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public function getFileChecker()
|
|
|
|
{
|
|
|
|
return $this->file_checker;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
public function getLocalReturnType(Type\Union $storage_return_type)
|
|
|
|
{
|
|
|
|
if ($this->local_return_type) {
|
|
|
|
return $this->local_return_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->local_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$this->file_checker->project_checker,
|
|
|
|
$storage_return_type,
|
|
|
|
$this->getFQCLN(),
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->local_return_type;
|
|
|
|
}
|
2016-08-14 05:26:45 +02:00
|
|
|
}
|