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\ParserFactory;
|
2016-11-02 07:29:00 +01:00
|
|
|
use PhpParser;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2016-12-08 21:57:18 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2017-02-01 01:21:33 +01:00
|
|
|
use Psalm\Issue\DuplicateClass;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\StatementsSource;
|
2016-12-31 00:08:07 +01:00
|
|
|
use Psalm\Storage\FileStorage;
|
2017-01-02 21:31:18 +01:00
|
|
|
use Psalm\Type;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
class FileChecker extends SourceChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
use CanAlias;
|
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
const PARSER_CACHE_DIRECTORY = 'php-parser';
|
2016-11-18 22:13:59 +01:00
|
|
|
const FILE_HASHES = 'file_hashes';
|
2016-11-02 07:29:00 +01:00
|
|
|
const REFERENCE_CACHE_NAME = 'references';
|
|
|
|
const GOOD_RUN_NAME = 'good_run';
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $file_name;
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-12-04 01:11:30 +01:00
|
|
|
protected $file_path;
|
2016-10-14 06:53:43 +02:00
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2017-01-08 01:33:33 +01:00
|
|
|
protected $actual_file_name;
|
2017-01-07 20:35:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2017-01-08 01:33:33 +01:00
|
|
|
protected $actual_file_path;
|
2017-01-07 20:35:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected $suppressed_issues = [];
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, array<string, string>>
|
2016-10-31 20:42:20 +01:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $namespace_aliased_classes = [];
|
2016-02-18 21:05:13 +01:00
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, array<string, string>>
|
|
|
|
*/
|
|
|
|
protected $namespace_aliased_classes_flipped = [];
|
|
|
|
|
2016-10-31 20:42:20 +01:00
|
|
|
/**
|
2016-12-25 02:08:58 +01:00
|
|
|
* @var array<int, \PhpParser\Node\Expr|\PhpParser\Node\Stmt>
|
2016-10-31 20:42:20 +01:00
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $preloaded_statements = [];
|
2016-04-04 01:41:54 +02:00
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
2016-10-05 19:24:46 +02:00
|
|
|
protected static $files_checked = [];
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-01-11 17:05:24 +01:00
|
|
|
public static $show_notices = true;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @var int|null
|
|
|
|
*/
|
2016-10-07 06:58:08 +02:00
|
|
|
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
|
|
|
|
*
|
2016-11-18 22:13:59 +01:00
|
|
|
* @var array<string, array<string,bool>>
|
2016-10-05 01:23:38 +02:00
|
|
|
*/
|
|
|
|
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-11-18 22:13:59 +01: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
|
|
|
/**
|
2016-11-18 22:13:59 +01:00
|
|
|
* @var array<string, array<int,string>>
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2016-11-18 22:13:59 +01:00
|
|
|
* @var array<int, string>|null
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2016-10-07 19:26:29 +02:00
|
|
|
protected static $deleted_files = null;
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* A list of return types, keyed by file
|
|
|
|
*
|
2016-11-13 05:59:31 +01:00
|
|
|
* @var array<string, array<int, array<string>>>
|
2016-11-13 00:51:48 +01:00
|
|
|
*/
|
|
|
|
protected static $docblock_return_types = [];
|
|
|
|
|
2016-11-18 22:13:59 +01:00
|
|
|
/**
|
|
|
|
* A map of filename hashes to contents hashes
|
|
|
|
*
|
|
|
|
* @var array<string, string>|null
|
|
|
|
*/
|
|
|
|
protected static $file_content_hashes = null;
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2016-12-31 00:08:07 +01:00
|
|
|
* A list of data useful to analyse files
|
|
|
|
*
|
|
|
|
* @var array<string, FileStorage>
|
|
|
|
*/
|
|
|
|
public static $storage = [];
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, ClassLikeChecker>
|
|
|
|
*/
|
|
|
|
protected $interface_checkers_no_methods = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, ClassLikeChecker>
|
|
|
|
*/
|
|
|
|
protected $class_checkers_no_methods = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<int, ClassLikeChecker>
|
|
|
|
*/
|
|
|
|
protected $class_checkers = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, FunctionChecker>
|
|
|
|
*/
|
|
|
|
protected $function_checkers = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<int, NamespaceChecker>
|
|
|
|
*/
|
|
|
|
protected $namespace_checkers = [];
|
|
|
|
|
|
|
|
/**
|
2017-01-12 06:54:41 +01:00
|
|
|
* @var Context
|
2017-01-02 21:31:18 +01:00
|
|
|
*/
|
2017-01-12 06:54:41 +01:00
|
|
|
public $context;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ProjectChecker
|
|
|
|
*/
|
|
|
|
public $project_checker;
|
|
|
|
|
2016-12-31 00:08:07 +01:00
|
|
|
/**
|
2017-01-02 07:07:44 +01:00
|
|
|
* @param string $file_path
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param ProjectChecker $project_checker
|
2017-01-02 07:07:44 +01:00
|
|
|
* @param array<int, PhpParser\Node\Expr|PhpParser\Node\Stmt> $preloaded_statements
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public function __construct($file_path, ProjectChecker $project_checker, array $preloaded_statements = [])
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-12-31 00:08:07 +01:00
|
|
|
$this->file_path = $file_path;
|
2016-12-08 04:38:57 +01:00
|
|
|
$this->file_name = Config::getInstance()->shortenFileName($this->file_path);
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = $project_checker;
|
2016-04-04 01:41:54 +02:00
|
|
|
|
2016-12-31 00:08:07 +01:00
|
|
|
if (!isset(self::$storage[$file_path])) {
|
|
|
|
self::$storage[$file_path] = new FileStorage();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-01-12 06:54:41 +01:00
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
$this->context = new Context();
|
2017-02-01 05:24:33 +01:00
|
|
|
$this->context->count_references = $project_checker->count_references;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param Context|null $file_context
|
2017-01-02 21:31:18 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function visit(Context $file_context = null)
|
|
|
|
{
|
2017-01-12 06:54:41 +01:00
|
|
|
$this->context = $file_context ?: $this->context;
|
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-12-25 02:08:58 +01:00
|
|
|
/** @var array<int, PhpParser\Node\Expr|PhpParser\Node\Stmt> */
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts = [];
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var array<int, PhpParser\Node\Stmt\Const_> */
|
|
|
|
$leftover_const_stmts = [];
|
2016-08-14 06:38:29 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker = new StatementsChecker($this);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-02-01 02:47:16 +01:00
|
|
|
$predefined_classlikes = $config->getPredefinedClassLikes();
|
2017-02-01 01:21:33 +01:00
|
|
|
|
2016-12-28 18:59:51 +01:00
|
|
|
$classes_to_check = [];
|
|
|
|
$interfaces_to_check = [];
|
2017-01-02 21:31:18 +01:00
|
|
|
$function_stmts = [];
|
2016-12-28 18:59:51 +01:00
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
2017-02-01 01:21:33 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassLike && $stmt->name) {
|
|
|
|
if (isset($predefined_classlikes[strtolower($stmt->name)])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateClass(
|
|
|
|
'Class ' . $stmt->name . ' has already been defined internally',
|
|
|
|
new \Psalm\CodeLocation($this, $stmt, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
2017-01-02 21:31:18 +01:00
|
|
|
$class_checker = new ClassChecker($stmt, $this, $stmt->name);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$fq_class_name = $class_checker->getFQCLN();
|
|
|
|
|
|
|
|
$this->class_checkers_no_methods[$fq_class_name] = $class_checker;
|
|
|
|
$classes_to_check[] = $class_checker;
|
2017-02-01 01:21:33 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
|
2017-01-02 21:31:18 +01:00
|
|
|
$class_checker = new InterfaceChecker($stmt, $this, $stmt->name);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$fq_class_name = $class_checker->getFQCLN();
|
|
|
|
|
|
|
|
$this->interface_checkers_no_methods[$fq_class_name] = $class_checker;
|
|
|
|
$interfaces_to_check[] = $class_checker;
|
2017-02-01 01:21:33 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
|
2017-01-02 21:31:18 +01:00
|
|
|
$trait_checker = new TraitChecker($stmt, $this, $stmt->name);
|
2017-02-01 01:21:33 +01:00
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Namespace_) {
|
|
|
|
$namespace_name = $stmt->name ? implode('\\', $stmt->name->parts) : '';
|
2016-07-24 23:06:54 +02:00
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
$namespace_checker = new NamespaceChecker($stmt, $this);
|
2016-11-13 00:51:48 +01:00
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
$namespace_checker->visit();
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
$this->namespace_checkers[] = $namespace_checker;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
$this->namespace_aliased_classes[$namespace_name] = $namespace_checker->getAliasedClasses();
|
|
|
|
$this->namespace_aliased_classes_flipped[$namespace_name] =
|
|
|
|
$namespace_checker->getAliasedClassesFlipped();
|
2017-01-02 21:31:18 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
$function_stmts[] = $stmt;
|
2017-01-08 17:24:01 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
$this->visitUse($stmt);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\GroupUse) {
|
|
|
|
$this->visitGroupUse($stmt);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-01-20 00:27:06 +01:00
|
|
|
$leftover_stmts[] = $stmt;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$function_checkers = [];
|
|
|
|
|
|
|
|
// hoist functions to the top
|
|
|
|
foreach ($function_stmts as $stmt) {
|
2017-01-06 07:07:11 +01:00
|
|
|
$function_checkers[$stmt->name] = new FunctionChecker($stmt, $this);
|
2017-01-02 21:31:18 +01:00
|
|
|
$function_id = $function_checkers[$stmt->name]->getMethodId();
|
|
|
|
$this->function_checkers[$function_id] = $function_checkers[$stmt->name];
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are any leftover statements, evaluate them,
|
|
|
|
// in turn causing the classes/interfaces be evaluated
|
|
|
|
if ($leftover_stmts) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$statements_checker->analyze($leftover_stmts, $this->context);
|
2016-12-28 18:59:51 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
// check any leftover interfaces not already evaluated
|
|
|
|
foreach ($this->interface_checkers_no_methods as $interface_checker) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$interface_checker->visit();
|
2016-12-28 18:59:51 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
// check any leftover classes not already evaluated
|
|
|
|
foreach ($this->class_checkers_no_methods as $class_checker) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$class_checker->visit();
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
2016-12-29 04:11:50 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->class_checkers = $classes_to_check;
|
|
|
|
$this->function_checkers = $function_checkers;
|
|
|
|
|
|
|
|
self::$files_checked[$this->file_path] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean $update_docblocks
|
2017-01-12 03:37:53 +01:00
|
|
|
* @param boolean $preserve_checkers
|
2017-01-02 21:31:18 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public function analyze($update_docblocks = false, $preserve_checkers = false)
|
2017-01-02 21:31:18 +01:00
|
|
|
{
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
foreach ($this->namespace_checkers as $namespace_checker) {
|
2017-01-13 20:06:05 +01:00
|
|
|
$namespace_checker->analyze(clone $this->context, $preserve_checkers, $update_docblocks);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-04-27 00:18:49 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
foreach ($this->class_checkers as $class_checker) {
|
2017-01-07 21:57:25 +01:00
|
|
|
$class_checker->analyze(null, $this->context, $update_docblocks);
|
2016-08-05 21:11:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
foreach ($this->function_checkers as $function_checker) {
|
2017-01-17 00:33:04 +01:00
|
|
|
$function_context = new Context($this->context->self);
|
2017-02-01 05:24:33 +01:00
|
|
|
$function_context->count_references = $this->project_checker->count_references;
|
2017-01-07 21:09:47 +01:00
|
|
|
$function_checker->analyze($function_context, $this->context);
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-16 18:59:09 +01:00
|
|
|
if (!$config->excludeIssueInFile('InvalidReturnType', $this->file_path)) {
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var string */
|
|
|
|
$method_id = $function_checker->getMethodId();
|
|
|
|
|
|
|
|
$return_type = FunctionChecker::getFunctionReturnType(
|
|
|
|
$method_id,
|
|
|
|
$this->file_path
|
|
|
|
);
|
|
|
|
|
|
|
|
$return_type_location = FunctionChecker::getFunctionReturnTypeLocation(
|
|
|
|
$method_id,
|
|
|
|
$this->file_path
|
|
|
|
);
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
$function_checker->verifyReturnType(
|
2017-01-02 21:31:18 +01:00
|
|
|
false,
|
|
|
|
$return_type,
|
|
|
|
null,
|
|
|
|
$return_type_location
|
|
|
|
);
|
|
|
|
}
|
2016-08-05 21:11:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if (!$preserve_checkers) {
|
|
|
|
$this->namespace_checkers = [];
|
2017-01-07 21:57:25 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$this->class_checkers = [];
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$this->function_checkers = [];
|
|
|
|
}
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
if ($update_docblocks && isset(self::$docblock_return_types[$this->file_name])) {
|
2016-11-13 05:59:31 +01:00
|
|
|
$line_upset = 0;
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
$file_lines = explode(PHP_EOL, (string)file_get_contents($this->file_path));
|
2016-11-13 05:59:31 +01:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
$file_docblock_updates = self::$docblock_return_types[$this->file_name];
|
2016-11-13 05:59:31 +01:00
|
|
|
|
|
|
|
foreach ($file_docblock_updates as $line_number => $type) {
|
2016-11-14 02:32:09 +01:00
|
|
|
self::updateDocblock($file_lines, $line_number, $line_upset, $type[0], $type[1], $type[2]);
|
2016-11-13 05:59:31 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
file_put_contents($this->file_path, implode(PHP_EOL, $file_lines));
|
2016-11-13 05:59:31 +01:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
echo 'Added/updated ' . count($file_docblock_updates) . ' docblocks in ' . $this->file_name . PHP_EOL;
|
2016-11-13 05:59:31 +01:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
/**
|
2017-01-12 06:54:41 +01:00
|
|
|
* @param string $method_id
|
2017-01-12 03:37:53 +01:00
|
|
|
* @param Context $this_context
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 06:54:41 +01:00
|
|
|
public function getMethodMutations($method_id, Context &$this_context)
|
2017-01-12 03:37:53 +01:00
|
|
|
{
|
2017-01-12 06:54:41 +01:00
|
|
|
list($fq_class_name, $method_name) = explode('::', $method_id);
|
2017-01-17 00:33:04 +01:00
|
|
|
$call_context = new Context((string) $this_context->vars_in_scope['$this']);
|
2017-01-12 03:37:53 +01:00
|
|
|
$call_context->collect_mutations = true;
|
|
|
|
|
|
|
|
foreach ($this_context->vars_possibly_in_scope as $var => $type) {
|
|
|
|
if (strpos($var, '$this->') === 0) {
|
|
|
|
$call_context->vars_possibly_in_scope[$var] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this_context->vars_in_scope as $var => $type) {
|
|
|
|
if (strpos($var, '$this->') === 0) {
|
|
|
|
$call_context->vars_in_scope[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$call_context->vars_in_scope['$this'] = $this_context->vars_in_scope['$this'];
|
|
|
|
|
2017-01-12 06:54:41 +01:00
|
|
|
$checked = false;
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
foreach ($this->class_checkers as $class_checker) {
|
2017-01-12 06:54:41 +01:00
|
|
|
if (strtolower($class_checker->getFQCLN()) === strtolower($fq_class_name)) {
|
|
|
|
$class_checker->getMethodMutations($method_name, $call_context);
|
|
|
|
$checked = true;
|
|
|
|
break;
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 06:54:41 +01:00
|
|
|
foreach ($this->namespace_checkers as $namespace_checker) {
|
|
|
|
foreach ($namespace_checker->class_checkers as $class_checker) {
|
|
|
|
if (strtolower($class_checker->getFQCLN()) === strtolower($fq_class_name)) {
|
|
|
|
$class_checker->getMethodMutations($method_name, $call_context);
|
|
|
|
$checked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$checked) {
|
|
|
|
throw new \UnexpectedValueException('Method ' . $method_id . ' could not be checked');
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:19:36 +01:00
|
|
|
foreach ($call_context->vars_possibly_in_scope as $var => $_) {
|
2017-01-12 03:37:53 +01:00
|
|
|
$this_context->vars_possibly_in_scope[$var] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($call_context->vars_in_scope as $var => $type) {
|
|
|
|
$this_context->vars_in_scope[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param Context|null $file_context
|
|
|
|
* @param boolean $update_docblocks
|
|
|
|
* @return void
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public function visitAndAnalyzeMethods(Context $file_context = null, $update_docblocks = false)
|
2016-01-11 20:21:29 +01:00
|
|
|
{
|
2017-01-19 18:15:42 +01:00
|
|
|
$this->project_checker->registerVisitedFile($this->file_path);
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->visit($file_context);
|
2017-01-07 21:57:25 +01:00
|
|
|
$this->analyze($update_docblocks);
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
2016-02-04 22:05:36 +01:00
|
|
|
|
2017-01-07 23:24:43 +01:00
|
|
|
/**
|
|
|
|
* Used when checking single files with multiple classlike declarations
|
|
|
|
*
|
|
|
|
* @param string $fq_class_name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function containsUnEvaluatedClassLike($fq_class_name)
|
|
|
|
{
|
|
|
|
return isset($this->interface_checkers_no_methods[$fq_class_name]) ||
|
|
|
|
isset($this->class_checkers_no_methods[$fq_class_name]);
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* When evaluating a file, we wait until a class is actually used to evaluate its contents
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @return null|false
|
2016-06-20 06:38:13 +02:00
|
|
|
*/
|
2017-02-01 05:24:33 +01:00
|
|
|
public function evaluateClassLike($fq_class_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2017-01-02 21:31:18 +01:00
|
|
|
if (isset($this->interface_checkers_no_methods[$fq_class_name])) {
|
2017-01-09 04:31:18 +01:00
|
|
|
$interface_checker = $this->interface_checkers_no_methods[$fq_class_name];
|
|
|
|
|
|
|
|
unset($this->interface_checkers_no_methods[$fq_class_name]);
|
|
|
|
|
|
|
|
if ($interface_checker->visit() === false) {
|
2017-01-02 21:31:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (isset($this->class_checkers_no_methods[$fq_class_name])) {
|
2017-01-09 04:31:18 +01:00
|
|
|
$class_checker = $this->class_checkers_no_methods[$fq_class_name];
|
|
|
|
|
|
|
|
unset($this->class_checkers_no_methods[$fq_class_name]);
|
|
|
|
|
|
|
|
if ($class_checker->visit(null, $this->context) === false) {
|
2017-01-02 21:31:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->project_checker->visitFileForClassLike($fq_class_name);
|
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-12-25 02:08:58 +01:00
|
|
|
* @return array<int, \PhpParser\Node\Expr|\PhpParser\Node\Stmt>
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-06-18 20:45:55 +02:00
|
|
|
protected function getStatements()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
return $this->preloaded_statements
|
|
|
|
? $this->preloaded_statements
|
2017-01-12 03:37:53 +01:00
|
|
|
: self::getStatementsForFile($this->file_path, $this->project_checker->debug_output);
|
2016-06-18 20:45:55 +02:00
|
|
|
}
|
|
|
|
|
2017-01-08 01:07:58 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function fileExists($file_path)
|
|
|
|
{
|
|
|
|
return file_exists($file_path) || isset($this->project_checker->fake_files[$file_path]);
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $file_path
|
|
|
|
* @param bool $debug_output
|
2016-12-25 02:08:58 +01:00
|
|
|
* @return array<int, \PhpParser\Node\Expr|\PhpParser\Node\Stmt>
|
2016-06-18 20:45:55 +02:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function getStatementsForFile($file_path, $debug_output = false)
|
2016-01-08 04:52:26 +01:00
|
|
|
{
|
|
|
|
$stmts = [];
|
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
$project_checker = ProjectChecker::getInstance();
|
2016-11-18 22:13:59 +01:00
|
|
|
$root_cache_directory = Config::getInstance()->getCacheDirectory();
|
2016-12-07 20:13:39 +01:00
|
|
|
$parser_cache_directory = $root_cache_directory
|
2017-01-18 04:10:21 +01:00
|
|
|
? $root_cache_directory . DIRECTORY_SEPARATOR . self::PARSER_CACHE_DIRECTORY
|
2016-12-07 20:13:39 +01:00
|
|
|
: null;
|
2016-01-08 16:47:15 +01:00
|
|
|
$from_cache = false;
|
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
$cache_location = null;
|
2016-11-18 22:13:59 +01:00
|
|
|
$name_cache_key = null;
|
2016-06-14 01:57:32 +02:00
|
|
|
|
2016-12-15 01:24:16 +01:00
|
|
|
$version = 'parsercache4';
|
2016-12-04 01:11:30 +01:00
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
$file_contents = $project_checker->getFileContents($file_path);
|
2016-12-04 01:11:30 +01:00
|
|
|
$file_content_hash = md5($version . $file_contents);
|
2016-12-08 04:38:57 +01:00
|
|
|
$name_cache_key = self::getParserCacheKey($file_path);
|
2016-11-04 22:45:12 +01:00
|
|
|
|
2016-11-18 22:13:59 +01:00
|
|
|
if (self::$file_content_hashes === null) {
|
|
|
|
/** @var array<string, string> */
|
2016-12-07 20:13:39 +01:00
|
|
|
self::$file_content_hashes = $root_cache_directory &&
|
2017-01-18 04:10:21 +01:00
|
|
|
is_readable($root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_HASHES)
|
|
|
|
? unserialize((string)file_get_contents($root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_HASHES))
|
2016-12-07 20:13:39 +01:00
|
|
|
: [];
|
2016-11-18 22:13:59 +01:00
|
|
|
}
|
2016-01-08 04:52:26 +01:00
|
|
|
|
2016-11-18 22:13:59 +01:00
|
|
|
if ($parser_cache_directory) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$cache_location = $parser_cache_directory . DIRECTORY_SEPARATOR . $name_cache_key;
|
2016-01-08 04:52:26 +01:00
|
|
|
|
2016-11-18 22:13:59 +01:00
|
|
|
if (isset(self::$file_content_hashes[$name_cache_key]) &&
|
|
|
|
$file_content_hash === self::$file_content_hashes[$name_cache_key] &&
|
|
|
|
is_readable($cache_location) &&
|
2016-12-08 04:38:57 +01:00
|
|
|
filemtime($cache_location) > filemtime($file_path)
|
2016-11-18 22:13:59 +01:00
|
|
|
) {
|
2016-12-25 02:08:58 +01:00
|
|
|
/** @var array<int, \PhpParser\Node\Expr|\PhpParser\Node\Stmt> */
|
2016-11-06 05:59:29 +01: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-11-06 05:59:29 +01:00
|
|
|
if (!$stmts) {
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($debug_output) {
|
|
|
|
echo 'Parsing ' . $file_path . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
$lexer = new PhpParser\Lexer([
|
|
|
|
'usedAttributes' => [
|
|
|
|
'comments', 'startLine', 'startFilePos', 'endFilePos'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, $lexer);
|
2016-01-08 04:52:26 +01:00
|
|
|
|
2016-12-25 02:08:58 +01:00
|
|
|
/** @var array<int, \PhpParser\Node\Expr|\PhpParser\Node\Stmt> */
|
2016-11-18 22:13:59 +01:00
|
|
|
$stmts = $parser->parse($file_contents);
|
2016-01-08 04:52:26 +01:00
|
|
|
}
|
|
|
|
|
2016-11-18 22:13:59 +01:00
|
|
|
if ($parser_cache_directory && $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-11-18 22:13:59 +01:00
|
|
|
if (!is_dir($parser_cache_directory)) {
|
|
|
|
mkdir($parser_cache_directory, 0777, true);
|
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-11-18 22:13:59 +01:00
|
|
|
|
|
|
|
self::$file_content_hashes[$name_cache_key] = $file_content_hash;
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
file_put_contents(
|
2017-01-18 04:10:21 +01:00
|
|
|
$root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_HASHES,
|
2016-12-07 20:13:39 +01:00
|
|
|
serialize(self::$file_content_hashes)
|
|
|
|
);
|
2016-01-08 16:47:15 +01:00
|
|
|
}
|
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-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
2016-12-17 06:48:31 +01:00
|
|
|
* @psalm-suppress MixedAssignment
|
2017-01-02 07:07:44 +01:00
|
|
|
* @psalm-suppress InvalidPropertyAssignment
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-10-05 19:24:46 +02:00
|
|
|
public static function loadReferenceCache()
|
|
|
|
{
|
2016-11-04 22:45:12 +01:00
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
if ($cache_directory) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME;
|
2016-10-05 19:24:46 +02:00
|
|
|
|
|
|
|
if (is_readable($cache_location)) {
|
2016-12-17 06:48:31 +01:00
|
|
|
$reference_cache = unserialize((string) file_get_contents($cache_location));
|
|
|
|
|
|
|
|
if (!is_array($reference_cache)) {
|
|
|
|
throw new \UnexpectedValueException('The reference cache must be an array');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$file_references = $reference_cache;
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-05 19:24:46 +02:00
|
|
|
public static function updateReferenceCache()
|
|
|
|
{
|
2016-11-04 22:45:12 +01:00
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
if ($cache_directory) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME;
|
2016-10-05 19:24:46 +02:00
|
|
|
|
|
|
|
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-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
|
2016-11-13 00:51:48 +01:00
|
|
|
* @return array<string, string>
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
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-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* @param string|null $namespace_name
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getAliasedClassesFlipped($namespace_name = null)
|
|
|
|
{
|
|
|
|
if ($namespace_name && isset($this->namespace_aliased_classes_flipped[$namespace_name])) {
|
|
|
|
return $this->namespace_aliased_classes_flipped[$namespace_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->aliased_classes_flipped;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $source_file
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public static function addFileReferenceToClass($source_file, $fq_class_name)
|
2016-10-05 01:23:38 +02:00
|
|
|
{
|
2016-10-05 19:24:46 +02:00
|
|
|
self::$referencing_files[$source_file] = true;
|
2016-11-07 23:29:51 +01:00
|
|
|
self::$file_references_to_class[$fq_class_name][$source_file] = true;
|
2016-10-05 01:23:38 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $source_file
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public static function addFileInheritanceToClass($source_file, $fq_class_name)
|
2016-10-05 23:08:20 +02:00
|
|
|
{
|
2016-11-07 23:29:51 +01:00
|
|
|
self::$files_inheriting_classes[$fq_class_name][$source_file] = true;
|
2016-10-05 23:08:20 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return array
|
|
|
|
*/
|
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])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$referenced_files = array_merge(
|
|
|
|
$referenced_files,
|
|
|
|
array_keys(self::$file_references_to_class[$file_class])
|
|
|
|
);
|
2016-10-05 01:23:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($referenced_files);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return array
|
|
|
|
*/
|
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])) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$referenced_files = array_merge(
|
|
|
|
$referenced_files,
|
|
|
|
array_keys(self::$files_inheriting_classes[$file_class])
|
|
|
|
);
|
2016-10-05 23:08:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-07 06:58:08 +02:00
|
|
|
public static function canDiffFiles()
|
|
|
|
{
|
2016-11-04 22:45:12 +01:00
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
2017-01-18 04:10:21 +01:00
|
|
|
return $cache_directory && file_exists($cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME);
|
2016-10-07 06:58:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 06:53:43 +02:00
|
|
|
/**
|
2016-11-06 05:59:29 +01:00
|
|
|
* @return int
|
2016-10-14 06:53:43 +02:00
|
|
|
*/
|
2016-11-06 05:59:29 +01:00
|
|
|
public static function getLastGoodRun()
|
2016-10-07 06:58:08 +02:00
|
|
|
{
|
|
|
|
if (self::$last_good_run === null) {
|
2016-11-04 22:45:12 +01:00
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
2017-01-18 04:10:21 +01:00
|
|
|
self::$last_good_run = filemtime($cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME) ?: 0;
|
2016-10-07 06:58:08 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
return self::$last_good_run;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-20 00:59:14 +01:00
|
|
|
* @param string $file_path
|
2016-11-06 05:59:29 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-01-20 00:59:14 +01:00
|
|
|
public static function hasFileChanged($file_path)
|
2016-11-06 05:59:29 +01:00
|
|
|
{
|
2017-01-20 00:59:14 +01:00
|
|
|
return filemtime($file_path) > self::getLastGoodRun();
|
2016-10-07 06:58:08 +02:00
|
|
|
}
|
|
|
|
|
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),
|
2016-12-07 20:13:39 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
function ($file_name) {
|
2016-12-07 20:13:39 +01:00
|
|
|
return !file_exists($file_name);
|
2016-10-07 19:26:29 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$deleted_files;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-11-06 05:59:29 +01:00
|
|
|
* @param int $start_time
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-06 05:59:29 +01:00
|
|
|
public static function goodRun($start_time)
|
2016-10-07 06:58:08 +02:00
|
|
|
{
|
2016-11-04 22:45:12 +01:00
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
if ($cache_directory) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$run_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME;
|
2016-10-07 06:58:08 +02:00
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
touch($run_cache_location, $start_time);
|
2016-10-07 19:26:29 +02:00
|
|
|
|
|
|
|
$deleted_files = self::getDeletedReferencedFiles();
|
|
|
|
|
|
|
|
if ($deleted_files) {
|
|
|
|
foreach ($deleted_files as $file) {
|
|
|
|
unset(self::$file_references[$file]);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
file_put_contents(
|
2017-01-18 04:10:21 +01:00
|
|
|
$cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME,
|
2016-11-02 07:29:00 +01:00
|
|
|
serialize(self::$file_references)
|
|
|
|
);
|
2016-10-07 19:26:29 +02:00
|
|
|
}
|
2016-11-06 05:59:29 +01:00
|
|
|
|
2017-01-18 04:10:21 +01:00
|
|
|
$cache_directory .= DIRECTORY_SEPARATOR . self::PARSER_CACHE_DIRECTORY;
|
2016-11-06 05:59:29 +01:00
|
|
|
|
|
|
|
if (is_dir($cache_directory)) {
|
2016-11-22 01:07:56 +01:00
|
|
|
/** @var array<string> */
|
2016-11-06 05:59:29 +01:00
|
|
|
$directory_files = scandir($cache_directory);
|
|
|
|
|
|
|
|
foreach ($directory_files as $directory_file) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$full_path = $cache_directory . DIRECTORY_SEPARATOR . $directory_file;
|
2016-11-06 05:59:29 +01:00
|
|
|
|
|
|
|
if ($directory_file[0] === '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
touch($full_path);
|
|
|
|
}
|
|
|
|
}
|
2016-10-07 06:58:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-10 07:09:47 +02:00
|
|
|
public static function clearCache()
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
2016-10-05 19:24:46 +02:00
|
|
|
self::$files_checked = [];
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2016-12-31 00:08:07 +01:00
|
|
|
self::$storage = [];
|
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-12-08 21:57:18 +01:00
|
|
|
IssueBuffer::clearCache();
|
2017-01-27 07:23:12 +01:00
|
|
|
FunctionLikeChecker::clearCache();
|
2016-05-10 20:00:44 +02:00
|
|
|
}
|
2016-11-06 05:59:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float $time_before
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function deleteOldParserCaches($time_before)
|
|
|
|
{
|
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
$removed_count = 0;
|
|
|
|
|
|
|
|
if ($cache_directory) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$cache_directory .= DIRECTORY_SEPARATOR . self::PARSER_CACHE_DIRECTORY;
|
2016-11-06 05:59:29 +01:00
|
|
|
|
|
|
|
if (is_dir($cache_directory)) {
|
2016-11-22 01:07:56 +01:00
|
|
|
/** @var array<string> */
|
2016-11-06 05:59:29 +01:00
|
|
|
$directory_files = scandir($cache_directory);
|
|
|
|
|
|
|
|
foreach ($directory_files as $directory_file) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$full_path = $cache_directory . DIRECTORY_SEPARATOR . $directory_file;
|
2016-11-06 05:59:29 +01:00
|
|
|
|
|
|
|
if ($directory_file[0] === '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filemtime($full_path) < $time_before && is_writable($full_path)) {
|
|
|
|
unlink($full_path);
|
|
|
|
$removed_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $removed_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string> $file_names
|
|
|
|
* @param int $min_time
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function touchParserCaches(array $file_names, $min_time)
|
|
|
|
{
|
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
if ($cache_directory) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$cache_directory .= DIRECTORY_SEPARATOR . self::PARSER_CACHE_DIRECTORY;
|
2016-11-06 05:59:29 +01:00
|
|
|
|
|
|
|
if (is_dir($cache_directory)) {
|
|
|
|
foreach ($file_names as $file_name) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$hash_file_name = $cache_directory . DIRECTORY_SEPARATOR . self::getParserCacheKey($file_name);
|
2016-11-06 05:59:29 +01:00
|
|
|
|
|
|
|
if (file_exists($hash_file_name)) {
|
|
|
|
if (filemtime($hash_file_name) < $min_time) {
|
|
|
|
touch($hash_file_name, $min_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getParserCacheKey($file_name)
|
|
|
|
{
|
|
|
|
return md5($file_name);
|
|
|
|
}
|
2016-11-13 00:51:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a docblock to the given file
|
2016-11-21 19:37:27 +01:00
|
|
|
*
|
|
|
|
* @param string $file_name
|
|
|
|
* @param int $line_number
|
|
|
|
* @param string $docblock
|
|
|
|
* @param string $new_type
|
|
|
|
* @param string $phpdoc_type
|
2016-11-13 17:24:46 +01:00
|
|
|
* @return void
|
2016-11-13 00:51:48 +01:00
|
|
|
*/
|
2016-11-14 02:32:09 +01:00
|
|
|
public static function addDocblockReturnType($file_name, $line_number, $docblock, $new_type, $phpdoc_type)
|
2016-11-13 05:59:31 +01:00
|
|
|
{
|
2016-11-13 20:36:10 +01:00
|
|
|
$new_type = str_replace(['<mixed, mixed>', '<empty, empty>'], '', $new_type);
|
2016-11-13 05:59:31 +01:00
|
|
|
|
2016-11-14 02:32:09 +01:00
|
|
|
self::$docblock_return_types[$file_name][$line_number] = [$docblock, $new_type, $phpdoc_type];
|
2016-11-13 05:59:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $file_lines
|
|
|
|
* @param int $line_number
|
|
|
|
* @param int $line_upset
|
2016-11-13 17:24:46 +01:00
|
|
|
* @param string $existing_docblock
|
2016-11-13 05:59:31 +01:00
|
|
|
* @param string $type
|
2016-11-21 19:37:27 +01:00
|
|
|
* @param string $phpdoc_type
|
2016-11-13 05:59:31 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-08 01:07:58 +01:00
|
|
|
public static function updateDocblock(
|
|
|
|
array &$file_lines,
|
|
|
|
$line_number,
|
|
|
|
&$line_upset,
|
|
|
|
$existing_docblock,
|
|
|
|
$type,
|
|
|
|
$phpdoc_type
|
|
|
|
) {
|
2016-11-13 05:59:31 +01:00
|
|
|
$line_number += $line_upset;
|
|
|
|
$function_line = $file_lines[$line_number - 1];
|
|
|
|
$left_padding = str_replace(ltrim($function_line), '', $function_line);
|
|
|
|
|
|
|
|
$line_before = $file_lines[$line_number - 2];
|
|
|
|
|
|
|
|
$parsed_docblock = [];
|
|
|
|
$existing_line_count = $existing_docblock ? substr_count($existing_docblock, PHP_EOL) + 1 : 0;
|
|
|
|
|
|
|
|
if ($existing_docblock) {
|
|
|
|
$parsed_docblock = CommentChecker::parseDocComment($existing_docblock);
|
2017-01-07 20:35:07 +01:00
|
|
|
} else {
|
2016-11-13 05:59:31 +01:00
|
|
|
$parsed_docblock['description'] = '';
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 02:32:09 +01:00
|
|
|
$parsed_docblock['specials']['return'] = [$phpdoc_type];
|
|
|
|
|
|
|
|
if ($type !== $phpdoc_type) {
|
|
|
|
$parsed_docblock['specials']['psalm-return'] = [$type];
|
|
|
|
}
|
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
$new_docblock_lines = CommentChecker::renderDocComment($parsed_docblock, $left_padding);
|
|
|
|
|
|
|
|
$line_upset += count($new_docblock_lines) - $existing_line_count;
|
2016-11-13 00:51:48 +01:00
|
|
|
|
2016-11-13 05:59:31 +01:00
|
|
|
array_splice($file_lines, $line_number - $existing_line_count - 1, $existing_line_count, $new_docblock_lines);
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
2017-01-07 20:35:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileName()
|
|
|
|
{
|
|
|
|
return $this->file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFilePath()
|
|
|
|
{
|
|
|
|
return $this->file_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-08 01:33:33 +01:00
|
|
|
* @param string $file_name
|
|
|
|
* @param string $file_path
|
2017-01-07 20:35:07 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-08 01:33:33 +01:00
|
|
|
public function setFileName($file_name, $file_path)
|
2017-01-07 20:35:07 +01:00
|
|
|
{
|
2017-01-08 01:33:33 +01:00
|
|
|
$this->actual_file_name = $this->file_name;
|
|
|
|
$this->actual_file_path = $this->file_path;
|
|
|
|
|
|
|
|
$this->file_name = $file_name;
|
|
|
|
$this->file_path = $file_path;
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCheckedFileName()
|
|
|
|
{
|
2017-01-08 01:33:33 +01:00
|
|
|
return $this->actual_file_name ?: $this->file_name;
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCheckedFilePath()
|
|
|
|
{
|
2017-01-08 01:33:33 +01:00
|
|
|
return $this->actual_file_path ?: $this->file_path;
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSuppressedIssues()
|
|
|
|
{
|
|
|
|
return $this->suppressed_issues;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFQCLN()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-09 06:26:40 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|