2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
|
|
|
|
2016-01-08 00:36:55 +01:00
|
|
|
namespace CodeInspector;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\Error;
|
|
|
|
use PhpParser\ParserFactory;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
class ClassChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
protected $_file_name;
|
|
|
|
protected $_class;
|
|
|
|
protected $_namespace;
|
|
|
|
protected $_aliased_classes;
|
2016-01-20 00:27:06 +01:00
|
|
|
protected $_absolute_class;
|
2016-01-30 00:48:09 +01:00
|
|
|
protected $_class_properties = [];
|
|
|
|
protected $_has_custom_get = false;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
protected static $_existing_classes = [];
|
2016-04-12 01:13:50 +02:00
|
|
|
protected static $_implementing_classes = [];
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function __construct(PhpParser\Node\Stmt\Class_ $class, StatementsSource $source, $absolute_class)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
$this->_class = $class;
|
2016-01-20 00:27:06 +01:00
|
|
|
$this->_namespace = $source->getNamespace();
|
|
|
|
$this->_aliased_classes = $source->getAliasedClasses();
|
|
|
|
$this->_file_name = $source->getFileName();
|
|
|
|
$this->_absolute_class = $absolute_class;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
self::$_existing_classes[$absolute_class] = 1;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function check()
|
|
|
|
{
|
|
|
|
if ($this->_class->extends instanceof PhpParser\Node\Name) {
|
|
|
|
self::checkClassName($this->_class->extends, $this->_namespace, $this->_aliased_classes, $this->_file_name);
|
|
|
|
}
|
|
|
|
|
2016-01-26 20:13:04 +01:00
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-01-30 00:48:09 +01:00
|
|
|
try {
|
|
|
|
new \ReflectionMethod($this->_absolute_class . '::__get');
|
|
|
|
$this->_has_custom_get = true;
|
|
|
|
|
|
|
|
} catch (\ReflectionException $e) {}
|
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
$method_checkers = [];
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
foreach ($this->_class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2016-03-23 18:05:25 +01:00
|
|
|
$method_checkers[] = new ClassMethodChecker($stmt, $this);
|
2016-01-26 20:13:04 +01:00
|
|
|
|
|
|
|
} else {
|
2016-01-30 00:48:09 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
|
|
|
foreach ($stmt->props as $property) {
|
|
|
|
$this->_class_properties[] = $property->name;
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
$leftover_stmts[] = $stmt;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
|
|
|
|
if ($leftover_stmts) {
|
|
|
|
$scope_vars = [];
|
|
|
|
$possibly_in_scope_vars = [];
|
|
|
|
|
|
|
|
(new StatementsChecker($this))->check($leftover_stmts, $scope_vars, $possibly_in_scope_vars);
|
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
|
|
|
|
// do the method checks after all class methods have been initialised
|
|
|
|
foreach ($method_checkers as $method_checker) {
|
|
|
|
$method_checker->check();
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkClassName(PhpParser\Node\Name $class_name, $namespace, array $aliased_classes, $file_name)
|
|
|
|
{
|
|
|
|
if ($class_name->parts[0] === 'static') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$absolute_class = self::getAbsoluteClassFromName($class_name, $namespace, $aliased_classes);
|
|
|
|
|
2016-02-18 21:05:13 +01:00
|
|
|
self::checkAbsoluteClass($absolute_class, $class_name, $file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkAbsoluteClass($absolute_class, PhpParser\NodeAbstract $stmt, $file_name)
|
|
|
|
{
|
2016-04-12 17:59:27 +02:00
|
|
|
if (empty($absolute_class)) {
|
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:06:01 +01:00
|
|
|
$absolute_class = preg_replace('/^\\\/', '', $absolute_class);
|
|
|
|
|
|
|
|
if (isset(self::$_existing_classes[$absolute_class])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists($absolute_class, true) && !interface_exists($absolute_class, true)) {
|
2016-02-18 21:05:13 +01:00
|
|
|
throw new CodeException('Class ' . $absolute_class . ' does not exist', $file_name, $stmt->getLine());
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-04-16 17:28:19 +02:00
|
|
|
if (class_exists($absolute_class, true) && strpos($absolute_class, '\\') === false) {
|
2016-03-17 19:06:01 +01:00
|
|
|
$reflection_class = new \ReflectionClass($absolute_class);
|
|
|
|
|
|
|
|
if ($reflection_class->getName() !== $absolute_class) {
|
|
|
|
throw new CodeException('Class ' . $absolute_class . ' has wrong casing', $file_name, $stmt->getLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
self::$_existing_classes[$absolute_class] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAbsoluteClassFromName(PhpParser\Node\Name $class_name, $namespace, array $aliased_classes)
|
|
|
|
{
|
|
|
|
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return implode('\\', $class_name->parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
return self::getAbsoluteClassFromString(implode('\\', $class_name->parts), $namespace, $aliased_classes);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public static function getAbsoluteClassFromString($class, $namespace, array $imported_namespaces)
|
|
|
|
{
|
2016-04-12 17:59:27 +02:00
|
|
|
if (empty($class)) {
|
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($class[0] === '\\') {
|
2016-02-18 21:05:13 +01:00
|
|
|
return substr($class, 1);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($class, '\\') !== false) {
|
|
|
|
$class_parts = explode('\\', $class);
|
|
|
|
$first_namespace = array_shift($class_parts);
|
|
|
|
|
|
|
|
if (isset($imported_namespaces[$first_namespace])) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return $imported_namespaces[$first_namespace] . '\\' . implode('\\', $class_parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif (isset($imported_namespaces[$class])) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return $imported_namespaces[$class];
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 01:11:11 +01:00
|
|
|
return ($namespace ? $namespace . '\\' : '') . $class;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return $this->_namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses()
|
|
|
|
{
|
|
|
|
return $this->_aliased_classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAbsoluteClass()
|
|
|
|
{
|
|
|
|
return $this->_absolute_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->_class->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassExtends()
|
|
|
|
{
|
|
|
|
return $this->_class->extends;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileName()
|
|
|
|
{
|
|
|
|
return $this->_file_name;
|
|
|
|
}
|
|
|
|
|
2016-01-30 00:48:09 +01:00
|
|
|
public function getClassChecker()
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
|
|
|
public function hasCustomGet()
|
|
|
|
{
|
|
|
|
return $this->_has_custom_get;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPropertyNames()
|
|
|
|
{
|
|
|
|
return $this->_class_properties;
|
|
|
|
}
|
2016-04-12 01:13:50 +02:00
|
|
|
|
|
|
|
public function classImplements($absolute_class, $interface)
|
|
|
|
{
|
|
|
|
if (isset($_implementing_classes[$absolute_class][$interface])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_implementing_classes[$absolute_class])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_implementations = class_implements($absolute_class);
|
|
|
|
|
|
|
|
if (!isset($class_implementations[$interface])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$_implementing_classes[$absolute_class] = $class_implementations;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|