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

753 lines
23 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;
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;
2016-11-02 07:29:00 +01:00
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
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-11-02 07:29:00 +01:00
const VISIBILITY_PUBLIC = 1;
const VISIBILITY_PROTECTED = 2;
const VISIBILITY_PRIVATE = 3;
2016-10-15 06:12:57 +02:00
/**
* @var array<string,string>
*/
protected static $method_files = [];
2016-10-15 06:12:57 +02:00
/**
* @var array<string,array<\Psalm\FunctionLikeParameter>>
*/
protected static $method_params = [];
2016-10-15 06:12:57 +02:00
/**
* @var array<string,string>
*/
protected static $method_namespaces = [];
2016-10-23 07:57:11 +02:00
2016-11-02 07:29:00 +01:00
/**
* @var array<string, Type\Union|null>
2016-11-02 07:29:00 +01:00
*/
protected static $method_return_types = [];
2016-11-01 05:39:41 +01:00
/**
* @var array<string, CodeLocation|null>
*/
protected static $method_return_type_locations = [];
2016-11-02 07:29:00 +01:00
/**
* @var array<string, string>
*/
2016-08-15 19:37:21 +02:00
protected static $cased_method_ids = [];
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
/**
* @var array<string, bool>
*/
protected static $static_methods = [];
2016-10-15 06:12:57 +02:00
/**
2016-11-01 05:39:41 +01:00
* @var array<string, string>
2016-10-15 06:12:57 +02:00
*/
2016-08-15 05:24:16 +02:00
protected static $declaring_methods = [];
2016-10-15 06:12:57 +02:00
/**
2016-11-01 05:39:41 +01:00
* @var array<string, array<string>>
2016-10-15 06:12:57 +02:00
*/
protected static $overridden_methods = [];
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
/**
* @var array<string, bool>
*/
protected static $have_reflected = [];
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
/**
* @var array<string, bool>
*/
protected static $have_registered = [];
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
/**
* @var array<string, string>
*/
protected static $method_visibility = [];
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
/**
* @var array<string, array<int, string>>
*/
protected static $method_suppress = [];
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
/**
* @var array<string, bool>
*/
protected static $deprecated_methods = [];
2016-04-18 19:31:59 +02:00
/**
* A dictionary of variadic methods
2016-11-02 07:29:00 +01:00
*
2016-11-01 05:39:41 +01:00
* @var array<string, bool>
*/
protected static $variadic_methods = [];
2016-10-30 17:46:18 +01:00
/**
* @param PhpParser\Node\FunctionLike $function
* @param StatementsSource $source
* @param array $this_vars
*/
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
$this->registerMethod($function);
$this->is_static = $function->isStatic();
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
2016-11-07 05:29:54 +01:00
* @return array<\Psalm\FunctionLikeParameter>|null
2016-10-15 06:12:57 +02:00
*/
public static function getMethodParams($method_id)
{
2016-08-15 05:24:16 +02:00
self::registerClassMethod($method_id);
2016-11-07 05:29:54 +01:00
if ($method_id = self::getDeclaringMethodId($method_id)) {
return self::$method_params[$method_id];
}
}
/**
* @param string $method_id
* @return boolean
*/
public static function isVariadic($method_id)
{
self::registerClassMethod($method_id);
$method_id = self::getDeclaringMethodId($method_id);
return isset(self::$variadic_methods[$method_id]);
}
/**
* @param string $method_id
* @return Type\Union|null
*/
public static function getMethodReturnType($method_id)
{
2016-08-15 05:24:16 +02:00
self::registerClassMethod($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);
2016-10-15 06:12:57 +02:00
if (self::$method_return_types[$method_id]) {
return clone self::$method_return_types[$method_id];
}
$overridden_method_ids = self::getOverriddenMethodIds($method_id);
foreach ($overridden_method_ids as $overridden_method_id) {
2016-11-20 17:51:19 +01:00
if (isset(self::$method_return_types[$overridden_method_id])) {
$implementary_return_type = self::$method_return_types[$overridden_method_id];
if ($implementary_return_type && $implementary_return_type->isNull()) {
return Type::getVoid();
}
return $implementary_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)
{
self::registerClassMethod($method_id);
/** @var string */
$method_id = self::getDeclaringMethodId($method_id);
if (!self::$method_return_type_locations[$method_id]) {
$overridden_method_ids = self::getOverriddenMethodIds($method_id);
foreach ($overridden_method_ids as $overridden_method_id) {
if (isset(self::$method_return_type_locations[$overridden_method_id])) {
$defined_location = self::$method_return_type_locations[$overridden_method_id];
break;
}
}
}
return self::$method_return_type_locations[$method_id];
}
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_id = $method->class . '::' . strtolower((string)$method->name);
self::$cased_method_ids[$method_id] = $method->class . '::' . $method->name;
if (strtolower((string)$method->name) === strtolower((string)$method->class)) {
self::setDeclaringMethodId($method->class . '::__construct', $method_id);
2016-11-21 20:36:06 +01:00
}
2016-08-15 05:24:16 +02:00
if (isset(self::$have_reflected[$method_id])) {
2016-11-02 07:29:00 +01:00
return null;
}
2016-10-15 06:12:57 +02:00
/** @var \ReflectionClass */
$declaring_class = $method->getDeclaringClass();
self::$have_reflected[$method_id] = true;
self::$static_methods[$method_id] = $method->isStatic();
self::$method_files[$method_id] = $method->getFileName();
2016-10-15 06:12:57 +02:00
self::$method_namespaces[$method_id] = $declaring_class->getNamespaceName();
self::$declaring_methods[$method_id] = $declaring_class->name . '::' . strtolower((string)$method->getName());
2016-11-02 07:29:00 +01:00
self::$method_visibility[$method_id] = $method->isPrivate()
? self::VISIBILITY_PRIVATE
: ($method->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PUBLIC);
2016-08-05 21:11:20 +02:00
$params = $method->getParameters();
2016-06-25 00:18:11 +02:00
$method_param_names = [];
2016-07-10 22:09:09 +02:00
$method_param_types = [];
2016-06-25 00:18:11 +02:00
self::$method_params[$method_id] = [];
2016-10-15 06:12:57 +02:00
/** @var \ReflectionParameter $param */
2016-08-14 05:26:45 +02:00
foreach ($params as $param) {
$param_array = self::getReflectionParamArray($param);
self::$method_params[$method_id][] = $param_array;
$method_param_names[$param->name] = true;
$method_param_types[$param->name] = $param_array->type;
}
2016-06-15 01:22:29 +02:00
$return_types = null;
2016-06-25 00:18:11 +02:00
$config = Config::getInstance();
2016-06-25 00:18:11 +02:00
$return_type = null;
self::$method_return_type_locations[$method_id] = null;
self::$method_return_types[$method_id] = $return_type;
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
*
2016-11-01 05:39:41 +01:00
* @param string $method_id
* @param CodeLocation $code_location
2016-11-01 05:39:41 +01:00
* @param array<int, string> $suppressed_issues
2016-11-02 07:29:00 +01:00
* @return bool
*/
public static function checkMethodStatic($method_id, CodeLocation $code_location, array $suppressed_issues)
{
2016-08-15 05:24:16 +02:00
self::registerClassMethod($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);
if (!self::$static_methods[$method_id]) {
if (IssueBuffer::accepts(
2016-11-01 05:39:41 +01:00
new InvalidStaticInvocation(
'Method ' . MethodChecker::getCasedMethodId($method_id) . ' is not static',
$code_location
2016-11-01 05:39:41 +01:00
),
$suppressed_issues
)) {
return false;
}
}
2016-11-02 07:29:00 +01:00
return true;
}
2016-11-02 07:29:00 +01:00
/**
* @param PhpParser\Node\Stmt\ClassMethod $method
2016-11-05 01:48:01 +01:00
* @return null|false
2016-11-02 07:29:00 +01:00
*/
protected function registerMethod(PhpParser\Node\Stmt\ClassMethod $method)
{
$method_id = $this->fq_class_name . '::' . strtolower($method->name);
$cased_method_id = self::$cased_method_ids[$method_id] = $this->fq_class_name . '::' . $method->name;
if (strtolower((string)$method->name) === strtolower((string)$this->fq_class_name)) {
self::setDeclaringMethodId($this->fq_class_name . '::__construct', $method_id);
}
if (isset(self::$have_reflected[$method_id]) || isset(self::$have_registered[$method_id])) {
$this->suppressed_issues = self::$method_suppress[$method_id];
2016-11-02 07:29:00 +01:00
return null;
}
self::$have_registered[$method_id] = true;
2016-08-15 05:24:16 +02:00
self::$declaring_methods[$method_id] = $method_id;
self::$static_methods[$method_id] = $method->isStatic();
self::$method_namespaces[$method_id] = $this->namespace;
self::$method_files[$method_id] = $this->file_name;
2016-06-25 00:18:11 +02:00
if ($method->isPrivate()) {
self::$method_visibility[$method_id] = self::VISIBILITY_PRIVATE;
2016-11-02 07:29:00 +01:00
} elseif ($method->isProtected()) {
self::$method_visibility[$method_id] = self::VISIBILITY_PROTECTED;
2016-11-02 07:29:00 +01:00
} else {
self::$method_visibility[$method_id] = self::VISIBILITY_PUBLIC;
}
self::$method_params[$method_id] = [];
2016-06-25 00:18:11 +02:00
$method_param_names = [];
foreach ($method->getParams() as $param) {
2016-11-02 07:29:00 +01:00
$param_array = $this->getTranslatedParam(
$param,
$this
2016-11-02 07:29:00 +01:00
);
self::$method_params[$method_id][] = $param_array;
$method_param_names[$param->name] = $param_array->type;
}
2016-06-25 00:18:11 +02:00
$config = Config::getInstance();
$return_type = null;
$return_type_location = null;
2016-06-25 00:18:11 +02:00
2016-10-15 06:12:57 +02:00
$doc_comment = $method->getDocComment();
2016-06-25 00:18:11 +02:00
2016-10-15 06:12:57 +02:00
self::$method_suppress[$method_id] = [];
2016-10-19 03:54:08 +02:00
if (isset($method->returnType)) {
2016-12-04 06:08:25 +01:00
$parser_return_type = $method->returnType;
$suffix = '';
if ($parser_return_type instanceof PhpParser\Node\NullableType) {
$suffix = '|null';
$parser_return_type = $parser_return_type->type;
}
2016-10-23 07:57:11 +02:00
$return_type = Type::parseString(
2016-12-04 06:08:25 +01:00
(is_string($parser_return_type)
? $parser_return_type
2016-11-08 01:16:51 +01:00
: ClassLikeChecker::getFQCLNFromNameObject(
2016-12-04 06:08:25 +01:00
$parser_return_type,
2016-11-02 07:29:00 +01:00
$this->namespace,
$this->getAliasedClasses()
)
2016-12-04 06:08:25 +01:00
) . $suffix
2016-10-23 07:57:11 +02:00
);
$return_type_location = new CodeLocation($this->getSource(), $method, false, self::RETURN_TYPE_REGEX);
2016-10-19 03:54:08 +02:00
}
2016-10-15 06:12:57 +02:00
if ($doc_comment) {
2016-10-28 16:54:20 +02:00
$docblock_info = null;
2016-10-15 06:12:57 +02:00
2016-10-28 16:54:20 +02:00
try {
$docblock_info = CommentChecker::extractDocblockInfo((string)$doc_comment, $doc_comment->getLine());
2016-11-02 07:29:00 +01:00
} catch (DocblockParseException $e) {
2016-10-28 16:54:20 +02:00
if (IssueBuffer::accepts(
new InvalidDocblock(
2016-11-07 23:07:59 +01:00
'Invalid type passed in docblock for ' . $cased_method_id,
new CodeLocation($this, $method)
2016-10-28 16:54:20 +02:00
)
)) {
return false;
}
}
2016-10-28 16:54:20 +02:00
if ($docblock_info) {
2016-11-13 05:59:31 +01:00
if ($docblock_info->deprecated) {
2016-10-28 16:54:20 +02:00
self::$deprecated_methods[$method_id] = true;
2016-10-15 06:12:57 +02:00
}
2016-11-13 05:59:31 +01:00
if ($docblock_info->variadic) {
2016-10-28 16:54:20 +02:00
self::$variadic_methods[$method_id] = true;
}
2016-11-13 05:59:31 +01:00
$this->suppressed_issues = $docblock_info->suppress;
2016-10-28 16:54:20 +02:00
self::$method_suppress[$method_id] = $this->suppressed_issues;
if ($config->use_docblock_types) {
2016-11-13 05:59:31 +01:00
if ($docblock_info->return_type) {
$docblock_return_type = Type::parseString(
2016-11-02 07:29:00 +01:00
$this->fixUpLocalType(
2016-11-13 05:59:31 +01:00
(string)$docblock_info->return_type,
$this->fq_class_name,
2016-11-02 07:29:00 +01:00
$this->namespace,
$this->getAliasedClasses()
)
);
if (!$return_type_location) {
$return_type_location = new CodeLocation($this->getSource(), $method, true);
}
if ($return_type && !TypeChecker::hasIdenticalTypes($return_type, $docblock_return_type)) {
if (IssueBuffer::accepts(
new InvalidDocblock(
'Docblock return type does not match method return type for ' . $this->getMethodId(),
new CodeLocation($this, $method, true)
)
)) {
return false;
}
} else {
$return_type = $docblock_return_type;
}
$return_type_location->setCommentLine($docblock_info->return_type_line_number);
2016-10-28 16:54:20 +02:00
}
2016-11-13 05:59:31 +01:00
if ($docblock_info->params) {
2016-10-28 16:54:20 +02:00
$this->improveParamsFromDocblock(
2016-11-13 05:59:31 +01:00
$docblock_info->params,
2016-10-28 16:54:20 +02:00
$method_param_names,
self::$method_params[$method_id],
new CodeLocation($this, $method, true)
2016-10-28 16:54:20 +02:00
);
}
2016-10-15 06:12:57 +02:00
}
2016-06-25 00:18:11 +02:00
}
}
self::$method_return_type_locations[$method_id] = $return_type_location;
self::$method_return_types[$method_id] = $return_type;
2016-11-02 07:29:00 +01:00
return null;
}
2016-10-15 06:12:57 +02:00
/**
* @param string $return_type
* @param string $method_id
* @return string
*/
protected static function fixUpReturnType($return_type, $method_id)
{
if (strpos($return_type, '[') !== false) {
$return_type = Type::convertSquareBrackets($return_type);
}
$return_type_tokens = Type::tokenize($return_type);
foreach ($return_type_tokens as $i => &$return_type_token) {
if ($return_type_token[0] === '\\') {
$return_type_token = substr($return_type_token, 1);
continue;
}
if (in_array($return_type_token, ['<', '>', '|', '?', ',', '{', '}', ':'])) {
continue;
}
if (isset($return_type_token[$i + 1]) && $return_type_token[$i + 1] === ':') {
continue;
}
$return_type_token = Type::fixScalarTerms($return_type_token);
if ($return_type_token[0] === strtoupper($return_type_token[0])) {
$fq_class_name = explode('::', $method_id)[0];
if ($return_type_token === '$this') {
$return_type_token = $fq_class_name;
continue;
}
2016-11-08 01:16:51 +01:00
$return_type_token = FileChecker::getFQCLNFromNameInFile(
2016-10-15 06:12:57 +02:00
$return_type_token,
self::$method_namespaces[$method_id],
self::$method_files[$method_id]
);
}
}
return implode('', $return_type_tokens);
}
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;
}
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);
2016-08-15 19:37:21 +02:00
$method_parts = explode('::', $method_id);
$method_parts[1] = strtolower($method_parts[1]);
$method_id = implode('::', $method_parts);
2016-08-15 19:37:21 +02:00
2016-08-15 05:24:16 +02:00
self::registerClassMethod($method_id);
2016-04-18 19:31:59 +02:00
2016-11-21 20:36:06 +01:00
$old_method_id = null;
2016-08-15 05:24:16 +02:00
if (isset(self::$declaring_methods[$method_id])) {
return true;
2016-07-23 16:58:53 +02:00
}
2016-11-21 20:36:06 +01:00
// support checking oldstyle constructors
if ($method_parts[1] === '__construct') {
2016-11-21 21:51:38 +01:00
$method_part_parts = explode('\\', $method_parts[0]);
$old_constructor_name = array_pop($method_part_parts);
2016-11-21 20:36:06 +01:00
$old_method_id = $method_parts[0] . '::' . $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
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
* @return void
*/
2016-08-15 08:12:27 +02:00
public static function registerClassMethod($method_id)
2016-04-18 19:31:59 +02:00
{
2016-08-15 05:24:16 +02:00
ClassLikeChecker::registerClass(explode('::', $method_id)[0]);
2016-04-18 19:31:59 +02:00
}
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)
{
2016-08-15 05:24:16 +02:00
self::registerClassMethod($method_id);
if (isset(self::$deprecated_methods[$method_id])) {
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
) {
2016-08-15 05:24:16 +02:00
self::registerClassMethod($method_id);
2016-10-15 06:12:57 +02:00
$declared_method_id = self::getDeclaringMethodId($method_id);
2016-04-18 19:31:59 +02:00
$method_class = explode('::', (string)$declared_method_id)[0];
2016-04-30 20:14:22 +02:00
$method_name = explode('::', $method_id)[1];
2016-04-18 19:31:59 +02:00
2016-08-15 05:24:16 +02:00
if (!isset(self::$method_visibility[$declared_method_id])) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
new InaccessibleMethod('Cannot access method ' . $method_id, $code_location),
2016-11-02 07:29:00 +01:00
$suppressed_issues
)) {
return false;
}
2016-04-18 19:31:59 +02:00
}
2016-11-08 01:16:51 +01:00
if ($source->getSource() instanceof TraitChecker && $method_class === $source->getFQCLN()) {
2016-11-02 07:29:00 +01:00
return null;
2016-08-13 17:10:43 +02:00
}
2016-08-15 05:24:16 +02:00
switch (self::$method_visibility[$declared_method_id]) {
2016-04-18 19:31:59 +02:00
case self::VISIBILITY_PUBLIC:
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
case self::VISIBILITY_PRIVATE:
if (!$calling_context || $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 self::VISIBILITY_PROTECTED:
if ($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
}
2016-11-02 07:29:00 +01:00
if (ClassChecker::classExtends($method_class, $calling_context) &&
MethodChecker::methodExists($calling_context . '::' . $method_name)
) {
return null;
2016-04-30 20:14:22 +02:00
}
if (!ClassChecker::classExtends($calling_context, $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)
{
2016-08-15 05:24:16 +02:00
self::$declaring_methods[$method_id] = $declaring_method_id;
}
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)
{
2016-11-07 05:29:54 +01:00
if (isset(self::$declaring_methods[$method_id])) {
return self::$declaring_methods[$method_id];
}
return null;
}
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)
{
self::$overridden_methods[$method_id][] = $overridden_method_id;
}
/**
* @param string $method_id
* @return array<string>
*/
public static function getOverriddenMethodIds($method_id)
{
return isset(self::$overridden_methods[$method_id]) ? self::$overridden_methods[$method_id] : [];
}
/**
* @param string $method_id
* @return string
*/
2016-08-15 19:37:21 +02:00
public static function getCasedMethodId($method_id)
{
2016-10-15 06:12:57 +02:00
$method_id = self::getDeclaringMethodId($method_id);
2016-08-15 19:37:21 +02:00
return self::$cased_method_ids[$method_id];
}
2016-11-02 07:29:00 +01:00
/**
* @return void
*/
2016-06-16 02:16:40 +02:00
public static function clearCache()
{
self::$method_files = [];
self::$method_params = [];
2016-08-15 19:37:21 +02:00
self::$cased_method_ids = [];
self::$deprecated_methods = [];
self::$method_namespaces = [];
self::$method_return_types = [];
self::$method_return_type_locations = [];
self::$static_methods = [];
2016-08-15 05:24:16 +02:00
self::$declaring_methods = [];
self::$have_reflected = [];
self::$have_registered = [];
self::$method_visibility = [];
2016-11-05 03:04:55 +01:00
self::$overridden_methods = [];
2016-06-16 02:16:40 +02:00
}
2016-02-04 15:22:46 +01:00
}