1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00
psalm/src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php

148 lines
4.8 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer;
2021-12-03 21:40:18 +01:00
use InvalidArgumentException;
use LogicException;
use PhpParser;
use Psalm\CodeLocation;
2021-12-03 20:11:20 +01:00
use Psalm\Context;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Issue\ParseError;
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;
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class InterfaceAnalyzer extends ClassLikeAnalyzer
{
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);
}
public function analyze(): void
{
if (!$this->class instanceof PhpParser\Node\Stmt\Interface_) {
2021-12-03 21:40:18 +01:00
throw new LogicException('Something went badly wrong');
}
$project_analyzer = $this->file_analyzer->project_analyzer;
$codebase = $project_analyzer->getCodebase();
$config = $project_analyzer->getConfig();
if ($this->class->extends) {
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(
$extended_interface_name,
$parent_reference_location
)) {
// we should not normally get here
return;
}
try {
$extended_interface_storage = $codebase->classlike_storage_provider->get($extended_interface_name);
2021-12-03 21:40:18 +01:00
} catch (InvalidArgumentException $e) {
continue;
}
if (!$extended_interface_storage->is_interface) {
$code_location = new CodeLocation(
$this,
$extended_interface
);
2021-12-03 20:11:20 +01:00
IssueBuffer::maybeAdd(
new UndefinedInterface(
$extended_interface_name . ' is not an interface',
$code_location,
$extended_interface_name
),
$this->getSuppressedIssues()
);
}
2019-02-24 07:33:25 +01:00
if ($codebase->store_node_types && $extended_interface_name) {
$bounds = $parent_reference_location->getSelectionBounds();
$codebase->analyzer->addOffsetReference(
$this->getFilePath(),
$bounds[0],
$bounds[1],
$extended_interface_name
);
}
}
}
2019-01-19 18:19:51 +01:00
$fq_interface_name = $this->getFQCLN();
if (!$fq_interface_name) {
2021-12-03 21:40:18 +01:00
throw new UnexpectedValueException('bad');
}
$class_storage = $codebase->classlike_storage_provider->get($fq_interface_name);
$interface_context = new Context($this->getFQCLN());
2022-02-24 01:50:05 +01:00
AttributesAnalyzer::analyze(
$this,
$interface_context,
$class_storage,
$this->class->attrGroups,
2022-03-14 20:15:07 +01:00
AttributesAnalyzer::TARGET_CLASS,
2022-02-24 01:50:05 +01:00
$class_storage->suppressed_issues + $this->getSuppressedIssues()
);
2020-11-22 07:15:52 +01:00
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();
$method_analyzer->analyze($interface_context, $type_provider);
$actual_method_id = $method_analyzer->getMethodId();
if ($stmt->name->name !== '__construct'
&& $config->reportIssueInFile('InvalidReturnType', $this->getFilePath())
) {
ClassAnalyzer::analyzeClassMethodReturnType(
$stmt,
$method_analyzer,
$this,
$type_provider,
$codebase,
$class_storage,
$fq_interface_name,
$actual_method_id,
$actual_method_id,
false
);
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
2021-12-03 20:11:20 +01:00
IssueBuffer::add(
new ParseError(
'Interfaces cannot have properties',
new CodeLocation($this, $stmt)
)
);
return;
2019-01-19 18:19:51 +01:00
}
}
}
}