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-04-04 01:41:54 +02:00
|
|
|
protected $_preloaded_statements = [];
|
|
|
|
|
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-03-23 18:05:25 +01:00
|
|
|
protected static $_includes_to_ignore = [];
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-01-11 17:05:24 +01:00
|
|
|
public static $show_notices = true;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-04-04 01:41:54 +02:00
|
|
|
public function __construct($file_name, array $preloaded_statements = [])
|
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-04-04 01:41:54 +02:00
|
|
|
|
|
|
|
if ($preloaded_statements) {
|
|
|
|
$this->_preloaded_statements = $preloaded_statements;
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function check($check_classes = true)
|
|
|
|
{
|
2016-04-04 01:41:54 +02:00
|
|
|
$stmts = $this->_preloaded_statements ?
|
|
|
|
$this->_preloaded_statements :
|
|
|
|
self::getStatements($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-04-27 00:18:49 +02:00
|
|
|
|
|
|
|
return $stmts;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 20:21:29 +01:00
|
|
|
public function checkWithClass($class_name)
|
|
|
|
{
|
2016-03-23 18:05:25 +01:00
|
|
|
$stmts = self::getStatements($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
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
public static function getStatements($file_name)
|
2016-01-08 04:52:26 +01:00
|
|
|
{
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2016-04-27 00:32:37 +02:00
|
|
|
if (!$stmts) {
|
|
|
|
throw new \InvalidArgumentException('File has no statements');
|
|
|
|
}
|
|
|
|
|
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-04-20 16:23:46 +02:00
|
|
|
public static function shouldCheckClassProperties($file_name)
|
2016-02-04 15:22:46 +01:00
|
|
|
{
|
2016-04-20 16:23:46 +02:00
|
|
|
return !self::$_class_property_fn || call_user_func(self::$_class_property_fn, $file_name);
|
2016-01-11 23:23:05 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-04-17 20:48:57 +02:00
|
|
|
public function registerFunction(PhpParser\Node\Stmt\Function_ $function)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-04-17 20:48:57 +02:00
|
|
|
$function_name = $function->name;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
$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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
public static function getFileCheckerFromFileName($file_name)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
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-03-23 18:05:25 +01:00
|
|
|
|
|
|
|
public static function ignoreIncludes(array $includes)
|
|
|
|
{
|
|
|
|
self::$_includes_to_ignore = $includes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getIncludesToIgnore()
|
|
|
|
{
|
|
|
|
return self::$_includes_to_ignore;
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|