2016-01-19 18:27:06 -05:00
|
|
|
<?php
|
|
|
|
|
2016-08-13 14:20:46 -04:00
|
|
|
namespace Psalm\Checker;
|
|
|
|
|
|
|
|
use Psalm\StatementsSource;
|
2016-08-13 18:54:49 -04:00
|
|
|
use Psalm\Context;
|
2016-01-19 18:27:06 -05:00
|
|
|
|
2016-02-04 09:22:46 -05:00
|
|
|
use PhpParser;
|
2016-01-19 18:27:06 -05:00
|
|
|
|
|
|
|
class NamespaceChecker implements StatementsSource
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
protected $namespace;
|
|
|
|
protected $namespace_name;
|
|
|
|
protected $declared_classes = [];
|
|
|
|
protected $aliased_classes = [];
|
|
|
|
protected $file_name;
|
2016-01-29 18:48:09 -05:00
|
|
|
|
2016-07-22 13:29:46 -04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-13 18:54:49 -04:00
|
|
|
protected $suppressed_issues;
|
2016-07-22 13:29:46 -04:00
|
|
|
|
2016-01-19 18:27:06 -05:00
|
|
|
public function __construct(\PhpParser\Node\Stmt\Namespace_ $namespace, StatementsSource $source)
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
$this->namespace = $namespace;
|
|
|
|
$this->namespace_name = implode('\\', $this->namespace->name->parts);
|
|
|
|
$this->file_name = $source->getFileName();
|
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 23:06:03 -04:00
|
|
|
public function check($check_classes = true, $check_class_statements = true)
|
2016-01-19 18:27:06 -05:00
|
|
|
{
|
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-08-13 18:54:49 -04:00
|
|
|
foreach ($this->namespace->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassLike) {
|
|
|
|
$absolute_class = ClassLikeChecker::getAbsoluteClassFromString($stmt->name, $this->namespace_name, []);
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
|
|
|
|
$this->declared_classes[$absolute_class] = 1;
|
|
|
|
|
|
|
|
if ($check_classes) {
|
|
|
|
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($absolute_class) ?: new ClassChecker($stmt, $this, $absolute_class);
|
|
|
|
$class_checker->check($check_class_statements);
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
|
|
|
|
if ($check_classes) {
|
|
|
|
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name) ?: new InterfaceChecker($stmt, $this, $absolute_class);
|
|
|
|
$this->declared_classes[] = $class_checker->getAbsoluteClass();
|
|
|
|
$class_checker->check(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
|
|
|
|
if ($check_classes) {
|
|
|
|
// register the trait checker
|
|
|
|
ClassLikeChecker::getClassLikeCheckerFromClass($absolute_class) ?: new TraitChecker($stmt, $this, $absolute_class);
|
|
|
|
}
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
foreach ($stmt->uses as $use) {
|
2016-08-13 18:54:49 -04:00
|
|
|
$this->aliased_classes[$use->alias] = implode('\\', $use->name->parts);
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$leftover_stmts[] = $stmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($leftover_stmts) {
|
|
|
|
$statments_checker = new StatementsChecker($this);
|
2016-08-13 21:14:32 -04:00
|
|
|
$context = new Context($this->file_name);
|
|
|
|
$statments_checker->check($leftover_stmts, $context);
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-08-13 18:54:49 -04:00
|
|
|
return $this->aliased_classes;
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-06-20 00:38:13 -04:00
|
|
|
/**
|
|
|
|
* Gets a list of the classes declared
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public function getDeclaredClasses()
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
return array_keys($this->declared_classes);
|
2016-06-20 00:38:13 -04:00
|
|
|
}
|
|
|
|
|
2016-01-19 18:27:06 -05:00
|
|
|
public function containsClass($class_name)
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
return isset($this->declared_classes[$class_name]);
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
return $this->namespace_name;
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses()
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
return $this->aliased_classes;
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-26 18:42:48 -04:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-19 18:27:06 -05:00
|
|
|
public function getAbsoluteClass()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:42:48 -04:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-19 18:27:06 -05:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:42:48 -04:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-08-13 14:20:46 -04:00
|
|
|
public function getClassLikeChecker()
|
2016-01-29 18:48:09 -05:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:42:48 -04:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-04-17 11:22:18 -04:00
|
|
|
public function getParentClass()
|
2016-01-19 18:27:06 -05:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileName()
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
return $this->file_name;
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-26 18:42:48 -04:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-19 18:27:06 -05:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-15 23:06:03 -04:00
|
|
|
|
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2016-07-22 13:29:46 -04:00
|
|
|
|
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
return $this->suppressed_issues;
|
2016-07-22 13:29:46 -04:00
|
|
|
}
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|