2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
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-08-13 20:20:46 +02:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\StatementsSource;
|
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-08-13 20:20:46 +02:00
|
|
|
abstract class ClassLikeChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-14 18:06:53 +02:00
|
|
|
protected static $SPECIAL_TYPES = ['int', 'string', 'float', 'bool', 'false', 'object', 'empty', 'callable', 'array'];
|
2016-07-25 06:31:29 +02:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
protected $file_name;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-12 07:38:29 +02:00
|
|
|
* @var string|null
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
protected $include_file_name;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt\ClassLike
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $class;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $namespace;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $aliased_classes;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $absolute_class;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $has_custom_get = false;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StatementsSource
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $source;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* The parent class
|
|
|
|
*
|
2016-10-03 17:38:59 +02:00
|
|
|
* @var string|null
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $parent_class;
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $suppressed_issues;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-07-25 00:02:03 +02:00
|
|
|
/**
|
2016-10-05 01:23:38 +02:00
|
|
|
* @var array<string,MethodChecker>
|
2016-07-25 00:02:03 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $method_checkers = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $this_class = null;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* A lookup table of all methods on a given class
|
|
|
|
*
|
2016-10-12 07:38:29 +02:00
|
|
|
* @var array<string,array<string,bool>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $class_methods = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table of cached ClassLikeCheckers
|
|
|
|
*
|
2016-10-05 01:23:38 +02:00
|
|
|
* @var array<string,self>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $class_checkers = [];
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* A lookup table for public class properties
|
|
|
|
*
|
2016-10-15 06:12:57 +02:00
|
|
|
* @var array<string,array<string,Type\Union>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $public_class_properties = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table for protected class properties
|
|
|
|
*
|
2016-10-15 06:12:57 +02:00
|
|
|
* @var array<string,array<string,Type\Union>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $protected_class_properties = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table for protected class properties
|
|
|
|
*
|
2016-10-15 06:12:57 +02:00
|
|
|
* @var array<string,array<string,Type\Union>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $private_class_properties = [];
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* A lookup table for public static class properties
|
|
|
|
*
|
2016-10-15 06:12:57 +02:00
|
|
|
* @var array<string,array<string,Type\Union>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $public_static_class_properties = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table for protected static class properties
|
|
|
|
*
|
2016-10-15 06:12:57 +02:00
|
|
|
* @var array<string,array<string,Type\Union>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $protected_static_class_properties = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table for private static class properties
|
|
|
|
*
|
2016-10-15 06:12:57 +02:00
|
|
|
* @var array<string,array<string,Type\Union>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $private_static_class_properties = [];
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* A lookup table for public class constants
|
|
|
|
*
|
2016-10-12 07:38:29 +02:00
|
|
|
* @var array<string,array<string,Type\Union>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $public_class_constants = [];
|
2016-07-25 05:38:52 +02:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* A lookup table to record which classes have been scanned
|
|
|
|
*
|
2016-10-05 01:23:38 +02:00
|
|
|
* @var array<string,bool>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-14 18:06:53 +02:00
|
|
|
protected static $registered_classes = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table used for storing the results of ClassChecker::classImplements
|
|
|
|
*
|
2016-10-12 07:38:29 +02:00
|
|
|
* @var array<string,array<string,string>>
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2016-08-15 05:24:16 +02:00
|
|
|
protected static $class_implements = [];
|
2016-08-14 18:06:53 +02:00
|
|
|
|
2016-10-05 01:23:38 +02:00
|
|
|
/** @var array<string,string> */
|
|
|
|
protected static $class_files = [];
|
|
|
|
|
|
|
|
/** @var array<string,array<int,string>> */
|
|
|
|
protected static $file_classes = [];
|
|
|
|
|
2016-08-08 20:36:18 +02:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSource $source, $absolute_class)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->class = $class;
|
|
|
|
$this->namespace = $source->getNamespace();
|
|
|
|
$this->aliased_classes = $source->getAliasedClasses();
|
|
|
|
$this->file_name = $source->getFileName();
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->include_file_name = $source->getIncludeFileName();
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->absolute_class = $absolute_class;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-10-05 01:23:38 +02:00
|
|
|
self::$class_files[$absolute_class] = $this->file_name;
|
|
|
|
self::$file_classes[$this->file_name][] = $absolute_class;
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
if (self::$this_class) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$class_checkers[$absolute_class] = $this;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-08-08 20:36:18 +02:00
|
|
|
public function check($check_methods = true, Context $class_context = null)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-15 06:52:25 +02:00
|
|
|
if (!$check_methods && !($this instanceof TraitChecker) && isset(self::$registered_classes[$this->absolute_class])) {
|
2016-08-15 05:24:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
self::$registered_classes[$this->absolute_class] = true;
|
|
|
|
|
2016-01-26 20:13:04 +01:00
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
$method_checkers = [];
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$class_methods[$this->absolute_class] = [];
|
2016-04-18 19:31:59 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_properties[$this->absolute_class] = [];
|
|
|
|
self::$protected_class_properties[$this->absolute_class] = [];
|
|
|
|
self::$private_class_properties[$this->absolute_class] = [];
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_static_class_properties[$this->absolute_class] = [];
|
|
|
|
self::$protected_static_class_properties[$this->absolute_class] = [];
|
|
|
|
self::$private_static_class_properties[$this->absolute_class] = [];
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_constants[$this->absolute_class] = [];
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
if (!$class_context) {
|
|
|
|
$class_context = new Context($this->file_name, $this->absolute_class);
|
|
|
|
$class_context->parent = $this->parent_class;
|
2016-10-14 00:27:23 +02:00
|
|
|
$class_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic($this->absolute_class)]);
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set all constants first
|
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
|
|
|
foreach ($stmt->consts as $const) {
|
|
|
|
self::$public_class_constants[$class_context->self][$const->name] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 18:06:53 +02:00
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
if ($this instanceof ClassChecker) {
|
|
|
|
if ($this->parent_class) {
|
2016-08-15 05:24:16 +02:00
|
|
|
if (self::checkAbsoluteClassOrInterface(
|
|
|
|
$this->parent_class,
|
|
|
|
$this->file_name,
|
|
|
|
$this->class->getLine(),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
self::registerClass($this->parent_class);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
|
|
|
$this->registerInheritedMethods($this->parent_class);
|
|
|
|
|
2016-10-05 23:08:20 +02:00
|
|
|
FileChecker::addFileInheritanceToClass(Config::getInstance()->getBaseDir() . $this->file_name, $this->parent_class);
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
self::$class_implements[$this->absolute_class] += self::$class_implements[$this->parent_class];
|
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
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];
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
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-07 02:27:13 +02:00
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
self::$public_class_constants[$this->absolute_class] = array_merge(
|
|
|
|
self::$public_class_constants[$this->parent_class],
|
|
|
|
self::$public_class_constants[$this->absolute_class]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
foreach (self::$class_implements[$this->absolute_class] as $interface_id => $interface_name) {
|
2016-08-15 05:24:16 +02:00
|
|
|
if (self::checkAbsoluteClassOrInterface(
|
|
|
|
$interface_name,
|
|
|
|
$this->file_name,
|
|
|
|
$this->class->getLine(),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::registerClass($interface_name);
|
2016-10-05 23:08:20 +02:00
|
|
|
|
|
|
|
FileChecker::addFileInheritanceToClass(Config::getInstance()->getBaseDir() . $this->file_name, $interface_name);
|
2016-08-14 01:44:24 +02:00
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2016-08-15 18:22:23 +02:00
|
|
|
$trait_checkers = [];
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2016-08-15 19:37:21 +02:00
|
|
|
$method_id = $this->absolute_class . '::' . strtolower($stmt->name);
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
if (!isset(self::$method_checkers[$method_id])) {
|
2016-08-15 21:14:28 +02:00
|
|
|
$method_checker = new MethodChecker($stmt, $this);
|
2016-05-16 05:06:03 +02:00
|
|
|
$method_checkers[$stmt->name] = $method_checker;
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
if (self::$this_class && !$check_methods) {
|
|
|
|
self::$method_checkers[$method_id] = $method_checker;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-08-14 00:54:49 +02:00
|
|
|
$method_checker = self::$method_checkers[$method_id];
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 18:22:23 +02:00
|
|
|
if (!$stmt->isAbstract()) {
|
2016-10-15 06:12:57 +02:00
|
|
|
MethodChecker::setDeclaringMethodId($class_context->self . '::' . $this->getMappedMethodName(strtolower($stmt->name)), $method_id);
|
2016-08-15 20:20:06 +02:00
|
|
|
self::$class_methods[$class_context->self][strtolower($stmt->name)] = true;
|
2016-08-15 18:22:23 +02:00
|
|
|
}
|
2016-08-15 06:52:25 +02:00
|
|
|
|
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) {
|
2016-09-01 06:07:29 +02:00
|
|
|
if ($adaptation->method && $adaptation->newName) {
|
|
|
|
$method_map[strtolower($adaptation->method)] = strtolower($adaptation->newName);
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->traits as $trait) {
|
2016-08-14 00:54:49 +02:00
|
|
|
$trait_name = self::getAbsoluteClassFromName($trait, $this->namespace, $this->aliased_classes);
|
2016-08-08 20:36:18 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
if (!trait_exists($trait_name)) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-14 00:54:49 +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
|
|
|
}
|
2016-08-08 20:36:18 +02:00
|
|
|
else {
|
|
|
|
try {
|
|
|
|
$reflection_trait = new \ReflectionClass($trait_name);
|
|
|
|
}
|
|
|
|
catch (\ReflectionException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-08-14 00:54:49 +02:00
|
|
|
new UndefinedTrait('Trait ' . $trait_name . ' has wrong casing', $this->file_name, $trait->getLine()),
|
|
|
|
$this->suppressed_issues
|
2016-08-08 20:36:18 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
|
2016-08-08 20:36:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/** @var TraitChecker */
|
2016-08-13 20:20:46 +02:00
|
|
|
$trait_checker = FileChecker::getClassLikeCheckerFromClass($trait_name);
|
2016-08-08 20:36:18 +02:00
|
|
|
|
2016-08-15 20:20:06 +02:00
|
|
|
$trait_checker->setMethodMap($method_map);
|
|
|
|
|
2016-08-15 18:22:23 +02:00
|
|
|
$trait_checker->check(false, $class_context);
|
2016-08-15 20:20:06 +02:00
|
|
|
|
2016-10-05 23:08:20 +02:00
|
|
|
FileChecker::addFileInheritanceToClass(Config::getInstance()->getBaseDir() . $this->file_name, $this->parent_class);
|
|
|
|
|
2016-08-15 20:20:06 +02:00
|
|
|
$trait_checkers[] = $trait_checker;
|
2016-08-08 20:36:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
} else {
|
2016-01-30 00:48:09 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
2016-08-13 20:20:46 +02:00
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
$type_in_comment = null;
|
2016-08-11 01:21:03 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
if ($comment && $config->use_docblock_types && count($stmt->props) === 1) {
|
|
|
|
$type_in_comment = CommentChecker::getTypeFromComment((string) $comment, null, $this);
|
|
|
|
}
|
2016-06-27 16:46:27 +02:00
|
|
|
|
2016-09-12 17:32:44 +02:00
|
|
|
$property_group_type = $type_in_comment ? $type_in_comment : null;
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
foreach ($stmt->props as $property) {
|
2016-09-12 17:32:44 +02:00
|
|
|
if (!$property_group_type) {
|
|
|
|
if (!$property->default) {
|
|
|
|
$property_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$property_type = StatementsChecker::getSimpleType($property->default) ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$property_type = $property_group_type;
|
|
|
|
}
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
if ($stmt->isStatic()) {
|
|
|
|
if ($stmt->isPublic()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_static_class_properties[$class_context->self][$property->name] = $property_type;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($stmt->isProtected()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$protected_static_class_properties[$class_context->self][$property->name] = $property_type;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($stmt->isPrivate()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$private_static_class_properties[$class_context->self][$property->name] = $property_type;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($stmt->isPublic()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_properties[$class_context->self][$property->name] = $property_type;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($stmt->isProtected()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$protected_class_properties[$class_context->self][$property->name] = $property_type;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($stmt->isPrivate()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$private_class_properties[$class_context->self][$property->name] = $property_type;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
$type_in_comment = null;
|
|
|
|
|
|
|
|
if ($comment && $config->use_docblock_types && count($stmt->consts) === 1) {
|
|
|
|
$type_in_comment = CommentChecker::getTypeFromComment((string) $comment, null, $this);
|
|
|
|
}
|
|
|
|
|
|
|
|
$const_type = $type_in_comment ? $type_in_comment : Type::getMixed();
|
|
|
|
|
|
|
|
foreach ($stmt->consts as $const) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_constants[$class_context->self][$const->name] = $const_type;
|
2016-08-13 20:20:46 +02: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-08-14 00:54:49 +02:00
|
|
|
if (method_exists($this->absolute_class, '__get')) {
|
|
|
|
$this->has_custom_get = true;
|
2016-04-20 12:55:26 +02:00
|
|
|
}
|
|
|
|
|
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-09-12 17:32:44 +02:00
|
|
|
$all_instance_properties = array_merge(
|
|
|
|
self::$public_class_properties[$this->absolute_class],
|
|
|
|
self::$protected_class_properties[$this->absolute_class],
|
|
|
|
self::$private_class_properties[$this->absolute_class]
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($all_instance_properties as $property_name => $property_type) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$class_context->vars_in_scope['$this->' . $property_name] = $property_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$all_static_properties = array_merge(
|
|
|
|
self::$public_static_class_properties[$this->absolute_class],
|
|
|
|
self::$protected_static_class_properties[$this->absolute_class],
|
|
|
|
self::$private_static_class_properties[$this->absolute_class]
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($all_static_properties as $property_name => $property_type) {
|
|
|
|
$class_context->vars_in_scope[$this->absolute_class . '::$' . $property_name] = $property_type;
|
2016-09-12 17:32:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:09:04 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
if ($this instanceof ClassChecker) {
|
|
|
|
foreach (ClassChecker::getInterfacesForClass($this->absolute_class) as $interface_id => $interface_name) {
|
|
|
|
if (isset(self::$public_class_constants[$interface_name])) {
|
|
|
|
self::$public_class_constants[$this->absolute_class] += self::$public_class_constants[$interface_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (self::$class_methods[$interface_name] as $method_name => $_) {
|
|
|
|
$mentioned_method_id = $interface_name . '::' . $method_name;
|
|
|
|
$implemented_method_id = $this->absolute_class . '::' . $method_name;
|
|
|
|
MethodChecker::setOverriddenMethodId($implemented_method_id, $mentioned_method_id);
|
|
|
|
|
|
|
|
if (!isset(self::$class_methods[$this->absolute_class])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnimplementedInterfaceMethod(
|
|
|
|
'Method ' . $method_name . ' is not defined on class ' . $this->absolute_class,
|
|
|
|
$file_name,
|
|
|
|
$line_number
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
|
|
|
|
if ($check_methods) {
|
|
|
|
foreach ($trait_checkers as $trait_checker) {
|
|
|
|
$trait_checker->check(true, $class_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do the method checks after all class methods have been initialised
|
|
|
|
foreach ($method_checkers as $method_checker) {
|
|
|
|
$method_checker->check(clone $class_context);
|
|
|
|
|
|
|
|
if (!$config->excludeIssueInFile('InvalidReturnType', $this->file_name)) {
|
|
|
|
$method_checker->checkReturnTypes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 02:54:17 +02:00
|
|
|
|
|
|
|
if (!$this->class->name) {
|
|
|
|
$this->class->name = $this->absolute_class;
|
|
|
|
}
|
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
|
2016-08-15 21:14:28 +02:00
|
|
|
* @return MethodChecker
|
2016-05-16 05:06:03 +02:00
|
|
|
*/
|
|
|
|
public static function getMethodChecker($method_id)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
if (isset(self::$method_checkers[$method_id])) {
|
|
|
|
return self::$method_checkers[$method_id];
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 21:14:28 +02:00
|
|
|
MethodChecker::registerClassMethod($method_id);
|
2016-08-15 08:12:27 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($method_id);
|
2016-08-05 21:11:20 +02:00
|
|
|
$declaring_class = explode('::', $declaring_method_id)[0];
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
$class_checker = FileChecker::getClassLikeCheckerFromClass($declaring_class);
|
2016-08-05 21:11:20 +02:00
|
|
|
|
|
|
|
if (!$class_checker) {
|
|
|
|
throw new \InvalidArgumentException('Could not get class checker for ' . $declaring_class);
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
foreach ($class_checker->class->stmts as $stmt) {
|
2016-08-05 21:11:20 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2016-08-15 19:37:21 +02:00
|
|
|
if ($declaring_method_id === $class_checker->absolute_class . '::' . strtolower($stmt->name)) {
|
2016-08-15 21:14:28 +02:00
|
|
|
$method_checker = new MethodChecker($stmt, $class_checker);
|
2016-08-15 08:12:27 +02:00
|
|
|
self::$method_checkers[$method_id] = $method_checker;
|
|
|
|
return $method_checker;
|
|
|
|
}
|
2016-08-05 21:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
2016-09-09 15:13:41 +02:00
|
|
|
*
|
2016-05-16 05:06:03 +02:00
|
|
|
* @param string $class_name
|
2016-08-13 20:20:46 +02:00
|
|
|
* @return self|null
|
2016-05-16 05:06:03 +02:00
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public static function getClassLikeCheckerFromClass($class_name)
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
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-07-25 06:31:29 +02:00
|
|
|
/**
|
2016-09-09 15:13:41 +02:00
|
|
|
* Check whether a class/interface exists
|
|
|
|
*
|
2016-07-25 06:31:29 +02:00
|
|
|
* @param string $absolute_class
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classOrInterfaceExists($absolute_class)
|
|
|
|
{
|
2016-08-13 20:20:46 +02:00
|
|
|
return ClassChecker::classExists($absolute_class) || InterfaceChecker::interfaceExists($absolute_class);
|
2016-07-25 05:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @param string $possible_parent
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classExtendsOrImplements($absolute_class, $possible_parent)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return ClassChecker::classExtends($absolute_class, $possible_parent)
|
|
|
|
|| ClassChecker::classImplements($absolute_class, $possible_parent);
|
2016-07-25 05:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-08-14 00:54:49 +02:00
|
|
|
$class_exists = ClassChecker::classExists($absolute_class);
|
|
|
|
$interface_exists = InterfaceChecker::interfaceExists($absolute_class);
|
|
|
|
|
|
|
|
if (!$class_exists && !$interface_exists) {
|
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-14 00:54:49 +02:00
|
|
|
if (($class_exists && !ClassChecker::hasCorrectCasing($absolute_class))
|
|
|
|
|| ($interface_exists && !InterfaceChecker::hasCorrectCasing($absolute_class))
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidClass('Class or interface ' . $absolute_class . ' has wrong casing', $file_name, $line_number),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-08-10 07:09:47 +02:00
|
|
|
}
|
2016-03-17 19:06:01 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
FileChecker::addFileReferenceToClass(Config::getInstance()->getBaseDir() . $file_name, $absolute_class);
|
2016-10-05 01:23:38 +02:00
|
|
|
|
2016-07-24 23:06:36 +02:00
|
|
|
return true;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* Gets the fully-qualified classname from a Name object
|
|
|
|
*
|
|
|
|
* @param PhpParser\Node\Name $class_name
|
|
|
|
* @param string $namespace
|
|
|
|
* @param array<int,string> $aliased_classes
|
|
|
|
* @return string
|
|
|
|
*/
|
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-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param string $namespace
|
|
|
|
* @param array $imported_namespaces
|
|
|
|
* @return string
|
|
|
|
*/
|
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);
|
|
|
|
|
2016-09-10 05:17:56 +02:00
|
|
|
if (isset($imported_namespaces[strtolower($first_namespace)])) {
|
|
|
|
return $imported_namespaces[strtolower($first_namespace)] . '\\' . implode('\\', $class_parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-09-10 05:17:56 +02:00
|
|
|
} elseif (isset($imported_namespaces[strtolower($class)])) {
|
|
|
|
return $imported_namespaces[strtolower($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()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->namespace;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->aliased_classes;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAbsoluteClass()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->absolute_class;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->class->name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-04-17 17:22:18 +02:00
|
|
|
public function getParentClass()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->parent_class;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileName()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->file_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-25 01:00:44 +02:00
|
|
|
public function getIncludeFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string|null $file_name
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
public function setIncludeFileName($file_name)
|
|
|
|
{
|
|
|
|
$this->include_file_name = $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCheckedFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name ?: $this->file_name;
|
|
|
|
}
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
public function getClassLikeChecker()
|
2016-01-30 00:48:09 +01:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->has_custom_get;
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @return boolean
|
2016-10-14 06:53:43 +02:00
|
|
|
* @psalm-suppress MixedMethodCall due to Reflection class weirdness
|
2016-10-12 07:38:29 +02:00
|
|
|
*/
|
2016-08-15 05:24:16 +02:00
|
|
|
public static function registerClass($class_name)
|
2016-01-30 00:48:09 +01:00
|
|
|
{
|
2016-08-15 05:24:16 +02:00
|
|
|
if (isset(self::$registered_classes[$class_name])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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()) {
|
2016-10-14 06:53:43 +02:00
|
|
|
$class_file_name = (string)$reflected_class->getFileName();
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-05 01:23:38 +02:00
|
|
|
$file_checker = new FileChecker($class_file_name);
|
|
|
|
|
|
|
|
$short_file_name = $file_checker->getFileName();
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
self::$class_files[$class_name] = $class_file_name;
|
|
|
|
self::$file_classes[$class_file_name][] = $class_name;
|
2016-10-05 01:23:38 +02:00
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
// this doesn't work on traits
|
2016-10-05 01:23:38 +02:00
|
|
|
$file_checker->check(true, false);
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class_properties = $reflected_class->getProperties();
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_properties[$class_name] = [];
|
|
|
|
self::$protected_class_properties[$class_name] = [];
|
|
|
|
self::$private_class_properties[$class_name] = [];
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_static_class_properties[$class_name] = [];
|
|
|
|
self::$protected_static_class_properties[$class_name] = [];
|
|
|
|
self::$private_static_class_properties[$class_name] = [];
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/** @var \ReflectionProperty $class_property */
|
2016-08-07 02:27:13 +02:00
|
|
|
foreach ($class_properties as $class_property) {
|
|
|
|
if ($class_property->isStatic()) {
|
|
|
|
if ($class_property->isPublic()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_static_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($class_property->isProtected()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$protected_static_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($class_property->isPrivate()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$private_static_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($class_property->isPublic()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($class_property->isProtected()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$protected_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($class_property->isPrivate()) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$private_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
|
|
|
|
$class_constants = $reflected_class->getConstants();
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_constants[$class_name] = [];
|
2016-08-13 20:20:46 +02:00
|
|
|
|
|
|
|
foreach ($class_constants as $name => $value) {
|
|
|
|
switch (gettype($value)) {
|
|
|
|
case 'boolean':
|
|
|
|
$const_type = Type::getBool();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'integer':
|
|
|
|
$const_type = Type::getInt();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'double':
|
|
|
|
$const_type = Type::getFloat();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
$const_type = Type::getString();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'array':
|
|
|
|
$const_type = Type::getArray();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NULL':
|
|
|
|
$const_type = Type::getNull();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$const_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_constants[$class_name][$name] = $const_type;
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
2016-08-14 01:44:24 +02:00
|
|
|
|
2016-08-14 18:06:53 +02:00
|
|
|
self::$registered_classes[$class_name] = true;
|
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
if (!$reflected_class->isTrait() && !$reflected_class->isInterface()) {
|
|
|
|
ClassChecker::getInterfacesForClass($class_name);
|
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
|
|
|
|
$reflection_methods = $reflected_class->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED);
|
|
|
|
|
|
|
|
self::$class_methods[$class_name] = [];
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/** @var \ReflectionMethod $reflection_method */
|
2016-08-15 05:24:16 +02:00
|
|
|
foreach ($reflection_methods as $reflection_method) {
|
2016-08-15 21:14:28 +02:00
|
|
|
MethodChecker::extractReflectionMethodInfo($reflection_method);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
|
|
|
if ($reflection_method->class !== $class_name) {
|
2016-10-15 06:12:57 +02:00
|
|
|
MethodChecker::setDeclaringMethodId(
|
2016-10-14 06:53:43 +02:00
|
|
|
$class_name . '::' . strtolower((string)$reflection_method->name),
|
|
|
|
$reflection_method->class . '::' . strtolower((string)$reflection_method->name)
|
2016-08-15 05:24:16 +02:00
|
|
|
);
|
2016-08-15 06:31:34 +02:00
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
self::$class_methods[$class_name][strtolower((string)$reflection_method->name)] = true;
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$reflection_method->isAbstract() && $reflection_method->getDeclaringClass()->getName() === $class_name) {
|
2016-10-14 06:53:43 +02:00
|
|
|
self::$class_methods[$class_name][strtolower((string)$reflection_method->getName())] = true;
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
|
|
|
|
return true;
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 20:20:06 +02:00
|
|
|
protected function registerInheritedMethods($parent_class)
|
2016-08-15 06:31:34 +02:00
|
|
|
{
|
|
|
|
$class_methods = self::$class_methods[$parent_class];
|
|
|
|
|
|
|
|
foreach ($class_methods as $method_name => $_) {
|
2016-08-15 20:20:06 +02:00
|
|
|
$parent_method_id = $parent_class . '::' . $method_name;
|
2016-10-15 06:12:57 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($parent_method_id);
|
2016-08-15 20:20:06 +02:00
|
|
|
$implemented_method_id = $this->absolute_class . '::' . $method_name;
|
2016-08-15 06:31:34 +02:00
|
|
|
|
2016-08-15 20:20:06 +02:00
|
|
|
if (!isset(self::$class_methods[$this->absolute_class][$method_name])) {
|
2016-10-15 06:12:57 +02:00
|
|
|
MethodChecker::setDeclaringMethodId($implemented_method_id, $declaring_method_id);
|
2016-08-15 20:20:06 +02:00
|
|
|
self::$class_methods[$this->absolute_class][$method_name] = true;
|
2016-10-15 06:12:57 +02:00
|
|
|
MethodChecker::setOverriddenMethodId($implemented_method_id, $declaring_method_id);
|
2016-08-15 06:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param mixed $visibility
|
2016-10-15 06:12:57 +02:00
|
|
|
* @return array<string,Type\Union>
|
2016-10-12 07:38:29 +02:00
|
|
|
*/
|
2016-08-07 02:27:13 +02:00
|
|
|
public static function getInstancePropertiesForClass($class_name, $visibility)
|
|
|
|
{
|
2016-08-15 05:24:16 +02:00
|
|
|
if (self::registerClass($class_name) === false) {
|
|
|
|
return [];
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
2016-08-14 00:54:49 +02:00
|
|
|
return self::$public_class_properties[$class_name];
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
|
|
|
|
return array_merge(
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_properties[$class_name],
|
|
|
|
self::$protected_class_properties[$class_name]
|
2016-08-07 02:27:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PRIVATE) {
|
|
|
|
return array_merge(
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_properties[$class_name],
|
|
|
|
self::$protected_class_properties[$class_name],
|
|
|
|
self::$private_class_properties[$class_name]
|
2016-08-07 02:27:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException('Must specify $visibility');
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param mixed $visibility
|
2016-10-15 06:12:57 +02:00
|
|
|
* @return array<string,Type\Union>
|
2016-10-12 07:38:29 +02:00
|
|
|
*/
|
2016-08-07 02:27:13 +02:00
|
|
|
public static function getStaticPropertiesForClass($class_name, $visibility)
|
|
|
|
{
|
2016-08-15 05:24:16 +02:00
|
|
|
if (self::registerClass($class_name) === false) {
|
|
|
|
return [];
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
2016-08-14 00:54:49 +02:00
|
|
|
return self::$public_static_class_properties[$class_name];
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
|
|
|
|
return array_merge(
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_static_class_properties[$class_name],
|
|
|
|
self::$protected_static_class_properties[$class_name]
|
2016-08-07 02:27:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
elseif ($visibility === ReflectionProperty::IS_PRIVATE) {
|
|
|
|
return array_merge(
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_static_class_properties[$class_name],
|
|
|
|
self::$protected_static_class_properties[$class_name],
|
|
|
|
self::$private_static_class_properties[$class_name]
|
2016-08-07 02:27:13 +02:00
|
|
|
);
|
|
|
|
}
|
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-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param mixed $visibility
|
|
|
|
* @return array<string,Type\Union>
|
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public static function getConstantsForClass($class_name, $visibility)
|
|
|
|
{
|
|
|
|
// remove for PHP 7.1 support
|
|
|
|
$visibility = ReflectionProperty::IS_PUBLIC;
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
if (self::registerClass($class_name) === false) {
|
|
|
|
return [];
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
2016-08-14 00:54:49 +02:00
|
|
|
return self::$public_class_constants[$class_name];
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException('Given $visibility not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setConstantType($class_name, $const_name, Type\Union $type)
|
|
|
|
{
|
2016-08-14 03:14:32 +02:00
|
|
|
self::$public_class_constants[$class_name][$const_name] = $type;
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* Get a list of suppressed issues
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-07-22 19:29:46 +02:00
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->suppressed_issues;
|
2016-04-12 01:13:50 +02:00
|
|
|
}
|
2016-04-17 17:22:18 +02:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public static function setThisClass($this_class)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$this_class = $this_class;
|
2016-05-16 22:12:02 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$class_checkers = [];
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-05-16 05:06:03 +02:00
|
|
|
public static function getThisClass()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return self::$this_class;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-08-15 20:20:06 +02:00
|
|
|
protected function getMappedMethodName($method_name)
|
|
|
|
{
|
|
|
|
return $method_name;
|
|
|
|
}
|
|
|
|
|
2016-10-05 01:23:38 +02:00
|
|
|
public static function getClassesForFile($file_name)
|
|
|
|
{
|
2016-10-05 19:24:46 +02:00
|
|
|
return isset(self::$file_classes[$file_name]) ? array_unique(self::$file_classes[$file_name]) : [];
|
2016-10-05 01:23:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 00:02:03 +02:00
|
|
|
public static function clearCache()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$this_class = null;
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-10-21 00:12:13 +02:00
|
|
|
self::$method_checkers = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$class_methods = [];
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$class_checkers = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_class_properties = [];
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$protected_class_properties = [];
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$private_class_properties = [];
|
2016-08-10 07:09:47 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$public_static_class_properties = [];
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$protected_static_class_properties = [];
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$private_static_class_properties = [];
|
2016-08-10 07:09:47 +02:00
|
|
|
|
2016-10-21 00:12:13 +02:00
|
|
|
self::$public_class_constants = [];
|
|
|
|
|
|
|
|
self::$registered_classes = [];
|
|
|
|
|
|
|
|
self::$class_implements = [];
|
|
|
|
|
|
|
|
self::$class_files = [];
|
|
|
|
|
|
|
|
self::$file_classes = [];
|
|
|
|
|
2016-08-15 17:01:50 +02:00
|
|
|
ClassChecker::clearCache();
|
|
|
|
InterfaceChecker::clearCache();
|
2016-07-25 00:02:03 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|