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

1511 lines
49 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-11-02 07:29:00 +01:00
use PhpParser;
use Psalm\CodeLocation;
2016-11-02 07:29:00 +01:00
use Psalm\Config;
use Psalm\Context;
2016-11-21 05:31:10 +01:00
use Psalm\Exception\DocblockParseException;
2016-07-26 00:37:44 +02:00
use Psalm\Issue\InvalidClass;
2016-11-21 05:31:10 +01:00
use Psalm\Issue\InvalidDocblock;
2016-10-31 20:17:54 +01:00
use Psalm\Issue\MissingPropertyType;
2016-07-26 00:37:44 +02:00
use Psalm\Issue\UndefinedClass;
use Psalm\Issue\UndefinedTrait;
2016-10-30 16:14:36 +01:00
use Psalm\Issue\UnimplementedInterfaceMethod;
2016-07-26 00:37:44 +02:00
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
2016-11-02 07:29:00 +01:00
use Psalm\Type;
use ReflectionClass;
use ReflectionMethod;
2016-08-05 21:11:20 +02:00
use ReflectionProperty;
2016-01-08 00:28:27 +01:00
2016-11-21 03:49:06 +01:00
abstract class ClassLikeChecker extends SourceChecker implements StatementsSource
2016-01-08 00:28:27 +01:00
{
2016-10-31 20:42:20 +01:00
/**
* @var array
*/
2016-11-02 07:29:00 +01:00
protected static $SPECIAL_TYPES = [
'int',
'string',
'float',
'bool',
'false',
'object',
'empty',
'callable',
'array',
'iterable'
2016-11-02 07:29:00 +01:00
];
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
2016-11-21 04:02:26 +01:00
/**
* @var StatementsSource
*/
protected $source;
2016-09-09 15:13:41 +02:00
/**
* @var string
*/
protected $fq_class_name;
2016-09-09 15:13:41 +02:00
/**
* @var bool
*/
protected $has_custom_get = false;
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
/**
* @var string|null
*/
protected static $this_class = null;
/**
* A lookup table of all public methods on a given class
*
* @var array<string,array<string,bool>>
*/
protected static $public_class_methods = [];
/**
* A lookup table of all protected methods on a given class
*
* @var array<string,array<string,bool>>
*/
protected static $protected_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
*
2016-10-31 20:17:54 +01:00
* @var array<string,array<string,Type\Union|false>>
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
*
2016-10-31 20:17:54 +01:00
* @var array<string,array<string,Type\Union|false>>
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
*
2016-10-31 20:17:54 +01:00
* @var array<string,array<string,Type\Union|false>>
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
*
2016-10-31 20:17:54 +01:00
* @var array<string,array<string,Type\Union|false>>
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
*
2016-10-31 20:17:54 +01:00
* @var array<string,array<string,Type\Union|false>>
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
*
2016-10-31 20:17:54 +01:00
* @var array<string,array<string,Type\Union|false>>
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
*
2016-10-12 07:38:29 +02:00
* @var array<string,array<string,Type\Union>>
2016-09-09 15:13:41 +02:00
*/
protected static $public_class_constants = [];
2016-07-25 05:38:52 +02:00
/**
* A lookup table for protected class constants
*
* @var array<string,array<string,Type\Union>>
*/
protected static $protected_class_constants = [];
/**
* A lookup table for private class constants
*
* @var array<string,array<string,Type\Union>>
*/
protected static $private_class_constants = [];
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 to record which classes are user-defined
*
* @var array<string,bool>
*/
protected static $user_defined = [];
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-11-20 17:51:19 +01:00
/**
* A lookup table for interface parents
*
* @var array<string, array<string>>
*/
protected static $parent_interfaces = [];
2016-11-02 07:29:00 +01:00
/**
* @var array<string,string>
*/
protected static $class_files = [];
2016-11-02 07:29:00 +01:00
/**
* @var array<string,array<int,string>>
*/
protected static $file_classes = [];
2016-11-02 07:29:00 +01:00
/**
* @var array<string,array<string,string>>|null
*/
protected static $property_map;
2016-12-12 19:50:46 +01:00
/**
* @var array<string, array<string, bool>>
*/
protected static $used_traits = [];
2016-11-02 07:29:00 +01:00
/**
* @param PhpParser\Node\Stmt\ClassLike $class
* @param StatementsSource $source
* @param string $fq_class_name
2016-11-02 07:29:00 +01:00
*/
public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSource $source, $fq_class_name)
2016-01-08 00:28:27 +01:00
{
$this->class = $class;
2016-11-13 00:51:48 +01:00
$this->source = $source;
$this->namespace = $source->getNamespace();
$this->aliased_classes = $source->getAliasedClasses();
2016-11-21 03:49:06 +01:00
$this->aliased_constants = $source->getAliasedConstants();
$this->aliased_functions = $source->getAliasedFunctions();
$this->file_name = $source->getFileName();
$this->file_path = $source->getFilePath();
2016-08-25 01:00:44 +02:00
$this->include_file_name = $source->getIncludeFileName();
$this->include_file_path = $source->getIncludeFilePath();
$this->fq_class_name = $fq_class_name;
2016-01-08 00:28:27 +01:00
$this->suppressed_issues = $source->getSuppressedIssues();
self::$class_files[$fq_class_name] = $this->file_name;
self::$file_classes[$this->file_name][] = $fq_class_name;
2016-08-15 05:24:16 +02:00
if (self::$this_class) {
self::$class_checkers[$fq_class_name] = $this;
}
2016-01-08 00:28:27 +01:00
}
2016-11-02 07:29:00 +01:00
/**
2016-11-13 17:24:46 +01:00
* @param bool $check_methods
* @param Context|null $class_context
* @param bool $update_docblocks
2016-11-02 07:29:00 +01:00
* @return false|null
*/
2016-11-13 00:51:48 +01:00
public function check($check_methods = true, Context $class_context = null, $update_docblocks = false)
2016-01-08 00:28:27 +01:00
{
2016-11-02 07:29:00 +01:00
if (!$check_methods &&
!($this instanceof TraitChecker) &&
isset(self::$registered_classes[$this->fq_class_name])
2016-11-02 07:29:00 +01:00
) {
return null;
2016-08-15 05:24:16 +02:00
}
2016-06-25 00:18:11 +02:00
$config = Config::getInstance();
self::$registered_classes[$this->fq_class_name] = true;
self::$user_defined[$this->fq_class_name] = true;
2016-08-15 05:24:16 +02:00
2016-01-26 20:13:04 +01:00
$leftover_stmts = [];
2016-11-05 01:49:04 +01:00
/** @var array<MethodChecker> */
2016-03-23 18:05:25 +01:00
$method_checkers = [];
2016-10-25 00:49:07 +02:00
$long_file_name = Config::getInstance()->getBaseDir() . $this->file_name;
self::$public_class_methods[$this->fq_class_name] = [];
self::$protected_class_methods[$this->fq_class_name] = [];
self::$public_class_properties[$this->fq_class_name] = [];
self::$protected_class_properties[$this->fq_class_name] = [];
self::$private_class_properties[$this->fq_class_name] = [];
2016-08-07 02:27:13 +02:00
self::$public_static_class_properties[$this->fq_class_name] = [];
self::$protected_static_class_properties[$this->fq_class_name] = [];
self::$private_static_class_properties[$this->fq_class_name] = [];
2016-08-07 02:27:13 +02:00
self::$public_class_constants[$this->fq_class_name] = [];
self::$protected_class_constants[$this->fq_class_name] = [];
self::$private_class_constants[$this->fq_class_name] = [];
2016-08-15 05:24:16 +02:00
if (!$class_context) {
$class_context = new Context($this->file_name, $this->fq_class_name);
2016-08-15 05:24:16 +02:00
$class_context->parent = $this->parent_class;
$class_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic($this->fq_class_name)]);
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) {
if ($stmt->isProtected()) {
self::$protected_class_constants[$class_context->self][$const->name] = Type::getMixed();
} elseif ($stmt->isPrivate()) {
self::$private_class_constants[$class_context->self][$const->name] = Type::getMixed();
} else {
self::$public_class_constants[$class_context->self][$const->name] = Type::getMixed();
}
2016-08-15 05:24:16 +02:00
}
}
}
2016-08-14 18:06:53 +02:00
if ($this instanceof ClassChecker) {
2016-11-02 14:24:36 +01:00
if ($this->parent_class && $this->registerParentClassProperties($this->parent_class) === false) {
return false;
2016-08-15 05:24:16 +02:00
}
2016-11-20 17:51:19 +01:00
}
2016-08-15 05:24:16 +02:00
2016-11-20 17:51:19 +01:00
if ($this instanceof InterfaceChecker || $this instanceof ClassChecker) {
2016-10-25 00:49:07 +02:00
$extra_interfaces = [];
2016-11-20 17:51:19 +01:00
if ($this instanceof InterfaceChecker) {
$parent_interfaces = InterfaceChecker::getParentInterfaces($this->fq_class_name);
$extra_interfaces = $parent_interfaces;
}
else {
$parent_interfaces = self::$class_implements[$this->fq_class_name];
}
foreach ($parent_interfaces as $interface_name) {
2016-11-08 01:16:51 +01:00
if (self::checkFullyQualifiedClassLikeName(
2016-08-15 05:24:16 +02:00
$interface_name,
new CodeLocation($this, $this->class, true),
2016-08-15 05:24:16 +02:00
$this->getSuppressedIssues()
2016-11-02 07:29:00 +01:00
) === false) {
2016-08-15 05:24:16 +02:00
return false;
}
2016-11-02 07:29:00 +01:00
$extra_interfaces = array_merge(
$extra_interfaces,
InterfaceChecker::getParentInterfaces($interface_name)
);
2016-10-25 00:49:07 +02:00
FileChecker::addFileInheritanceToClass($long_file_name, $interface_name);
}
$extra_interfaces = array_unique($extra_interfaces);
2016-10-05 23:08:20 +02:00
2016-10-25 00:49:07 +02:00
foreach ($extra_interfaces as $extra_interface_name) {
FileChecker::addFileInheritanceToClass($long_file_name, $extra_interface_name);
2016-11-20 17:51:19 +01:00
if ($this instanceof ClassChecker) {
self::$class_implements[$this->fq_class_name][strtolower($extra_interface_name)] =
$extra_interface_name;
}
else {
$this->registerInheritedMethods($extra_interface_name);
}
2016-11-02 17:14:21 +01:00
}
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-11-02 07:29:00 +01:00
$this->visitClassMethod(
$stmt,
$class_context,
$method_checkers,
self::$this_class && !$check_methods
);
2016-11-02 14:24:36 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
$this->visitTraitUse($stmt, $class_context, $trait_checkers);
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
2016-11-02 07:29:00 +01:00
$this->visitPropertyDeclaration(
$stmt,
$class_context,
$config,
$check_methods
);
2016-11-02 14:24:36 +01:00
$leftover_stmts[] = $stmt;
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
$this->visitClassConstDeclaration($stmt, $class_context, $config);
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 (MethodChecker::methodExists($this->fq_class_name . '::__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->fq_class_name],
self::$protected_class_properties[$this->fq_class_name],
self::$private_class_properties[$this->fq_class_name]
2016-09-12 17:32:44 +02:00
);
foreach ($all_instance_properties as $property_name => $property_type) {
2016-10-31 20:17:54 +01:00
$class_context->vars_in_scope['$this->' . $property_name] = $property_type ?: Type::getMixed();
2016-10-14 00:27:23 +02:00
}
$all_static_properties = array_merge(
self::$public_static_class_properties[$this->fq_class_name],
self::$protected_static_class_properties[$this->fq_class_name],
self::$private_static_class_properties[$this->fq_class_name]
2016-10-14 00:27:23 +02:00
);
foreach ($all_static_properties as $property_name => $property_type) {
$class_context->vars_in_scope[$this->fq_class_name . '::$' . $property_name] = $property_type
2016-11-02 07:29:00 +01:00
?: Type::getMixed();
2016-09-12 17:32:44 +02:00
}
$config = Config::getInstance();
2016-10-15 06:12:57 +02:00
if ($this instanceof ClassChecker) {
foreach (ClassChecker::getInterfacesForClass($this->fq_class_name) as $interface_id => $interface_name) {
2016-10-15 06:12:57 +02:00
if (isset(self::$public_class_constants[$interface_name])) {
self::$public_class_constants[$this->fq_class_name] +=
2016-11-02 07:29:00 +01:00
self::$public_class_constants[$interface_name];
2016-10-15 06:12:57 +02:00
}
2016-12-15 01:24:02 +01:00
foreach (self::$public_class_methods[$interface_name] as $method_name => $_) {
2016-10-15 06:12:57 +02:00
$mentioned_method_id = $interface_name . '::' . $method_name;
$implemented_method_id = $this->fq_class_name . '::' . $method_name;
2016-10-15 06:12:57 +02:00
MethodChecker::setOverriddenMethodId($implemented_method_id, $mentioned_method_id);
2016-12-15 01:24:02 +01:00
if (!isset(self::$public_class_methods[$this->fq_class_name])) {
2016-10-15 06:12:57 +02:00
if (IssueBuffer::accepts(
new UnimplementedInterfaceMethod(
'Method ' . $method_name . ' is not defined on class ' . $this->fq_class_name,
new CodeLocation($this, $this->class)
2016-10-15 06:12:57 +02:00
),
2016-10-30 16:14:36 +01:00
$this->suppressed_issues
2016-10-15 06:12:57 +02:00
)) {
return false;
}
2016-11-02 07:29:00 +01:00
return null;
2016-10-15 06:12:57 +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)) {
2016-12-07 20:13:39 +01:00
/** @var string */
$method_id = $method_checker->getMethodId();
$return_type_location = MethodChecker::getMethodReturnTypeLocation(
$method_id,
$secondary_return_type_location
);
$method_checker->checkReturnTypes(
$update_docblocks,
MethodChecker::getMethodReturnType($method_id),
$return_type_location,
$secondary_return_type_location
);
}
}
}
2016-10-21 02:54:17 +02:00
if (!$this->class->name) {
$this->class->name = $this->fq_class_name;
2016-10-21 02:54:17 +02:00
}
2016-11-02 07:29:00 +01:00
return null;
}
2016-11-02 14:24:36 +01:00
/**
* @param string $parent_class
* @return false|null
*/
protected function registerParentClassProperties($parent_class)
{
if (!$this->class instanceof PhpParser\Node\Stmt\Class_) {
throw new \UnexpectedValueException('Cannot register parent class where none exists');
}
if (!$this->class->extends) {
throw new \UnexpectedValueException('Cannot register parent class where none exists');
}
2016-11-08 01:16:51 +01:00
if (self::checkFullyQualifiedClassLikeName(
2016-11-02 14:24:36 +01:00
$parent_class,
new CodeLocation($this, $this->class->extends, true),
2016-11-02 14:24:36 +01:00
$this->getSuppressedIssues()
) === false
) {
return false;
}
self::registerClass($parent_class);
$this->registerInheritedMethods($parent_class);
FileChecker::addFileInheritanceToClass(Config::getInstance()->getBaseDir() . $this->file_name, $parent_class);
self::$class_implements[$this->fq_class_name] += self::$class_implements[$parent_class];
2016-11-02 14:24:36 +01:00
self::$public_class_properties[$this->fq_class_name] = self::$public_class_properties[$parent_class];
self::$protected_class_properties[$this->fq_class_name] = self::$protected_class_properties[$parent_class];
2016-11-02 14:24:36 +01:00
self::$public_static_class_properties[$this->fq_class_name] =
2016-11-04 01:51:56 +01:00
self::$public_static_class_properties[$parent_class];
self::$protected_static_class_properties[$this->fq_class_name] =
2016-11-04 01:51:56 +01:00
self::$protected_static_class_properties[$parent_class];
2016-11-02 14:24:36 +01:00
self::$public_class_constants[$this->fq_class_name] = self::$public_class_constants[$parent_class];
self::$protected_class_constants[$this->fq_class_name] =
self::$protected_class_constants[$parent_class];
2016-11-04 01:51:56 +01:00
if (isset(self::$used_traits[$parent_class])) {
self::$used_traits[$this->fq_class_name] = self::$used_traits[$parent_class];
}
2016-11-04 01:51:56 +01:00
return null;
2016-11-02 14:24:36 +01:00
}
2016-11-04 01:51:56 +01:00
/**
2016-11-05 01:10:59 +01:00
* @param PhpParser\Node\Stmt\ClassMethod $stmt
* @param Context $class_context
2016-11-05 01:49:04 +01:00
* @param array<MethodChecker> $method_checkers
2016-11-05 01:10:59 +01:00
* @param bool $cache_method_checker
* @return void
2016-11-04 01:51:56 +01:00
*/
protected function visitClassMethod(
PhpParser\Node\Stmt\ClassMethod $stmt,
Context $class_context,
array &$method_checkers,
$cache_method_checker
) {
$method_id = $this->fq_class_name . '::' . strtolower($stmt->name);
2016-11-02 14:24:36 +01:00
if (!isset(self::$method_checkers[$method_id])) {
$method_checker = new MethodChecker($stmt, $this);
$method_checkers[$stmt->name] = $method_checker;
if ($cache_method_checker) {
self::$method_checkers[$method_id] = $method_checker;
}
} else {
$method_checker = self::$method_checkers[$method_id];
}
if (!$stmt->isAbstract()) {
2016-11-02 07:29:00 +01:00
MethodChecker::setDeclaringMethodId(
$class_context->self . '::' . $this->getMappedMethodName(strtolower($stmt->name)),
$method_id
);
2016-11-02 14:24:36 +01:00
}
2016-12-07 20:13:39 +01:00
if ($stmt->isPublic()) {
self::$public_class_methods[$class_context->self][strtolower($stmt->name)] = true;
} elseif ($stmt->isProtected()) {
self::$protected_class_methods[$class_context->self][strtolower($stmt->name)] = true;
}
2016-11-02 14:24:36 +01:00
}
2016-11-04 01:51:56 +01:00
/**
* @param PhpParser\Node\Stmt\TraitUse $stmt
* @param Context $class_context
2016-11-05 01:10:59 +01:00
* @param array<TraitChecker> $trait_checkers
2016-11-04 01:51:56 +01:00
* @return false|null
*/
protected function visitTraitUse(
PhpParser\Node\Stmt\TraitUse $stmt,
Context $class_context,
array &$trait_checkers
) {
2016-11-02 14:24:36 +01:00
$method_map = [];
foreach ($stmt->adaptations as $adaptation) {
if ($adaptation instanceof PhpParser\Node\Stmt\TraitUseAdaptation\Alias) {
if ($adaptation->method && $adaptation->newName) {
$method_map[strtolower($adaptation->method)] = strtolower($adaptation->newName);
}
}
}
foreach ($stmt->traits as $trait) {
2016-11-08 01:16:51 +01:00
$trait_name = self::getFQCLNFromNameObject(
2016-11-02 07:29:00 +01:00
$trait,
$this->namespace,
$this->aliased_classes
);
2016-11-02 14:24:36 +01:00
if (!TraitChecker::traitExists($trait_name)) {
if (IssueBuffer::accepts(
new UndefinedTrait(
'Trait ' . $trait_name . ' does not exist',
new CodeLocation($this, $trait)
),
2016-11-02 14:24:36 +01:00
$this->suppressed_issues
)) {
return false;
}
} else {
2016-12-12 19:50:46 +01:00
if (!TraitChecker::hasCorrectCase($trait_name)) {
2016-11-02 14:24:36 +01:00
if (IssueBuffer::accepts(
2016-11-04 01:51:56 +01:00
new UndefinedTrait(
'Trait ' . $trait_name . ' has wrong casing',
new CodeLocation($this, $trait)
2016-11-04 01:51:56 +01:00
),
2016-11-02 14:24:36 +01:00
$this->suppressed_issues
)) {
return false;
}
continue;
}
2016-12-12 19:50:46 +01:00
if (isset(self::$class_checkers[$trait_name])) {
/** @var TraitChecker */
$trait_checker = self::$class_checkers[$trait_name];
} else {
/** @var TraitChecker */
$trait_checker = FileChecker::getClassLikeCheckerFromClass($trait_name);
}
2016-11-02 14:24:36 +01:00
$trait_checker->setMethodMap($method_map);
$trait_checker->check(false, $class_context);
2016-12-12 19:50:46 +01:00
ClassLikeChecker::registerTraitUse($this->fq_class_name, $trait_name);
2016-11-04 01:51:56 +01:00
FileChecker::addFileInheritanceToClass(
Config::getInstance()->getBaseDir() . $this->file_name,
2016-11-05 01:49:04 +01:00
$trait_name
2016-11-04 01:51:56 +01:00
);
2016-11-02 14:24:36 +01:00
$trait_checkers[] = $trait_checker;
}
}
2016-11-04 01:51:56 +01:00
return null;
2016-11-02 14:24:36 +01:00
}
2016-11-04 01:51:56 +01:00
/**
2016-11-05 01:10:59 +01:00
* @param PhpParser\Node\Stmt\Property $stmt
* @param Context $class_context
* @param Config $config
* @param bool $check_property_types
2016-11-21 05:45:10 +01:00
* @return false|null
2016-11-04 01:51:56 +01:00
*/
protected function visitPropertyDeclaration(
PhpParser\Node\Stmt\Property $stmt,
Context $class_context,
Config $config,
$check_property_types
) {
2016-11-02 14:24:36 +01:00
$comment = $stmt->getDocComment();
$type_in_comment = null;
if ($comment && $config->use_docblock_types) {
2016-11-21 05:31:10 +01:00
try {
$type_in_comment = CommentChecker::getTypeFromComment((string) $comment, null, $this);
} catch (DocblockParseException $e) {
2016-11-21 05:31:10 +01:00
if (IssueBuffer::accepts(
new InvalidDocblock(
2016-11-21 05:45:10 +01:00
(string)$e->getMessage(),
new CodeLocation($this, $this->class, true)
2016-11-21 05:31:10 +01:00
)
)) {
return false;
}
}
2016-11-02 14:24:36 +01:00
} elseif (!$comment && $check_property_types) {
if (IssueBuffer::accepts(
new MissingPropertyType(
'Property ' . $this->fq_class_name . '::$' . $stmt->props[0]->name . ' does not have a ' .
2016-11-04 01:51:56 +01:00
'declared type',
new CodeLocation($this, $stmt)
2016-11-02 14:24:36 +01:00
),
$this->suppressed_issues
)) {
// fall through
}
}
$property_group_type = $type_in_comment ? $type_in_comment : null;
foreach ($stmt->props as $property) {
if (!$property_group_type) {
if (!$property->default || !$config->use_property_default_for_type) {
2016-11-02 14:24:36 +01:00
$property_type = false;
} else {
$property_type = StatementsChecker::getSimpleType($property->default) ?: Type::getMixed();
}
} else {
$property_type = count($stmt->props) === 1 ? $property_group_type : clone $property_group_type;
}
if ($stmt->isStatic()) {
if ($stmt->isPublic()) {
self::$public_static_class_properties[$class_context->self][$property->name] = $property_type;
} elseif ($stmt->isProtected()) {
self::$protected_static_class_properties[$class_context->self][$property->name] = $property_type;
} elseif ($stmt->isPrivate()) {
self::$private_static_class_properties[$class_context->self][$property->name] = $property_type;
}
} else {
if ($stmt->isPublic()) {
self::$public_class_properties[$class_context->self][$property->name] = $property_type;
} elseif ($stmt->isProtected()) {
self::$protected_class_properties[$class_context->self][$property->name] = $property_type;
} elseif ($stmt->isPrivate()) {
self::$private_class_properties[$class_context->self][$property->name] = $property_type;
}
}
}
}
2016-11-04 01:51:56 +01:00
/**
* @param PhpParser\Node\Stmt\ClassConst $stmt
* @param Context $class_context
* @param Config $config
* @return void
*/
protected function visitClassConstDeclaration(
PhpParser\Node\Stmt\ClassConst $stmt,
Context $class_context,
Config $config
) {
2016-11-02 14:24:36 +01:00
$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) {
if ($stmt->isProtected()) {
self::$protected_class_constants[$class_context->self][$const->name] = $const_type;
} elseif ($stmt->isPrivate()) {
self::$private_class_constants[$class_context->self][$const->name] = $const_type;
} else {
self::$public_class_constants[$class_context->self][$const->name] = $const_type;
}
2016-11-02 14:24:36 +01:00
}
}
/**
* 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
2016-11-07 05:29:54 +01:00
/** @var string */
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];
$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) {
if ($declaring_method_id === $class_checker->fq_class_name . '::' . 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 $fq_class_name
* @return bool
*/
public static function classOrInterfaceExists($fq_class_name)
{
return ClassChecker::classExists($fq_class_name) || InterfaceChecker::interfaceExists($fq_class_name);
2016-07-25 05:38:52 +02:00
}
/**
* @param string $fq_class_name
2016-07-25 05:38:52 +02:00
* @param string $possible_parent
* @return bool
*/
public static function classExtendsOrImplements($fq_class_name, $possible_parent)
2016-07-25 05:38:52 +02:00
{
return ClassChecker::classExtends($fq_class_name, $possible_parent)
|| ClassChecker::classImplements($fq_class_name, $possible_parent);
2016-07-25 05:38:52 +02:00
}
/**
* @param string $fq_class_name
* @param CodeLocation $code_location
2016-11-05 01:10:59 +01:00
* @param array<string> $suppressed_issues
2016-07-25 05:38:52 +02:00
* @return bool|null
2016-04-27 00:42:48 +02:00
*/
2016-11-08 01:16:51 +01:00
public static function checkFullyQualifiedClassLikeName(
$fq_class_name,
CodeLocation $code_location,
2016-11-02 07:29:00 +01:00
array $suppressed_issues
) {
if (empty($fq_class_name)) {
2016-04-12 17:59:27 +02:00
throw new \InvalidArgumentException('$class cannot be empty');
}
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name);
2016-03-17 19:06:01 +01:00
if (in_array($fq_class_name, ['callable', 'iterable'])) {
return true;
}
$class_exists = ClassChecker::classExists($fq_class_name);
$interface_exists = InterfaceChecker::interfaceExists($fq_class_name);
if (!$class_exists && !$interface_exists) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new UndefinedClass(
'Class or interface ' . $fq_class_name . ' does not exist',
$code_location
2016-11-02 07:29:00 +01:00
),
$suppressed_issues
)) {
return false;
}
2016-11-02 07:29:00 +01:00
return null;
2016-01-08 00:28:27 +01:00
}
if (($class_exists && !ClassChecker::hasCorrectCasing($fq_class_name))
|| ($interface_exists && !InterfaceChecker::hasCorrectCasing($fq_class_name))
) {
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new InvalidClass(
'Class or interface ' . $fq_class_name . ' has wrong casing',
$code_location
2016-11-02 07:29:00 +01:00
),
$suppressed_issues
)) {
return false;
2016-08-10 07:09:47 +02:00
}
2016-03-17 19:06:01 +01:00
}
2016-12-08 04:38:57 +01:00
FileChecker::addFileReferenceToClass(
Config::getInstance()->getBaseDir() . $code_location->file_name,
$fq_class_name
);
return true;
2016-01-08 00:28:27 +01:00
}
/**
2016-11-02 07:29:00 +01:00
* Gets the fully-qualified class name from a Name object
*
* @param PhpParser\Node\Name $class_name
* @param string $namespace
* @param array<int,string> $aliased_classes
* @return string
*/
2016-11-08 01:16:51 +01:00
public static function getFQCLNFromNameObject(
2016-11-02 07:29:00 +01:00
PhpParser\Node\Name $class_name,
$namespace,
array $aliased_classes
) {
2016-01-08 00:28:27 +01:00
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
return implode('\\', $class_name->parts);
2016-01-08 00:28:27 +01:00
}
2016-11-08 01:16:51 +01:00
return self::getFQCLNFromString(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<string, string> $imported_namespaces
2016-10-12 07:38:29 +02:00
* @return string
*/
2016-11-08 01:16:51 +01:00
public static function getFQCLNFromString($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
}
2016-11-02 07:29:00 +01:00
/**
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}
2016-11-13 00:51:48 +01:00
/**
* @return array<string, string>
*/
public function getAliasedClassesFlipped()
{
2016-11-13 17:24:46 +01:00
if ($this->source instanceof NamespaceChecker || $this->source instanceof FileChecker) {
return $this->source->getAliasedClassesFlipped();
}
return [];
2016-11-13 00:51:48 +01:00
}
2016-11-02 07:29:00 +01:00
/**
* @return string
*/
2016-11-08 01:16:51 +01:00
public function getFQCLN()
{
return $this->fq_class_name;
}
2016-11-02 07:29:00 +01:00
/**
* @return string|null
2016-11-02 07:29:00 +01:00
*/
public function getClassName()
{
return $this->class->name;
}
/**
* @return string|null
*/
public function getParentClass()
{
return $this->parent_class;
}
2016-11-02 07:29:00 +01:00
/**
* @return $this
*/
public function getClassLikeChecker()
{
return $this;
}
2016-04-27 00:42:48 +02:00
/**
* @return bool
*/
public function isStatic()
{
return false;
}
2016-11-02 07:29:00 +01:00
/**
* @return bool
*/
public function hasCustomGet()
{
return $this->has_custom_get;
}
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-08-15 05:24:16 +02:00
if (isset(self::$registered_classes[$class_name])) {
return true;
}
2016-10-30 17:46:18 +01:00
if (!$class_name || strpos($class_name, '::') !== false) {
throw new \InvalidArgumentException('Invalid class name ' . $class_name);
}
2016-08-07 17:35:27 +02:00
try {
2016-10-30 06:13:33 +01:00
$old_level = error_reporting();
error_reporting(0);
2016-08-07 17:35:27 +02:00
$reflected_class = new ReflectionClass($class_name);
2016-10-30 06:13:33 +01:00
error_reporting($old_level);
2016-11-02 07:29:00 +01:00
} catch (\ReflectionException $e) {
2016-08-07 17:35:27 +02:00
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
$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-10-30 16:14:36 +01:00
if (self::inPropertyMap($class_name)) {
$public_mapped_properties = self::getPropertyMap()[strtolower($class_name)];
foreach ($public_mapped_properties as $property_name => $public_mapped_property) {
2016-11-02 07:29:00 +01:00
self::$public_class_properties[$class_name][$property_name] = Type::parseString(
$public_mapped_property
);
2016-10-30 16:14:36 +01:00
}
}
2016-11-02 14:24:36 +01:00
} else {
self::registerReflectedClass($class_name, $reflected_class);
2016-08-07 02:27:13 +02:00
}
2016-11-02 14:24:36 +01:00
return true;
}
/**
* @param string $class_name
* @param ReflectionClass $reflected_class
* @return void
*/
protected static function registerReflectedClass($class_name, ReflectionClass $reflected_class)
{
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-11-02 14:24:36 +01:00
self::$public_static_class_properties[$class_name] = [];
self::$protected_static_class_properties[$class_name] = [];
self::$private_static_class_properties[$class_name] = [];
2016-11-02 14:24:36 +01:00
$parent_class = $reflected_class->getParentClass();
2016-11-02 14:24:36 +01:00
if ($parent_class) {
$parent_class_name = $parent_class->getName();
self::registerClass($parent_class_name);
2016-11-02 14:24:36 +01:00
self::$public_class_properties[$class_name] = self::$public_class_properties[$parent_class_name];
self::$protected_class_properties[$class_name] = self::$protected_class_properties[$parent_class_name];
2016-11-04 01:51:56 +01:00
self::$public_static_class_properties[$class_name] =
self::$public_static_class_properties[$parent_class_name];
self::$protected_static_class_properties[$class_name] =
self::$protected_static_class_properties[$parent_class_name];
2016-11-02 14:24:36 +01:00
}
2016-11-02 14:24:36 +01:00
$class_properties = $reflected_class->getProperties();
/** @var \ReflectionProperty $class_property */
foreach ($class_properties as $class_property) {
if ($class_property->isStatic()) {
if ($class_property->isPublic()) {
2016-11-02 07:29:00 +01:00
self::$public_static_class_properties[$class_name][$class_property->getName()] =
Type::getMixed();
2016-11-02 14:24:36 +01:00
} elseif ($class_property->isProtected()) {
2016-11-02 07:29:00 +01:00
self::$protected_static_class_properties[$class_name][$class_property->getName()] =
Type::getMixed();
2016-11-02 14:24:36 +01:00
} elseif ($class_property->isPrivate()) {
2016-11-02 07:29:00 +01:00
self::$private_static_class_properties[$class_name][$class_property->getName()] =
Type::getMixed();
2016-08-07 02:27:13 +02:00
}
2016-11-02 14:24:36 +01:00
} else {
if ($class_property->isPublic()) {
2016-11-02 07:29:00 +01:00
self::$public_class_properties[$class_name][$class_property->getName()] =
Type::getMixed();
2016-11-02 14:24:36 +01:00
} elseif ($class_property->isProtected()) {
2016-11-02 07:29:00 +01:00
self::$protected_class_properties[$class_name][$class_property->getName()] =
Type::getMixed();
2016-11-02 14:24:36 +01:00
} elseif ($class_property->isPrivate()) {
2016-11-02 07:29:00 +01:00
self::$private_class_properties[$class_name][$class_property->getName()] =
Type::getMixed();
2016-08-07 02:27:13 +02:00
}
}
2016-11-02 14:24:36 +01:00
}
2016-11-02 14:24:36 +01:00
if (self::inPropertyMap($class_name)) {
$public_mapped_properties = self::getPropertyMap()[strtolower($class_name)];
2016-11-02 14:24:36 +01:00
foreach ($public_mapped_properties as $property_name => $public_mapped_property) {
2016-11-02 07:29:00 +01:00
self::$public_class_properties[$class_name][$property_name] = Type::parseString(
$public_mapped_property
);
}
2016-11-02 14:24:36 +01:00
}
2016-11-02 14:24:36 +01:00
$class_constants = $reflected_class->getConstants();
2016-11-20 17:51:19 +01:00
if ($reflected_class->isInterface()) {
self::$parent_interfaces[$class_name] = [];
}
2016-11-02 14:24:36 +01:00
self::$public_class_constants[$class_name] = [];
self::$private_class_constants[$class_name] = [];
self::$protected_class_constants[$class_name] = [];
2016-11-02 14:24:36 +01:00
foreach ($class_constants as $name => $value) {
2016-11-21 03:49:06 +01:00
self::$public_class_constants[$class_name][$name] = self::getTypeFromValue($value);
2016-11-02 14:24:36 +01:00
}
2016-08-14 18:06:53 +02:00
2016-11-02 14:24:36 +01:00
self::$registered_classes[$class_name] = true;
2016-08-15 05:24:16 +02:00
2016-11-02 14:24:36 +01:00
if (!$reflected_class->isTrait() && !$reflected_class->isInterface()) {
ClassChecker::getInterfacesForClass($class_name);
}
2016-08-15 05:24:16 +02:00
2016-11-04 01:51:56 +01:00
$reflection_methods = $reflected_class->getMethods(
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
);
2016-08-15 05:24:16 +02:00
self::$protected_class_methods[$class_name] = [];
self::$public_class_methods[$class_name] = [];
2016-08-15 05:24:16 +02:00
2016-11-21 04:40:19 +01:00
/** @var \ReflectionMethod $reflection_method */
2016-11-02 14:24:36 +01:00
foreach ($reflection_methods as $reflection_method) {
MethodChecker::extractReflectionMethodInfo($reflection_method);
2016-08-15 06:31:34 +02:00
2016-11-02 14:24:36 +01:00
if ($reflection_method->class !== $class_name) {
MethodChecker::setDeclaringMethodId(
$class_name . '::' . strtolower((string)$reflection_method->name),
$reflection_method->class . '::' . strtolower((string)$reflection_method->name)
);
2016-08-15 05:24:16 +02:00
self::$public_class_methods[$class_name][strtolower((string)$reflection_method->name)] = true;
2016-08-15 05:24:16 +02:00
}
2016-11-04 01:51:56 +01:00
if (!$reflection_method->isAbstract() &&
$reflection_method->getDeclaringClass()->getName() === $class_name
) {
self::$public_class_methods[$class_name][strtolower((string)$reflection_method->getName())] = true;
2016-11-02 14:24:36 +01:00
}
}
}
2016-11-02 07:29:00 +01:00
/**
* @param string $parent_class
* @return void
*/
2016-08-15 20:20:06 +02:00
protected function registerInheritedMethods($parent_class)
2016-08-15 06:31:34 +02:00
{
$public_class_methods = self::$public_class_methods[$parent_class];
foreach ($public_class_methods as $method_name => $_) {
$parent_method_id = $parent_class . '::' . $method_name;
/** @var string */
$declaring_method_id = MethodChecker::getDeclaringMethodId($parent_method_id);
$implemented_method_id = $this->fq_class_name . '::' . $method_name;
if (!isset(self::$public_class_methods[$this->fq_class_name][$method_name])) {
MethodChecker::setDeclaringMethodId($implemented_method_id, $declaring_method_id);
self::$public_class_methods[$this->fq_class_name][$method_name] = true;
MethodChecker::setOverriddenMethodId($implemented_method_id, $declaring_method_id);
}
}
$protected_class_methods = self::$protected_class_methods[$parent_class];
2016-08-15 06:31:34 +02:00
foreach ($protected_class_methods as $method_name => $_) {
2016-08-15 20:20:06 +02:00
$parent_method_id = $parent_class . '::' . $method_name;
2016-11-07 05:29:54 +01:00
/** @var string */
2016-10-15 06:12:57 +02:00
$declaring_method_id = MethodChecker::getDeclaringMethodId($parent_method_id);
$implemented_method_id = $this->fq_class_name . '::' . $method_name;
2016-08-15 06:31:34 +02:00
if (!isset(self::$protected_class_methods[$this->fq_class_name][$method_name])) {
2016-10-15 06:12:57 +02:00
MethodChecker::setDeclaringMethodId($implemented_method_id, $declaring_method_id);
self::$protected_class_methods[$this->fq_class_name][$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-31 20:17:54 +01:00
* @return array<string,Type\Union|false>
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) {
return self::$public_class_properties[$class_name];
2016-08-07 02:27:13 +02:00
}
2016-11-02 14:24:36 +01:00
if ($visibility === ReflectionProperty::IS_PROTECTED) {
2016-08-07 02:27:13 +02:00
return array_merge(
self::$public_class_properties[$class_name],
self::$protected_class_properties[$class_name]
2016-08-07 02:27:13 +02:00
);
}
2016-11-02 14:24:36 +01:00
if ($visibility === ReflectionProperty::IS_PRIVATE) {
2016-08-07 02:27:13 +02:00
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');
}
2016-10-12 07:38:29 +02:00
/**
* @param string $class_name
* @param mixed $visibility
2016-10-31 20:42:20 +01:00
* @return array<string,Type\Union|false>
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) {
return self::$public_static_class_properties[$class_name];
2016-08-07 02:27:13 +02:00
}
2016-11-02 14:24:36 +01:00
if ($visibility === ReflectionProperty::IS_PROTECTED) {
2016-08-07 02:27:13 +02:00
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
);
}
2016-11-02 14:24:36 +01:00
if ($visibility === ReflectionProperty::IS_PRIVATE) {
2016-08-07 02:27:13 +02:00
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
2016-11-21 03:49:06 +01:00
/**
* Gets the Psalm type from a particular value
*
* @param mixed $value
* @return Type\Union
*/
public static function getTypeFromValue($value)
{
switch (gettype($value)) {
case 'boolean':
return Type::getBool();
case 'integer':
return Type::getInt();
case 'double':
return Type::getFloat();
case 'string':
return Type::getString();
case 'array':
return Type::getArray();
case 'NULL':
return Type::getNull();
default:
return Type::getMixed();
}
}
2016-10-12 07:38:29 +02:00
/**
* @param string $class_name
* @param mixed $visibility
* @return array<string,Type\Union>
*/
public static function getConstantsForClass($class_name, $visibility)
{
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];
}
if ($visibility === ReflectionProperty::IS_PROTECTED) {
return array_merge(
self::$public_class_constants[$class_name],
self::$protected_class_constants[$class_name]
);
}
if ($visibility === ReflectionProperty::IS_PRIVATE) {
return array_merge(
self::$public_class_constants[$class_name],
self::$protected_class_constants[$class_name],
self::$private_class_constants[$class_name]
);
}
throw new \InvalidArgumentException('Must specify $visibility');
}
2016-11-02 07:29:00 +01:00
/**
* @param string $class_name
* @param string $const_name
* @param Type\Union $type
* @param int $visibility
2016-11-02 07:29:00 +01:00
* @return void
*/
public static function setConstantType($class_name, $const_name, Type\Union $type, $visibility)
{
if ($visibility === ReflectionProperty::IS_PUBLIC) {
self::$public_class_constants[$class_name][$const_name] = $type;
} elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
self::$protected_class_constants[$class_name][$const_name] = $type;
} elseif ($visibility === ReflectionProperty::IS_PRIVATE) {
self::$private_class_constants[$class_name][$const_name] = $type;
}
}
2016-11-02 07:29:00 +01:00
/**
2016-11-06 01:17:22 +01:00
* @param string|null $this_class
2016-11-02 07:29:00 +01:00
* @return void
*/
public static function setThisClass($this_class)
{
self::$this_class = $this_class;
2016-05-16 22:12:02 +02:00
self::$class_checkers = [];
}
/**
* @return string|null
*/
public static function getThisClass()
{
return self::$this_class;
}
2016-07-25 00:02:03 +02:00
2016-11-02 07:29:00 +01:00
/**
* @param string $method_name
* @return mixed
*/
2016-08-15 20:20:06 +02:00
protected function getMappedMethodName($method_name)
{
return $method_name;
}
2016-11-02 07:29:00 +01:00
/**
* @param string $file_name
* @return array<string>
2016-11-02 07:29:00 +01:00
*/
public static function getClassesForFile($file_name)
{
return isset(self::$file_classes[$file_name]) ? array_unique(self::$file_classes[$file_name]) : [];
}
2016-10-30 16:14:36 +01:00
/**
* @param string $fq_class_name
2016-10-30 16:14:36 +01:00
* @return boolean
*/
public static function isUserDefined($fq_class_name)
{
self::registerClass($fq_class_name);
return isset(self::$user_defined[$fq_class_name]);
}
/**
* Gets the method/function call map
*
* @return array<string, array<string, string>>
* @psalm-suppress MixedInferredReturnType as the use of require buggers things up
*/
protected static function getPropertyMap()
{
if (self::$property_map !== null) {
return self::$property_map;
}
2016-12-07 07:12:19 +01:00
/** @var array<string, array> */
$property_map = require_once(__DIR__.'/../PropertyMap.php');
self::$property_map = [];
foreach ($property_map as $key => $value) {
$cased_key = strtolower($key);
self::$property_map[$cased_key] = $value;
}
return self::$property_map;
}
2016-11-02 07:29:00 +01:00
/**
* @param string $class_name
* @return bool
*/
public static function inPropertyMap($class_name)
{
return isset(self::getPropertyMap()[strtolower($class_name)]);
}
2016-12-12 19:50:46 +01:00
/**
* @param string $fq_class_name
* @param string $fq_trait_name
* @return void
*/
public static function registerTraitUse($fq_class_name, $fq_trait_name)
{
self::$used_traits[$fq_class_name][$fq_trait_name] = true;
}
/**
* @return bool
*/
public static function classUsesTrait($fq_class_name, $fq_trait_name)
{
return isset(self::$used_traits[$fq_class_name][$fq_trait_name]);
}
2016-11-02 07:29:00 +01:00
/**
* @return void
*/
2016-07-25 00:02:03 +02:00
public static function clearCache()
{
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
self::$protected_class_methods = [];
self::$public_class_methods = [];
2016-10-21 00:12:13 +02:00
self::$class_checkers = [];
2016-07-25 00:02:03 +02:00
self::$public_class_properties = [];
2016-10-21 00:12:13 +02:00
self::$protected_class_properties = [];
2016-10-21 00:12:13 +02:00
self::$private_class_properties = [];
2016-08-10 07:09:47 +02:00
self::$public_static_class_properties = [];
2016-10-21 00:12:13 +02:00
self::$protected_static_class_properties = [];
2016-10-21 00:12:13 +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::$protected_class_constants = [];
self::$private_class_constants = [];
2016-10-21 00:12:13 +02:00
self::$registered_classes = [];
self::$class_implements = [];
self::$class_files = [];
self::$file_classes = [];
2016-12-12 19:50:46 +01:00
self::$used_traits = [];
2016-08-15 17:01:50 +02:00
ClassChecker::clearCache();
InterfaceChecker::clearCache();
2016-12-12 19:50:46 +01:00
TraitChecker::clearCache();
2016-07-25 00:02:03 +02:00
}
2016-01-08 00:28:27 +01:00
}