2016-08-13 20:20:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Checker;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\StatementsSource;
|
2016-08-14 01:44:24 +02:00
|
|
|
use Psalm\Context;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
|
|
|
class ClassChecker extends ClassLikeChecker
|
|
|
|
{
|
2016-10-10 07:35:12 +02:00
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt\Class_
|
|
|
|
*/
|
|
|
|
protected $class;
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* A lookup table of existing classes
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $existing_classes = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table of existing classes, all lowercased
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $existing_classes_ci = [];
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table used for caching the results of classExtends calls
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $class_extends = [];
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Class_ $class
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param string $absolute_class
|
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public function __construct(PhpParser\Node\Stmt\Class_ $class, StatementsSource $source, $absolute_class)
|
|
|
|
{
|
|
|
|
parent::__construct($class, $source, $absolute_class);
|
2016-08-14 00:54:49 +02:00
|
|
|
|
|
|
|
self::$existing_classes[$absolute_class] = true;
|
|
|
|
self::$existing_classes_ci[strtolower($absolute_class)] = true;
|
|
|
|
|
|
|
|
self::$class_implements[$absolute_class] = [];
|
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
if ($this->class->extends) {
|
|
|
|
$this->parent_class = self::getAbsoluteClassFromName($this->class->extends, $this->namespace, $this->aliased_classes);
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
foreach ($class->implements as $interface_name) {
|
|
|
|
$absolute_interface_name = self::getAbsoluteClassFromName($interface_name, $this->namespace, $this->aliased_classes);
|
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
self::$class_implements[$absolute_class][strtolower($absolute_interface_name)] = $absolute_interface_name;
|
2016-08-14 00:54:49 +02:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* Determine whether or not a given class exists
|
|
|
|
*
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public static function classExists($absolute_class)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
if (isset(self::$existing_classes_ci[strtolower($absolute_class)])) {
|
|
|
|
return self::$existing_classes_ci[strtolower($absolute_class)];
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($absolute_class, self::$SPECIAL_TYPES)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
if (class_exists($absolute_class)) {
|
|
|
|
$reflected_class = new \ReflectionClass($absolute_class);
|
|
|
|
|
|
|
|
self::$existing_classes_ci[strtolower($absolute_class)] = true;
|
|
|
|
self::$existing_classes[$reflected_class->getName()] = true;
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-14 19:00:41 +02:00
|
|
|
// we can only be sure that the case-sensitive version does not exist
|
|
|
|
self::$existing_classes[$absolute_class] = false;
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* Determine whether or not a class has the correct casing
|
|
|
|
*
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
public static function hasCorrectCasing($absolute_class)
|
|
|
|
{
|
|
|
|
if (!self::classExists($absolute_class)) {
|
|
|
|
throw new \InvalidArgumentException('Cannot check casing on nonexistent class ' . $absolute_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset(self::$existing_classes[$absolute_class]);
|
|
|
|
}
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
/**
|
2016-09-09 15:13:41 +02:00
|
|
|
* Determine whether or not a class extends a parent
|
|
|
|
*
|
2016-08-13 20:20:46 +02:00
|
|
|
* @param string $absolute_class
|
|
|
|
* @param string $possible_parent
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classExtends($absolute_class, $possible_parent)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
if (isset(self::$class_extends[$absolute_class][$possible_parent])) {
|
|
|
|
return self::$class_extends[$absolute_class][$possible_parent];
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!self::classExists($absolute_class) || !self::classExists($possible_parent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
if (!isset(self::$class_extends[$absolute_class])) {
|
|
|
|
self::$class_extends[$absolute_class] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$class_extends[$absolute_class][$possible_parent] = is_subclass_of($absolute_class, $possible_parent);
|
|
|
|
|
|
|
|
return self::$class_extends[$absolute_class][$possible_parent];
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* Get all the interfaces a given class implements
|
|
|
|
*
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
public static function getInterfacesForClass($absolute_class)
|
|
|
|
{
|
2016-08-14 01:44:24 +02:00
|
|
|
if (!isset(self::$class_implements[$absolute_class])) {
|
2016-10-12 07:38:29 +02:00
|
|
|
/** @var string[] */
|
2016-08-22 22:41:45 +02:00
|
|
|
$class_implements = class_implements($absolute_class);
|
|
|
|
|
|
|
|
self::$class_implements[$absolute_class] = [];
|
|
|
|
|
|
|
|
foreach ($class_implements as $interface) {
|
|
|
|
self::$class_implements[$absolute_class][strtolower($interface)] = $interface;
|
|
|
|
}
|
2016-08-14 01:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$class_implements[$absolute_class];
|
2016-08-14 00:54:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-09 15:13:41 +02:00
|
|
|
* Check whether a class implements an interface
|
|
|
|
*
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @param string $interface
|
2016-08-14 00:54:49 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classImplements($absolute_class, $interface)
|
|
|
|
{
|
2016-08-22 22:41:45 +02:00
|
|
|
$interface_id = strtolower($interface);
|
|
|
|
|
2016-10-21 00:05:28 +02:00
|
|
|
if ($interface_id === 'callable' && $absolute_class === 'Closure') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
if (isset(self::$class_implements[$absolute_class][$interface_id])) {
|
2016-08-14 00:54:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset(self::$class_implements[$absolute_class])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ClassChecker::classExists($absolute_class)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
if (in_array($interface_id, self::$SPECIAL_TYPES)) {
|
2016-08-14 00:54:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
$class_implementations = self::getInterfacesForClass($absolute_class);
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
return isset($class_implementations[$interface_id]);
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
2016-08-15 17:01:50 +02:00
|
|
|
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$existing_classes = [];
|
|
|
|
self::$existing_classes_ci = [];
|
|
|
|
|
|
|
|
self::$class_extends = [];
|
|
|
|
|
2016-08-15 21:14:28 +02:00
|
|
|
MethodChecker::clearCache();
|
2016-08-15 17:01:50 +02:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|