2016-05-16 05:06:03 +02:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2021-12-03 21:40:18 +01:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use LogicException;
|
2016-05-16 05:06:03 +02:00
|
|
|
use PhpParser;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\CodeLocation;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Internal\Provider\NodeDataProvider;
|
|
|
|
use Psalm\Issue\ParseError;
|
2019-03-07 17:16:40 +01:00
|
|
|
use Psalm\Issue\UndefinedInterface;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2021-12-03 21:40:18 +01:00
|
|
|
use UnexpectedValueException;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class InterfaceAnalyzer extends ClassLikeAnalyzer
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2020-09-07 01:36:47 +02:00
|
|
|
public function __construct(
|
|
|
|
PhpParser\Node\Stmt\Interface_ $interface,
|
|
|
|
SourceAnalyzer $source,
|
|
|
|
string $fq_interface_name
|
|
|
|
) {
|
2017-01-09 05:58:06 +01:00
|
|
|
parent::__construct($interface, $source, $fq_interface_name);
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:02:52 +02:00
|
|
|
public function analyze(): void
|
2018-01-14 19:08:24 +01:00
|
|
|
{
|
|
|
|
if (!$this->class instanceof PhpParser\Node\Stmt\Interface_) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new LogicException('Something went badly wrong');
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
$project_analyzer = $this->file_analyzer->project_analyzer;
|
|
|
|
$codebase = $project_analyzer->getCodebase();
|
|
|
|
$config = $project_analyzer->getConfig();
|
2018-11-02 04:40:36 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
if ($this->class->extends) {
|
2018-01-14 19:08:24 +01:00
|
|
|
foreach ($this->class->extends as $extended_interface) {
|
|
|
|
$extended_interface_name = self::getFQCLNFromNameObject(
|
|
|
|
$extended_interface,
|
|
|
|
$this->getAliases()
|
|
|
|
);
|
|
|
|
|
|
|
|
$parent_reference_location = new CodeLocation($this, $extended_interface);
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (!$codebase->classOrInterfaceExists(
|
2018-01-14 19:08:24 +01:00
|
|
|
$extended_interface_name,
|
|
|
|
$parent_reference_location
|
|
|
|
)) {
|
|
|
|
// we should not normally get here
|
|
|
|
return;
|
|
|
|
}
|
2018-11-02 04:40:36 +01:00
|
|
|
|
2019-03-07 17:16:40 +01:00
|
|
|
try {
|
|
|
|
$extended_interface_storage = $codebase->classlike_storage_provider->get($extended_interface_name);
|
2021-12-03 21:40:18 +01:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2019-03-07 17:16:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$extended_interface_storage->is_interface) {
|
|
|
|
$code_location = new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$extended_interface
|
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2019-03-07 17:16:40 +01:00
|
|
|
new UndefinedInterface(
|
|
|
|
$extended_interface_name . ' is not an interface',
|
2019-06-06 20:48:33 +02:00
|
|
|
$code_location,
|
|
|
|
$extended_interface_name
|
2019-03-07 17:16:40 +01:00
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
2019-03-07 17:16:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types && $extended_interface_name) {
|
2018-11-02 04:40:36 +01:00
|
|
|
$bounds = $parent_reference_location->getSelectionBounds();
|
|
|
|
|
|
|
|
$codebase->analyzer->addOffsetReference(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$bounds[0],
|
|
|
|
$bounds[1],
|
|
|
|
$extended_interface_name
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-19 18:19:51 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
$fq_interface_name = $this->getFQCLN();
|
|
|
|
|
|
|
|
if (!$fq_interface_name) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException('bad');
|
2019-07-29 01:03:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_interface_name);
|
|
|
|
|
2020-11-22 07:15:52 +01:00
|
|
|
foreach ($class_storage->attributes as $attribute) {
|
|
|
|
AttributeAnalyzer::analyze(
|
|
|
|
$this,
|
|
|
|
$attribute,
|
|
|
|
$class_storage->suppressed_issues + $this->getSuppressedIssues(),
|
|
|
|
1,
|
|
|
|
$class_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-19 18:19:51 +01:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
|
|
|
$method_analyzer = new MethodAnalyzer($stmt, $this);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$type_provider = new NodeDataProvider();
|
2019-11-25 17:44:54 +01:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$method_analyzer->analyze(new Context($this->getFQCLN()), $type_provider);
|
2019-07-29 01:03:12 +02:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$actual_method_id = $method_analyzer->getMethodId();
|
2019-07-29 01:03:12 +02:00
|
|
|
|
|
|
|
if ($stmt->name->name !== '__construct'
|
|
|
|
&& $config->reportIssueInFile('InvalidReturnType', $this->getFilePath())
|
|
|
|
) {
|
|
|
|
ClassAnalyzer::analyzeClassMethodReturnType(
|
|
|
|
$stmt,
|
|
|
|
$method_analyzer,
|
|
|
|
$this,
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider,
|
2019-07-29 01:03:12 +02:00
|
|
|
$codebase,
|
|
|
|
$class_storage,
|
|
|
|
$fq_interface_name,
|
|
|
|
$actual_method_id,
|
2020-03-29 05:37:42 +02:00
|
|
|
$actual_method_id,
|
|
|
|
false
|
2019-07-29 01:03:12 +02:00
|
|
|
);
|
2019-07-29 01:44:36 +02:00
|
|
|
}
|
2019-05-23 16:21:56 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
2021-12-03 20:11:20 +01:00
|
|
|
IssueBuffer::add(
|
|
|
|
new ParseError(
|
2019-05-23 16:21:56 +02:00
|
|
|
'Interfaces cannot have properties',
|
|
|
|
new CodeLocation($this, $stmt)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
2019-01-19 18:19:51 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|