1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 02:47:02 +01:00
psalm/src/Psalm/Checker/FileChecker.php

850 lines
26 KiB
PHP
Raw Normal View History

2016-01-08 00:28:27 +01:00
<?php
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;
use Psalm\Config;
use Psalm\Context;
2016-11-02 07:29:00 +01:00
use Psalm\StatementsSource;
2016-11-21 03:49:06 +01:00
class FileChecker extends SourceChecker implements StatementsSource
2016-01-08 00:28:27 +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';
2016-10-14 06:53:43 +02:00
/**
* @var string
*/
protected $real_file_name;
2016-10-14 06:53:43 +02:00
2016-11-13 00:51:48 +01:00
/**
* @var array<string, array<string, string>>
2016-10-31 20:42:20 +01:00
*/
protected $namespace_aliased_classes = [];
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
/**
* @var array<int, \PhpParser\Node>
*/
protected $preloaded_statements = [];
2016-04-04 01:41:54 +02:00
2016-10-14 06:53:43 +02:00
/**
2016-11-01 05:39:41 +01:00
* @var array<string, static>
2016-10-14 06:53:43 +02:00
*/
protected static $file_checkers = [];
2016-11-01 05:39:41 +01:00
/**
* @var array<string, bool>
*/
2016-08-15 05:24:16 +02:00
protected static $functions_checked = [];
2016-11-01 05:39:41 +01:00
/**
* @var array<string, bool>
*/
protected static $classes_checked = [];
2016-11-01 05:39:41 +01:00
/**
* @var array<string, bool>
*/
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;
/**
* 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>>
*/
protected static $file_references_to_class = [];
/**
* 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>}>
*/
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
/**
* @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
{
$this->real_file_name = $file_name;
2016-11-21 03:49:06 +01:00
$this->file_name = Config::getInstance()->shortenFileName($file_name);
2016-01-11 23:23:05 +01:00
2016-11-21 03:49:06 +01:00
self::$file_checkers[$this->file_name] = $this;
self::$file_checkers[$file_name] = $this;
2016-04-04 01:41:54 +02:00
if ($preloaded_statements) {
$this->preloaded_statements = $preloaded_statements;
2016-04-04 01:41:54 +02:00
}
2016-01-08 00:28:27 +01:00
}
2016-11-02 07:29:00 +01:00
/**
* @param bool $check_classes
* @param bool $check_functions
* @param Context|null $file_context
* @param bool $cache
2016-11-13 17:24:46 +01:00
* @param bool $update_docblocks
2016-11-02 07:29:00 +01:00
* @return array|null
*/
2016-11-13 00:51:48 +01:00
public function check(
$check_classes = true,
$check_functions = true,
Context $file_context = null,
$cache = true,
$update_docblocks = false
) {
2016-11-21 03:49:06 +01:00
if ($cache && isset(self::$functions_checked[$this->file_name])) {
2016-11-02 07:29:00 +01:00
return null;
2016-08-05 21:11:20 +02:00
}
2016-08-15 05:24:16 +02:00
if ($cache && $check_classes && !$check_functions && isset(self::$classes_checked[$this->real_file_name])) {
2016-11-02 07:29:00 +01:00
return null;
2016-08-05 21:11:20 +02:00
}
if ($cache && !$check_classes && !$check_functions && isset(self::$files_checked[$this->real_file_name])) {
2016-11-02 07:29:00 +01:00
return null;
2016-08-05 21:11:20 +02:00
}
if (!$file_context) {
2016-11-21 03:49:06 +01:00
$file_context = new Context($this->file_name);
}
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
$leftover_stmts = [];
$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) {
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_ &&
$stmt->name instanceof PhpParser\Node\Name)
2016-08-15 05:24:16 +02:00
|| $stmt instanceof PhpParser\Node\Stmt\Function_
) {
if ($leftover_stmts) {
$statments_checker->check($leftover_stmts, $file_context);
$leftover_stmts = [];
2016-01-08 00:28:27 +01:00
}
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
if ($check_classes) {
2016-11-02 07:29:00 +01:00
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name)
?: new ClassChecker($stmt, $this, $stmt->name);
2016-11-08 01:16:51 +01:00
$this->declared_classes[] = $class_checker->getFQCLN();
2016-11-13 00:51:48 +01:00
$class_checker->check($check_functions, null, $update_docblocks);
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
if ($check_classes) {
2016-11-02 07:29:00 +01:00
$class_checker = ClassLikeChecker::getClassLikeCheckerFromClass($stmt->name)
?: new InterfaceChecker($stmt, $this, $stmt->name);
2016-11-08 01:16:51 +01:00
$this->declared_classes[] = $class_checker->getFQCLN();
$class_checker->check(false);
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Trait_) {
if ($check_classes) {
2016-11-02 07:29:00 +01: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-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Namespace_ &&
$stmt->name instanceof PhpParser\Node\Name
) {
$namespace_name = implode('\\', $stmt->name->parts);
$namespace_checker = new NamespaceChecker($stmt, $this);
2016-11-13 00:51:48 +01:00
$namespace_checker->check(
2016-11-02 07:29:00 +01:00
$check_classes,
2016-11-13 00:51:48 +01:00
$check_functions,
$update_docblocks
2016-11-02 07:29:00 +01:00
);
2016-11-13 00:51:48 +01:00
$this->namespace_aliased_classes[$namespace_name] = $namespace_checker->getAliasedClasses();
$this->namespace_aliased_classes_flipped[$namespace_name] = $namespace_checker->getAliasedClassesFlipped();
$this->declared_classes = array_merge($namespace_checker->getDeclaredClasses());
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_ && $check_functions) {
2016-11-21 03:49:06 +01:00
$function_context = new Context($this->file_name, $file_context->self);
$function_checkers[$stmt->name]->check($function_context, $file_context);
2016-10-20 20:26:03 +02:00
2016-11-21 03:49:06 +01:00
if (!$config->excludeIssueInFile('InvalidReturnType', $this->file_name)) {
2016-10-20 20:26:03 +02:00
$function_checkers[$stmt->name]->checkReturnTypes();
}
2016-08-15 05:24:16 +02:00
}
2016-11-02 07:29:00 +01:00
} else {
$leftover_stmts[] = $stmt;
2016-01-08 00:28:27 +01:00
}
}
if ($leftover_stmts) {
$statments_checker->check($leftover_stmts, $file_context);
}
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) {
self::$classes_checked[$this->real_file_name] = true;
2016-08-05 21:11:20 +02:00
}
self::$files_checked[$this->real_file_name] = true;
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-11-13 17:24:46 +01:00
$file_lines = explode(PHP_EOL, (string)file_get_contents($this->real_file_name));
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) {
self::updateDocblock($file_lines, $line_number, $line_upset, $type[0], $type[1], $type[2]);
2016-11-13 05:59:31 +01:00
}
file_put_contents($this->real_file_name, implode(PHP_EOL, $file_lines));
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
}
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-11-08 01:16:51 +01:00
public static function getFQCLNFromNameInFile($class, $namespace, $file_name)
2016-01-11 20:21:29 +01:00
{
if (isset(self::$file_checkers[$file_name])) {
$aliased_classes = self::$file_checkers[$file_name]->getAliasedClasses($namespace);
} else {
$file_checker = new FileChecker($file_name);
$file_checker->check(false, false, new Context($file_name));
$aliased_classes = $file_checker->getAliasedClasses($namespace);
}
2016-11-08 01:16:51 +01:00
return ClassLikeChecker::getFQCLNFromString($class, $namespace, $aliased_classes);
}
2016-02-04 22:05:36 +01:00
/**
* Gets a list of the classes declared in that file
2016-11-02 07:29:00 +01:00
*
* @param string $file_name
* @return array<string>
*/
public static function getDeclaredClassesInFile($file_name)
2016-01-08 00:28:27 +01:00
{
if (isset(self::$file_checkers[$file_name])) {
$file_checker = self::$file_checkers[$file_name];
2016-11-02 07:29:00 +01:00
} else {
$file_checker = new FileChecker($file_name);
$file_checker->check(false, false, new Context($file_name));
2016-01-08 00:28:27 +01: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-11-02 07:29:00 +01: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-11-01 05:39:41 +01:00
* @return array<int, \PhpParser\Node>
2016-06-18 20:45:55 +02:00
*/
public static function getStatementsForFile($file_name)
2016-01-08 04:52:26 +01:00
{
$stmts = [];
2016-11-18 22:13:59 +01:00
$root_cache_directory = Config::getInstance()->getCacheDirectory();
$parser_cache_directory = $root_cache_directory ? $root_cache_directory . '/' . self::PARSER_CACHE_DIRECTORY : null;
2016-01-08 16:47:15 +01:00
$from_cache = false;
$cache_location = null;
2016-11-18 22:13:59 +01:00
$name_cache_key = null;
2016-11-18 22:13:59 +01:00
$file_contents = (string)file_get_contents($file_name);
$file_content_hash = md5($file_contents);
$name_cache_key = self::getParserCacheKey($file_name);
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> */
self::$file_content_hashes = $root_cache_directory && is_readable($root_cache_directory . '/' . self::FILE_HASHES)
? unserialize((string)file_get_contents($root_cache_directory . '/' . self::FILE_HASHES))
: [];
}
2016-01-08 04:52:26 +01:00
2016-11-18 22:13:59 +01:00
if ($parser_cache_directory) {
$cache_location = $parser_cache_directory . '/' . $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) &&
filemtime($cache_location) > filemtime($file_name)
) {
/** @var array<int, \PhpParser\Node> */
$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
}
}
if (!$stmts) {
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
2016-01-08 04:52:26 +01:00
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);
} 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;
file_put_contents($root_cache_directory . '/' . self::FILE_HASHES, 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
*/
public static function loadReferenceCache()
{
2016-11-04 22:45:12 +01:00
$cache_directory = Config::getInstance()->getCacheDirectory();
if ($cache_directory) {
$cache_location = $cache_directory . '/' . 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 23:08:20 +02:00
return false;
}
2016-11-02 07:29:00 +01:00
/**
* @return void
*/
public static function updateReferenceCache()
{
2016-11-04 22:45:12 +01:00
$cache_directory = Config::getInstance()->getCacheDirectory();
if ($cache_directory) {
$cache_location = $cache_directory . '/' . 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
];
}
file_put_contents($cache_location, serialize(self::$file_references));
}
}
2016-04-27 00:42:48 +02:00
/**
* @return null
*/
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
*/
public function getAliasedClasses($namespace_name = null)
{
if ($namespace_name && isset($this->namespace_aliased_classes[$namespace_name])) {
return $this->namespace_aliased_classes[$namespace_name];
}
return $this->aliased_classes;
}
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
/**
* @return string
*/
public function getRealFileName()
{
return $this->real_file_name;
}
2016-11-02 07:29:00 +01:00
/**
* @param string $file_name
* @return mixed
*/
2016-03-23 18:05:25 +01:00
public static function getFileCheckerFromFileName($file_name)
{
return self::$file_checkers[$file_name];
}
2016-10-12 07:38:29 +02:00
/**
* @param string $class_name
* @return ClassLikeChecker|null
*/
public static function getClassLikeCheckerFromClass($class_name)
{
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);
if (isset(self::$file_checkers[$file_name])) {
$file_checker = self::$file_checkers[$file_name];
2016-11-02 07:29:00 +01:00
} else {
$file_checker = new FileChecker($file_name);
}
2016-08-05 21:11:20 +02:00
$file_checker->check(true, false, null, false);
return ClassLikeChecker::getClassLikeCheckerFromClass($class_name);
}
2016-11-02 07:29:00 +01:00
/**
* @return void
*/
2016-10-31 00:13:09 +01:00
protected function registerUses()
{
foreach ($this->getStatements() as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\Use_) {
2016-11-21 03:49:06 +01:00
$this->visitUse($stmt);
2016-10-31 00:13:09 +01:00
}
}
}
2016-11-02 07:29:00 +01:00
/**
* @param string $source_file
* @param string $fq_class_name
2016-11-02 07:29:00 +01:00
* @return void
*/
public static function addFileReferenceToClass($source_file, $fq_class_name)
{
self::$referencing_files[$source_file] = true;
self::$file_references_to_class[$fq_class_name][$source_file] = true;
}
2016-11-02 07:29:00 +01:00
/**
* @param string $source_file
* @param string $fq_class_name
2016-11-02 07:29:00 +01:00
* @return void
*/
public static function addFileInheritanceToClass($source_file, $fq_class_name)
2016-10-05 23:08:20 +02: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
*/
public static function calculateFilesReferencingFile($file)
{
$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])
);
}
}
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>
*/
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-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();
return $cache_directory && file_exists($cache_directory . '/' . self::GOOD_RUN_NAME);
2016-10-07 06:58:08 +02:00
}
2016-10-14 06:53:43 +02:00
/**
* @return int
2016-10-14 06:53:43 +02: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();
self::$last_good_run = filemtime($cache_directory . '/' . self::GOOD_RUN_NAME) ?: 0;
2016-10-07 06:58:08 +02:00
}
return self::$last_good_run;
}
/**
* @param string $file
* @return boolean
*/
public static function hasFileChanged($file)
{
return filemtime($file) > 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-11-02 07:29:00 +01:00
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-11-02 07:29:00 +01:00
/**
* @param int $start_time
2016-11-02 07:29:00 +01:00
* @return void
*/
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) {
$run_cache_location = $cache_directory . '/' . self::GOOD_RUN_NAME;
2016-10-07 06:58:08 +02: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(
2016-11-04 22:45:12 +01:00
$cache_directory . '/' . self::REFERENCE_CACHE_NAME,
2016-11-02 07:29:00 +01:00
serialize(self::$file_references)
);
2016-10-07 19:26:29 +02:00
}
$cache_directory .= '/' . self::PARSER_CACHE_DIRECTORY;
if (is_dir($cache_directory)) {
$directory_files = scandir($cache_directory);
foreach ($directory_files as $directory_file) {
$full_path = $cache_directory . '/' . $directory_file;
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
{
self::$file_checkers = [];
2016-08-15 05:24:16 +02:00
self::$functions_checked = [];
self::$classes_checked = [];
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();
}
/**
* @param float $time_before
* @return int
*/
public static function deleteOldParserCaches($time_before)
{
$cache_directory = Config::getInstance()->getCacheDirectory();
$removed_count = 0;
if ($cache_directory) {
$cache_directory .= '/' . self::PARSER_CACHE_DIRECTORY;
if (is_dir($cache_directory)) {
$directory_files = scandir($cache_directory);
foreach ($directory_files as $directory_file) {
$full_path = $cache_directory . '/' . $directory_file;
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) {
$cache_directory .= '/' . self::PARSER_CACHE_DIRECTORY;
if (is_dir($cache_directory)) {
foreach ($file_names as $file_name) {
$hash_file_name = $cache_directory . '/' . self::getParserCacheKey($file_name);
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-13 17:24:46 +01:00
* @param string $file_name
* @param int $line_number
* @param string $new_type
* @return void
2016-11-13 00:51:48 +01:00
*/
public static function addDocblockReturnType($file_name, $line_number, $docblock, $new_type, $phpdoc_type)
2016-11-13 05:59:31 +01:00
{
$new_type = str_replace(['<mixed, mixed>', '<empty, empty>'], '', $new_type);
2016-11-13 05:59:31 +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
* @return void
*/
public static function updateDocblock(array &$file_lines, $line_number, &$line_upset, $existing_docblock, $type, $phpdoc_type)
2016-11-13 00:51:48 +01:00
{
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);
}
else {
$parsed_docblock['description'] = '';
2016-11-13 00:51:48 +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
}
2016-01-08 00:28:27 +01:00
}