1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 02:07:37 +01:00
psalm/src/Psalm/Checker/NamespaceChecker.php

239 lines
5.5 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Checker;
2016-10-15 06:12:57 +02:00
use PhpParser\Node\Stmt\Namespace_;
2016-02-04 15:22:46 +01:00
use PhpParser;
2016-11-02 07:29:00 +01:00
use Psalm\Context;
use Psalm\StatementsSource;
class NamespaceChecker implements StatementsSource
{
2016-10-15 06:12:57 +02:00
/**
* @var Namespace_
*/
protected $namespace;
2016-10-15 06:12:57 +02:00
/**
* @var string
*/
protected $namespace_name;
2016-10-15 06:12:57 +02:00
/**
* @var array
*/
protected $declared_classes = [];
2016-10-15 06:12:57 +02:00
/**
* @var array
*/
protected $aliased_classes = [];
2016-10-15 06:12:57 +02:00
/**
* @var string
*/
protected $file_name;
2016-10-15 06:12:57 +02:00
/**
* @var string|null
*/
2016-08-25 01:00:44 +02:00
protected $include_file_name;
/**
* @var array
*/
protected $suppressed_issues;
2016-11-02 07:29:00 +01:00
/**
* @param Namespace_ $namespace
* @param StatementsSource $source
*/
2016-10-15 06:12:57 +02:00
public function __construct(Namespace_ $namespace, StatementsSource $source)
{
$this->namespace = $namespace;
2016-10-15 06:12:57 +02:00
$this->namespace_name = $this->namespace->name ? implode('\\', $this->namespace->name->parts) : '';
$this->file_name = $source->getFileName();
2016-08-25 01:00:44 +02:00
$this->include_file_name = $source->getIncludeFileName();
$this->suppressed_issues = $source->getSuppressedIssues();
}
2016-11-02 07:29:00 +01:00
/**
* @param bool $check_classes
* @param bool $check_class_statements
* @return array
*/
public function check($check_classes = true, $check_class_statements = true)
{
$leftover_stmts = [];
foreach ($this->namespace->stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassLike) {
2016-11-08 01:16:51 +01:00
$fq_class_name = ClassLikeChecker::getFQCLNFromString($stmt->name, $this->namespace_name, []);
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
$this->declared_classes[$fq_class_name] = 1;
if ($check_classes) {
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($fq_class_name)
?: new ClassChecker($stmt, $this, $fq_class_name);
2016-11-02 07:29:00 +01:00
$class_checker->check($check_class_statements);
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
if ($check_classes) {
2016-11-02 07:29:00 +01:00
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name)
?: new InterfaceChecker($stmt, $this, $fq_class_name);
2016-11-08 01:16:51 +01:00
$this->declared_classes[] = $class_checker->getFQCLN();
$class_checker->check(false);
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
if ($check_classes) {
// register the trait checker
ClassLikeChecker::getClassLikeCheckerFromClass($fq_class_name)
?: new TraitChecker($stmt, $this, $fq_class_name);
}
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
foreach ($stmt->uses as $use) {
$this->aliased_classes[strtolower($use->alias)] = implode('\\', $use->name->parts);
}
2016-11-02 07:29:00 +01:00
} else {
$leftover_stmts[] = $stmt;
}
}
if ($leftover_stmts) {
$statments_checker = new StatementsChecker($this);
$context = new Context($this->file_name);
$statments_checker->check($leftover_stmts, $context);
}
return $this->aliased_classes;
}
/**
* Gets a list of the classes declared
2016-11-02 07:29:00 +01:00
*
* @return array<string>
*/
public function getDeclaredClasses()
{
return array_keys($this->declared_classes);
}
2016-11-02 07:29:00 +01:00
/**
* @param string $class_name
* @return bool
*/
public function containsClass($class_name)
{
return isset($this->declared_classes[$class_name]);
}
2016-11-02 07:29:00 +01:00
/**
* @return string
*/
public function getNamespace()
{
return $this->namespace_name;
}
2016-11-02 07:29:00 +01:00
/**
* @return array
*/
public function getAliasedClasses()
{
return $this->aliased_classes;
}
2016-04-27 00:42:48 +02:00
/**
* @return null
*/
2016-11-08 01:16:51 +01:00
public function getFQCLN()
{
return null;
}
2016-04-27 00:42:48 +02:00
/**
* @return null
*/
public function getClassName()
{
return null;
}
2016-04-27 00:42:48 +02:00
/**
* @return null
*/
public function getClassLikeChecker()
{
return null;
}
2016-04-27 00:42:48 +02:00
/**
* @return string|null
2016-04-27 00:42:48 +02:00
*/
public function getParentClass()
{
return null;
}
2016-11-02 07:29:00 +01:00
/**
* @return string
*/
public function getFileName()
{
return $this->file_name;
}
2016-11-02 07:29:00 +01:00
/**
* @return null|string
*/
2016-08-25 01:00:44 +02:00
public function getIncludeFileName()
{
return $this->include_file_name;
}
/**
* @param string|null $file_name
2016-11-02 07:29:00 +01:00
* @return void
*/
2016-08-25 01:00:44 +02:00
public function setIncludeFileName($file_name)
{
$this->include_file_name = $file_name;
}
2016-11-02 07:29:00 +01:00
/**
* @return string
*/
2016-08-25 01:00:44 +02:00
public function getCheckedFileName()
{
return $this->include_file_name ?: $this->file_name;
}
2016-04-27 00:42:48 +02:00
/**
* @return bool
*/
public function isStatic()
{
return false;
}
2016-11-02 07:29:00 +01:00
/**
* @return null
*/
public function getSource()
{
return null;
}
/**
* Get a list of suppressed issues
2016-11-02 07:29:00 +01:00
*
* @return array<string>
*/
public function getSuppressedIssues()
{
return $this->suppressed_issues;
}
}