2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
|
|
|
|
2016-01-08 00:36:55 +01:00
|
|
|
namespace CodeInspector;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
use CodeInspector\Issue\UndefinedMethod;
|
|
|
|
use CodeInspector\Issue\InaccessibleMethod;
|
|
|
|
use CodeInspector\Issue\InvalidReturnType;
|
2016-07-22 19:29:46 +02:00
|
|
|
use CodeInspector\Issue\DeprecatedMethod;
|
2016-06-25 00:18:11 +02:00
|
|
|
use CodeInspector\Issue\InvalidDocblock;
|
2016-07-22 19:29:46 +02:00
|
|
|
use CodeInspector\Issue\InvalidStaticInvocation;
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
|
|
|
class ClassMethodChecker extends FunctionChecker
|
|
|
|
{
|
2016-04-16 22:28:25 +02:00
|
|
|
protected static $_method_comments = [];
|
|
|
|
protected static $_method_files = [];
|
|
|
|
protected static $_method_params = [];
|
|
|
|
protected static $_method_namespaces = [];
|
|
|
|
protected static $_method_return_types = [];
|
|
|
|
protected static $_static_methods = [];
|
|
|
|
protected static $_declaring_classes = [];
|
|
|
|
protected static $_existing_methods = [];
|
2016-04-17 17:22:18 +02:00
|
|
|
protected static $_have_reflected = [];
|
2016-04-17 18:27:47 +02:00
|
|
|
protected static $_have_registered = [];
|
|
|
|
protected static $_inherited_methods = [];
|
2016-05-22 18:14:48 +02:00
|
|
|
protected static $_declaring_class = [];
|
2016-04-18 19:31:59 +02:00
|
|
|
protected static $_method_visibility = [];
|
2016-04-27 00:18:49 +02:00
|
|
|
protected static $_new_docblocks = [];
|
2016-07-22 19:29:46 +02:00
|
|
|
protected static $_method_suppress = [];
|
|
|
|
protected static $_deprecated_methods = [];
|
2016-04-18 19:31:59 +02:00
|
|
|
|
|
|
|
const VISIBILITY_PUBLIC = 1;
|
|
|
|
const VISIBILITY_PROTECTED = 2;
|
|
|
|
const VISIBILITY_PRIVATE = 3;
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public function __construct(PhpParser\Node\FunctionLike $function, StatementsSource $source, array $this_vars = [])
|
2016-04-16 22:28:25 +02:00
|
|
|
{
|
|
|
|
parent::__construct($function, $source);
|
|
|
|
|
2016-04-20 19:35:22 +02:00
|
|
|
if ($function instanceof PhpParser\Node\Stmt\ClassMethod) {
|
|
|
|
$this->_registerMethod($function);
|
|
|
|
$this->_is_static = $function->isStatic();
|
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-06-14 01:57:32 +02:00
|
|
|
* @return false|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-05-09 14:56:07 +02:00
|
|
|
public function checkReturnTypes($update_doc_comment = false)
|
2016-04-27 00:18:49 +02:00
|
|
|
{
|
2016-05-09 14:56:07 +02:00
|
|
|
if (!$this->_function->stmts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:52:12 +02:00
|
|
|
if ($this->_function->name === '__construct') {
|
|
|
|
// we know that constructors always return this
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:18:49 +02:00
|
|
|
if (!isset(self::$_new_docblocks[$this->_file_name])) {
|
|
|
|
self::$_new_docblocks[$this->_file_name] = [];
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
$method_id = $this->_absolute_class . '::' . $this->_function->name;
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
$method_return_types = self::getMethodReturnTypes($method_id);
|
|
|
|
|
|
|
|
if (!$method_return_types) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 01:04:51 +02:00
|
|
|
// passing it through fleshOutReturnTypes eradicates errant $ vars
|
|
|
|
$declared_return_type = StatementsChecker::fleshOutReturnTypes(
|
2016-07-22 19:29:46 +02:00
|
|
|
$method_return_types,
|
2016-07-19 01:04:51 +02:00
|
|
|
[],
|
|
|
|
$method_id
|
|
|
|
);
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($declared_return_type) {
|
|
|
|
$inferred_return_types = EffectsAnalyser::getReturnTypes($this->_function->stmts, true);
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
if (!$inferred_return_types) {
|
2016-06-17 22:05:28 +02:00
|
|
|
if ($declared_return_type->isVoid()) {
|
2016-06-17 00:52:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ScopeChecker::onlyThrows($this->_function->stmts)) {
|
|
|
|
// if there's a single throw statement, it's presumably an exception saying this method is not to be used
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-06-17 00:52:12 +02:00
|
|
|
new InvalidReturnType(
|
2016-06-17 23:34:52 +02:00
|
|
|
'No return type was found for method ' . $method_id . ' but return type \'' . $declared_return_type . '\' was expected',
|
2016-06-17 00:52:12 +02:00
|
|
|
$this->_file_name,
|
|
|
|
$this->_function->getLine()
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inferred_return_type = Type::combineTypes($inferred_return_types);
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($inferred_return_type && !$inferred_return_type->isMixed() && !$declared_return_type->isMixed()) {
|
2016-06-16 07:19:52 +02:00
|
|
|
if ($inferred_return_type->isNull() && $declared_return_type->isVoid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-24 22:21:42 +02:00
|
|
|
if (!self::hasIdenticalTypes($declared_return_type, $inferred_return_type)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidReturnType(
|
|
|
|
'The given return type \'' . $declared_return_type . '\' for ' . $method_id . ' is incorrect, got \'' . $inferred_return_type . '\'',
|
|
|
|
$this->_file_name,
|
|
|
|
$this->_function->getLine()
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-07-24 22:21:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2016-07-24 22:21:42 +02:00
|
|
|
protected static function hasIdenticalTypes(Type\Union $declared_type, Type\Union $inferred_type)
|
|
|
|
{
|
|
|
|
if ($declared_type->isNullable() !== $inferred_type->isNullable()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-10 23:20:04 +02:00
|
|
|
|
2016-07-24 22:21:42 +02:00
|
|
|
$simple_declared_types = array_filter(array_keys($declared_type->types), function($type_value) { $type_value !== 'null'; });
|
2016-06-10 23:20:04 +02:00
|
|
|
|
2016-07-24 22:21:42 +02:00
|
|
|
$simple_inferred_types = array_filter(array_keys($inferred_type->types), function($type_value) { $type_value !== 'null'; });
|
2016-06-20 22:54:43 +02:00
|
|
|
|
2016-07-24 22:21:42 +02:00
|
|
|
// gets elements A△B
|
|
|
|
$differing_types = array_diff($simple_inferred_types, $simple_declared_types);
|
|
|
|
|
|
|
|
if (count($differing_types)) {
|
|
|
|
// check whether the differing types are subclasses of declared return types
|
|
|
|
$truly_different = false;
|
|
|
|
|
|
|
|
foreach ($differing_types as $differing_type) {
|
|
|
|
$is_match = false;
|
|
|
|
|
|
|
|
foreach ($simple_declared_types as $simple_declared_type) {
|
|
|
|
var_dump($simple_declared_type, $differing_type);
|
|
|
|
if (is_subclass_of($differing_type, $simple_declared_type) ||
|
|
|
|
(in_array($differing_type, ['float', 'double', 'int']) && in_array($simple_declared_type, ['float', 'double', 'int'])) ||
|
|
|
|
(in_array($differing_type, ['boolean', 'bool']) && in_array($simple_declared_type, ['boolean', 'bool']))
|
|
|
|
) {
|
|
|
|
$is_match = true;
|
|
|
|
break;
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 22:21:42 +02:00
|
|
|
|
|
|
|
if (!$is_match) {
|
|
|
|
$truly_different = true;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-07-24 22:21:42 +02:00
|
|
|
return $truly_different;
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
2016-07-24 22:21:42 +02:00
|
|
|
|
|
|
|
foreach ($declared_type->types as $key => $declared_atomic_type) {
|
|
|
|
$inferred_atomic_type = $inferred_type->types[$key];
|
|
|
|
|
|
|
|
if (!($declared_atomic_type instanceof Type\Generic)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($inferred_atomic_type instanceof Type\Generic) && $declared_atomic_type instanceof Type\Generic) {
|
|
|
|
// @todo handle this better
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::hasIdenticalTypes($declared_atomic_type->type_params[0], $inferred_atomic_type->type_params[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-04-27 00:18:49 +02:00
|
|
|
}
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
public static function getMethodParams($method_id)
|
|
|
|
{
|
2016-04-18 19:31:59 +02:00
|
|
|
self::_populateData($method_id);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
|
|
|
return self::$_method_params[$method_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getMethodReturnTypes($method_id)
|
|
|
|
{
|
2016-04-18 19:31:59 +02:00
|
|
|
self::_populateData($method_id);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-06-16 07:19:52 +02:00
|
|
|
return self::$_method_return_types[$method_id] ? clone self::$_method_return_types[$method_id] : null;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-17 17:22:18 +02:00
|
|
|
public static function extractReflectionMethodInfo($method_id)
|
2016-04-16 22:28:25 +02:00
|
|
|
{
|
2016-05-22 18:14:48 +02:00
|
|
|
if (isset(self::$_have_reflected[$method_id]) || isset(self::$_have_registered[$method_id])) {
|
2016-04-17 17:22:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:58:15 +02:00
|
|
|
try {
|
|
|
|
$method = new \ReflectionMethod($method_id);
|
|
|
|
}
|
|
|
|
catch (\ReflectionException $e) {
|
|
|
|
// maybe it's an old-timey constructor
|
|
|
|
|
|
|
|
$absolute_class = explode('::', $method_id)[0];
|
2016-06-17 23:34:52 +02:00
|
|
|
$class_name = array_pop(explode('\\', $absolute_class));
|
2016-06-17 22:58:15 +02:00
|
|
|
|
|
|
|
$alt_method_id = $absolute_class . '::' . $class_name;
|
|
|
|
|
|
|
|
$method = new \ReflectionMethod($alt_method_id);
|
|
|
|
}
|
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
self::$_have_reflected[$method_id] = true;
|
2016-04-16 22:28:25 +02:00
|
|
|
|
|
|
|
self::$_static_methods[$method_id] = $method->isStatic();
|
|
|
|
self::$_method_files[$method_id] = $method->getFileName();
|
|
|
|
self::$_method_namespaces[$method_id] = $method->getDeclaringClass()->getNamespaceName();
|
2016-05-22 18:14:48 +02:00
|
|
|
self::$_declaring_classes[$method_id] = $method->getDeclaringClass()->name . '::' . $method->getName();
|
2016-04-18 19:31:59 +02:00
|
|
|
self::$_method_visibility[$method_id] = $method->isPrivate() ?
|
|
|
|
self::VISIBILITY_PRIVATE :
|
|
|
|
($method->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PUBLIC);
|
2016-04-16 22:28:25 +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
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
self::$_method_params[$method_id] = [];
|
|
|
|
foreach ($params as $param) {
|
2016-07-10 22:09:09 +02:00
|
|
|
$param_type_string = null;
|
2016-04-16 22:28:25 +02:00
|
|
|
|
|
|
|
if ($param->isArray()) {
|
2016-07-10 22:09:09 +02:00
|
|
|
$param_type_string = 'array';
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-07-13 17:19:07 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$param_class = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$param_class = $param->getClass();
|
|
|
|
}
|
|
|
|
catch (\ReflectionException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param_class && self::$_method_files[$method_id]) {
|
|
|
|
$param_type_string = $param->getClass()->getName();
|
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-07-11 16:19:06 +02:00
|
|
|
$is_nullable = false;
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
try {
|
|
|
|
$is_nullable = $param->getDefaultValue() === null;
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2016-07-10 22:09:09 +02:00
|
|
|
if ($param_type_string && $is_nullable) {
|
|
|
|
$param_type_string .= '|null';
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
catch (\ReflectionException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2016-07-10 22:09:09 +02:00
|
|
|
$param_name = $param->getName();
|
|
|
|
$param_type = $param_type_string ? Type::parseString($param_type_string) : Type::getMixed();
|
|
|
|
|
|
|
|
$method_param_names[$param_name] = true;
|
|
|
|
$method_param_types[$param_name] = $param_type;
|
2016-06-25 00:18:11 +02:00
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
self::$_method_params[$method_id][] = [
|
2016-07-10 22:09:09 +02:00
|
|
|
'name' => $param_name,
|
2016-04-16 22:28:25 +02:00
|
|
|
'by_ref' => $param->isPassedByReference(),
|
2016-07-10 22:09:09 +02:00
|
|
|
'type' => $param_type,
|
2016-07-11 16:19:06 +02:00
|
|
|
'is_nullable' => $is_nullable
|
2016-04-16 22:28:25 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
$return_types = null;
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
$config = Config::getInstance();
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
$return_type = null;
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
$docblock_info = CommentChecker::extractDocblockInfo($method->getDocComment());
|
2016-06-25 00:18:11 +02:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
if ($docblock_info['deprecated']) {
|
|
|
|
self::$_deprecated_methods[$method_id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$_method_suppress[$method_id] = $docblock_info['suppress'];
|
|
|
|
|
|
|
|
if ($config->use_docblock_types) {
|
2016-06-25 00:18:11 +02:00
|
|
|
if ($docblock_info['return_type']) {
|
|
|
|
$return_type = Type::parseString(
|
|
|
|
self::_fixUpReturnType($docblock_info['return_type'], $method_id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info['params']) {
|
|
|
|
foreach ($docblock_info['params'] as $docblock_param) {
|
2016-07-11 16:15:32 +02:00
|
|
|
$docblock_param_name = $docblock_param['name'];
|
2016-06-25 00:18:11 +02:00
|
|
|
|
|
|
|
if (isset($method_param_names[$param_name])) {
|
2016-07-10 22:09:09 +02:00
|
|
|
foreach (self::$_method_params[$method_id] as &$param_info) {
|
2016-07-11 16:15:32 +02:00
|
|
|
if ($param_info['name'] === $docblock_param_name) {
|
|
|
|
$docblock_param_type_string = $docblock_param['type'];
|
|
|
|
|
|
|
|
$existing_param_type = $param_info['type'];
|
|
|
|
|
|
|
|
$new_param_type = Type::parseString(
|
|
|
|
self::_fixUpReturnType($docblock_param_type_string, $method_id)
|
2016-07-10 22:09:09 +02:00
|
|
|
);
|
2016-07-11 16:15:32 +02:00
|
|
|
|
2016-07-12 01:57:01 +02:00
|
|
|
// only fix the type if we're dealing with an undefined or generic type
|
|
|
|
if ($existing_param_type->isMixed() || $new_param_type->hasGeneric()) {
|
|
|
|
$existing_param_type_nullable = $param_info['is_nullable'];
|
|
|
|
|
|
|
|
if ($existing_param_type_nullable && !$new_param_type->isNullable()) {
|
|
|
|
$new_param_type->types['null'] = Type::getNull(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_info['type'] = $new_param_type;
|
2016-07-11 16:15:32 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 22:09:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-25 00:18:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
2016-06-25 00:18:11 +02:00
|
|
|
|
|
|
|
self::$_method_return_types[$method_id] = $return_type;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 18:27:47 +02:00
|
|
|
protected static function _copyToChildMethod($method_id, $child_method_id)
|
2016-04-17 17:22:18 +02:00
|
|
|
{
|
2016-04-17 18:27:47 +02:00
|
|
|
if (!isset(self::$_have_registered[$method_id]) && !isset(self::$_have_reflected[$method_id])) {
|
|
|
|
self::extractReflectionMethodInfo($method_id);
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:31:59 +02:00
|
|
|
if (self::$_method_visibility[$method_id] !== self::VISIBILITY_PRIVATE) {
|
|
|
|
self::$_method_files[$child_method_id] = self::$_method_files[$method_id];
|
|
|
|
self::$_method_params[$child_method_id] = self::$_method_params[$method_id];
|
|
|
|
self::$_method_namespaces[$child_method_id] = self::$_method_namespaces[$method_id];
|
|
|
|
self::$_method_return_types[$child_method_id] = self::$_method_return_types[$method_id];
|
|
|
|
self::$_static_methods[$child_method_id] = self::$_static_methods[$method_id];
|
|
|
|
self::$_method_visibility[$child_method_id] = self::$_method_visibility[$method_id];
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-04-18 19:31:59 +02:00
|
|
|
self::$_declaring_classes[$child_method_id] = self::$_declaring_classes[$method_id];
|
|
|
|
self::$_existing_methods[$child_method_id] = 1;
|
|
|
|
}
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
/**
|
|
|
|
* Determines whether a given method is static or not
|
|
|
|
* @param string $method_id
|
|
|
|
*/
|
2016-07-22 19:29:46 +02:00
|
|
|
public static function checkMethodStatic($method_id, $file_name, $line_number, array $suppressed_issues)
|
2016-04-16 22:28:25 +02:00
|
|
|
{
|
2016-04-18 19:31:59 +02:00
|
|
|
self::_populateData($method_id);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
if (!self::$_static_methods[$method_id]) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidStaticInvocation('Method ' . $method_id . ' is not static', $file_name, $line_number),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _registerMethod(PhpParser\Node\Stmt\ClassMethod $method)
|
|
|
|
{
|
|
|
|
$method_id = $this->_absolute_class . '::' . $method->name;
|
2016-05-22 18:14:48 +02:00
|
|
|
|
|
|
|
if (isset(self::$_have_reflected[$method_id]) || isset(self::$_have_registered[$method_id])) {
|
2016-07-22 19:29:46 +02:00
|
|
|
$this->_suppressed_issues = self::$_method_suppress[$method_id];
|
|
|
|
|
2016-05-22 18:14:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-17 18:27:47 +02:00
|
|
|
self::$_have_registered[$method_id] = true;
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-05-22 18:14:48 +02:00
|
|
|
self::$_declaring_classes[$method_id] = $method_id;
|
2016-04-16 22:28:25 +02:00
|
|
|
self::$_static_methods[$method_id] = $method->isStatic();
|
|
|
|
self::$_method_comments[$method_id] = $method->getDocComment() ?: '';
|
|
|
|
|
|
|
|
self::$_method_namespaces[$method_id] = $this->_namespace;
|
|
|
|
self::$_method_files[$method_id] = $this->_file_name;
|
|
|
|
self::$_existing_methods[$method_id] = 1;
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
if ($method->isPrivate()) {
|
|
|
|
self::$_method_visibility[$method_id] = self::VISIBILITY_PRIVATE;
|
|
|
|
}
|
|
|
|
elseif ($method->isProtected()) {
|
|
|
|
self::$_method_visibility[$method_id] = self::VISIBILITY_PROTECTED;
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
else {
|
2016-06-25 00:18:11 +02:00
|
|
|
self::$_method_visibility[$method_id] = self::VISIBILITY_PUBLIC;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self::$_method_params[$method_id] = [];
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
$method_param_names = [];
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
foreach ($method->getParams() as $param) {
|
|
|
|
$param_type = null;
|
|
|
|
|
|
|
|
if ($param->type) {
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($param->type instanceof Type) {
|
|
|
|
$param_type = $param_type;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-20 06:38:13 +02:00
|
|
|
if (is_string($param->type)) {
|
|
|
|
$param_type_string = $param->type;
|
|
|
|
}
|
|
|
|
elseif ($param->type instanceof PhpParser\Node\Name\FullyQualified) {
|
|
|
|
$param_type_string = implode('\\', $param->type->parts);
|
|
|
|
}
|
|
|
|
elseif ($param->type->parts === ['self']) {
|
|
|
|
$param_type_string = $this->_absolute_class;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$param_type_string = ClassChecker::getAbsoluteClassFromString(implode('\\', $param->type->parts), $this->_namespace, $this->_aliased_classes);
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$is_nullable = $param->default !== null &&
|
2016-04-16 22:28:25 +02:00
|
|
|
$param->default instanceof \PhpParser\Node\Expr\ConstFetch &&
|
|
|
|
$param->default->name instanceof PhpParser\Node\Name &&
|
|
|
|
$param->default->name->parts = ['null'];
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($param_type_string) {
|
|
|
|
if ($is_nullable) {
|
|
|
|
$param_type_string .= '|null';
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_type = Type::parseString($param_type_string);
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
$method_param_names[$param->name] = $param_type;
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
self::$_method_params[$method_id][] = [
|
|
|
|
'name' => $param->name,
|
|
|
|
'by_ref' => $param->byRef,
|
2016-06-20 06:38:13 +02:00
|
|
|
'type' => $param_type ?: Type::getMixed(),
|
2016-04-16 22:28:25 +02:00
|
|
|
];
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
2016-06-25 00:18:11 +02:00
|
|
|
|
|
|
|
$config = Config::getInstance();
|
|
|
|
$return_type = null;
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
$docblock_info = CommentChecker::extractDocblockInfo($method->getDocComment());
|
2016-06-25 00:18:11 +02:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
if ($docblock_info['deprecated']) {
|
|
|
|
self::$_deprecated_methods[$method_id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$_method_suppress[$method_id] = $docblock_info['suppress'];
|
|
|
|
$this->_suppressed_issues = $docblock_info['suppress'];
|
|
|
|
|
|
|
|
if ($config->use_docblock_types) {
|
2016-06-25 00:18:11 +02:00
|
|
|
if ($docblock_info['return_type']) {
|
2016-06-29 22:42:30 +02:00
|
|
|
$return_type =
|
|
|
|
Type::parseString(
|
2016-07-12 06:50:16 +02:00
|
|
|
$this->fixUpLocalType(
|
2016-06-29 22:42:30 +02:00
|
|
|
$docblock_info['return_type'],
|
2016-07-12 06:50:16 +02:00
|
|
|
$this->_absolute_class,
|
2016-06-29 22:42:30 +02:00
|
|
|
$this->_namespace,
|
|
|
|
$this->_aliased_classes
|
|
|
|
)
|
|
|
|
);
|
2016-06-25 00:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($docblock_info['params']) {
|
|
|
|
foreach ($docblock_info['params'] as $docblock_param) {
|
|
|
|
$param_name = $docblock_param['name'];
|
|
|
|
|
|
|
|
if (!array_key_exists($param_name, $method_param_names)) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InvalidDocblock(
|
|
|
|
'Parameter $' . $param_name .' does not appear in the argument list for ' . $method_id,
|
|
|
|
$this->_file_name,
|
|
|
|
$method->getLine()
|
|
|
|
)
|
2016-06-25 00:18:11 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:42:30 +02:00
|
|
|
$param_type =
|
|
|
|
Type::parseString(
|
2016-07-12 06:50:16 +02:00
|
|
|
$this->fixUpLocalType(
|
2016-06-29 22:42:30 +02:00
|
|
|
$docblock_param['type'],
|
2016-07-12 06:50:16 +02:00
|
|
|
$this->_absolute_class,
|
2016-06-29 22:42:30 +02:00
|
|
|
$this->_namespace,
|
|
|
|
$this->_aliased_classes
|
|
|
|
)
|
|
|
|
);
|
2016-06-25 00:18:11 +02:00
|
|
|
|
|
|
|
if ($method_param_names[$param_name] && !$method_param_names[$param_name]->isMixed()) {
|
|
|
|
if (!$param_type->isIn($method_param_names[$param_name])) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-06-25 00:18:11 +02:00
|
|
|
new InvalidDocblock(
|
|
|
|
'Parameter $' . $param_name .' has wrong type \'' . $param_type . '\', should be \'' . $method_param_names[$param_name] . '\'',
|
|
|
|
$this->_file_name,
|
|
|
|
$method->getLine()
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$_method_return_types[$method_id] = $return_type;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 06:50:16 +02:00
|
|
|
public static function fixUpLocalType($return_type, $absolute_class, $namespace, $aliased_classes)
|
2016-04-16 22:28:25 +02:00
|
|
|
{
|
|
|
|
if (strpos($return_type, '[') !== false) {
|
2016-07-12 06:50:16 +02:00
|
|
|
$return_type = Type::convertSquareBrackets($return_type);
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 06:50:16 +02:00
|
|
|
$return_type_tokens = Type::tokenize($return_type);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
|
|
|
foreach ($return_type_tokens as &$return_type_token) {
|
|
|
|
if ($return_type_token[0] === '\\') {
|
|
|
|
$return_type_token = substr($return_type_token, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-14 07:23:57 +02:00
|
|
|
if (in_array($return_type_token, ['<', '>', '|'])) {
|
2016-04-16 22:28:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-12 06:50:16 +02:00
|
|
|
$return_type_token = Type::fixScalarTerms($return_type_token);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-07-12 06:50:16 +02:00
|
|
|
if ($return_type_token[0] === strtoupper($return_type_token[0])) {
|
|
|
|
if ($return_type === '$this' && $absolute_class) {
|
2016-04-16 22:28:25 +02:00
|
|
|
$return_type_token = $absolute_class;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_token = ClassChecker::getAbsoluteClassFromString($return_type_token, $namespace, $aliased_classes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('', $return_type_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _fixUpReturnType($return_type, $method_id)
|
|
|
|
{
|
|
|
|
if (strpos($return_type, '[') !== false) {
|
2016-07-12 06:50:16 +02:00
|
|
|
$return_type = Type::convertSquareBrackets($return_type);
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 06:50:16 +02:00
|
|
|
$return_type_tokens = Type::tokenize($return_type);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
|
|
|
foreach ($return_type_tokens as &$return_type_token) {
|
|
|
|
if ($return_type_token[0] === '\\') {
|
|
|
|
$return_type_token = substr($return_type_token, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-14 07:23:57 +02:00
|
|
|
if (in_array($return_type_token, ['<', '>', '|'])) {
|
2016-04-16 22:28:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-12 06:50:16 +02:00
|
|
|
$return_type_token = Type::fixScalarTerms($return_type_token);
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
if ($return_type_token[0] === strtoupper($return_type_token[0])) {
|
|
|
|
$absolute_class = explode('::', $method_id)[0];
|
|
|
|
|
|
|
|
if ($return_type_token === '$this') {
|
|
|
|
$return_type_token = $absolute_class;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_token = FileChecker::getAbsoluteClassFromNameInFile($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
|
|
|
/**
|
2016-07-24 22:21:42 +02:00
|
|
|
* @return bool|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-07-22 19:29:46 +02:00
|
|
|
public static function checkMethodExists($method_id, $file_name, $line_number, array $suppresssed_issues)
|
2016-04-16 22:28:25 +02:00
|
|
|
{
|
|
|
|
if (isset(self::$_existing_methods[$method_id])) {
|
2016-07-24 22:21:42 +02:00
|
|
|
return true;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:31:59 +02:00
|
|
|
$method_parts = explode('::', $method_id);
|
|
|
|
|
|
|
|
if (method_exists($method_parts[0], $method_parts[1])) {
|
2016-04-16 22:28:25 +02:00
|
|
|
self::$_existing_methods[$method_id] = 1;
|
2016-07-24 22:21:42 +02:00
|
|
|
return true;
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-07-23 16:58:53 +02:00
|
|
|
if (isset(self::$_have_registered[$method_id])) {
|
|
|
|
self::$_existing_methods[$method_id] = 1;
|
2016-07-24 22:21:42 +02:00
|
|
|
return true;
|
2016-07-23 16:58:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new UndefinedMethod('Method ' . $method_id . ' does not exist', $file_name, $line_number),
|
|
|
|
$suppresssed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _populateData($method_id)
|
|
|
|
{
|
2016-05-22 18:14:48 +02:00
|
|
|
if (!isset(self::$_have_reflected[$method_id]) && !isset(self::$_have_registered[$method_id])) {
|
2016-04-18 19:31:59 +02:00
|
|
|
if (isset(self::$_inherited_methods[$method_id])) {
|
|
|
|
self::_copyToChildMethod(self::$_inherited_methods[$method_id], $method_id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::extractReflectionMethodInfo($method_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
public static function checkMethodNotDeprecated($method_id, $file_name, $line_number, array $suppresssed_issues)
|
|
|
|
{
|
|
|
|
self::_populateData($method_id);
|
|
|
|
|
|
|
|
if (isset(self::$_deprecated_methods[$method_id])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedMethod('The method ' . $method_id . ' has been marked as deprecated', $file_name, $line_number),
|
|
|
|
$suppresssed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-06-14 01:57:32 +02:00
|
|
|
* @return false|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-07-22 19:29:46 +02:00
|
|
|
public static function checkMethodVisibility($method_id, $calling_context, $file_name, $line_number, array $suppresssed_issues)
|
2016-04-18 19:31:59 +02:00
|
|
|
{
|
|
|
|
self::_populateData($method_id);
|
|
|
|
|
|
|
|
$method_class = explode('::', $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
|
|
|
|
|
|
|
if (!isset(self::$_method_visibility[$method_id])) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InaccessibleMethod('Cannot access method ' . $method_id, $file_name, $line_number),
|
|
|
|
$suppresssed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (self::$_method_visibility[$method_id]) {
|
|
|
|
case self::VISIBILITY_PUBLIC:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PRIVATE:
|
|
|
|
if (!$calling_context || $method_class !== $calling_context) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InaccessibleMethod(
|
|
|
|
'Cannot access private method ' . $method_id . ' from context ' . $calling_context,
|
|
|
|
$file_name,
|
|
|
|
$line_number
|
|
|
|
),
|
|
|
|
$suppresssed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PROTECTED:
|
|
|
|
if ($method_class === $calling_context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$calling_context) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InaccessibleMethod('Cannot access protected method ' . $method_id, $file_name, $line_number),
|
|
|
|
$suppresssed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 20:14:22 +02:00
|
|
|
if (is_subclass_of($method_class, $calling_context) && method_exists($calling_context, $method_name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:31:59 +02:00
|
|
|
if (!is_subclass_of($calling_context, $method_class)) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InaccessibleMethod(
|
|
|
|
'Cannot access protected method ' . $method_id . ' from context ' . $calling_context,
|
|
|
|
$file_name,
|
|
|
|
$line_number
|
|
|
|
),
|
|
|
|
$suppresssed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 18:27:47 +02:00
|
|
|
|
|
|
|
public static function registerInheritedMethod($parent_method_id, $method_id)
|
|
|
|
{
|
2016-05-22 18:14:48 +02:00
|
|
|
// only register the method if it's not already there
|
|
|
|
if (!isset(self::$_declaring_classes[$method_id])) {
|
|
|
|
self::$_declaring_classes[$method_id] = $parent_method_id;
|
|
|
|
}
|
|
|
|
|
2016-04-17 18:27:47 +02:00
|
|
|
self::$_inherited_methods[$method_id] = $parent_method_id;
|
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-05-22 18:14:48 +02:00
|
|
|
public static function getDeclaringMethod($method_id)
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2016-05-22 18:14:48 +02:00
|
|
|
if (isset(self::$_declaring_classes[$method_id])) {
|
|
|
|
return self::$_declaring_classes[$method_id];
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$method_name = explode('::', $method_id)[1];
|
|
|
|
|
|
|
|
$parent_method_id = (new \ReflectionMethod($method_id))->getDeclaringClass()->getName() . '::' . $method_name;
|
|
|
|
|
2016-05-22 18:14:48 +02:00
|
|
|
self::$_declaring_classes[$method_id] = $parent_method_id;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
|
|
|
return $parent_method_id;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:18:49 +02:00
|
|
|
public static function getNewDocblocksForFile($file_name)
|
|
|
|
{
|
|
|
|
return isset(self::$_new_docblocks[$file_name]) ? self::$_new_docblocks[$file_name] : [];
|
|
|
|
}
|
2016-06-16 02:16:40 +02:00
|
|
|
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$_method_comments = [];
|
|
|
|
self::$_method_files = [];
|
|
|
|
self::$_method_params = [];
|
|
|
|
self::$_method_namespaces = [];
|
|
|
|
self::$_method_return_types = [];
|
|
|
|
self::$_static_methods = [];
|
|
|
|
self::$_declaring_classes = [];
|
|
|
|
self::$_existing_methods = [];
|
|
|
|
self::$_have_reflected = [];
|
|
|
|
self::$_have_registered = [];
|
|
|
|
self::$_inherited_methods = [];
|
|
|
|
self::$_declaring_class = [];
|
|
|
|
self::$_method_visibility = [];
|
|
|
|
self::$_new_docblocks = [];
|
|
|
|
}
|
2016-02-04 15:22:46 +01:00
|
|
|
}
|