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-04-17 22:20:36 +02:00
|
|
|
use CodeInspector\Exception\UndefinedMethodException;
|
2016-04-18 19:31:59 +02:00
|
|
|
use CodeInspector\Exception\InaccessibleMethodException;
|
2016-05-09 14:56:07 +02:00
|
|
|
use CodeInspector\Exception\ReturnTypeException;
|
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 = [];
|
2016-04-17 17:26:29 +02:00
|
|
|
protected static $_method_custom_calls = [];
|
2016-04-17 18:27:47 +02:00
|
|
|
protected static $_inherited_methods = [];
|
2016-05-21 01:07:05 +02:00
|
|
|
protected static $_implementing_methods = [];
|
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-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-09 14:56:07 +02:00
|
|
|
const TYPE_REGEX = '(\\\?[A-Za-z0-9\<\>\[\]|\\\]+[A-Za-z0-9\<\>\[\]]|\$[a-zA-Z_0-9\<\>\|\[\]]+)';
|
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
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
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-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-05-09 14:56:07 +02:00
|
|
|
$existing_return_types = self::getMethodReturnTypes($method_id);
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
if ($existing_return_types) {
|
|
|
|
$return_types = EffectsAnalyser::getReturnTypes($this->_function->stmts, true);
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
if ($return_types && $return_types !== ['mixed']) {
|
|
|
|
$simple_existing_return_types = array_map(
|
|
|
|
function ($value) {
|
|
|
|
return preg_replace('/<.*$/', '', $value);
|
|
|
|
},
|
|
|
|
$existing_return_types
|
|
|
|
);
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
if (count(array_diff($return_types, $simple_existing_return_types)) && count(array_diff($return_types, $existing_return_types))) {
|
|
|
|
if ($update_doc_comment) {
|
|
|
|
$doc_comment = $this->_function->getDocComment();
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
$this->_registerNewDocComment($return_types, $doc_comment);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new ReturnTypeException(
|
|
|
|
'The given return type for ' . $method_id . ' is incorrect, expecting ' . implode('|', $return_types),
|
|
|
|
$this->_file_name,
|
|
|
|
$this->_function->getLine()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($update_doc_comment) {
|
|
|
|
$return_types = EffectsAnalyser::getReturnTypes($this->_function->stmts, true);
|
|
|
|
|
|
|
|
if ($return_types && $return_types !== ['mixed']) {
|
2016-04-27 00:18:49 +02:00
|
|
|
$doc_comment = $this->_function->getDocComment();
|
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
$this->_registerNewDocComment($return_types, $doc_comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Comment\Doc|null $doc_comment
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _registerNewDocComment(array $return_types, \PhpParser\Comment\Doc $doc_comment = null)
|
|
|
|
{
|
|
|
|
$inverted_aliased_classes = array_flip($this->_aliased_classes);
|
|
|
|
$absolute_class = $this->_absolute_class;
|
|
|
|
$class_name = array_pop(explode('\\', $absolute_class));
|
|
|
|
|
|
|
|
// add leading namespace separator to classes
|
|
|
|
$return_types = array_map(
|
|
|
|
function ($return_type) use ($inverted_aliased_classes, $absolute_class, $class_name) {
|
|
|
|
$type_tokens = TypeChecker::tokenize($return_type);
|
|
|
|
|
|
|
|
foreach ($type_tokens as &$token) {
|
|
|
|
if ($token === '<' || $token === '>' || $token[0] !== strtoupper($token[0])) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
if (isset($inverted_aliased_classes[$token])) {
|
|
|
|
$token = $inverted_aliased_classes[$token];
|
|
|
|
}
|
|
|
|
else if ($token === $absolute_class) {
|
|
|
|
$token = $class_name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$token = '\\' . $token;
|
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
return implode('', $type_tokens);
|
|
|
|
},
|
|
|
|
$return_types
|
|
|
|
);
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
if ($doc_comment) {
|
|
|
|
$parsed_doc_comment = StatementsChecker::parseDocComment($doc_comment->getText());
|
|
|
|
$parsed_doc_comment['specials']['return'] = [implode('|', $return_types)];
|
|
|
|
$new_doc_comment_text = StatementsChecker::renderDocComment($parsed_doc_comment);
|
2016-04-27 00:18:49 +02:00
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
else {
|
|
|
|
$new_doc_comment_text = "/**\n * @return " . implode('|', $return_types) . "\n */";
|
|
|
|
}
|
|
|
|
|
|
|
|
$start_at = $doc_comment ? $doc_comment->getLine() : $this->_function->getLine();
|
|
|
|
$old_line_count = $doc_comment ? substr_count($doc_comment->getText(), PHP_EOL) + 1 : 0;
|
|
|
|
|
|
|
|
self::$_new_docblocks[$this->_file_name][$start_at] = ['new_text' => $new_doc_comment_text, 'old_line_count' => $old_line_count];
|
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
|
|
|
|
|
|
|
$return_types = self::$_method_return_types[$method_id];
|
|
|
|
|
|
|
|
return $return_types;
|
|
|
|
}
|
|
|
|
|
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-04-17 17:22:18 +02:00
|
|
|
if (isset(self::$_have_reflected[$method_id])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
$method = new \ReflectionMethod($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();
|
|
|
|
self::$_declaring_classes[$method_id] = $method->getDeclaringClass()->name;
|
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();
|
|
|
|
|
|
|
|
self::$_method_params[$method_id] = [];
|
|
|
|
foreach ($params as $param) {
|
|
|
|
$param_type = null;
|
|
|
|
|
|
|
|
if ($param->isArray()) {
|
|
|
|
$param_type = 'array';
|
|
|
|
|
|
|
|
} elseif ($param->getClass() && self::$_method_files[$method_id]) {
|
|
|
|
$param_type = $param->getClass()->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_nullable = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$is_nullable = $param->getDefaultValue() === null;
|
|
|
|
}
|
|
|
|
catch (\ReflectionException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$_method_params[$method_id][] = [
|
|
|
|
'name' => $param->getName(),
|
|
|
|
'by_ref' => $param->isPassedByReference(),
|
|
|
|
'type' => $param_type,
|
|
|
|
'is_nullable' => $is_nullable
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_types = [];
|
|
|
|
|
|
|
|
$comments = StatementsChecker::parseDocComment($method->getDocComment() ?: '');
|
|
|
|
|
|
|
|
if ($comments) {
|
|
|
|
if (isset($comments['specials']['return'])) {
|
|
|
|
$return_blocks = explode(' ', $comments['specials']['return'][0]);
|
|
|
|
foreach ($return_blocks as $block) {
|
|
|
|
if ($block && preg_match('/^' . self::TYPE_REGEX . '$/', $block)) {
|
|
|
|
$return_types = explode('|', $block);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($comments['specials']['call'])) {
|
|
|
|
self::$_method_custom_calls[$method_id] = [];
|
|
|
|
|
|
|
|
$call_blocks = $comments['specials']['call'];
|
|
|
|
foreach ($comments['specials']['call'] as $block) {
|
|
|
|
if ($block) {
|
|
|
|
self::$_method_custom_calls[$method_id][] = trim($block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_types = array_filter($return_types, function ($entry) {
|
|
|
|
return !empty($entry) && $entry !== '[type]';
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($return_types) {
|
|
|
|
foreach ($return_types as &$return_type) {
|
|
|
|
$return_type = self::_fixUpReturnType($return_type, $method_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$_method_return_types[$method_id] = $return_types;
|
|
|
|
}
|
|
|
|
|
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-04-27 00:18:49 +02:00
|
|
|
* @return bool
|
2016-04-16 22:28:25 +02:00
|
|
|
*/
|
|
|
|
public static function isGivenMethodStatic($method_id)
|
|
|
|
{
|
2016-04-18 19:31:59 +02:00
|
|
|
self::_populateData($method_id);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
|
|
|
return self::$_static_methods[$method_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _registerMethod(PhpParser\Node\Stmt\ClassMethod $method)
|
|
|
|
{
|
|
|
|
$method_id = $this->_absolute_class . '::' . $method->name;
|
2016-04-17 18:27:47 +02:00
|
|
|
self::$_have_registered[$method_id] = true;
|
2016-04-16 22:28:25 +02:00
|
|
|
|
|
|
|
self::$_declaring_classes[$method_id] = $this->_absolute_class;
|
|
|
|
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-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
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
$comments = StatementsChecker::parseDocComment($method->getDocComment());
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
$return_types = [];
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
if (isset($comments['specials']['return'])) {
|
|
|
|
$return_blocks = explode(' ', $comments['specials']['return'][0]);
|
|
|
|
foreach ($return_blocks as $block) {
|
|
|
|
if ($block) {
|
|
|
|
if ($block && preg_match('/^' . self::TYPE_REGEX . '$/', $block)) {
|
|
|
|
$return_types = explode('|', $block);
|
|
|
|
break;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
if (isset($comments['specials']['call'])) {
|
|
|
|
self::$_method_custom_calls[$method_id] = [];
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
$call_blocks = $comments['specials']['call'];
|
|
|
|
foreach ($comments['specials']['call'] as $block) {
|
|
|
|
if ($block) {
|
|
|
|
self::$_method_custom_calls[$method_id][] = trim($block);
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
$return_types = array_filter($return_types, function ($entry) {
|
|
|
|
return !empty($entry) && $entry !== '[type]';
|
|
|
|
});
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
foreach ($return_types as &$return_type) {
|
|
|
|
$return_type = $this->_fixUpLocalReturnType($return_type, $method_id, $this->_namespace, $this->_aliased_classes);
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
self::$_method_return_types[$method_id] = $return_types;
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
self::$_method_params[$method_id] = [];
|
|
|
|
|
|
|
|
foreach ($method->getParams() as $param) {
|
|
|
|
$param_type = null;
|
|
|
|
|
|
|
|
if ($param->type) {
|
|
|
|
if (is_string($param->type)) {
|
|
|
|
$param_type = $param->type;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($param->type instanceof PhpParser\Node\Name\FullyQualified) {
|
|
|
|
$param_type = implode('\\', $param->type->parts);
|
2016-05-09 21:58:48 +02:00
|
|
|
|
|
|
|
} elseif ($param->type->parts === ['self']) {
|
|
|
|
$param_type = $this->_absolute_class;
|
|
|
|
|
|
|
|
} else {
|
2016-04-16 22:28:25 +02:00
|
|
|
$param_type = ClassChecker::getAbsoluteClassFromString(implode('\\', $param->type->parts), $this->_namespace, $this->_aliased_classes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_nullable = $param->default !== null &&
|
|
|
|
$param->default instanceof \PhpParser\Node\Expr\ConstFetch &&
|
|
|
|
$param->default->name instanceof PhpParser\Node\Name &&
|
|
|
|
$param->default->name->parts = ['null'];
|
|
|
|
|
|
|
|
self::$_method_params[$method_id][] = [
|
|
|
|
'name' => $param->name,
|
|
|
|
'by_ref' => $param->byRef,
|
|
|
|
'type' => $param_type,
|
|
|
|
'is_nullable' => $is_nullable
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _fixUpLocalReturnType($return_type, $method_id, $namespace, $aliased_classes)
|
|
|
|
{
|
|
|
|
if (strpos($return_type, '[') !== false) {
|
|
|
|
$return_type = TypeChecker::convertSquareBrackets($return_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_tokens = TypeChecker::tokenize($return_type);
|
|
|
|
|
|
|
|
foreach ($return_type_tokens as &$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 ($return_type_token[0] === strtoupper($return_type_token[0])) {
|
|
|
|
$absolute_class = explode('::', $method_id)[0];
|
|
|
|
|
|
|
|
if ($return_type === '$this') {
|
|
|
|
$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) {
|
|
|
|
$return_type = TypeChecker::convertSquareBrackets($return_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_type_tokens = TypeChecker::tokenize($return_type);
|
|
|
|
|
|
|
|
foreach ($return_type_tokens as &$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 ($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
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-16 22:28:25 +02:00
|
|
|
public static function checkMethodExists($method_id, $file_name, $stmt)
|
|
|
|
{
|
|
|
|
if (isset(self::$_existing_methods[$method_id])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return;
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-04-18 19:31:59 +02:00
|
|
|
throw new UndefinedMethodException('Method ' . $method_id . ' does not exist', $file_name, $stmt->getLine());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _populateData($method_id)
|
|
|
|
{
|
|
|
|
if (!isset(self::$_have_registered[$method_id]) && !isset(self::$_have_registered[$method_id])) {
|
|
|
|
if (isset(self::$_inherited_methods[$method_id])) {
|
|
|
|
self::_copyToChildMethod(self::$_inherited_methods[$method_id], $method_id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::extractReflectionMethodInfo($method_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-18 19:31:59 +02:00
|
|
|
public static function checkMethodVisibility($method_id, $calling_context, $file_name, $line_number)
|
|
|
|
{
|
|
|
|
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])) {
|
|
|
|
throw new InaccessibleMethodException('Cannot access method ' . $method_id, $file_name, $line_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (self::$_method_visibility[$method_id]) {
|
|
|
|
case self::VISIBILITY_PUBLIC:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PRIVATE:
|
|
|
|
if (!$calling_context || $method_class !== $calling_context) {
|
2016-05-21 01:07:05 +02:00
|
|
|
throw new InaccessibleMethodException('Cannot access private method ' . $method_id . ' from context ' . $calling_context, $file_name, $line_number);
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PROTECTED:
|
|
|
|
if ($method_class === $calling_context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$calling_context) {
|
|
|
|
throw new InaccessibleMethodException('Cannot access protected method ' . $method_id, $file_name, $line_number);
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
throw new InaccessibleMethodException('Cannot access protected method ' . $method_id . ' from context ' . $calling_context, $file_name, $line_number);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
self::$_inherited_methods[$method_id] = $parent_method_id;
|
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public static function getDefiningParentMethod($method_id)
|
|
|
|
{
|
2016-05-21 01:07:05 +02:00
|
|
|
if (isset(self::$_implementing_methods[$method_id])) {
|
|
|
|
return self::$_implementing_methods[$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-21 01:07:05 +02:00
|
|
|
self::$_implementing_methods[$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-02-04 15:22:46 +01:00
|
|
|
}
|