1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 22:01:48 +01:00
psalm/src/Psalm/Checker/InterfaceChecker.php

143 lines
4.0 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Checker;
use PhpParser;
use Psalm\StatementsSource;
class InterfaceChecker extends ClassLikeChecker
{
2016-11-02 02:29:00 -04:00
/**
* @var array<string, bool>
*/
protected static $existing_interfaces = [];
2016-11-01 00:39:41 -04:00
2016-11-02 02:29:00 -04:00
/**
* @var array<string, bool>
*/
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');
}
parent::__construct($interface, $source, $interface_name);
$storage = self::$storage[$interface_name];
self::$existing_interfaces[$interface_name] = true;
self::$existing_interfaces_ci[strtolower($interface_name)] = true;
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
);
$storage->parent_interfaces[] = $extended_interface_name;
2016-10-24 18:49:07 -04:00
}
}
}
2016-10-15 00:12:57 -04:00
/**
* @param string $interface
* @return boolean
*/
public static function interfaceExists($interface)
{
if (isset(self::$existing_interfaces_ci[strtolower($interface)])) {
return self::$existing_interfaces_ci[strtolower($interface)];
}
if (in_array($interface, self::$SPECIAL_TYPES)) {
return false;
}
2016-12-30 20:05:32 -05:00
$old_level = error_reporting();
error_reporting(0);
$interface_exists = interface_exists($interface, true);
error_reporting($old_level);
if ($interface_exists) {
$reflected_interface = new \ReflectionClass($interface);
self::$existing_interfaces_ci[strtolower($interface)] = true;
self::$existing_interfaces[$reflected_interface->getName()] = true;
return true;
}
2016-08-08 14:36:18 -04:00
self::$existing_interfaces_ci[strtolower($interface)] = false;
self::$existing_interfaces_ci[$interface] = false;
return false;
}
2016-10-15 00:12:57 -04:00
/**
* @param string $interface
* @return boolean
*/
public static function hasCorrectCasing($interface)
{
if (!self::interfaceExists(strtolower($interface))) {
throw new \InvalidArgumentException('Cannot check casing on nonexistent class ' . $interface);
}
return isset(self::$existing_interfaces[$interface]);
}
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)
{
2016-12-30 20:05:32 -05:00
if (self::registerClass($interface_name) === false) {
throw new \UnexpectedValueException('Cannot deal with unfound file');
}
2016-10-24 18:49:07 -04:00
$extended_interfaces = [];
$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 = [];
}
}