1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/src/Psalm/Checker/ClassLikeChecker.php

941 lines
31 KiB
PHP
Raw Normal View History

2016-01-08 00:28:27 +01:00
<?php
namespace Psalm\Checker;
2016-01-08 00:28:27 +01:00
2016-07-26 00:37:44 +02:00
use Psalm\Issue\InvalidClass;
use Psalm\Issue\UndefinedClass;
use Psalm\Issue\UndefinedTrait;
use Psalm\IssueBuffer;
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;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
2016-08-05 21:11:20 +02:00
use ReflectionProperty;
2016-01-08 00:28:27 +01: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-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
/**
* @var string
*/
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
*/
protected $class;
2016-09-09 15:13:41 +02:00
/**
* @var string
*/
protected $namespace;
2016-09-09 15:13:41 +02:00
/**
* @var array<string>
*/
protected $aliased_classes;
2016-09-09 15:13:41 +02:00
/**
* @var string
*/
protected $absolute_class;
2016-09-09 15:13:41 +02:00
/**
* @var bool
*/
protected $has_custom_get = false;
2016-09-09 15:13:41 +02:00
/**
* @var StatementsSource
*/
protected $source;
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
*/
protected $parent_class;
/**
* @var array
*/
protected $suppressed_issues;
2016-07-25 00:02:03 +02:00
/**
* @var array<string,MethodChecker>
2016-07-25 00:02:03 +02:00
*/
protected static $method_checkers = [];
2016-07-25 00:02:03 +02:00
2016-09-09 15:13:41 +02:00
protected static $this_class = null;
2016-09-09 15:13:41 +02:00
/**
* A lookup table of all methods on a given class
*
* @var array<string,string>
2016-09-09 15:13:41 +02:00
*/
protected static $class_methods = [];
2016-09-09 15:13:41 +02:00
/**
* A lookup table of cached ClassLikeCheckers
*
* @var array<string,self>
2016-09-09 15:13:41 +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
*
* @var array<string,string>
2016-09-09 15:13:41 +02:00
*/
protected static $public_class_properties = [];
2016-09-09 15:13:41 +02:00
/**
* A lookup table for protected class properties
*
* @var array<string,string>
2016-09-09 15:13:41 +02:00
*/
protected static $protected_class_properties = [];
2016-09-09 15:13:41 +02:00
/**
* A lookup table for protected class properties
*
* @var array<string,string>
2016-09-09 15:13:41 +02:00
*/
protected static $private_class_properties = [];
2016-09-09 15:13:41 +02:00
/**
* A lookup table for public static class properties
*
* @var array<string,string>
2016-09-09 15:13:41 +02:00
*/
protected static $public_static_class_properties = [];
2016-09-09 15:13:41 +02:00
/**
* A lookup table for protected static class properties
*
* @var array<string,string>
2016-09-09 15:13:41 +02:00
*/
protected static $protected_static_class_properties = [];
2016-09-09 15:13:41 +02:00
/**
* A lookup table for private static class properties
*
* @var array<string,string>
2016-09-09 15:13:41 +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
*
* @var array<string,string>
2016-09-09 15:13:41 +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
*
* @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
*
* @var array<string,bool>
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
/** @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
{
$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();
$this->absolute_class = $absolute_class;
2016-01-08 00:28:27 +01:00
$this->suppressed_issues = $source->getSuppressedIssues();
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) {
self::$class_checkers[$absolute_class] = $this;
}
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 = [];
self::$class_methods[$this->absolute_class] = [];
2016-04-18 19:31:59 +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
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
self::$public_class_constants[$this->absolute_class] = [];
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;
$class_context->vars_in_scope['this'] = new Type\Union([new Type\Atomic($this->absolute_class)]);
}
// 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
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;
}
self::registerClass($this->parent_class);
$this->registerInheritedMethods($this->parent_class);
self::$class_implements[$this->absolute_class] += self::$class_implements[$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-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]
);
}
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);
}
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];
}
}
2016-08-07 02:27:13 +02:00
}
2016-08-05 21:11:20 +02:00
$trait_checkers = [];
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);
if (!isset(self::$method_checkers[$method_id])) {
$method_checker = new MethodChecker($stmt, $this);
$method_checkers[$stmt->name] = $method_checker;
if (self::$this_class && !$check_methods) {
self::$method_checkers[$method_id] = $method_checker;
}
}
else {
$method_checker = self::$method_checkers[$method_id];
}
if (!$stmt->isAbstract()) {
MethodChecker::setDeclaringMethod($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 06:52:25 +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);
}
}
}
foreach ($stmt->traits as $trait) {
$trait_name = self::getAbsoluteClassFromName($trait, $this->namespace, $this->aliased_classes);
2016-08-08 20:36:18 +02:00
if (!trait_exists($trait_name)) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
new UndefinedTrait('Trait ' . $trait_name . ' does not exist', $this->file_name, $trait->getLine()),
$this->suppressed_issues
)) {
return false;
}
}
2016-08-08 20:36:18 +02:00
else {
try {
$reflection_trait = new \ReflectionClass($trait_name);
}
catch (\ReflectionException $e) {
if (IssueBuffer::accepts(
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;
}
$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);
$trait_checker->check(false, $class_context);
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 {
if ($stmt instanceof PhpParser\Node\Stmt\Property) {
$comment = $stmt->getDocComment();
$type_in_comment = null;
2016-08-11 01:21:03 +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
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()) {
self::$public_static_class_properties[$class_context->self][$property->name] = $property_type;
2016-08-07 02:27:13 +02:00
}
elseif ($stmt->isProtected()) {
self::$protected_static_class_properties[$class_context->self][$property->name] = $property_type;
2016-08-07 02:27:13 +02:00
}
elseif ($stmt->isPrivate()) {
self::$private_static_class_properties[$class_context->self][$property->name] = $property_type;
2016-08-07 02:27:13 +02:00
}
}
else {
if ($stmt->isPublic()) {
self::$public_class_properties[$class_context->self][$property->name] = $property_type;
2016-08-07 02:27:13 +02:00
}
elseif ($stmt->isProtected()) {
self::$protected_class_properties[$class_context->self][$property->name] = $property_type;
2016-08-07 02:27:13 +02:00
}
elseif ($stmt->isPrivate()) {
self::$private_class_properties[$class_context->self][$property->name] = $property_type;
2016-08-07 02:27:13 +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) {
self::$public_class_constants[$class_context->self][$const->name] = $const_type;
}
}
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
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) {
(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) {
$class_context->vars_in_scope['this->' . $property_name] = $property_type;
}
$config = Config::getInstance();
2016-08-05 21:11:20 +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) {
2016-06-27 16:46:27 +02:00
$method_checker->check(clone $class_context);
if (!$config->excludeIssueInFile('InvalidReturnType', $this->file_name)) {
$method_checker->checkReturnTypes();
}
}
}
}
/**
* Used in deep method evaluation, we get method checkers on the current or parent
* classes
*
* @param string $method_id
* @return MethodChecker
*/
public static function getMethodChecker($method_id)
{
if (isset(self::$method_checkers[$method_id])) {
return self::$method_checkers[$method_id];
}
MethodChecker::registerClassMethod($method_id);
2016-08-15 08:12:27 +02:00
$declaring_method_id = MethodChecker::getDeclaringMethod($method_id);
2016-08-05 21:11:20 +02:00
$declaring_class = explode('::', $declaring_method_id)[0];
$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);
}
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)) {
$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-08-05 21:11:20 +02:00
throw new \InvalidArgumentException('Method checker not found');
}
/**
* Returns a class checker for the given class, if one has already been registered
2016-09-09 15:13:41 +02:00
*
* @param string $class_name
* @return self|null
*/
public static function getClassLikeCheckerFromClass($class_name)
{
if (isset(self::$class_checkers[$class_name])) {
return self::$class_checkers[$class_name];
2016-03-23 18:05:25 +01:00
}
return null;
2016-01-08 00:28:27 +01:00
}
/**
2016-09-09 15:13:41 +02:00
* Check whether a class/interface exists
*
* @param string $absolute_class
* @return bool
*/
public static function classOrInterfaceExists($absolute_class)
{
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)
{
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
*/
public static function checkAbsoluteClassOrInterface($absolute_class, $file_name, $line_number, array $suppressed_issues)
{
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);
$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(
new UndefinedClass('Class or interface ' . $absolute_class . ' does not exist', $file_name, $line_number),
$suppressed_issues
)) {
return false;
}
return;
2016-01-08 00:28:27 +01: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
}
FileChecker::addFileReferenceToClass(Config::getInstance()->getBaseDir() . $file_name, $absolute_class);
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) {
return implode('\\', $class_name->parts);
2016-01-08 00:28:27 +01:00
}
return self::getAbsoluteClassFromString(implode('\\', $class_name->parts), $namespace, $aliased_classes);
2016-01-08 00:28:27 +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] === '\\') {
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[strtolower($first_namespace)])) {
return $imported_namespaces[strtolower($first_namespace)] . '\\' . implode('\\', $class_parts);
2016-01-08 00:28:27 +01:00
}
} elseif (isset($imported_namespaces[strtolower($class)])) {
return $imported_namespaces[strtolower($class)];
2016-01-08 00:28:27 +01:00
}
return ($namespace ? $namespace . '\\' : '') . $class;
2016-01-08 00:28:27 +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;
}
public function getParentClass()
{
return $this->parent_class;
}
public function getFileName()
{
return $this->file_name;
}
2016-08-25 01:00:44 +02:00
public function getIncludeFileName()
{
return $this->include_file_name;
}
public function setIncludeFileName($file_name)
{
$this->include_file_name = $file_name;
}
public function getCheckedFileName()
{
return $this->include_file_name ?: $this->file_name;
}
public function getClassLikeChecker()
{
return $this;
}
2016-04-27 00:42:48 +02:00
/**
* @return bool
*/
public function isStatic()
{
return false;
}
public function hasCustomGet()
{
return $this->has_custom_get;
}
2016-08-15 05:24:16 +02:00
public static function registerClass($class_name)
{
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()) {
$class_file_name = $reflected_class->getFileName();
$file_checker = new FileChecker($class_file_name);
$short_file_name = $file_checker->getFileName();
self::$class_files[$class_name] = $class_file_name;
self::$file_classes[$class_file_name][] = $class_name;
2016-08-15 05:24:16 +02:00
// this doesn't work on traits
$file_checker->check(true, false);
2016-08-07 02:27:13 +02:00
}
else {
$class_properties = $reflected_class->getProperties();
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
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
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();
2016-08-07 02:27:13 +02:00
}
elseif ($class_property->isProtected()) {
self::$protected_static_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
2016-08-07 02:27:13 +02:00
}
elseif ($class_property->isPrivate()) {
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()) {
self::$public_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
2016-08-07 02:27:13 +02:00
}
elseif ($class_property->isProtected()) {
self::$protected_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
2016-08-07 02:27:13 +02:00
}
elseif ($class_property->isPrivate()) {
self::$private_class_properties[$class_name][$class_property->getName()] = Type::getMixed();
2016-08-07 02:27:13 +02:00
}
}
}
$class_constants = $reflected_class->getConstants();
self::$public_class_constants[$class_name] = [];
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();
}
self::$public_class_constants[$class_name][$name] = $const_type;
}
2016-08-14 18:06:53 +02:00
self::$registered_classes[$class_name] = true;
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] = [];
foreach ($reflection_methods as $reflection_method) {
MethodChecker::extractReflectionMethodInfo($reflection_method);
2016-08-15 05:24:16 +02:00
if ($reflection_method->class !== $class_name) {
MethodChecker::setDeclaringMethod(
2016-08-15 19:37:21 +02:00
$class_name . '::' . strtolower($reflection_method->name),
$reflection_method->class . '::' . strtolower($reflection_method->name)
2016-08-15 05:24:16 +02:00
);
2016-08-15 06:31:34 +02:00
2016-08-15 20:20:06 +02:00
self::$class_methods[$class_name][strtolower($reflection_method->name)] = true;
2016-08-15 05:24:16 +02:00
}
if (!$reflection_method->isAbstract() && $reflection_method->getDeclaringClass()->getName() === $class_name) {
2016-08-15 20:20:06 +02:00
self::$class_methods[$class_name][strtolower($reflection_method->getName())] = true;
2016-08-15 05:24:16 +02:00
}
}
}
2016-08-15 05:24:16 +02:00
return true;
}
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;
$declaring_method_id = MethodChecker::getDeclaringMethod($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])) {
MethodChecker::setDeclaringMethod($implemented_method_id, $declaring_method_id);
2016-08-15 20:20:06 +02:00
self::$class_methods[$this->absolute_class][$method_name] = true;
2016-08-15 06:31:34 +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) {
return self::$public_class_properties[$class_name];
2016-08-07 02:27:13 +02:00
}
elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
return array_merge(
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(
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');
}
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) {
return self::$public_static_class_properties[$class_name];
2016-08-07 02:27:13 +02:00
}
elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
return array_merge(
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(
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-04-12 01:13:50 +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 [];
}
if ($visibility === ReflectionProperty::IS_PUBLIC) {
return self::$public_class_constants[$class_name];
}
throw new \InvalidArgumentException('Given $visibility not supported');
}
public static function setConstantType($class_name, $const_name, Type\Union $type)
{
self::$public_class_constants[$class_name][$const_name] = $type;
}
public function getSource()
{
return null;
}
public function getSuppressedIssues()
{
return $this->suppressed_issues;
2016-04-12 01:13:50 +02:00
}
public static function setThisClass($this_class)
{
self::$this_class = $this_class;
2016-05-16 22:12:02 +02:00
self::$class_checkers = [];
}
public static function getThisClass()
{
return self::$this_class;
}
2016-07-25 00:02:03 +02:00
2016-08-15 20:20:06 +02:00
protected function getMappedMethodName($method_name)
{
return $method_name;
}
public static function getClassesForFile($file_name)
{
return isset(self::$file_classes[$file_name]) ? array_unique(self::$file_classes[$file_name]) : [];
}
2016-07-25 00:02:03 +02:00
public static function clearCache()
{
self::$method_checkers = [];
2016-07-25 00:02:03 +02:00
self::$this_class = null;
2016-07-25 00:02:03 +02:00
self::$class_implements = [];
2016-07-25 00:02:03 +02:00
self::$class_methods = [];
self::$class_checkers = [];
2016-07-25 00:02:03 +02:00
self::$public_class_properties = [];
self::$protected_class_properties = [];
self::$private_class_properties = [];
2016-08-10 07:09:47 +02:00
self::$public_static_class_properties = [];
self::$protected_static_class_properties = [];
self::$private_static_class_properties = [];
2016-08-10 07:09:47 +02:00
self::$class_references = [];
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
}