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 FileChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
protected $_file_name;
|
|
|
|
protected $_namespace;
|
|
|
|
protected $_aliased_classes = [];
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
protected $_function_params = [];
|
|
|
|
protected $_class_name;
|
|
|
|
|
2016-02-18 21:05:13 +01:00
|
|
|
protected $_namespace_aliased_classes = [];
|
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
protected static $_class_property_fn = null;
|
|
|
|
protected static $_var_dump_fn = null;
|
|
|
|
|
2016-01-08 04:52:26 +01:00
|
|
|
protected static $_cache_dir = null;
|
2016-01-08 00:28:27 +01:00
|
|
|
protected static $_file_checkers = [];
|
2016-01-20 00:27:06 +01:00
|
|
|
protected static $_functions = [];
|
|
|
|
|
2016-01-11 17:05:24 +01:00
|
|
|
public static $show_notices = true;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
public function __construct($file_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
$this->_file_name = $file_name;
|
2016-01-11 23:23:05 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
self::$_file_checkers[$this->_file_name] = $this;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function check($check_classes = true)
|
|
|
|
{
|
2016-01-08 04:52:26 +01:00
|
|
|
$stmts = self::_getStatments($this->_file_name);
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
if ($check_classes) {
|
2016-02-27 01:11:11 +01:00
|
|
|
(new ClassChecker($stmt, $this, $stmt->name))->check();
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
|
|
|
|
// @todo check interfaces
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
|
|
|
|
// @todo check trait
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Namespace_) {
|
|
|
|
$namespace_name = implode('\\', $stmt->name->parts);
|
|
|
|
|
|
|
|
$namespace_checker = new NamespaceChecker($stmt, $this);
|
2016-02-18 21:05:13 +01:00
|
|
|
$this->_namespace_aliased_classes[$namespace_name] = $namespace_checker->check($check_classes);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
2016-01-08 00:28:27 +01:00
|
|
|
foreach ($stmt->uses as $use) {
|
|
|
|
$this->_aliased_classes[$use->alias] = implode('\\', $use->name->parts);
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$leftover_stmts[] = $stmt;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 20:21:29 +01:00
|
|
|
public function checkWithClass($class_name)
|
|
|
|
{
|
|
|
|
$stmts = self::_getStatments($this->_file_name);
|
2016-01-20 00:27:06 +01:00
|
|
|
$this->_class_name = $class_name;
|
2016-01-11 20:21:29 +01:00
|
|
|
|
|
|
|
$class_method = new PhpParser\Node\Stmt\ClassMethod($class_name, ['stmts' => $stmts]);
|
|
|
|
|
2016-02-04 22:05:36 +01:00
|
|
|
$class = new PhpParser\Node\Stmt\Class_($class_name);
|
|
|
|
|
|
|
|
$class_checker = new ClassChecker($class, $this, $class_name);
|
|
|
|
|
|
|
|
(new ClassMethodChecker($class_method, $class_checker))->check();
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public static function getAbsoluteClassFromNameInFile($class, $namespace, $file_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
|
|
|
if (isset(self::$_file_checkers[$file_name])) {
|
2016-01-20 00:27:06 +01:00
|
|
|
$aliased_classes = self::$_file_checkers[$file_name]->getAliasedClasses($namespace);
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
|
|
|
$file_checker = new FileChecker($file_name);
|
|
|
|
$file_checker->check(false);
|
|
|
|
$aliased_classes = $file_checker->getAliasedClasses($namespace);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
return ClassChecker::getAbsoluteClassFromString($class, $namespace, $aliased_classes);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
|
|
|
|
protected static function _getStatments($file_name)
|
|
|
|
{
|
|
|
|
$contents = file_get_contents($file_name);
|
|
|
|
|
|
|
|
$stmts = [];
|
|
|
|
|
2016-01-08 16:47:15 +01:00
|
|
|
$from_cache = false;
|
|
|
|
|
2016-01-08 04:52:26 +01:00
|
|
|
if (self::$_cache_dir) {
|
|
|
|
$key = md5($contents);
|
|
|
|
|
|
|
|
$cache_location = self::$_cache_dir . '/' . $key;
|
|
|
|
|
|
|
|
if (is_readable($cache_location)) {
|
|
|
|
$stmts = unserialize(file_get_contents($cache_location));
|
2016-01-08 16:47:15 +01:00
|
|
|
$from_cache = true;
|
2016-01-08 04:52:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$stmts) {
|
|
|
|
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
|
|
|
$stmts = $parser->parse($contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::$_cache_dir) {
|
2016-01-08 16:47:15 +01:00
|
|
|
if ($from_cache) {
|
|
|
|
touch($cache_location);
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-01-08 16:47:15 +01:00
|
|
|
if (!file_exists(self::$_cache_dir)) {
|
|
|
|
mkdir(self::$_cache_dir);
|
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
|
2016-01-08 16:47:15 +01:00
|
|
|
file_put_contents($cache_location, serialize($stmts));
|
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stmts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setCacheDir($cache_dir)
|
|
|
|
{
|
|
|
|
self::$_cache_dir = $cache_dir;
|
|
|
|
}
|
2016-01-11 23:23:05 +01:00
|
|
|
|
|
|
|
public static function shouldCheckVarDumps($file_name)
|
|
|
|
{
|
2016-02-04 15:22:46 +01:00
|
|
|
return !self::$_var_dump_fn || call_user_func(self::$_var_dump_fn, $file_name);
|
|
|
|
}
|
|
|
|
|
2016-02-04 21:53:20 +01:00
|
|
|
public static function shouldCheckClassProperties($file_name, ClassChecker $class_checker = null)
|
2016-02-04 15:22:46 +01:00
|
|
|
{
|
|
|
|
return !self::$_class_property_fn || call_user_func(self::$_class_property_fn, $file_name, $class_checker);
|
2016-01-11 23:23:05 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
public function registerFunction(PhpParser\Node\Stmt\Function_ $function, $absolute_class = null)
|
|
|
|
{
|
|
|
|
$function_name = ($absolute_class ? $absolute_class . '::' : '') . $function->name;
|
|
|
|
|
|
|
|
$this->_function_params[$function_name] = [];
|
|
|
|
|
|
|
|
foreach ($function->params as $param) {
|
|
|
|
$this->_function_params[$function_name][] = $param->byRef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAliasedClasses($namespace_name = null)
|
|
|
|
{
|
2016-02-18 21:05:13 +01:00
|
|
|
if ($namespace_name && isset($this->_namespace_aliased_classes[$namespace_name])) {
|
|
|
|
return $this->_namespace_aliased_classes[$namespace_name];
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
return $this->_aliased_classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAbsoluteClass()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->_class_name;
|
|
|
|
}
|
|
|
|
|
2016-01-30 00:48:09 +01:00
|
|
|
public function getClassChecker()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getClassExtends()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileName()
|
|
|
|
{
|
|
|
|
return $this->_file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileCheckerFromFileName($file_name)
|
|
|
|
{
|
|
|
|
return self::$_file_checkers[$file_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasFunction($function_name)
|
|
|
|
{
|
|
|
|
return isset($this->_function_params[$function_name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isPassedByReference($function_name, $argument_offset)
|
|
|
|
{
|
|
|
|
return $argument_offset < count($this->_function_params[$function_name]) && $this->_function_params[$function_name][$argument_offset];
|
|
|
|
}
|
2016-02-04 15:22:46 +01:00
|
|
|
|
2016-02-04 22:05:36 +01:00
|
|
|
public static function checkClassPropertiesFor(callable $fn)
|
|
|
|
{
|
2016-02-04 15:22:46 +01:00
|
|
|
self::$_class_property_fn = $fn;
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:05:36 +01:00
|
|
|
public static function checkVarDumpsFor(callable $fn)
|
|
|
|
{
|
2016-02-04 15:22:46 +01:00
|
|
|
self::$_var_dump_fn = $fn;
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|