1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/src/Psalm/Checker/MethodChecker.php

600 lines
19 KiB
PHP
Raw Normal View History

2016-01-08 00:28:27 +01:00
<?php
namespace Psalm\Checker;
2016-01-08 00:28:27 +01:00
2016-11-02 07:29:00 +01:00
use PhpParser;
use Psalm\CodeLocation;
2016-11-02 07:29:00 +01:00
use Psalm\Config;
use Psalm\Exception\DocblockParseException;
2016-07-26 00:37:44 +02:00
use Psalm\Issue\DeprecatedMethod;
2017-01-02 07:20:47 +01:00
use Psalm\Issue\DuplicateParam;
2016-11-02 07:29:00 +01:00
use Psalm\Issue\InaccessibleMethod;
2016-07-26 00:37:44 +02:00
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidStaticInvocation;
use Psalm\Issue\NonStaticSelfCall;
2016-11-02 07:29:00 +01:00
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
use Psalm\Storage\MethodStorage;
use Psalm\Type;
2016-01-08 00:28:27 +01:00
class MethodChecker extends FunctionLikeChecker
2016-01-08 00:28:27 +01:00
{
2016-10-30 17:46:18 +01:00
/**
* @param PhpParser\Node\FunctionLike $function
* @param StatementsSource $source
* @param array $this_vars
2016-12-17 06:48:31 +01:00
* @psalm-suppress MixedAssignment
2016-10-30 17:46:18 +01:00
*/
public function __construct($function, StatementsSource $source, array $this_vars = [])
{
2016-10-15 06:12:57 +02:00
if (!$function instanceof PhpParser\Node\Stmt\ClassMethod) {
throw new \InvalidArgumentException('Must be called with a ClassMethod');
}
parent::__construct($function, $source);
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
2016-12-24 12:03:55 +01:00
* @return array<int, \Psalm\FunctionLikeParameter>|null
2016-10-15 06:12:57 +02:00
*/
public static function getMethodParams($method_id)
{
2016-11-07 05:29:54 +01:00
if ($method_id = self::getDeclaringMethodId($method_id)) {
$storage = self::getStorage($method_id);
if ($storage) {
return $storage->params;
}
2016-11-07 05:29:54 +01:00
}
}
/**
* @param string $method_id
* @return boolean
*/
public static function isVariadic($method_id)
{
$method_id = (string)self::getDeclaringMethodId($method_id);
list($fq_class_name, $method_name) = explode('::', $method_id);
2017-01-09 05:58:06 +01:00
$fq_class_name = strtolower($fq_class_name);
return ClassLikeChecker::$storage[$fq_class_name]->methods[$method_name]->variadic;
}
/**
* @param string $method_id
* @return Type\Union|null
*/
public static function getMethodReturnType($method_id)
{
2016-11-07 05:29:54 +01:00
/** @var string */
2016-10-15 06:12:57 +02:00
$method_id = self::getDeclaringMethodId($method_id);
list($fq_class_name, $method_name) = explode('::', $method_id);
if (!ClassLikeChecker::isUserDefined($fq_class_name) && FunctionChecker::inCallMap($method_id)) {
return FunctionChecker::getReturnTypeFromCallMap($method_id);
}
$storage = self::getStorage($method_id);
if (!$storage) {
throw new \UnexpectedValueException('$storage should not be null');
2016-10-15 06:12:57 +02:00
}
if ($storage->return_type) {
return clone $storage->return_type;
}
2016-10-15 06:12:57 +02:00
2017-01-09 05:58:06 +01:00
$fq_class_name = strtolower($fq_class_name);
$class_storage = ClassLikeChecker::$storage[$fq_class_name];
foreach ($class_storage->overridden_method_ids[$method_name] as $overridden_method_id) {
$overridden_storage = self::getStorage($overridden_method_id);
if ($overridden_storage && $overridden_storage->return_type) {
if ($overridden_storage->return_type->isNull()) {
return Type::getVoid();
}
return clone $overridden_storage->return_type;
2016-10-15 06:12:57 +02:00
}
}
return null;
}
/**
* @param string $method_id
* @param CodeLocation|null $defined_location
* @return CodeLocation|null
*/
public static function getMethodReturnTypeLocation($method_id, CodeLocation &$defined_location = null)
{
/** @var string */
$method_id = self::getDeclaringMethodId($method_id);
$storage = self::getStorage($method_id);
if (!$storage) {
throw new \UnexpectedValueException('$storage should not be null');
}
if (!$storage->return_type_location) {
$overridden_method_ids = self::getOverriddenMethodIds($method_id);
foreach ($overridden_method_ids as $overridden_method_id) {
$overridden_storage = self::getStorage($overridden_method_id);
if ($overridden_storage && $overridden_storage->return_type_location) {
$defined_location = $overridden_storage->return_type_location;
break;
}
}
}
return $storage->return_type_location;
}
2016-04-27 00:42:48 +02:00
/**
2016-11-02 07:29:00 +01:00
* @param \ReflectionMethod $method
* @return null
2016-04-27 00:42:48 +02:00
*/
2016-08-15 05:24:16 +02:00
public static function extractReflectionMethodInfo(\ReflectionMethod $method)
{
$method_name = strtolower($method->getName());
2017-01-09 05:58:06 +01:00
$class_storage = ClassLikeChecker::$storage[strtolower($method->class)];
if (isset($class_storage->methods[strtolower($method_name)])) {
return;
}
$method_id = $method->class . '::' . $method_name;
$storage = $class_storage->methods[strtolower($method_name)] = new MethodStorage();
$storage->cased_name = $method->name;
if (strtolower((string)$method->name) === strtolower((string)$method->class)) {
self::setDeclaringMethodId($method->class . '::__construct', $method->class . '::' . $method_name);
2016-12-31 17:49:04 +01:00
self::setAppearingMethodId($method->class . '::__construct', $method->class . '::' . $method_name);
2016-11-21 20:36:06 +01:00
}
2016-10-15 06:12:57 +02:00
/** @var \ReflectionClass */
$declaring_class = $method->getDeclaringClass();
$storage->is_static = $method->isStatic();
$storage->file_name = $method->getFileName();
$storage->namespace = $declaring_class->getNamespaceName();
$class_storage->declaring_method_ids[$method_name] =
$declaring_class->name . '::' . strtolower((string)$method->getName());
2016-12-31 17:49:04 +01:00
$class_storage->appearing_method_ids[$method_name] = $class_storage->declaring_method_ids[$method_name];
$class_storage->overridden_method_ids[$method_name] = [];
$storage->visibility = $method->isPrivate()
? ClassLikeChecker::VISIBILITY_PRIVATE
: ($method->isProtected() ? ClassLikeChecker::VISIBILITY_PROTECTED : ClassLikeChecker::VISIBILITY_PUBLIC);
2016-08-05 21:11:20 +02:00
$possible_params = FunctionChecker::getParamsFromCallMap($method_id);
2016-06-25 00:18:11 +02:00
if ($possible_params === null) {
$params = $method->getParameters();
$storage->params = [];
/** @var \ReflectionParameter $param */
foreach ($params as $param) {
$param_array = self::getReflectionParamData($param);
$storage->params[] = $param_array;
$storage->param_types[$param->name] = $param_array->type;
}
} else {
$storage->params = $possible_params[0];
}
$storage->required_param_count = 0;
foreach ($storage->params as $i => $param) {
if (!$param->is_optional) {
$storage->required_param_count = $i + 1;
}
}
2016-11-02 07:29:00 +01:00
return null;
}
/**
* Determines whether a given method is static or not
2016-11-02 07:29:00 +01:00
*
* @param string $method_id
* @param bool $self_call
* @param CodeLocation $code_location
* @param array<string> $suppressed_issues
2016-11-02 07:29:00 +01:00
* @return bool
*/
2017-01-12 03:37:53 +01:00
public static function checkStatic(
$method_id,
$self_call,
CodeLocation $code_location,
array $suppressed_issues
) {
2016-11-07 05:29:54 +01:00
/** @var string */
2016-10-15 06:12:57 +02:00
$method_id = self::getDeclaringMethodId($method_id);
$storage = self::getStorage($method_id);
if (!$storage) {
throw new \UnexpectedValueException('$storage should not be null');
}
if (!$storage->is_static) {
if ($self_call) {
if (IssueBuffer::accepts(
new NonStaticSelfCall(
'Method ' . MethodChecker::getCasedMethodId($method_id) . ' is not static, but is called using self::',
$code_location
),
$suppressed_issues
)) {
return false;
}
} else {
if (IssueBuffer::accepts(
new InvalidStaticInvocation(
'Method ' . MethodChecker::getCasedMethodId($method_id) . ' is not static, but is called statically',
$code_location
),
$suppressed_issues
)) {
return false;
}
}
}
2016-11-02 07:29:00 +01:00
return true;
}
2016-04-27 00:42:48 +02:00
/**
* @param string $method_id
* @param CodeLocation $code_location
* @param array $suppressed_issues
* @return bool|null
2016-04-27 00:42:48 +02:00
*/
public static function checkMethodExists($method_id, CodeLocation $code_location, array $suppressed_issues)
2016-10-23 18:24:53 +02:00
{
if (self::methodExists($method_id)) {
return true;
}
2017-01-09 05:58:06 +01:00
list($fq_class_name, $method_name) = explode('::', $method_id);
2016-10-23 18:24:53 +02:00
if (IssueBuffer::accepts(
new UndefinedMethod('Method ' . $method_id . ' does not exist', $code_location),
2016-11-02 07:29:00 +01:00
$suppressed_issues
2016-10-23 18:24:53 +02:00
)) {
return false;
}
2016-11-02 07:29:00 +01:00
return null;
2016-10-23 18:24:53 +02:00
}
/**
* Whether or not a given method exists
2016-11-02 07:29:00 +01:00
*
2016-10-23 18:24:53 +02:00
* @param string $method_id
* @return bool
*/
public static function methodExists($method_id)
{
// remove trailing backslash if it exists
$method_id = preg_replace('/^\\\\/', '', $method_id);
list($fq_class_name, $method_name) = explode('::', $method_id);
$method_name = strtolower($method_name);
$method_id = $fq_class_name . '::' . $method_name;
2016-08-15 19:37:21 +02:00
2016-11-21 20:36:06 +01:00
$old_method_id = null;
2017-01-09 05:58:06 +01:00
$fq_class_name_lower = strtolower($fq_class_name);
if (!isset(ClassLikeChecker::$storage[$fq_class_name_lower])) {
throw new \UnexpectedValueException('Storage should exist for ' . $fq_class_name);
2016-12-31 02:39:12 +01:00
}
2017-01-09 05:58:06 +01:00
$class_storage = ClassLikeChecker::$storage[$fq_class_name_lower];
if (isset($class_storage->declaring_method_ids[$method_name])) {
return true;
2016-07-23 16:58:53 +02:00
}
2016-11-21 20:36:06 +01:00
// support checking oldstyle constructors
if ($method_name === '__construct') {
$method_name_parts = explode('\\', $fq_class_name);
$old_constructor_name = array_pop($method_name_parts);
$old_method_id = $fq_class_name . '::' . $old_constructor_name;
}
2016-11-21 20:36:06 +01:00
if (FunctionChecker::inCallMap($method_id) || ($old_method_id && FunctionChecker::inCallMap($method_id))) {
2016-10-22 23:35:59 +02:00
return true;
}
2016-10-23 18:24:53 +02:00
return false;
2016-04-18 19:31:59 +02:00
}
/**
* @param string $method_id
* @return MethodStorage
*/
public static function getStorage($method_id)
{
list($fq_class_name, $method_name) = explode('::', $method_id);
2017-01-09 05:58:06 +01:00
$fq_class_name_lower = strtolower($fq_class_name);
$class_storage = ClassLikeChecker::$storage[$fq_class_name_lower];
if (!isset($class_storage->methods[strtolower($method_name)])) {
throw new \UnexpectedValueException('$storage should not be null for ' . $method_id);
}
return $class_storage->methods[strtolower($method_name)];
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
* @param CodeLocation $code_location
* @param array $suppressed_issues
2016-10-15 06:12:57 +02:00
* @return false|null
*/
public static function checkMethodNotDeprecated($method_id, CodeLocation $code_location, array $suppressed_issues)
{
$method_id = (string) self::getDeclaringMethodId($method_id);
$storage = self::getStorage($method_id);
if ($storage->deprecated) {
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new DeprecatedMethod(
'The method ' . MethodChecker::getCasedMethodId($method_id) . ' has been marked as deprecated',
$code_location
2016-11-02 07:29:00 +01:00
),
$suppressed_issues
)) {
return false;
}
}
2016-11-02 07:29:00 +01:00
return null;
}
2016-04-27 00:42:48 +02:00
/**
2016-08-13 17:10:43 +02:00
* @param string $method_id
2016-11-01 05:39:41 +01:00
* @param string|null $calling_context
2016-08-13 17:28:06 +02:00
* @param StatementsSource $source
* @param CodeLocation $code_location
2016-11-02 07:29:00 +01:00
* @param array $suppressed_issues
* @return false|null
2016-04-27 00:42:48 +02:00
*/
2016-11-02 07:29:00 +01:00
public static function checkMethodVisibility(
$method_id,
$calling_context,
StatementsSource $source,
CodeLocation $code_location,
2016-11-02 07:29:00 +01:00
array $suppressed_issues
) {
$declaring_method_id = self::getDeclaringMethodId($method_id);
$appearing_method_id = self::getAppearingMethodId($method_id);
2016-04-18 19:31:59 +02:00
list($method_class, $method_name) = explode('::', (string)$method_id);
list($declaring_method_class) = explode('::', (string)$declaring_method_id);
list($appearing_method_class) = explode('::', (string)$appearing_method_id);
// if the calling class is the same, we know the method exists, so it must be visible
if ($appearing_method_class === $calling_context) {
2016-12-12 19:50:46 +01:00
return null;
}
if ($source->getSource() instanceof TraitChecker && $declaring_method_class === $source->getFQCLN()) {
2016-11-02 07:29:00 +01:00
return null;
2016-08-13 17:10:43 +02:00
}
$storage = self::getStorage((string)$declaring_method_id);
if (!$storage) {
throw new \UnexpectedValueException('$storage should not be null');
}
switch ($storage->visibility) {
case ClassLikeChecker::VISIBILITY_PUBLIC:
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
case ClassLikeChecker::VISIBILITY_PRIVATE:
if (!$calling_context || $appearing_method_class !== $calling_context) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
new InaccessibleMethod(
2016-11-02 07:29:00 +01:00
'Cannot access private method ' . MethodChecker::getCasedMethodId($method_id) .
' from context ' . $calling_context,
$code_location
),
2016-11-02 07:29:00 +01:00
$suppressed_issues
)) {
return false;
}
2016-04-18 19:31:59 +02:00
}
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
case ClassLikeChecker::VISIBILITY_PROTECTED:
if ($appearing_method_class === $calling_context) {
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
}
if (!$calling_context) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new InaccessibleMethod(
'Cannot access protected method ' . $method_id,
$code_location
2016-11-02 07:29:00 +01:00
),
$suppressed_issues
)) {
return false;
}
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
}
$file_checker = $source->getFileChecker();
if (ClassChecker::classExtends($appearing_method_class, $calling_context)) {
2016-11-02 07:29:00 +01:00
return null;
2016-04-30 20:14:22 +02:00
}
if (!ClassChecker::classExtends($calling_context, $appearing_method_class)) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
new InaccessibleMethod(
2016-11-02 07:29:00 +01:00
'Cannot access protected method ' . MethodChecker::getCasedMethodId($method_id) .
' from context ' . $calling_context,
$code_location
),
2016-11-02 07:29:00 +01:00
$suppressed_issues
)) {
return false;
}
2016-04-18 19:31:59 +02:00
}
}
2016-11-02 07:29:00 +01:00
return null;
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
* @param string $declaring_method_id
2016-11-02 07:29:00 +01:00
* @return void
2016-10-15 06:12:57 +02:00
*/
public static function setDeclaringMethodId($method_id, $declaring_method_id)
{
list($fq_class_name, $method_name) = explode('::', $method_id);
2016-12-31 01:36:35 +01:00
2017-01-09 05:58:06 +01:00
ClassLikeChecker::$storage[strtolower($fq_class_name)]->declaring_method_ids[$method_name] = $declaring_method_id;
}
2016-12-31 17:49:04 +01:00
/**
* @param string $method_id
* @param string $appearing_method_id
* @return void
*/
public static function setAppearingMethodId($method_id, $appearing_method_id)
{
list($fq_class_name, $method_name) = explode('::', $method_id);
2017-01-09 05:58:06 +01:00
ClassLikeChecker::$storage[strtolower($fq_class_name)]->appearing_method_ids[$method_name] = $appearing_method_id;
2016-12-31 17:49:04 +01:00
}
2016-10-12 07:38:29 +02:00
/**
* @param string $method_id
2016-11-07 05:29:54 +01:00
* @return string|null
2016-10-12 07:38:29 +02:00
*/
2016-10-15 06:12:57 +02:00
public static function getDeclaringMethodId($method_id)
{
2017-01-12 03:37:53 +01:00
$method_id = strtolower($method_id);
list($fq_class_name, $method_name) = explode('::', $method_id);
2016-11-07 05:29:54 +01:00
2017-01-12 03:37:53 +01:00
$fq_class_name = $fq_class_name;
2017-01-09 05:58:06 +01:00
if (!isset(ClassLikeChecker::$storage[$fq_class_name])) {
throw new \UnexpectedValueException('$storage should not be null for ' . $fq_class_name);
}
if (isset(ClassLikeChecker::$storage[$fq_class_name]->declaring_method_ids[$method_name])) {
return ClassLikeChecker::$storage[$fq_class_name]->declaring_method_ids[$method_name];
}
}
2016-12-31 17:49:04 +01:00
/**
* Get the class this method appears in (vs is declared in, which could give a trait)
*
* @param string $method_id
* @return string|null
*/
public static function getAppearingMethodId($method_id)
{
2017-01-12 03:37:53 +01:00
$method_id = strtolower($method_id);
2016-12-31 17:49:04 +01:00
2017-01-12 03:37:53 +01:00
list($fq_class_name, $method_name) = explode('::', $method_id);
2017-01-09 05:58:06 +01:00
if (!isset(ClassLikeChecker::$storage[$fq_class_name])) {
throw new \UnexpectedValueException('$storage should not be null for ' . $fq_class_name);
}
2016-12-31 17:49:04 +01:00
if (isset(ClassLikeChecker::$storage[$fq_class_name]->appearing_method_ids[$method_name])) {
return ClassLikeChecker::$storage[$fq_class_name]->appearing_method_ids[$method_name];
}
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
* @param string $overridden_method_id
2016-11-02 07:29:00 +01:00
* @return void
2016-10-15 06:12:57 +02:00
*/
public static function setOverriddenMethodId($method_id, $overridden_method_id)
{
list($fq_class_name, $method_name) = explode('::', $method_id);
2017-01-09 05:58:06 +01:00
ClassLikeChecker::$storage[strtolower($fq_class_name)]->overridden_method_ids[$method_name][] = $overridden_method_id;
2016-10-15 06:12:57 +02:00
}
/**
* @param string $method_id
* @return array<string>
*/
public static function getOverriddenMethodIds($method_id)
{
list($fq_class_name, $method_name) = explode('::', $method_id);
2017-01-09 05:58:06 +01:00
$class_storage = ClassLikeChecker::$storage[strtolower($fq_class_name)];
2016-12-31 01:36:35 +01:00
if (isset($class_storage->overridden_method_ids[$method_name])) {
return $class_storage->overridden_method_ids[$method_name];
}
return [];
2016-10-15 06:12:57 +02:00
}
/**
* @param string $original_method_id
2016-10-15 06:12:57 +02:00
* @return string
*/
public static function getCasedMethodId($original_method_id)
2016-08-15 19:37:21 +02:00
{
$method_id = self::getDeclaringMethodId($original_method_id);
if ($method_id === null) {
throw new \UnexpectedValueException('Cannot get declaring method id for ' . $original_method_id);
}
$storage = self::getStorage($method_id);
if (!$storage) {
throw new \UnexpectedValueException('$storage should not be null');
}
list($fq_class_name) = explode('::', $method_id);
return $fq_class_name . '::' .$storage->cased_name;
2016-08-15 19:37:21 +02:00
}
2016-11-02 07:29:00 +01:00
/**
* @return void
*/
2016-06-16 02:16:40 +02:00
public static function clearCache()
{
2016-06-16 02:16:40 +02:00
}
2016-02-04 15:22:46 +01:00
}