2016-01-20 00:27:06 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
|
|
|
|
|
|
|
use Psalm\StatementsSource;
|
2016-08-14 00:54:49 +02:00
|
|
|
use Psalm\Context;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
class NamespaceChecker implements StatementsSource
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $namespace;
|
|
|
|
protected $namespace_name;
|
|
|
|
protected $declared_classes = [];
|
|
|
|
protected $aliased_classes = [];
|
|
|
|
protected $file_name;
|
2016-08-25 01:00:44 +02:00
|
|
|
protected $include_file_name;
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $suppressed_issues;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function __construct(\PhpParser\Node\Stmt\Namespace_ $namespace, StatementsSource $source)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->namespace = $namespace;
|
|
|
|
$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();
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->suppressed_issues = $source->getSuppressedIssues();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
public function check($check_classes = true, $check_class_statements = true)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-08-14 00:54:49 +02: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-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
foreach ($stmt->uses as $use) {
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->aliased_classes[$use->alias] = implode('\\', $use->name->parts);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$leftover_stmts[] = $stmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($leftover_stmts) {
|
|
|
|
$statments_checker = new StatementsChecker($this);
|
2016-08-14 03:14:32 +02:00
|
|
|
$context = new Context($this->file_name);
|
|
|
|
$statments_checker->check($leftover_stmts, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->aliased_classes;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
/**
|
|
|
|
* Gets a list of the classes declared
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public function getDeclaredClasses()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return array_keys($this->declared_classes);
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function containsClass($class_name)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return isset($this->declared_classes[$class_name]);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->namespace_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->aliased_classes;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getAbsoluteClass()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public function getClassLikeChecker()
|
2016-01-30 00:48:09 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-04-17 17:22:18 +02:00
|
|
|
public function getParentClass()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileName()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->file_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-25 01:00:44 +02:00
|
|
|
public function getIncludeFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setIncludeFileName($file_name)
|
|
|
|
{
|
|
|
|
$this->include_file_name = $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCheckedFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name ?: $this->file_name;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2016-07-22 19:29:46 +02:00
|
|
|
|
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->suppressed_issues;
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|