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;
|
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-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
|
2017-01-09 05:58:06 +01:00
|
|
|
* @param string $fq_interface_name
|
2016-10-15 06:12:57 +02:00
|
|
|
*/
|
2017-01-09 05:58:06 +01:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $interface, StatementsSource $source, $fq_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');
|
|
|
|
}
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
parent::__construct($interface, $source, $fq_interface_name);
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$fq_interface_name_lower = strtolower($fq_interface_name);
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[$fq_interface_name_lower];
|
|
|
|
|
|
|
|
$project_checker = $source->getFileChecker()->project_checker;
|
2017-01-19 16:55:58 +01:00
|
|
|
$project_checker->addFullyQualifiedInterfaceName($fq_interface_name, $source->getFilePath());
|
2016-08-14 01:44:24 +02:00
|
|
|
|
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
|
|
|
);
|
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->parent_interfaces[] = $extended_interface_name;
|
2016-10-25 00:49:07 +02:00
|
|
|
}
|
2016-08-14 01:44:24 +02:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
2017-01-09 05:58:06 +01:00
|
|
|
* @param string $fq_interface_name
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param FileChecker $file_checker
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-05-25 04:07:49 +02:00
|
|
|
* @return bool
|
2016-10-15 06:12:57 +02:00
|
|
|
*/
|
2017-02-01 05:24:33 +01:00
|
|
|
public static function interfaceExists($fq_interface_name, FileChecker $file_checker)
|
2016-08-13 20:20:46 +02:00
|
|
|
{
|
2017-01-09 07:48:55 +01:00
|
|
|
if (isset(self::$SPECIAL_TYPES[strtolower($fq_interface_name)])) {
|
2016-08-14 00:54:49 +02:00
|
|
|
return false;
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($file_checker->evaluateClassLike($fq_interface_name) === false) {
|
2017-01-02 01:09:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
return $file_checker->project_checker->hasFullyQualifiedInterfaceName($fq_interface_name);
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
2017-01-09 05:58:06 +01:00
|
|
|
* @param string $fq_interface_name
|
|
|
|
* @param FileChecker $file_checker
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-05-25 04:07:49 +02:00
|
|
|
* @return bool
|
2016-10-15 06:12:57 +02:00
|
|
|
*/
|
2017-01-09 05:58:06 +01:00
|
|
|
public static function hasCorrectCasing($fq_interface_name, FileChecker $file_checker)
|
2016-08-14 00:54:49 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
return isset($file_checker->project_checker->existing_interfaces[$fq_interface_name]);
|
2016-08-14 00:54:49 +02:00
|
|
|
}
|
2016-08-15 17:01:50 +02:00
|
|
|
|
2016-10-30 17:46:18 +01:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $interface_name
|
|
|
|
* @param string $possible_parent
|
2017-03-11 01:36:17 +01:00
|
|
|
* @param FileChecker $file_checker
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-05-25 04:07:49 +02:00
|
|
|
* @return bool
|
2016-10-30 17:46:18 +01:00
|
|
|
*/
|
2017-03-11 01:36:17 +01:00
|
|
|
public static function interfaceExtends($interface_name, $possible_parent, FileChecker $file_checker)
|
2016-10-25 17:20:06 +02:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
return in_array($possible_parent, self::getParentInterfaces($interface_name, $file_checker), true);
|
2016-10-25 17:20:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-25 00:49:07 +02:00
|
|
|
/**
|
2017-01-09 05:58:06 +01:00
|
|
|
* @param string $fq_interface_name
|
2017-03-11 01:36:17 +01:00
|
|
|
* @param FileChecker $file_checker
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-25 00:49:07 +02:00
|
|
|
* @return array<string> all interfaces extended by $interface_name
|
|
|
|
*/
|
2017-03-11 01:36:17 +01:00
|
|
|
public static function getParentInterfaces($fq_interface_name, FileChecker $file_checker)
|
2016-10-25 00:49:07 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01: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-31 02:05:32 +01:00
|
|
|
}
|
2016-10-25 00:49:07 +02:00
|
|
|
|
|
|
|
$extended_interfaces = [];
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[$fq_interface_name];
|
2016-12-30 18:41:14 +01:00
|
|
|
|
|
|
|
foreach ($storage->parent_interfaces as $extended_interface_name) {
|
2016-10-25 00:49:07 +02:00
|
|
|
$extended_interfaces[] = $extended_interface_name;
|
|
|
|
|
2017-03-11 01:36:17 +01:00
|
|
|
if (!self::interfaceExists($extended_interface_name, $file_checker)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-25 00:49:07 +02:00
|
|
|
$extended_interfaces = array_merge(
|
2017-03-11 01:36:17 +01:00
|
|
|
self::getParentInterfaces($extended_interface_name, $file_checker),
|
2016-10-25 00:49:07 +02:00
|
|
|
$extended_interfaces
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extended_interfaces;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|