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

110 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Checker;
use PhpParser;
use Psalm\StatementsSource;
class InterfaceChecker extends ClassLikeChecker
{
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
2017-01-08 23:58:06 -05:00
* @param string $fq_interface_name
2016-10-15 00:12:57 -04:00
*/
2017-01-08 23:58:06 -05:00
public function __construct(PhpParser\Node\Stmt\ClassLike $interface, StatementsSource $source, $fq_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');
}
2017-01-08 23:58:06 -05:00
parent::__construct($interface, $source, $fq_interface_name);
2017-01-08 23:58:06 -05:00
$fq_interface_name_lower = strtolower($fq_interface_name);
2017-01-08 23:58:06 -05:00
$storage = self::$storage[$fq_interface_name_lower];
$project_checker = $source->getFileChecker()->project_checker;
$project_checker->addFullyQualifiedInterfaceName($fq_interface_name);
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,
2017-01-07 14:35:07 -05:00
$this
2016-11-02 02:29:00 -04:00
);
2017-01-08 23:58:06 -05:00
$source->getFileChecker()->evaluateClassLike($extended_interface_name, false);
$storage->parent_interfaces[] = $extended_interface_name;
2016-10-24 18:49:07 -04:00
}
}
}
2016-10-15 00:12:57 -04:00
/**
2017-01-08 23:58:06 -05:00
* @param string $fq_interface_name
* @param FileChecker $file_checker
2017-01-08 23:58:06 -05:00
* @param bool $visit_file
2016-10-15 00:12:57 -04:00
* @return boolean
*/
2017-01-08 23:58:06 -05:00
public static function interfaceExists($fq_interface_name, FileChecker $file_checker, $visit_file = false)
{
2017-01-08 23:58:06 -05:00
if (isset(self::$SPECIAL_TYPES[$fq_interface_name])) {
return false;
}
2017-01-08 23:58:06 -05:00
if ($file_checker->evaluateClassLike($fq_interface_name, $visit_file) === false) {
return false;
}
2017-01-08 23:58:06 -05:00
return $file_checker->project_checker->hasFullyQualifiedInterfaceName($fq_interface_name);
}
2016-10-15 00:12:57 -04:00
/**
2017-01-08 23:58:06 -05:00
* @param string $fq_interface_name
* @param FileChecker $file_checker
2016-10-15 00:12:57 -04:00
* @return boolean
*/
2017-01-08 23:58:06 -05:00
public static function hasCorrectCasing($fq_interface_name, FileChecker $file_checker)
{
2017-01-08 23:58:06 -05:00
return isset($file_checker->project_checker->existing_interfaces[$fq_interface_name]);
}
2016-08-15 11:01:50 -04:00
2016-10-30 12:46:18 -04:00
/**
* @param string $interface_name
* @param string $possible_parent
2016-10-30 12:46:18 -04:00
* @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
/**
2017-01-08 23:58:06 -05:00
* @param string $fq_interface_name
2016-10-24 18:49:07 -04:00
* @return array<string> all interfaces extended by $interface_name
*/
2017-01-08 23:58:06 -05:00
public static function getParentInterfaces($fq_interface_name)
2016-10-24 18:49:07 -04:00
{
2017-01-08 23:58:06 -05:00
$fq_interface_name = strtolower($fq_interface_name);
if (!isset(self::$storage[$fq_interface_name])) {
throw new \UnexpectedValueException('Invalid storage for ' . $fq_interface_name);
2016-12-30 20:05:32 -05:00
}
2016-10-24 18:49:07 -04:00
$extended_interfaces = [];
2017-01-08 23:58:06 -05:00
$storage = self::$storage[$fq_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;
}
}