2016-01-20 00:27:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace CodeInspector;
|
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
class NamespaceChecker implements StatementsSource
|
|
|
|
{
|
|
|
|
protected $_namespace;
|
|
|
|
protected $_namespace_name;
|
|
|
|
protected $_contained_classes = [];
|
|
|
|
protected $_aliased_classes = [];
|
|
|
|
protected $_file_name;
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function __construct(\PhpParser\Node\Stmt\Namespace_ $namespace, StatementsSource $source)
|
|
|
|
{
|
|
|
|
$this->_namespace = $namespace;
|
|
|
|
$this->_namespace_name = implode('\\', $this->_namespace->name->parts);
|
|
|
|
$this->_file_name = $source->getFileName();
|
|
|
|
}
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
foreach ($this->_namespace->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
$absolute_class = ClassChecker::getAbsoluteClassFromString($stmt->name, $this->_namespace_name, []);
|
|
|
|
$this->_contained_classes[$absolute_class] = 1;
|
|
|
|
|
|
|
|
if ($check_classes) {
|
2016-05-16 05:06:03 +02:00
|
|
|
$class_checker = ClassChecker::getClassCheckerFromClass($absolute_class) ?: new ClassChecker($stmt, $this, $absolute_class);
|
|
|
|
$class_checker->check($check_class_statements);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
|
|
|
|
// @todo check interface
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
|
2016-05-16 05:06:03 +02:00
|
|
|
$absolute_class = ClassChecker::getAbsoluteClassFromString($stmt->name, $this->_namespace_name, []);
|
|
|
|
|
|
|
|
if ($check_classes) {
|
|
|
|
$trait_checker = ClassChecker::getClassCheckerFromClass($absolute_class) ?: new TraitChecker($stmt, $this, $absolute_class);
|
|
|
|
$trait_checker->check($check_class_statements);
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
foreach ($stmt->uses as $use) {
|
|
|
|
$this->_aliased_classes[$use->alias] = implode('\\', $use->name->parts);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$leftover_stmts[] = $stmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($leftover_stmts) {
|
|
|
|
$statments_checker = new StatementsChecker($this);
|
|
|
|
$existing_vars = [];
|
|
|
|
$existing_vars_in_scope = [];
|
|
|
|
$statments_checker->check($leftover_stmts, $existing_vars, $existing_vars_in_scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_aliased_classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function containsClass($class_name)
|
|
|
|
{
|
|
|
|
return isset($this->_contained_classes[$class_name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return $this->_namespace_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses()
|
|
|
|
{
|
|
|
|
return $this->_aliased_classes;
|
|
|
|
}
|
|
|
|
|
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-01-30 00:48:09 +01:00
|
|
|
public function getClassChecker()
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return $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-01-20 00:27:06 +01:00
|
|
|
}
|