2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\InvalidClass;
|
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedTrait;
|
|
|
|
use Psalm\IssueBuffer;
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\Error;
|
|
|
|
use PhpParser\ParserFactory;
|
2016-04-17 17:22:18 +02:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionException;
|
|
|
|
use ReflectionMethod;
|
2016-08-05 21:11:20 +02:00
|
|
|
use ReflectionProperty;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
class ClassChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-07-25 15:02:30 +02:00
|
|
|
protected static $SPECIAL_TYPES = ['int', 'string', 'double', 'float', 'bool', 'false', 'object', 'empty', 'callable', 'array'];
|
2016-07-25 06:31:29 +02:00
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
protected $_file_name;
|
|
|
|
protected $_class;
|
|
|
|
protected $_namespace;
|
|
|
|
protected $_aliased_classes;
|
2016-01-20 00:27:06 +01:00
|
|
|
protected $_absolute_class;
|
2016-01-30 00:48:09 +01:00
|
|
|
protected $_has_custom_get = false;
|
2016-05-16 05:06:03 +02:00
|
|
|
protected $_source;
|
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
/** @var string|null */
|
|
|
|
protected $_parent_class;
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_suppressed_issues;
|
|
|
|
|
2016-07-25 00:02:03 +02:00
|
|
|
/**
|
|
|
|
* @var array<ClassMethodChecker>
|
|
|
|
*/
|
|
|
|
protected static $_method_checkers = [];
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
protected static $_this_class = null;
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
protected static $_existing_classes = [];
|
2016-07-25 15:02:30 +02:00
|
|
|
protected static $_existing_classes_ci = [];
|
2016-08-07 17:35:27 +02:00
|
|
|
protected static $_existing_interfaces_ci = [];
|
2016-07-25 05:38:52 +02:00
|
|
|
protected static $_class_implements = [];
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
protected static $_class_methods = [];
|
2016-05-16 05:06:03 +02:00
|
|
|
protected static $_class_checkers = [];
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
protected static $_public_class_properties = [];
|
|
|
|
protected static $_protected_class_properties = [];
|
|
|
|
protected static $_private_class_properties = [];
|
|
|
|
|
|
|
|
protected static $_public_static_class_properties = [];
|
|
|
|
protected static $_protected_static_class_properties = [];
|
|
|
|
protected static $_private_static_class_properties = [];
|
2016-07-23 16:58:53 +02:00
|
|
|
|
2016-07-25 05:38:52 +02:00
|
|
|
protected static $_class_extends = [];
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function __construct(PhpParser\Node\Stmt\Class_ $class, StatementsSource $source, $absolute_class)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
$this->_class = $class;
|
2016-01-20 00:27:06 +01:00
|
|
|
$this->_namespace = $source->getNamespace();
|
|
|
|
$this->_aliased_classes = $source->getAliasedClasses();
|
|
|
|
$this->_file_name = $source->getFileName();
|
|
|
|
$this->_absolute_class = $absolute_class;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
$this->_suppressed_issues = $source->getSuppressedIssues();
|
|
|
|
|
2016-08-07 18:23:29 +02:00
|
|
|
$this->_parent_class = $this->_class->extends
|
|
|
|
? ClassChecker::getAbsoluteClassFromName($this->_class->extends, $this->_namespace, $this->_aliased_classes)
|
|
|
|
: null;
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-07-24 23:06:36 +02:00
|
|
|
self::$_existing_classes[$absolute_class] = true;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
|
|
|
if (self::$_this_class) {
|
|
|
|
self::$_class_checkers[$absolute_class] = $this;
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
public function check($check_methods = true, $method_id = null)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-04-17 17:22:18 +02:00
|
|
|
if ($this->_parent_class) {
|
2016-07-26 20:58:19 +02:00
|
|
|
if (self::checkAbsoluteClassOrInterface(
|
|
|
|
$this->_parent_class,
|
|
|
|
$this->_file_name,
|
|
|
|
$this->_class->getLine(),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
self::_registerClassProperties($this->_parent_class);
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
$this->_registerInheritedMethods($this->_parent_class);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-01-26 20:13:04 +01:00
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
$method_checkers = [];
|
|
|
|
|
2016-04-18 19:31:59 +02:00
|
|
|
self::$_class_methods[$this->_absolute_class] = [];
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
self::$_public_class_properties[$this->_absolute_class] = [];
|
|
|
|
self::$_protected_class_properties[$this->_absolute_class] = [];
|
|
|
|
self::$_private_class_properties[$this->_absolute_class] = [];
|
|
|
|
|
|
|
|
self::$_public_static_class_properties[$this->_absolute_class] = [];
|
|
|
|
self::$_protected_static_class_properties[$this->_absolute_class] = [];
|
|
|
|
self::$_private_static_class_properties[$this->_absolute_class] = [];
|
|
|
|
|
|
|
|
if ($this->_parent_class) {
|
|
|
|
self::$_public_class_properties[$this->_absolute_class] = self::$_public_class_properties[$this->_parent_class];
|
|
|
|
self::$_protected_class_properties[$this->_absolute_class] = self::$_protected_class_properties[$this->_parent_class];
|
|
|
|
|
|
|
|
self::$_public_static_class_properties[$this->_absolute_class] = self::$_public_static_class_properties[$this->_parent_class];
|
|
|
|
self::$_protected_static_class_properties[$this->_absolute_class] = self::$_protected_static_class_properties[$this->_parent_class];
|
|
|
|
}
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2016-06-27 16:46:27 +02:00
|
|
|
$class_context = new Context();
|
|
|
|
|
2016-08-08 17:28:14 +02:00
|
|
|
$class_context->self = $this->_absolute_class;
|
|
|
|
$class_context->parent = $this->_parent_class;
|
|
|
|
$class_context->vars_in_scope['this'] = new Type\Union([new Type\Atomic($this->_absolute_class)]);
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
foreach ($this->_class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2016-05-16 05:06:03 +02:00
|
|
|
$method_id = $this->_absolute_class . '::' . $stmt->name;
|
|
|
|
|
|
|
|
if (!isset(self::$_method_checkers[$method_id])) {
|
|
|
|
$method_checker = new ClassMethodChecker($stmt, $this);
|
|
|
|
$method_checkers[$stmt->name] = $method_checker;
|
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
if (self::$_this_class && !$check_methods) {
|
2016-05-16 05:06:03 +02:00
|
|
|
self::$_method_checkers[$method_id] = $method_checker;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$method_checker = self::$_method_checkers[$method_id];
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:31:59 +02:00
|
|
|
self::$_class_methods[$this->_absolute_class][] = $stmt->name;
|
2016-05-16 05:06:03 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
$method_map = [];
|
|
|
|
foreach ($stmt->adaptations as $adaptation) {
|
|
|
|
if ($adaptation instanceof PhpParser\Node\Stmt\TraitUseAdaptation\Alias) {
|
|
|
|
$method_map[$adaptation->method] = $adaptation->newName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->traits as $trait) {
|
|
|
|
$trait_name = self::getAbsoluteClassFromName($trait, $this->_namespace, $this->_aliased_classes);
|
|
|
|
if (!trait_exists($trait_name)) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new UndefinedTrait('Trait ' . $trait_name . ' does not exist', $this->_file_name, $trait->getLine()),
|
|
|
|
$this->_suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
$this->_registerInheritedMethods($trait_name, $method_map);
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
|
|
|
|
} else {
|
2016-01-30 00:48:09 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
|
|
|
foreach ($stmt->props as $property) {
|
2016-06-24 00:45:46 +02:00
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
$type_in_comment = null;
|
2016-06-25 00:18:11 +02:00
|
|
|
if ($comment && $config->use_docblock_types) {
|
2016-07-25 21:05:58 +02:00
|
|
|
$type_in_comment = CommentChecker::getTypeFromComment((string) $comment, null, $this);
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
2016-06-27 16:46:27 +02:00
|
|
|
|
|
|
|
$property_type = $type_in_comment ? Type::parseString($type_in_comment) : Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
|
|
|
|
if ($stmt->isStatic()) {
|
|
|
|
if ($stmt->isPublic()) {
|
|
|
|
self::$_public_static_class_properties[$this->_absolute_class][$property->name] = $property_type;
|
|
|
|
}
|
|
|
|
elseif ($stmt->isProtected()) {
|
|
|
|
self::$_protected_static_class_properties[$this->_absolute_class][$property->name] = $property_type;
|
|
|
|
}
|
|
|
|
elseif ($stmt->isPrivate()) {
|
|
|
|
self::$_private_static_class_properties[$this->_absolute_class][$property->name] = $property_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($stmt->isPublic()) {
|
|
|
|
self::$_public_class_properties[$this->_absolute_class][$property->name] = $property_type;
|
|
|
|
}
|
|
|
|
elseif ($stmt->isProtected()) {
|
|
|
|
self::$_protected_class_properties[$this->_absolute_class][$property->name] = $property_type;
|
|
|
|
}
|
|
|
|
elseif ($stmt->isPrivate()) {
|
|
|
|
self::$_private_class_properties[$this->_absolute_class][$property->name] = $property_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 16:46:27 +02:00
|
|
|
if (!$stmt->isStatic()) {
|
|
|
|
$class_context->vars_in_scope['this->' . $property->name] = $property_type;
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
$leftover_stmts[] = $stmt;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
|
2016-04-20 12:55:26 +02:00
|
|
|
if (method_exists($this->_absolute_class, '__get')) {
|
|
|
|
$this->_has_custom_get = true;
|
|
|
|
}
|
|
|
|
|
2016-01-26 20:13:04 +01:00
|
|
|
if ($leftover_stmts) {
|
2016-08-08 17:28:14 +02:00
|
|
|
(new StatementsChecker($this))->check($leftover_stmts, $class_context);
|
2016-01-26 20:13:04 +01:00
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-06-21 00:09:04 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
if ($check_methods) {
|
2016-05-16 05:06:03 +02:00
|
|
|
// do the method checks after all class methods have been initialised
|
|
|
|
foreach ($method_checkers as $method_checker) {
|
2016-06-27 16:46:27 +02:00
|
|
|
$method_checker->check(clone $class_context);
|
2016-06-21 00:09:04 +02:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
if (!$config->excludeIssueInFile('InvalidReturnType', $this->_file_name)) {
|
2016-06-21 00:09:04 +02:00
|
|
|
$method_checker->checkReturnTypes();
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used in deep method evaluation, we get method checkers on the current or parent
|
|
|
|
* classes
|
|
|
|
*
|
|
|
|
* @param string $method_id
|
|
|
|
* @return ClassMethodChecker
|
|
|
|
*/
|
|
|
|
public static function getMethodChecker($method_id)
|
|
|
|
{
|
|
|
|
if (isset(self::$_method_checkers[$method_id])) {
|
|
|
|
return self::$_method_checkers[$method_id];
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
$declaring_method_id = ClassMethodChecker::getDeclaringMethod($method_id);
|
|
|
|
$declaring_class = explode('::', $declaring_method_id)[0];
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
$class_checker = FileChecker::getClassCheckerFromClass($declaring_class);
|
|
|
|
|
|
|
|
if (!$class_checker) {
|
|
|
|
throw new \InvalidArgumentException('Could not get class checker for ' . $declaring_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class_checker->_class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
|
|
|
$method_checker = new ClassMethodChecker($stmt, $class_checker);
|
|
|
|
$method_id = $class_checker->_absolute_class . '::' . $stmt->name;
|
|
|
|
self::$_method_checkers[$method_id] = $method_checker;
|
|
|
|
return $method_checker;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
throw new \InvalidArgumentException('Method checker not found');
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a class checker for the given class, if one has already been registered
|
|
|
|
* @param string $class_name
|
|
|
|
* @return ClassChecker|null
|
|
|
|
*/
|
|
|
|
public static function getClassCheckerFromClass($class_name)
|
|
|
|
{
|
|
|
|
if (isset(self::$_class_checkers[$class_name])) {
|
|
|
|
return self::$_class_checkers[$class_name];
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
|
|
|
return null;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-07-25 21:05:58 +02:00
|
|
|
* @return bool|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-07-24 23:06:36 +02:00
|
|
|
public static function checkClassName(PhpParser\Node\Name $class_name, $namespace, array $aliased_classes, $file_name, array $suppressed_issues)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
if ($class_name->parts[0] === 'static') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$absolute_class = self::getAbsoluteClassFromName($class_name, $namespace, $aliased_classes);
|
|
|
|
|
2016-07-25 06:31:29 +02:00
|
|
|
return self::checkAbsoluteClassOrInterface($absolute_class, $file_name, $class_name->getLine(), $suppressed_issues);
|
2016-02-18 21:05:13 +01:00
|
|
|
}
|
|
|
|
|
2016-07-25 05:38:52 +02:00
|
|
|
public static function classExists($absolute_class)
|
|
|
|
{
|
2016-08-07 17:35:27 +02:00
|
|
|
if (isset(self::$_existing_classes_ci[strtolower($absolute_class)])) {
|
2016-07-25 05:38:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:31:29 +02:00
|
|
|
if (in_array($absolute_class, self::$SPECIAL_TYPES)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (class_exists($absolute_class, true)) {
|
2016-08-07 17:35:27 +02:00
|
|
|
self::$_existing_classes_ci[strtolower($absolute_class)] = true;
|
2016-07-25 05:38:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:31:29 +02:00
|
|
|
public static function interfaceExists($absolute_class)
|
|
|
|
{
|
2016-08-07 17:35:27 +02:00
|
|
|
if (isset(self::$_existing_interfaces_ci[strtolower($absolute_class)])) {
|
2016-07-25 06:31:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interface_exists($absolute_class, true)) {
|
2016-08-07 17:35:27 +02:00
|
|
|
self::$_existing_interfaces_ci[strtolower($absolute_class)] = true;
|
2016-07-25 06:31:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classOrInterfaceExists($absolute_class)
|
|
|
|
{
|
|
|
|
return self::classExists($absolute_class) || self::interfaceExists($absolute_class);
|
|
|
|
}
|
|
|
|
|
2016-07-25 05:38:52 +02:00
|
|
|
/**
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @param string $possible_parent
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classExtends($absolute_class, $possible_parent)
|
|
|
|
{
|
|
|
|
if (isset(self::$_class_extends[$absolute_class][$possible_parent])) {
|
|
|
|
return self::$_class_extends[$absolute_class][$possible_parent];
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:31:29 +02:00
|
|
|
if (!self::classExists($absolute_class) || !self::classExists($possible_parent)) {
|
2016-07-25 06:12:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-25 05:38:52 +02:00
|
|
|
if (!isset(self::$_class_extends[$absolute_class])) {
|
|
|
|
self::$_class_extends[$absolute_class] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$_class_extends[$absolute_class][$possible_parent] = is_subclass_of($absolute_class, $possible_parent);
|
|
|
|
|
|
|
|
return self::$_class_extends[$absolute_class][$possible_parent];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @param string $possible_parent
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classExtendsOrImplements($absolute_class, $possible_parent)
|
|
|
|
{
|
|
|
|
return self::classExtends($absolute_class, $possible_parent) || self::classImplements($absolute_class, $possible_parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @param string $file_name
|
|
|
|
* @param int $line_number
|
|
|
|
* @param array<string> $suppressed_issues
|
|
|
|
* @return bool|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-07-25 06:31:29 +02:00
|
|
|
public static function checkAbsoluteClassOrInterface($absolute_class, $file_name, $line_number, array $suppressed_issues)
|
2016-02-18 21:05:13 +01:00
|
|
|
{
|
2016-04-12 17:59:27 +02:00
|
|
|
if (empty($absolute_class)) {
|
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:06:01 +01:00
|
|
|
$absolute_class = preg_replace('/^\\\/', '', $absolute_class);
|
|
|
|
|
2016-07-25 06:31:29 +02:00
|
|
|
if (!self::classOrInterfaceExists($absolute_class)) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-25 06:31:29 +02:00
|
|
|
new UndefinedClass('Class or interface ' . $absolute_class . ' does not exist', $file_name, $line_number),
|
2016-07-24 23:06:36 +02:00
|
|
|
$suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 23:06:36 +02:00
|
|
|
|
|
|
|
return;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-08-07 17:35:27 +02:00
|
|
|
if (isset(self::$_existing_classes_ci[strtolower($absolute_class)])) {
|
2016-04-17 17:22:18 +02:00
|
|
|
$reflection_class = new ReflectionClass($absolute_class);
|
2016-03-17 19:06:01 +01:00
|
|
|
|
|
|
|
if ($reflection_class->getName() !== $absolute_class) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-25 06:31:29 +02:00
|
|
|
new InvalidClass('Class or interface ' . $absolute_class . ' has wrong casing', $file_name, $line_number),
|
2016-07-24 23:06:36 +02:00
|
|
|
$suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-17 19:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 23:06:36 +02:00
|
|
|
return true;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAbsoluteClassFromName(PhpParser\Node\Name $class_name, $namespace, array $aliased_classes)
|
|
|
|
{
|
|
|
|
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return implode('\\', $class_name->parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
return self::getAbsoluteClassFromString(implode('\\', $class_name->parts), $namespace, $aliased_classes);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public static function getAbsoluteClassFromString($class, $namespace, array $imported_namespaces)
|
|
|
|
{
|
2016-04-12 17:59:27 +02:00
|
|
|
if (empty($class)) {
|
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($class[0] === '\\') {
|
2016-02-18 21:05:13 +01:00
|
|
|
return substr($class, 1);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($class, '\\') !== false) {
|
|
|
|
$class_parts = explode('\\', $class);
|
|
|
|
$first_namespace = array_shift($class_parts);
|
|
|
|
|
|
|
|
if (isset($imported_namespaces[$first_namespace])) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return $imported_namespaces[$first_namespace] . '\\' . implode('\\', $class_parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif (isset($imported_namespaces[$class])) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return $imported_namespaces[$class];
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 01:11:11 +01:00
|
|
|
return ($namespace ? $namespace . '\\' : '') . $class;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return $this->_namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses()
|
|
|
|
{
|
|
|
|
return $this->_aliased_classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAbsoluteClass()
|
|
|
|
{
|
|
|
|
return $this->_absolute_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->_class->name;
|
|
|
|
}
|
|
|
|
|
2016-04-17 17:22:18 +02:00
|
|
|
public function getParentClass()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-04-17 17:22:18 +02:00
|
|
|
return $this->_parent_class;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileName()
|
|
|
|
{
|
|
|
|
return $this->_file_name;
|
|
|
|
}
|
|
|
|
|
2016-01-30 00:48:09 +01:00
|
|
|
public function getClassChecker()
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
|
|
|
public function hasCustomGet()
|
|
|
|
{
|
|
|
|
return $this->_has_custom_get;
|
|
|
|
}
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
protected static function _registerClassProperties($class_name)
|
2016-01-30 00:48:09 +01:00
|
|
|
{
|
2016-08-07 17:35:27 +02:00
|
|
|
try {
|
|
|
|
$reflected_class = new ReflectionClass($class_name);
|
|
|
|
}
|
|
|
|
catch (\ReflectionException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
|
|
|
if ($reflected_class->isUserDefined()) {
|
|
|
|
$class_file_name = $reflected_class->getFileName();
|
|
|
|
|
|
|
|
(new FileChecker($class_file_name))->check(true, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class_properties = $reflected_class->getProperties();
|
|
|
|
|
|
|
|
self::$_public_class_properties[$class_name] = [];
|
|
|
|
self::$_protected_class_properties[$class_name] = [];
|
|
|
|
self::$_private_class_properties[$class_name] = [];
|
|
|
|
|
|
|
|
self::$_public_static_class_properties[$class_name] = [];
|
|
|
|
self::$_protected_static_class_properties[$class_name] = [];
|
|
|
|
self::$_private_static_class_properties[$class_name] = [];
|
|
|
|
|
|
|
|
foreach ($class_properties as $class_property) {
|
|
|
|
if ($class_property->isStatic()) {
|
|
|
|
if ($class_property->isPublic()) {
|
|
|
|
self::$_public_static_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
|
|
|
}
|
|
|
|
elseif ($class_property->isProtected()) {
|
|
|
|
self::$_protected_static_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
|
|
|
}
|
|
|
|
elseif ($class_property->isPrivate()) {
|
|
|
|
self::$_private_static_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($class_property->isPublic()) {
|
|
|
|
self::$_public_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
|
|
|
}
|
|
|
|
elseif ($class_property->isProtected()) {
|
|
|
|
self::$_protected_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
|
|
|
}
|
|
|
|
elseif ($class_property->isPrivate()) {
|
|
|
|
self::$_private_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getInstancePropertiesForClass($class_name, $visibility)
|
|
|
|
{
|
|
|
|
if (!isset(self::$_public_class_properties[$class_name])) {
|
2016-08-07 17:35:27 +02:00
|
|
|
if (self::_registerClassProperties($class_name) === false) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
|
|
|
return self::$_public_class_properties[$class_name];
|
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
|
|
|
|
return array_merge(
|
|
|
|
self::$_public_class_properties[$class_name],
|
|
|
|
self::$_protected_class_properties[$class_name]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PRIVATE) {
|
|
|
|
return array_merge(
|
|
|
|
self::$_public_class_properties[$class_name],
|
|
|
|
self::$_protected_class_properties[$class_name],
|
|
|
|
self::$_private_class_properties[$class_name]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException('Must specify $visibility');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getStaticPropertiesForClass($class_name, $visibility)
|
|
|
|
{
|
|
|
|
if (!isset(self::$_public_static_class_properties[$class_name])) {
|
|
|
|
self::_registerClassProperties($class_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
|
|
|
return self::$_public_static_class_properties[$class_name];
|
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
|
|
|
|
return array_merge(
|
|
|
|
self::$_public_static_class_properties[$class_name],
|
|
|
|
self::$_protected_static_class_properties[$class_name]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PRIVATE) {
|
|
|
|
return array_merge(
|
|
|
|
self::$_public_static_class_properties[$class_name],
|
|
|
|
self::$_protected_static_class_properties[$class_name],
|
|
|
|
self::$_private_static_class_properties[$class_name]
|
|
|
|
);
|
|
|
|
}
|
2016-07-23 16:58:53 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
throw new \InvalidArgumentException('Must specify $visibility');
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
2016-04-12 01:13:50 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
|
|
|
return $this->_suppressed_issues;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-06-15 01:22:29 +02:00
|
|
|
public static function classImplements($absolute_class, $interface)
|
2016-04-12 01:13:50 +02:00
|
|
|
{
|
2016-07-25 05:38:52 +02:00
|
|
|
if (isset(self::$_class_implements[$absolute_class][$interface])) {
|
2016-04-12 01:13:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-25 05:38:52 +02:00
|
|
|
if (isset(self::$_class_implements[$absolute_class])) {
|
2016-04-12 01:13:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-25 15:23:38 +02:00
|
|
|
if (!ClassChecker::classExists($absolute_class)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($interface, self::$SPECIAL_TYPES)) {
|
2016-07-25 06:12:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-12 01:13:50 +02:00
|
|
|
$class_implementations = class_implements($absolute_class);
|
|
|
|
|
|
|
|
if (!isset($class_implementations[$interface])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-25 05:38:52 +02:00
|
|
|
self::$_class_implements[$absolute_class] = $class_implementations;
|
2016-04-12 01:13:50 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
protected function _registerInheritedMethods($parent_class, array $method_map = null)
|
2016-04-17 17:22:18 +02:00
|
|
|
{
|
2016-05-16 05:06:03 +02:00
|
|
|
if (!isset(self::$_class_methods[$parent_class])) {
|
2016-04-17 17:22:18 +02:00
|
|
|
$class_methods = [];
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
$reflection_class = new ReflectionClass($parent_class);
|
2016-04-17 17:22:18 +02:00
|
|
|
|
|
|
|
$reflection_methods = $reflection_class->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED);
|
|
|
|
|
|
|
|
foreach ($reflection_methods as $reflection_method) {
|
2016-06-03 01:21:35 +02:00
|
|
|
if (!$reflection_method->isAbstract() && $reflection_method->getDeclaringClass()->getName() === $parent_class) {
|
2016-05-17 00:10:59 +02:00
|
|
|
$method_name = $reflection_method->getName();
|
|
|
|
$class_methods[] = $method_name;
|
|
|
|
}
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
2016-04-17 18:27:47 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
self::$_class_methods[$parent_class] = $class_methods;
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-05-16 05:06:03 +02:00
|
|
|
$class_methods = self::$_class_methods[$parent_class];
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class_methods as $method_name) {
|
2016-05-22 18:14:48 +02:00
|
|
|
$parent_method_id = $parent_class . '::' . $method_name;
|
|
|
|
$implemented_method_id = $this->_absolute_class . '::' . (isset($method_map[$method_name]) ? $method_map[$method_name] : $method_name);
|
|
|
|
|
|
|
|
ClassMethodChecker::registerInheritedMethod($parent_method_id, $implemented_method_id);
|
2016-04-17 17:22:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
|
|
|
public static function setThisClass($this_class)
|
|
|
|
{
|
|
|
|
self::$_this_class = $this_class;
|
2016-05-16 22:12:02 +02:00
|
|
|
|
|
|
|
self::$_class_checkers = [];
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getThisClass()
|
|
|
|
{
|
|
|
|
return self::$_this_class;
|
|
|
|
}
|
2016-07-25 00:02:03 +02:00
|
|
|
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$_method_checkers = [];
|
|
|
|
|
|
|
|
self::$_this_class = null;
|
|
|
|
|
|
|
|
self::$_existing_classes = [];
|
2016-07-25 05:38:52 +02:00
|
|
|
self::$_class_implements = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
|
|
|
|
self::$_class_methods = [];
|
|
|
|
self::$_class_checkers = [];
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
self::$_public_class_properties = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|