2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
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-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
class FileChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $real_file_name;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $short_file_name;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
protected $include_file_name;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $namespace;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $aliased_classes = [];
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $namespace_aliased_classes = [];
|
2016-02-18 21:05:13 +01:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $preloaded_statements = [];
|
2016-04-04 01:41:54 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $declared_classes = [];
|
2016-06-20 06:38:13 +02: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-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $cache_dir = null;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string,static>
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $file_checkers = [];
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
protected static $functions_checked = [];
|
2016-08-14 00:54:49 +02:00
|
|
|
protected static $classes_checked = [];
|
2016-10-05 19:24:46 +02:00
|
|
|
protected static $files_checked = [];
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2016-01-11 17:05:24 +01:00
|
|
|
public static $show_notices = true;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
const REFERENCE_CACHE_NAME = 'references';
|
2016-10-07 06:58:08 +02:00
|
|
|
const GOOD_RUN_NAME = 'good_run';
|
|
|
|
|
|
|
|
protected static $last_good_run = null;
|
2016-10-05 19:24:46 +02:00
|
|
|
|
2016-10-05 01:23:38 +02:00
|
|
|
/**
|
|
|
|
* A lookup table used for getting all the files that reference a class
|
|
|
|
*
|
|
|
|
* @var array<string,array<string,bool>>
|
|
|
|
*/
|
|
|
|
protected static $file_references_to_class = [];
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
/**
|
|
|
|
* A lookup table used for getting all the files referenced by a file
|
|
|
|
*
|
2016-10-28 19:24:06 +02:00
|
|
|
* @var array<string,array{a:array<int,string>,i:array<int,string>}>
|
2016-10-05 19:24:46 +02:00
|
|
|
*/
|
|
|
|
protected static $file_references = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table used for getting all the files that reference any other file
|
|
|
|
*
|
|
|
|
* @var array<string,array<string,bool>>
|
|
|
|
*/
|
|
|
|
protected static $referencing_files = [];
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @var array<string,array<int,string>>
|
|
|
|
*/
|
2016-10-05 23:08:20 +02:00
|
|
|
protected static $files_inheriting_classes = [];
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* A list of all files deleted since the last successful run
|
|
|
|
*
|
|
|
|
* @var array<int,string>|null
|
|
|
|
*/
|
2016-10-07 19:26:29 +02:00
|
|
|
protected static $deleted_files = null;
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
* @param array $preloaded_statements
|
|
|
|
*/
|
2016-04-04 01:41:54 +02:00
|
|
|
public function __construct($file_name, array $preloaded_statements = [])
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->real_file_name = $file_name;
|
|
|
|
$this->short_file_name = Config::getInstance()->shortenFileName($file_name);
|
2016-01-11 23:23:05 +01:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$file_checkers[$this->short_file_name] = $this;
|
|
|
|
self::$file_checkers[$file_name] = $this;
|
2016-04-04 01:41:54 +02:00
|
|
|
|
|
|
|
if ($preloaded_statements) {
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->preloaded_statements = $preloaded_statements;
|
2016-04-04 01:41:54 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
public function check($check_classes = true, $check_functions = true, Context $file_context = null, $cache = true)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-10-05 19:24:46 +02:00
|
|
|
if ($cache && isset(self::$functions_checked[$this->short_file_name])) {
|
2016-08-05 21:11:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
if ($cache && $check_classes && !$check_functions && isset(self::$classes_checked[$this->real_file_name])) {
|
2016-08-05 21:11:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
if ($cache && !$check_classes && !$check_functions && isset(self::$files_checked[$this->real_file_name])) {
|
2016-08-05 21:11:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-24 23:06:54 +02:00
|
|
|
if (!$file_context) {
|
2016-08-14 03:14:32 +02:00
|
|
|
$file_context = new Context($this->short_file_name);
|
2016-07-24 23:06:54 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 20:26:03 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
$stmts = $this->getStatements();
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2016-08-14 06:38:29 +02:00
|
|
|
$statments_checker = new StatementsChecker($this);
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
$function_checkers = [];
|
|
|
|
|
2016-10-31 00:13:09 +01:00
|
|
|
$this->registerUses();
|
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
// hoist functions to the top
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
$function_checkers[$stmt->name] = new FunctionChecker($stmt, $this, $file_context->file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
2016-07-24 23:06:54 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Interface_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Trait_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Namespace_
|
2016-08-15 05:24:16 +02:00
|
|
|
|| $stmt instanceof PhpParser\Node\Stmt\Function_
|
2016-07-24 23:06:54 +02:00
|
|
|
) {
|
|
|
|
if ($leftover_stmts) {
|
|
|
|
$statments_checker->check($leftover_stmts, $file_context);
|
|
|
|
$leftover_stmts = [];
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-07-24 23:06:54 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
2016-10-30 06:13:33 +01:00
|
|
|
|
2016-07-24 23:06:54 +02:00
|
|
|
if ($check_classes) {
|
2016-08-13 20:20:46 +02:00
|
|
|
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name) ?: new ClassChecker($stmt, $this, $stmt->name);
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->declared_classes[] = $class_checker->getAbsoluteClass();
|
2016-08-15 05:24:16 +02:00
|
|
|
$class_checker->check($check_functions);
|
2016-07-24 23:06:54 +02:00
|
|
|
}
|
2016-10-30 06:13:33 +01:00
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
|
2016-08-14 00:54:49 +02:00
|
|
|
if ($check_classes) {
|
|
|
|
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name) ?: new InterfaceChecker($stmt, $this, $stmt->name);
|
|
|
|
$this->declared_classes[] = $class_checker->getAbsoluteClass();
|
|
|
|
$class_checker->check(false);
|
|
|
|
}
|
2016-07-24 23:06:54 +02:00
|
|
|
|
2016-10-30 06:13:33 +01:00
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
|
2016-07-24 23:06:54 +02:00
|
|
|
if ($check_classes) {
|
2016-08-13 20:20:46 +02:00
|
|
|
$trait_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name) ?: new TraitChecker($stmt, $this, $stmt->name);
|
2016-08-15 05:24:16 +02:00
|
|
|
$trait_checker->check($check_functions);
|
2016-07-24 23:06:54 +02:00
|
|
|
}
|
|
|
|
|
2016-10-30 06:13:33 +01:00
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Stmt\Namespace_ && $stmt->name instanceof PhpParser\Node\Name) {
|
2016-07-24 23:06:54 +02:00
|
|
|
$namespace_name = implode('\\', $stmt->name->parts);
|
|
|
|
|
|
|
|
$namespace_checker = new NamespaceChecker($stmt, $this);
|
2016-08-15 05:24:16 +02:00
|
|
|
$this->namespace_aliased_classes[$namespace_name] = $namespace_checker->check($check_classes, $check_functions);
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->declared_classes = array_merge($namespace_checker->getDeclaredClasses());
|
2016-08-14 06:38:29 +02:00
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
elseif ($stmt instanceof PhpParser\Node\Stmt\Function_ && $check_functions) {
|
|
|
|
$function_context = new Context($this->short_file_name, $file_context->self);
|
|
|
|
$function_checkers[$stmt->name]->check($function_context);
|
2016-10-20 20:26:03 +02:00
|
|
|
|
2016-10-20 20:36:15 +02:00
|
|
|
if (!$config->excludeIssueInFile('InvalidReturnType', $this->short_file_name)) {
|
2016-10-20 20:26:03 +02:00
|
|
|
$function_checkers[$stmt->name]->checkReturnTypes();
|
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
2016-10-30 06:13:33 +01:00
|
|
|
|
2016-08-14 06:38:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts[] = $stmt;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 06:13:33 +01:00
|
|
|
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($leftover_stmts) {
|
2016-07-24 23:06:54 +02:00
|
|
|
$statments_checker->check($leftover_stmts, $file_context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
if ($check_functions) {
|
|
|
|
self::$functions_checked[$this->real_file_name] = true;
|
2016-08-05 21:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($check_classes) {
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$classes_checked[$this->real_file_name] = true;
|
2016-08-05 21:11:20 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
self::$files_checked[$this->real_file_name] = true;
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2016-10-30 06:13:33 +01:00
|
|
|
|
|
|
|
|
2016-04-27 00:18:49 +02:00
|
|
|
return $stmts;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param string $namespace
|
|
|
|
* @param string $file_name
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-20 06:38:13 +02:00
|
|
|
public static function getAbsoluteClassFromNameInFile($class, $namespace, $file_name)
|
2016-01-11 20:21:29 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
if (isset(self::$file_checkers[$file_name])) {
|
|
|
|
$aliased_classes = self::$file_checkers[$file_name]->getAliasedClasses($namespace);
|
2016-01-11 20:21:29 +01:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
} else {
|
|
|
|
$file_checker = new FileChecker($file_name);
|
2016-08-14 03:14:32 +02:00
|
|
|
$file_checker->check(false, false, new Context($file_name));
|
2016-06-20 06:38:13 +02:00
|
|
|
$aliased_classes = $file_checker->getAliasedClasses($namespace);
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
return ClassLikeChecker::getAbsoluteClassFromString($class, $namespace, $aliased_classes);
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
2016-02-04 22:05:36 +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 $this->declared_classes;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
/**
|
|
|
|
* Gets a list of the classes declared in that file
|
|
|
|
* @param string $file_name
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public static function getDeclaredClassesInFile($file_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
if (isset(self::$file_checkers[$file_name])) {
|
|
|
|
$file_checker = self::$file_checkers[$file_name];
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-01-20 00:27:06 +01:00
|
|
|
$file_checker = new FileChecker($file_name);
|
2016-08-14 03:14:32 +02:00
|
|
|
$file_checker->check(false, false, new Context($file_name));
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
return $file_checker->getDeclaredClasses();
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-09-09 22:21:49 +02:00
|
|
|
* @return array<int, \PhpParser\Node>
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-06-18 20:45:55 +02:00
|
|
|
protected function getStatements()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->preloaded_statements ?
|
|
|
|
$this->preloaded_statements :
|
|
|
|
self::getStatementsForFile($this->real_file_name);
|
2016-06-18 20:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-14 06:53:43 +02:00
|
|
|
* @param string $file_name
|
2016-06-18 20:45:55 +02:00
|
|
|
* @return array<\PhpParser\Node>
|
|
|
|
*/
|
|
|
|
public static function getStatementsForFile($file_name)
|
2016-01-08 04:52:26 +01:00
|
|
|
{
|
2016-09-01 06:07:29 +02:00
|
|
|
$contents = (string) file_get_contents($file_name);
|
2016-01-08 04:52:26 +01:00
|
|
|
|
|
|
|
$stmts = [];
|
|
|
|
|
2016-01-08 16:47:15 +01:00
|
|
|
$from_cache = false;
|
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
$cache_location = null;
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
if (self::$cache_dir) {
|
2016-01-08 04:52:26 +01:00
|
|
|
$key = md5($contents);
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
$cache_location = self::$cache_dir . '/' . $key;
|
2016-01-08 04:52:26 +01:00
|
|
|
|
|
|
|
if (is_readable($cache_location)) {
|
2016-09-01 06:07:29 +02:00
|
|
|
$stmts = unserialize((string) file_get_contents($cache_location));
|
2016-01-08 16:47:15 +01:00
|
|
|
$from_cache = true;
|
2016-01-08 04:52:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 17:28:06 +02:00
|
|
|
if (!$stmts && $contents) {
|
2016-01-08 04:52:26 +01:00
|
|
|
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
|
|
|
$stmts = $parser->parse($contents);
|
|
|
|
}
|
|
|
|
|
2016-09-01 06:07:29 +02:00
|
|
|
if (self::$cache_dir && $cache_location) {
|
2016-01-08 16:47:15 +01:00
|
|
|
if ($from_cache) {
|
|
|
|
touch($cache_location);
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-08-14 00:54:49 +02:00
|
|
|
if (!file_exists(self::$cache_dir)) {
|
|
|
|
mkdir(self::$cache_dir);
|
2016-01-08 16:47:15 +01:00
|
|
|
}
|
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) {
|
2016-04-28 21:18:36 +02:00
|
|
|
return [];
|
2016-04-27 00:32:37 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 04:52:26 +01:00
|
|
|
return $stmts;
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
public static function loadReferenceCache()
|
|
|
|
{
|
|
|
|
if (self::$cache_dir) {
|
|
|
|
$cache_location = self::$cache_dir . '/' . self::REFERENCE_CACHE_NAME;
|
|
|
|
|
|
|
|
if (is_readable($cache_location)) {
|
|
|
|
self::$file_references = unserialize((string) file_get_contents($cache_location));
|
2016-10-05 23:08:20 +02:00
|
|
|
return true;
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-05 23:08:20 +02:00
|
|
|
|
|
|
|
return false;
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function updateReferenceCache()
|
|
|
|
{
|
|
|
|
if (self::$cache_dir) {
|
|
|
|
$cache_location = self::$cache_dir . '/' . self::REFERENCE_CACHE_NAME;
|
|
|
|
|
|
|
|
foreach (self::$files_checked as $file => $_) {
|
2016-10-05 23:08:20 +02:00
|
|
|
$all_file_references = array_unique(
|
|
|
|
array_merge(
|
|
|
|
isset(self::$file_references[$file]['a']) ? self::$file_references[$file]['a'] : [],
|
|
|
|
self::calculateFilesReferencingFile($file)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$inheritance_references = array_unique(
|
|
|
|
array_merge(
|
|
|
|
isset(self::$file_references[$file]['i']) ? self::$file_references[$file]['i'] : [],
|
|
|
|
self::calculateFilesInheritingFile($file)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
self::$file_references[$file] = [
|
|
|
|
'a' => $all_file_references,
|
|
|
|
'i' => $inheritance_references
|
|
|
|
];
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($cache_location, serialize(self::$file_references));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:52:26 +01:00
|
|
|
public static function setCacheDir($cache_dir)
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$cache_dir = $cache_dir;
|
2016-01-08 04:52:26 +01:00
|
|
|
}
|
2016-01-11 23:23:05 +01:00
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @param string|null $namespace_name
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getAliasedClasses($namespace_name = null)
|
|
|
|
{
|
2016-08-14 00:54:49 +02: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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
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->short_file_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
public function getRealFileName()
|
|
|
|
{
|
|
|
|
return $this->real_file_name;
|
|
|
|
}
|
|
|
|
|
2016-08-25 01:00:44 +02:00
|
|
|
public function getIncludeFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string|null $file_name
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
public function setIncludeFileName($file_name)
|
|
|
|
{
|
|
|
|
$this->include_file_name = $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCheckedFileName()
|
|
|
|
{
|
|
|
|
return $this->include_file_name ?: $this->short_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-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* Get a list of suppressed issues
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
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-03-23 18:05:25 +01:00
|
|
|
public static function getFileCheckerFromFileName($file_name)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return self::$file_checkers[$file_name];
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @return ClassLikeChecker|null
|
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public static function getClassLikeCheckerFromClass($class_name)
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2016-10-30 06:13:33 +01:00
|
|
|
$old_level = error_reporting();
|
|
|
|
error_reporting(0);
|
2016-10-14 06:53:43 +02:00
|
|
|
$file_name = (string)(new \ReflectionClass($class_name))->getFileName();
|
2016-10-30 06:13:33 +01:00
|
|
|
error_reporting($old_level);
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
if (isset(self::$file_checkers[$file_name])) {
|
|
|
|
$file_checker = self::$file_checkers[$file_name];
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$file_checker = new FileChecker($file_name);
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
$file_checker->check(true, false, null, false);
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
return ClassLikeChecker::getClassLikeCheckerFromClass($class_name);
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 00:13:09 +01:00
|
|
|
protected function registerUses()
|
|
|
|
{
|
|
|
|
foreach ($this->getStatements() as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
foreach ($stmt->uses as $use) {
|
|
|
|
$this->aliased_classes[strtolower($use->alias)] = implode('\\', $use->name->parts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 01:23:38 +02:00
|
|
|
public static function addFileReferenceToClass($source_file, $absolute_class)
|
|
|
|
{
|
2016-10-05 19:24:46 +02:00
|
|
|
self::$referencing_files[$source_file] = true;
|
2016-10-05 01:23:38 +02:00
|
|
|
self::$file_references_to_class[$absolute_class][$source_file] = true;
|
|
|
|
}
|
|
|
|
|
2016-10-05 23:08:20 +02:00
|
|
|
public static function addFileInheritanceToClass($source_file, $absolute_class)
|
|
|
|
{
|
|
|
|
self::$files_inheriting_classes[$absolute_class][$source_file] = true;
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:24:46 +02:00
|
|
|
public static function calculateFilesReferencingFile($file)
|
2016-10-05 01:23:38 +02:00
|
|
|
{
|
|
|
|
$referenced_files = [];
|
|
|
|
|
|
|
|
$file_classes = ClassLikeChecker::getClassesForFile($file);
|
|
|
|
|
|
|
|
foreach ($file_classes as $file_class) {
|
|
|
|
if (isset(self::$file_references_to_class[$file_class])) {
|
|
|
|
$referenced_files = array_merge($referenced_files, array_keys(self::$file_references_to_class[$file_class]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($referenced_files);
|
|
|
|
}
|
|
|
|
|
2016-10-05 23:08:20 +02:00
|
|
|
public static function calculateFilesInheritingFile($file)
|
|
|
|
{
|
|
|
|
$referenced_files = [];
|
|
|
|
|
|
|
|
$file_classes = ClassLikeChecker::getClassesForFile($file);
|
|
|
|
|
|
|
|
foreach ($file_classes as $file_class) {
|
|
|
|
if (isset(self::$files_inheriting_classes[$file_class])) {
|
|
|
|
$referenced_files = array_merge($referenced_files, array_keys(self::$files_inheriting_classes[$file_class]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($referenced_files);
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-10-05 19:24:46 +02:00
|
|
|
public static function getFilesReferencingFile($file)
|
|
|
|
{
|
2016-10-05 23:08:20 +02:00
|
|
|
return isset(self::$file_references[$file]['a']) ? self::$file_references[$file]['a'] : [];
|
|
|
|
}
|
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-10-05 23:08:20 +02:00
|
|
|
public static function getFilesInheritingFromFile($file)
|
|
|
|
{
|
|
|
|
return isset(self::$file_references[$file]['i']) ? self::$file_references[$file]['i'] : [];
|
2016-10-05 19:24:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-07 06:58:08 +02:00
|
|
|
public static function canDiffFiles()
|
|
|
|
{
|
|
|
|
return self::$cache_dir && file_exists(self::$cache_dir . '/' . self::GOOD_RUN_NAME);
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-07 06:58:08 +02:00
|
|
|
public static function hasFileChanged($file)
|
|
|
|
{
|
|
|
|
if (self::$last_good_run === null) {
|
|
|
|
self::$last_good_run = filemtime(self::$cache_dir . '/' . self::GOOD_RUN_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
return filemtime($file) > self::$last_good_run;
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
2016-10-07 19:26:29 +02:00
|
|
|
public static function getDeletedReferencedFiles()
|
|
|
|
{
|
|
|
|
if (self::$deleted_files === null) {
|
|
|
|
self::$deleted_files = array_filter(
|
|
|
|
array_keys(self::$file_references),
|
|
|
|
function($file_name) {
|
2016-10-14 06:53:43 +02:00
|
|
|
return !file_exists((string)$file_name);
|
2016-10-07 19:26:29 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$deleted_files;
|
|
|
|
}
|
|
|
|
|
2016-10-07 06:58:08 +02:00
|
|
|
public static function goodRun()
|
|
|
|
{
|
|
|
|
if (self::$cache_dir) {
|
2016-10-07 19:26:29 +02:00
|
|
|
$run_cache_location = self::$cache_dir . '/' . self::GOOD_RUN_NAME;
|
2016-10-07 06:58:08 +02:00
|
|
|
|
2016-10-07 19:26:29 +02:00
|
|
|
touch($run_cache_location);
|
|
|
|
|
|
|
|
$deleted_files = self::getDeletedReferencedFiles();
|
|
|
|
|
|
|
|
if ($deleted_files) {
|
|
|
|
|
|
|
|
foreach ($deleted_files as $file) {
|
|
|
|
unset(self::$file_references[$file]);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents(self::$cache_dir . '/' . self::REFERENCE_CACHE_NAME, serialize(self::$file_references));
|
|
|
|
}
|
2016-10-07 06:58:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 07:09:47 +02:00
|
|
|
public static function clearCache()
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$file_checkers = [];
|
2016-05-10 20:00:44 +02:00
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
self::$functions_checked = [];
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$classes_checked = [];
|
2016-10-05 19:24:46 +02:00
|
|
|
self::$files_checked = [];
|
2016-08-15 17:01:50 +02:00
|
|
|
|
|
|
|
ClassLikeChecker::clearCache();
|
2016-10-21 00:12:13 +02:00
|
|
|
FunctionChecker::clearCache();
|
2016-10-21 00:16:17 +02:00
|
|
|
StatementsChecker::clearCache();
|
2016-05-10 20:00:44 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|