1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 02:47:02 +01:00
psalm/src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php

136 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Issue\UndefinedInterface;
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class InterfaceAnalyzer extends ClassLikeAnalyzer
{
2016-10-15 06:12:57 +02:00
/**
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
* @param PhpParser\Node\Stmt\Interface_ $interface
2017-01-09 05:58:06 +01:00
* @param string $fq_interface_name
2016-10-15 06:12:57 +02:00
*/
2018-11-06 03:57:36 +01:00
public function __construct(PhpParser\Node\Stmt\Interface_ $interface, SourceAnalyzer $source, $fq_interface_name)
2016-08-08 20:36:18 +02:00
{
2017-01-09 05:58:06 +01:00
parent::__construct($interface, $source, $fq_interface_name);
}
/**
* @return void
*/
public function analyze()
{
if (!$this->class instanceof PhpParser\Node\Stmt\Interface_) {
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);
} catch (\InvalidArgumentException $e) {
continue;
}
if (!$extended_interface_storage->is_interface) {
$code_location = new CodeLocation(
$this,
$extended_interface
);
if (\Psalm\IssueBuffer::accepts(
new UndefinedInterface(
$extended_interface_name . ' is not an interface',
$code_location,
$extended_interface_name
),
$this->getSuppressedIssues()
)) {
// fall through
}
}
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) {
throw new \UnexpectedValueException('bad');
}
$class_storage = $codebase->classlike_storage_provider->get($fq_interface_name);
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);
$type_provider = new \Psalm\Internal\Provider\NodeDataProvider();
$method_analyzer->analyze(new \Psalm\Context($this->getFQCLN()), $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) {
\Psalm\IssueBuffer::add(
new \Psalm\Issue\ParseError(
'Interfaces cannot have properties',
new CodeLocation($this, $stmt)
)
);
return;
2019-01-19 18:19:51 +01:00
}
}
}
}