1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-05 21:19:03 +01:00
psalm/src/Psalm/Checker/InterfaceChecker.php

117 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Checker;
use PhpParser;
use Psalm\StatementsSource;
class InterfaceChecker extends ClassLikeChecker
{
2016-10-15 06:12:57 +02:00
/**
2016-11-05 02:14:04 +01:00
* @param PhpParser\Node\Stmt\ClassLike $interface
2016-10-15 06:12:57 +02:00
* @param StatementsSource $source
* @param string $interface_name
*/
2016-11-05 02:14:04 +01:00
public function __construct(PhpParser\Node\Stmt\ClassLike $interface, StatementsSource $source, $interface_name)
2016-08-08 20:36:18 +02:00
{
2016-11-05 02:14:04 +01: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 17:51:19 +01:00
if ($interface->extends) {
2016-10-25 00:49:07 +02:00
foreach ($interface->extends as $extended_interface) {
2016-11-08 01:16:51 +01:00
$extended_interface_name = self::getFQCLNFromNameObject(
2016-11-02 07:29:00 +01:00
$extended_interface,
2017-01-07 20:35:07 +01:00
$this
2016-11-02 07:29:00 +01:00
);
$source->getFileChecker()->evaluateClassLike($extended_interface_name);
$storage->parent_interfaces[] = $extended_interface_name;
2016-10-25 00:49:07 +02:00
}
}
}
2016-10-15 06:12:57 +02:00
/**
* @param string $interface
* @param FileChecker $file_checker
2016-10-15 06:12:57 +02:00
* @return boolean
*/
public static function interfaceExists($interface, FileChecker $file_checker)
{
if (isset(self::$SPECIAL_TYPES[$interface])) {
return false;
}
if ($file_checker->evaluateClassLike($interface) === false) {
return false;
}
if (isset(self::$existing_interfaces_ci[strtolower($interface)])) {
return self::$existing_interfaces_ci[strtolower($interface)];
}
2016-08-08 20:36:18 +02: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;
}
return true;
}
2016-10-15 06:12:57 +02:00
/**
* @param string $interface_name
2016-10-15 06:12:57 +02:00
* @return boolean
*/
public static function hasCorrectCasing($interface_name)
{
if (!isset(self::$existing_interfaces_ci[strtolower($interface_name)])) {
throw new \UnexpectedValueException('Invalid storage for ' . $interface_name);
}
return isset(self::$existing_interfaces[$interface_name]);
}
2016-08-15 17:01:50 +02:00
2016-10-30 17:46:18 +01:00
/**
* @param string $interface_name
* @param string $possible_parent
2016-10-30 17:46:18 +01:00
* @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
2016-10-25 00:49:07 +02:00
* @return array<string> all interfaces extended by $interface_name
*/
public static function getParentInterfaces($interface_name)
{
if (!isset(self::$storage[$interface_name])) {
throw new \UnexpectedValueException('Invalid storage for ' . $interface_name);
2016-12-31 02:05:32 +01:00
}
2016-10-25 00:49:07 +02:00
$extended_interfaces = [];
$storage = self::$storage[$interface_name];
foreach ($storage->parent_interfaces as $extended_interface_name) {
2016-10-25 00:49:07 +02:00
$extended_interfaces[] = $extended_interface_name;
$extended_interfaces = array_merge(
self::getParentInterfaces($extended_interface_name),
$extended_interfaces
);
}
return $extended_interfaces;
}
}