2016-05-16 05:06:03 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
|
|
|
use PhpParser;
|
2016-08-14 00:54:49 +02:00
|
|
|
use Psalm\StatementsSource;
|
|
|
|
use Psalm\Context;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
class InterfaceChecker extends ClassLikeChecker
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2016-10-25 00:49:07 +02:00
|
|
|
/**
|
|
|
|
* @var array<string,array<string>>
|
|
|
|
*/
|
|
|
|
protected static $parent_interfaces = [];
|
2016-08-14 01:44:24 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $existing_interfaces = [];
|
|
|
|
protected static $existing_interfaces_ci = [];
|
2016-08-08 20:36:18 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Interface_ $interface
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param string $interface_name
|
|
|
|
*/
|
2016-08-22 22:41:45 +02:00
|
|
|
public function __construct(PhpParser\Node\Stmt\Interface_ $interface, StatementsSource $source, $interface_name)
|
2016-08-08 20:36:18 +02:00
|
|
|
{
|
2016-08-22 22:41:45 +02:00
|
|
|
parent::__construct($interface, $source, $interface_name);
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
self::$existing_interfaces[$interface_name] = true;
|
|
|
|
self::$existing_interfaces_ci[strtolower($interface_name)] = true;
|
2016-08-14 01:44:24 +02:00
|
|
|
|
2016-10-25 00:49:07 +02:00
|
|
|
if ($interface->extends) {
|
|
|
|
self::$parent_interfaces[$interface_name] = [];
|
|
|
|
|
|
|
|
foreach ($interface->extends as $extended_interface) {
|
|
|
|
$extended_interface_name = self::getAbsoluteClassFromName($extended_interface, $this->namespace, $this->aliased_classes);
|
|
|
|
self::$parent_interfaces[$interface_name][] = $extended_interface_name;
|
|
|
|
}
|
2016-08-14 01:44:24 +02:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $interface
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-08-22 22:41:45 +02:00
|
|
|
public static function interfaceExists($interface)
|
2016-08-13 20:20:46 +02:00
|
|
|
{
|
2016-08-22 22:41:45 +02:00
|
|
|
if (isset(self::$existing_interfaces_ci[strtolower($interface)])) {
|
|
|
|
return self::$existing_interfaces_ci[strtolower($interface)];
|
2016-08-14 00:54:49 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
if (in_array($interface, self::$SPECIAL_TYPES)) {
|
2016-08-14 00:54:49 +02:00
|
|
|
return false;
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
if (interface_exists($interface, true)) {
|
|
|
|
$reflected_interface = new \ReflectionClass($interface);
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
self::$existing_interfaces_ci[strtolower($interface)] = true;
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$existing_interfaces[$reflected_interface->getName()] = true;
|
2016-08-13 20:20:46 +02:00
|
|
|
return true;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-08-08 20:36:18 +02:00
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
self::$existing_interfaces_ci[strtolower($interface)] = false;
|
|
|
|
self::$existing_interfaces_ci[$interface] = false;
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
return false;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $interface
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-08-22 22:41:45 +02:00
|
|
|
public static function hasCorrectCasing($interface)
|
2016-08-14 00:54:49 +02:00
|
|
|
{
|
2016-08-22 22:41:45 +02:00
|
|
|
if (!self::interfaceExists(strtolower($interface))) {
|
|
|
|
throw new \InvalidArgumentException('Cannot check casing on nonexistent class ' . $interface);
|
2016-08-14 00:54:49 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 22:41:45 +02:00
|
|
|
return isset(self::$existing_interfaces[$interface]);
|
2016-08-14 00:54:49 +02:00
|
|
|
}
|
2016-08-15 17:01:50 +02:00
|
|
|
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$existing_interfaces = [];
|
|
|
|
self::$existing_interfaces_ci = [];
|
|
|
|
}
|
2016-10-25 00:49:07 +02:00
|
|
|
|
2016-10-30 17:46:18 +01:00
|
|
|
/**
|
|
|
|
* @param string $interface_name
|
|
|
|
* @param string $possible_parent
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-25 17:20:06 +02:00
|
|
|
public static function interfaceExtends($interface_name, $possible_parent)
|
|
|
|
{
|
|
|
|
return in_array($possible_parent, self::getParentInterfaces($interface_name));
|
|
|
|
}
|
|
|
|
|
2016-10-25 00:49:07 +02:00
|
|
|
/**
|
|
|
|
* @param string $interface_name
|
|
|
|
* @return array<string> all interfaces extended by $interface_name
|
|
|
|
*/
|
|
|
|
public static function getParentInterfaces($interface_name)
|
|
|
|
{
|
|
|
|
self::registerClass($interface_name);
|
|
|
|
|
|
|
|
$extended_interfaces = [];
|
|
|
|
|
|
|
|
if (!isset(self::$parent_interfaces[$interface_name])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (self::$parent_interfaces[$interface_name] as $extended_interface_name) {
|
|
|
|
$extended_interfaces[] = $extended_interface_name;
|
|
|
|
|
|
|
|
$extended_interfaces = array_merge(
|
|
|
|
self::getParentInterfaces($extended_interface_name),
|
|
|
|
$extended_interfaces
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extended_interfaces;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|