1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 02:47:02 +01:00
psalm/src/Psalm/Checker/MethodChecker.php

637 lines
20 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-07-26 00:37:44 +02:00
use Psalm\Issue\DeprecatedMethod;
2016-11-02 07:29:00 +01:00
use Psalm\Issue\InaccessibleMethod;
2016-07-26 00:37:44 +02:00
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\Provider\ClassLikeStorageProvider;
use Psalm\StatementsSource;
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
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)
{
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
2017-05-27 02:16:18 +02:00
*
* @return array<int, \Psalm\FunctionLikeParameter>
2016-10-15 06:12:57 +02:00
*/
public static function getMethodParams(ProjectChecker $project_checker, $method_id)
{
if ($method_id = self::getDeclaringMethodId($project_checker, $method_id)) {
$storage = $project_checker->codebase->getMethodStorage($method_id);
return $storage->params;
2016-11-07 05:29:54 +01:00
}
throw new \UnexpectedValueException('Cannot get method params for ' . $method_id);
}
/**
* @param string $method_id
2017-05-27 02:16:18 +02:00
*
2017-05-25 04:07:49 +02:00
* @return bool
*/
public static function isVariadic(ProjectChecker $project_checker, $method_id)
{
$method_id = (string)self::getDeclaringMethodId($project_checker, $method_id);
list($fq_class_name, $method_name) = explode('::', $method_id);
return $project_checker->classlike_storage_provider->get($fq_class_name)->methods[$method_name]->variadic;
}
/**
* @param string $method_id
2018-01-26 16:59:30 +01:00
* @param string $self_class
2017-05-27 02:16:18 +02:00
*
* @return Type\Union|null
*/
2018-01-26 16:59:30 +01:00
public static function getMethodReturnType(ProjectChecker $project_checker, $method_id, &$self_class)
{
2018-01-26 19:51:00 +01:00
$declaring_method_id = self::getDeclaringMethodId($project_checker, $method_id);
2018-01-26 19:51:00 +01:00
if (!$declaring_method_id) {
return null;
}
2018-01-26 19:51:00 +01:00
$appearing_method_id = self::getAppearingMethodId($project_checker, $method_id);
if (!$appearing_method_id) {
return null;
}
list($appearing_fq_class_name, $appearing_method_name) = explode('::', $appearing_method_id);
2018-01-26 19:51:00 +01:00
if (!ClassLikeChecker::isUserDefined($project_checker, $appearing_fq_class_name)
&& FunctionChecker::inCallMap($appearing_method_id)
) {
2018-01-26 19:51:00 +01:00
return FunctionChecker::getReturnTypeFromCallMap($appearing_method_id);
}
2018-01-26 19:51:00 +01:00
$storage = $project_checker->codebase->getMethodStorage($declaring_method_id);
if ($storage->return_type) {
2018-01-26 19:51:00 +01:00
$self_class = $appearing_fq_class_name;
2018-01-26 16:59:30 +01:00
return clone $storage->return_type;
}
2016-10-15 06:12:57 +02:00
2018-01-26 19:51:00 +01:00
$class_storage = $project_checker->classlike_storage_provider->get($appearing_fq_class_name);
2018-01-26 19:56:53 +01:00
if (!isset($class_storage->overridden_method_ids[$appearing_method_name])) {
2018-01-26 19:51:00 +01:00
return null;
}
2018-01-26 19:51:00 +01:00
foreach ($class_storage->overridden_method_ids[$appearing_method_name] as $overridden_method_id) {
$overridden_storage = $project_checker->codebase->getMethodStorage($overridden_method_id);
if ($overridden_storage->return_type) {
if ($overridden_storage->return_type->isNull()) {
return Type::getVoid();
}
2018-01-26 16:59:30 +01:00
list($fq_overridden_class) = explode('::', $overridden_method_id);
$overridden_class_storage =
$project_checker->classlike_storage_provider->get($fq_overridden_class);
$overridden_return_type = clone $overridden_storage->return_type;
if ($overridden_class_storage->template_types) {
$generic_types = [];
$overridden_return_type->replaceTemplateTypesWithStandins(
$overridden_class_storage->template_types,
$generic_types
);
}
2018-01-26 16:59:30 +01:00
$self_class = $overridden_class_storage->name;
return $overridden_return_type;
2016-10-15 06:12:57 +02:00
}
}
return null;
}
/**
* @param string $method_id
*
* @return bool
*/
public static function getMethodReturnsByRef(ProjectChecker $project_checker, $method_id)
{
$method_id = self::getDeclaringMethodId($project_checker, $method_id);
if (!$method_id) {
return false;
}
2018-01-28 18:29:14 +01:00
list($fq_class_name) = explode('::', $method_id);
if (!ClassLikeChecker::isUserDefined($project_checker, $fq_class_name)
&& FunctionChecker::inCallMap($method_id)
) {
return false;
}
$storage = $project_checker->codebase->getMethodStorage($method_id);
2018-01-13 07:25:13 +01:00
return $storage->returns_by_ref;
}
/**
* @param string $method_id
* @param CodeLocation|null $defined_location
2017-05-27 02:16:18 +02:00
*
* @return CodeLocation|null
*/
public static function getMethodReturnTypeLocation(
ProjectChecker $project_checker,
$method_id,
CodeLocation &$defined_location = null
) {
$method_id = self::getDeclaringMethodId($project_checker, $method_id);
if ($method_id === null) {
return null;
}
$storage = $project_checker->codebase->getMethodStorage($method_id);
if (!$storage->return_type_location) {
$overridden_method_ids = self::getOverriddenMethodIds($project_checker, $method_id);
foreach ($overridden_method_ids as $overridden_method_id) {
$overridden_storage = $project_checker->codebase->getMethodStorage($overridden_method_id);
if ($overridden_storage->return_type_location) {
$defined_location = $overridden_storage->return_type_location;
break;
}
}
}
return $storage->return_type_location;
}
/**
* 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
2017-05-27 02:16:18 +02:00
*
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,
ProjectChecker $project_checker,
CodeLocation $code_location,
array $suppressed_issues
) {
2016-11-07 05:29:54 +01:00
/** @var string */
$method_id = self::getDeclaringMethodId($project_checker, $method_id);
$storage = $project_checker->codebase->getMethodStorage($method_id);
if (!$storage->is_static) {
if ($self_call) {
if (IssueBuffer::accepts(
new NonStaticSelfCall(
'Method ' . MethodChecker::getCasedMethodId($project_checker, $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($project_checker, $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
2017-05-27 02:16:18 +02:00
*
* @return bool|null
2016-04-27 00:42:48 +02:00
*/
public static function checkMethodExists(
ProjectChecker $project_checker,
$method_id,
CodeLocation $code_location,
array $suppressed_issues
) {
2018-02-01 06:50:01 +01:00
if ($project_checker->codebase->methodExists($method_id, $code_location)) {
2016-10-23 18:24:53 +02:00
return true;
}
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
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
* @param CodeLocation $code_location
* @param array $suppressed_issues
2017-05-27 02:16:18 +02:00
*
2016-10-15 06:12:57 +02:00
* @return false|null
*/
public static function checkMethodNotDeprecated(
ProjectChecker $project_checker,
$method_id,
CodeLocation $code_location,
array $suppressed_issues
) {
$method_id = (string) self::getDeclaringMethodId($project_checker, $method_id);
$storage = $project_checker->codebase->getMethodStorage($method_id);
if ($storage->deprecated) {
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new DeprecatedMethod(
'The method ' . MethodChecker::getCasedMethodId($project_checker, $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
*
* @return bool
*/
public static function isMethodVisible(
$method_id,
$calling_context,
StatementsSource $source
) {
$project_checker = $source->getFileChecker()->project_checker;
2018-02-01 06:50:01 +01:00
$codebase = $project_checker->codebase;
$declaring_method_id = self::getDeclaringMethodId($project_checker, $method_id);
if (!$declaring_method_id) {
$method_name = explode('::', $method_id)[1];
if ($method_name === '__construct') {
return true;
}
throw new \UnexpectedValueException('$declaring_method_id not expected to be null here');
}
$appearing_method_id = self::getAppearingMethodId($project_checker, $method_id);
$appearing_method_class = null;
if ($appearing_method_id) {
list($appearing_method_class) = explode('::', $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) {
return true;
}
}
list($declaring_method_class) = explode('::', $declaring_method_id);
if ($source->getSource() instanceof TraitChecker && $declaring_method_class === $source->getFQCLN()) {
return true;
}
$storage = $project_checker->codebase->getMethodStorage($declaring_method_id);
switch ($storage->visibility) {
case ClassLikeChecker::VISIBILITY_PUBLIC:
return true;
case ClassLikeChecker::VISIBILITY_PRIVATE:
if (!$calling_context || $appearing_method_class !== $calling_context) {
return false;
}
return true;
case ClassLikeChecker::VISIBILITY_PROTECTED:
if (!$calling_context) {
return false;
}
if ($appearing_method_class
2018-02-01 06:50:01 +01:00
&& $codebase->classExtends($appearing_method_class, $calling_context)
) {
return true;
}
if ($appearing_method_class
2018-02-01 06:50:01 +01:00
&& !$codebase->classExtends($calling_context, $appearing_method_class)
) {
return false;
}
}
return true;
}
/**
* @param string $method_id
* @param string|null $calling_context
* @param StatementsSource $source
* @param CodeLocation $code_location
* @param array $suppressed_issues
2017-05-27 02:16:18 +02:00
*
* @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
) {
$project_checker = $source->getFileChecker()->project_checker;
2018-02-01 06:50:01 +01:00
$codebase = $project_checker->codebase;
$declaring_method_id = self::getDeclaringMethodId($project_checker, $method_id);
2016-04-18 19:31:59 +02:00
2017-07-09 20:50:57 +02:00
if (!$declaring_method_id) {
$method_name = explode('::', $method_id)[1];
if ($method_name === '__construct') {
return null;
}
throw new \UnexpectedValueException('$declaring_method_id not expected to be null here');
}
$appearing_method_id = self::getAppearingMethodId($project_checker, $method_id);
2017-07-09 20:50:57 +02:00
$appearing_method_class = null;
if ($appearing_method_id) {
list($appearing_method_class) = explode('::', $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) {
return null;
}
2016-12-12 19:50:46 +01:00
}
2017-07-09 20:50:57 +02:00
list($declaring_method_class) = explode('::', $declaring_method_id);
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 = $project_checker->codebase->getMethodStorage($declaring_method_id);
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(
'Cannot access private method ' .
MethodChecker::getCasedMethodId($project_checker, $method_id) .
2016-11-02 07:29:00 +01:00
' 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:
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
}
if ($appearing_method_class
2018-02-01 06:50:01 +01:00
&& $codebase->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 ($appearing_method_class
2018-02-01 06:50:01 +01:00
&& !$codebase->classExtends($calling_context, $appearing_method_class)
) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
new InaccessibleMethod(
'Cannot access protected method ' .
MethodChecker::getCasedMethodId($project_checker, $method_id) .
2016-11-02 07:29:00 +01:00
' 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
2017-05-27 02:16:18 +02:00
*
2016-11-02 07:29:00 +01:00
* @return void
2016-10-15 06:12:57 +02:00
*/
public static function setDeclaringMethodId(
ClassLikeStorageProvider $storage_provider,
$method_id,
$declaring_method_id
) {
list($fq_class_name, $method_name) = explode('::', $method_id);
2016-12-31 01:36:35 +01:00
$class_storage = $storage_provider->get($fq_class_name);
$class_storage->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
2017-05-27 02:16:18 +02:00
*
2016-12-31 17:49:04 +01:00
* @return void
*/
public static function setAppearingMethodId(
ClassLikeStorageProvider $storage_provider,
$method_id,
$appearing_method_id
) {
2016-12-31 17:49:04 +01:00
list($fq_class_name, $method_name) = explode('::', $method_id);
$class_storage = $storage_provider->get($fq_class_name);
$class_storage->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
2017-05-27 02:16:18 +02:00
*
2016-11-07 05:29:54 +01:00
* @return string|null
2016-10-12 07:38:29 +02:00
*/
public static function getDeclaringMethodId(ProjectChecker $project_checker, $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
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
if (isset($class_storage->declaring_method_ids[$method_name])) {
return $class_storage->declaring_method_ids[$method_name];
}
if ($class_storage->abstract && isset($class_storage->overridden_method_ids[$method_name])) {
return $class_storage->overridden_method_ids[$method_name][0];
}
}
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
2017-05-27 02:16:18 +02:00
*
2016-12-31 17:49:04 +01:00
* @return string|null
*/
public static function getAppearingMethodId(ProjectChecker $project_checker, $method_id)
2016-12-31 17:49:04 +01:00
{
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
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
if (isset($class_storage->appearing_method_ids[$method_name])) {
return $class_storage->appearing_method_ids[$method_name];
2016-12-31 17:49:04 +01:00
}
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
* @param string $overridden_method_id
2017-05-27 02:16:18 +02:00
*
2016-11-02 07:29:00 +01:00
* @return void
2016-10-15 06:12:57 +02:00
*/
public static function setOverriddenMethodId(
ClassLikeStorageProvider $storage_provider,
$method_id,
$overridden_method_id
) {
list($fq_class_name, $method_name) = explode('::', $method_id);
$class_storage = $storage_provider->get($fq_class_name);
$class_storage->overridden_method_ids[$method_name][] = $overridden_method_id;
2016-10-15 06:12:57 +02:00
}
/**
* @param string $method_id
2017-05-27 02:16:18 +02:00
*
2016-10-15 06:12:57 +02:00
* @return array<string>
*/
public static function getOverriddenMethodIds(ProjectChecker $project_checker, $method_id)
2016-10-15 06:12:57 +02:00
{
list($fq_class_name, $method_name) = explode('::', $method_id);
$class_storage = $project_checker->classlike_storage_provider->get($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
2017-05-27 02:16:18 +02:00
*
2016-10-15 06:12:57 +02:00
* @return string
*/
public static function getCasedMethodId(ProjectChecker $project_checker, $original_method_id)
2016-08-15 19:37:21 +02:00
{
$method_id = self::getDeclaringMethodId($project_checker, $original_method_id);
if ($method_id === null) {
throw new \UnexpectedValueException('Cannot get declaring method id for ' . $original_method_id);
}
$storage = $project_checker->codebase->getMethodStorage($method_id);
list($fq_class_name) = explode('::', $method_id);
2017-05-25 04:07:49 +02:00
return $fq_class_name . '::' . $storage->cased_name;
2016-08-15 19:37:21 +02:00
}
2016-02-04 15:22:46 +01:00
}