2016-05-15 23:06:03 -04:00
|
|
|
<?php
|
2016-08-13 14:20:46 -04:00
|
|
|
namespace Psalm\Checker;
|
2016-05-15 23:06:03 -04:00
|
|
|
|
|
|
|
use PhpParser;
|
2016-08-13 18:54:49 -04:00
|
|
|
use Psalm\StatementsSource;
|
2016-05-15 23:06:03 -04:00
|
|
|
|
2016-08-13 14:20:46 -04:00
|
|
|
class InterfaceChecker extends ClassLikeChecker
|
2016-05-15 23:06:03 -04:00
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
2016-08-13 18:54:49 -04:00
|
|
|
protected static $existing_interfaces = [];
|
2016-11-01 00:39:41 -04:00
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
2016-08-13 18:54:49 -04:00
|
|
|
protected static $existing_interfaces_ci = [];
|
2016-08-08 14:36:18 -04:00
|
|
|
|
2016-10-15 00:12:57 -04:00
|
|
|
/**
|
2016-11-04 21:14:04 -04:00
|
|
|
* @param PhpParser\Node\Stmt\ClassLike $interface
|
2016-10-15 00:12:57 -04:00
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param string $interface_name
|
|
|
|
*/
|
2016-11-04 21:14:04 -04:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $interface, StatementsSource $source, $interface_name)
|
2016-08-08 14:36:18 -04:00
|
|
|
{
|
2016-11-04 21:14:04 -04:00
|
|
|
if (!$interface instanceof PhpParser\Node\Stmt\Interface_) {
|
|
|
|
throw new \InvalidArgumentException('Expecting an interface');
|
|
|
|
}
|
|
|
|
|
2016-08-22 16:41:45 -04:00
|
|
|
parent::__construct($interface, $source, $interface_name);
|
2016-08-13 14:20:46 -04:00
|
|
|
|
2016-12-30 12:41:14 -05:00
|
|
|
$storage = self::$storage[$interface_name];
|
|
|
|
|
2016-08-22 16:41:45 -04:00
|
|
|
self::$existing_interfaces[$interface_name] = true;
|
|
|
|
self::$existing_interfaces_ci[strtolower($interface_name)] = true;
|
2016-08-13 19:44:24 -04:00
|
|
|
|
2016-11-20 11:51:19 -05:00
|
|
|
if ($interface->extends) {
|
2016-10-24 18:49:07 -04:00
|
|
|
foreach ($interface->extends as $extended_interface) {
|
2016-11-07 19:16:51 -05:00
|
|
|
$extended_interface_name = self::getFQCLNFromNameObject(
|
2016-11-02 02:29:00 -04:00
|
|
|
$extended_interface,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
|
|
|
|
2016-12-30 12:41:14 -05:00
|
|
|
$storage->parent_interfaces[] = $extended_interface_name;
|
2016-10-24 18:49:07 -04:00
|
|
|
}
|
2016-08-13 19:44:24 -04:00
|
|
|
}
|
2016-08-13 14:20:46 -04:00
|
|
|
}
|
|
|
|
|
2016-10-15 00:12:57 -04:00
|
|
|
/**
|
|
|
|
* @param string $interface
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-08-22 16:41:45 -04:00
|
|
|
public static function interfaceExists($interface)
|
2016-08-13 14:20:46 -04:00
|
|
|
{
|
2016-08-22 16:41:45 -04:00
|
|
|
if (isset(self::$existing_interfaces_ci[strtolower($interface)])) {
|
|
|
|
return self::$existing_interfaces_ci[strtolower($interface)];
|
2016-08-13 18:54:49 -04:00
|
|
|
}
|
|
|
|
|
2016-08-22 16:41:45 -04:00
|
|
|
if (in_array($interface, self::$SPECIAL_TYPES)) {
|
2016-08-13 18:54:49 -04:00
|
|
|
return false;
|
2016-08-13 14:20:46 -04:00
|
|
|
}
|
|
|
|
|
2017-01-01 19:09:17 -05:00
|
|
|
if (self::registerClassLike($interface) === false) {
|
|
|
|
self::$existing_interfaces_ci[strtolower($interface)] = false;
|
2016-12-30 20:05:32 -05:00
|
|
|
|
2017-01-01 19:09:17 -05:00
|
|
|
return false;
|
2016-05-15 23:06:03 -04:00
|
|
|
}
|
2016-08-08 14:36:18 -04:00
|
|
|
|
2017-01-01 19:09:17 -05:00
|
|
|
if (!isset(self::$existing_interfaces_ci[strtolower($interface)])) {
|
|
|
|
// it exists, but it's not an interface
|
|
|
|
self::$existing_interfaces_ci[strtolower($interface)] = false;
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-13 18:54:49 -04:00
|
|
|
|
2017-01-01 19:09:17 -05:00
|
|
|
return true;
|
2016-05-15 23:06:03 -04:00
|
|
|
}
|
2016-08-13 18:54:49 -04:00
|
|
|
|
2016-10-15 00:12:57 -04:00
|
|
|
/**
|
|
|
|
* @param string $interface
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-08-22 16:41:45 -04:00
|
|
|
public static function hasCorrectCasing($interface)
|
2016-08-13 18:54:49 -04:00
|
|
|
{
|
2016-08-22 16:41:45 -04:00
|
|
|
if (!self::interfaceExists(strtolower($interface))) {
|
|
|
|
throw new \InvalidArgumentException('Cannot check casing on nonexistent class ' . $interface);
|
2016-08-13 18:54:49 -04:00
|
|
|
}
|
|
|
|
|
2016-08-22 16:41:45 -04:00
|
|
|
return isset(self::$existing_interfaces[$interface]);
|
2016-08-13 18:54:49 -04:00
|
|
|
}
|
2016-08-15 11:01:50 -04:00
|
|
|
|
2016-10-30 12:46:18 -04:00
|
|
|
/**
|
|
|
|
* @param string $interface_name
|
|
|
|
* @param string $possible_parent
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-25 11:20:06 -04:00
|
|
|
public static function interfaceExtends($interface_name, $possible_parent)
|
|
|
|
{
|
|
|
|
return in_array($possible_parent, self::getParentInterfaces($interface_name));
|
|
|
|
}
|
|
|
|
|
2016-10-24 18:49:07 -04:00
|
|
|
/**
|
|
|
|
* @param string $interface_name
|
|
|
|
* @return array<string> all interfaces extended by $interface_name
|
|
|
|
*/
|
|
|
|
public static function getParentInterfaces($interface_name)
|
|
|
|
{
|
2017-01-01 19:09:17 -05:00
|
|
|
if (self::registerClassLike($interface_name) === false) {
|
2016-12-30 20:05:32 -05:00
|
|
|
throw new \UnexpectedValueException('Cannot deal with unfound file');
|
|
|
|
}
|
2016-10-24 18:49:07 -04:00
|
|
|
|
|
|
|
$extended_interfaces = [];
|
|
|
|
|
2016-12-30 12:41:14 -05:00
|
|
|
$storage = self::$storage[$interface_name];
|
|
|
|
|
|
|
|
foreach ($storage->parent_interfaces as $extended_interface_name) {
|
2016-10-24 18:49:07 -04:00
|
|
|
$extended_interfaces[] = $extended_interface_name;
|
|
|
|
|
|
|
|
$extended_interfaces = array_merge(
|
|
|
|
self::getParentInterfaces($extended_interface_name),
|
|
|
|
$extended_interfaces
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extended_interfaces;
|
|
|
|
}
|
2016-11-02 02:29:00 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$existing_interfaces = [];
|
|
|
|
self::$existing_interfaces_ci = [];
|
|
|
|
}
|
2016-05-15 23:06:03 -04:00
|
|
|
}
|