2017-02-18 19:41:27 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Provider;
|
|
|
|
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
2017-07-29 21:05:06 +02:00
|
|
|
use Psalm\Checker\ProjectChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Config;
|
2017-02-18 19:41:27 +01:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
/**
|
|
|
|
* @psalm-type IssueData = array{
|
|
|
|
* severity: string,
|
|
|
|
* line_from: int,
|
|
|
|
* line_to: int,
|
|
|
|
* type: string,
|
|
|
|
* message: string,
|
|
|
|
* file_name: string,
|
|
|
|
* file_path: string,
|
|
|
|
* snippet: string,
|
|
|
|
* from: int,
|
|
|
|
* to: int,
|
|
|
|
* snippet_from: int,
|
|
|
|
* snippet_to: int,
|
|
|
|
* column_from: int,
|
|
|
|
* column_to: int
|
|
|
|
* }
|
|
|
|
*/
|
2017-02-18 23:49:34 +01:00
|
|
|
/**
|
|
|
|
* Used to determine which files reference other files, necessary for using the --diff
|
|
|
|
* option from the command line.
|
|
|
|
*/
|
2017-02-18 19:41:27 +01:00
|
|
|
class FileReferenceProvider
|
|
|
|
{
|
|
|
|
const REFERENCE_CACHE_NAME = 'references';
|
2018-09-26 00:37:24 +02:00
|
|
|
const CORRECT_METHODS_CACHE_NAME = 'correct_methods';
|
|
|
|
const CLASS_METHOD_CACHE_NAME = 'class_method_references';
|
2018-09-26 22:33:59 +02:00
|
|
|
const ISSUES_CACHE_NAME = 'issues';
|
2017-02-18 19:41:27 +01: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 = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table used for getting all the files that reference any other file
|
|
|
|
*
|
|
|
|
* @var array<string,array<string,bool>>
|
|
|
|
*/
|
|
|
|
protected static $referencing_files = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, array<int,string>>
|
|
|
|
*/
|
|
|
|
protected static $files_inheriting_classes = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of all files deleted since the last successful run
|
|
|
|
*
|
|
|
|
* @var array<int, string>|null
|
|
|
|
*/
|
|
|
|
protected static $deleted_files = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table used for getting all the files referenced by a file
|
|
|
|
*
|
|
|
|
* @var array<string, array{a:array<int, string>, i:array<int, string>}>
|
|
|
|
*/
|
|
|
|
protected static $file_references = [];
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, array<string, bool>>
|
|
|
|
*/
|
|
|
|
private static $class_method_references = [];
|
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, array<int, IssueData>>
|
|
|
|
*/
|
|
|
|
private static $issues = [];
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public static function getDeletedReferencedFiles()
|
|
|
|
{
|
|
|
|
if (self::$deleted_files === null) {
|
|
|
|
self::$deleted_files = array_filter(
|
|
|
|
array_keys(self::$file_references),
|
|
|
|
/**
|
|
|
|
* @param string $file_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function ($file_name) {
|
|
|
|
return !file_exists($file_name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$deleted_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $source_file
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param string $fq_class_name_lc
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function addFileReferenceToClass($source_file, $fq_class_name_lc)
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
|
|
|
self::$referencing_files[$source_file] = true;
|
2017-07-25 22:11:02 +02:00
|
|
|
self::$file_references_to_class[$fq_class_name_lc][$source_file] = true;
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 18:38:38 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, array<string,bool>>
|
|
|
|
*/
|
|
|
|
public static function getAllFileReferences()
|
|
|
|
{
|
|
|
|
return self::$file_references_to_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, array<string,bool>> $references
|
2018-05-03 19:56:30 +02:00
|
|
|
* @psalm-suppress MixedTypeCoercion
|
2017-08-22 18:38:38 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addFileReferences(array $references)
|
|
|
|
{
|
|
|
|
self::$file_references_to_class = array_merge_recursive($references, self::$file_references_to_class);
|
|
|
|
}
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
/**
|
|
|
|
* @param string $source_file
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param string $fq_class_name_lc
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function addFileInheritanceToClass($source_file, $fq_class_name_lc)
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
self::$files_inheriting_classes[$fq_class_name_lc][$source_file] = true;
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function calculateFilesReferencingFile(ProjectChecker $project_checker, $file)
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
|
|
|
$referenced_files = [];
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$file_classes = ClassLikeChecker::getClassesForFile($project_checker, $file);
|
2017-02-18 19:41:27 +01:00
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
foreach ($file_classes as $file_class_lc => $_) {
|
|
|
|
if (isset(self::$file_references_to_class[$file_class_lc])) {
|
2017-02-18 19:41:27 +01:00
|
|
|
$referenced_files = array_merge(
|
|
|
|
$referenced_files,
|
2018-02-12 04:49:19 +01:00
|
|
|
array_keys(self::$file_references_to_class[$file_class_lc])
|
2017-02-18 19:41:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($referenced_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function calculateFilesInheritingFile(ProjectChecker $project_checker, $file)
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
|
|
|
$referenced_files = [];
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$file_classes = ClassLikeChecker::getClassesForFile($project_checker, $file);
|
2017-02-18 19:41:27 +01:00
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
foreach ($file_classes as $file_class_lc => $_) {
|
|
|
|
if (isset(self::$files_inheriting_classes[$file_class_lc])) {
|
2017-02-18 19:41:27 +01:00
|
|
|
$referenced_files = array_merge(
|
|
|
|
$referenced_files,
|
2018-02-12 04:49:19 +01:00
|
|
|
array_keys(self::$files_inheriting_classes[$file_class_lc])
|
2017-02-18 19:41:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($referenced_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function removeDeletedFilesFromReferences()
|
|
|
|
{
|
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
$deleted_files = self::getDeletedReferencedFiles();
|
|
|
|
|
|
|
|
if ($deleted_files) {
|
|
|
|
foreach ($deleted_files as $file) {
|
|
|
|
unset(self::$file_references[$file]);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents(
|
|
|
|
$cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME,
|
|
|
|
serialize(self::$file_references)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public static function getFilesReferencingFile($file)
|
|
|
|
{
|
|
|
|
return isset(self::$file_references[$file]['a']) ? self::$file_references[$file]['a'] : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public static function getFilesInheritingFromFile($file)
|
|
|
|
{
|
|
|
|
return isset(self::$file_references[$file]['i']) ? self::$file_references[$file]['i'] : [];
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
/**
|
2018-09-26 23:54:08 +02:00
|
|
|
* @return array<string, array<string, bool>>
|
2018-09-26 00:37:24 +02:00
|
|
|
*/
|
2018-09-26 23:54:08 +02:00
|
|
|
public static function getMethodsReferencing()
|
2018-09-26 00:37:24 +02:00
|
|
|
{
|
2018-09-26 23:54:08 +02:00
|
|
|
return self::$class_method_references;
|
2018-09-26 00:37:24 +02:00
|
|
|
}
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @psalm-suppress MixedAssignment
|
2018-05-03 19:56:30 +02:00
|
|
|
* @psalm-suppress MixedTypeCoercion
|
2017-02-18 19:41:27 +01:00
|
|
|
*/
|
|
|
|
public static function loadReferenceCache()
|
|
|
|
{
|
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
if ($cache_directory) {
|
2018-09-26 22:33:59 +02:00
|
|
|
$reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME;
|
2017-02-18 19:41:27 +01:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
if (!is_readable($reference_cache_location)) {
|
2018-09-26 00:37:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-18 19:41:27 +01:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
$reference_cache = unserialize((string) file_get_contents($reference_cache_location));
|
2018-09-26 00:37:24 +02:00
|
|
|
|
|
|
|
if (!is_array($reference_cache)) {
|
|
|
|
throw new \UnexpectedValueException('The reference cache must be an array');
|
|
|
|
}
|
2017-02-18 19:41:27 +01:00
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
self::$file_references = $reference_cache;
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
$class_method_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_METHOD_CACHE_NAME;
|
2018-09-26 00:37:24 +02:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
if (!is_readable($class_method_cache_location)) {
|
2018-09-26 00:37:24 +02:00
|
|
|
return false;
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
2018-09-26 00:37:24 +02:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
$class_method_reference_cache = unserialize((string) file_get_contents($class_method_cache_location));
|
2018-09-26 00:37:24 +02:00
|
|
|
|
|
|
|
if (!is_array($class_method_reference_cache)) {
|
|
|
|
throw new \UnexpectedValueException('The reference cache must be an array');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$class_method_references = $class_method_reference_cache;
|
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
$issues_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::ISSUES_CACHE_NAME;
|
|
|
|
|
|
|
|
if (!is_readable($issues_cache_location)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$issues_cache = unserialize((string) file_get_contents($issues_cache_location));
|
|
|
|
|
|
|
|
if (!is_array($issues_cache)) {
|
|
|
|
throw new \UnexpectedValueException('The reference cache must be an array');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$issues = $issues_cache;
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
return true;
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, bool> $visited_files
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-18 19:41:27 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function updateReferenceCache(ProjectChecker $project_checker, array $visited_files)
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
if ($cache_directory
|
|
|
|
&& !$project_checker->cache_provider instanceof \Psalm\Provider\NoCache\NoParserCacheProvider
|
|
|
|
) {
|
2017-02-18 19:41:27 +01:00
|
|
|
foreach ($visited_files as $file => $_) {
|
|
|
|
$all_file_references = array_unique(
|
|
|
|
array_merge(
|
|
|
|
isset(self::$file_references[$file]['a']) ? self::$file_references[$file]['a'] : [],
|
2017-07-29 21:05:06 +02:00
|
|
|
FileReferenceProvider::calculateFilesReferencingFile($project_checker, $file)
|
2017-02-18 19:41:27 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$inheritance_references = array_unique(
|
|
|
|
array_merge(
|
|
|
|
isset(self::$file_references[$file]['i']) ? self::$file_references[$file]['i'] : [],
|
2017-07-29 21:05:06 +02:00
|
|
|
FileReferenceProvider::calculateFilesInheritingFile($project_checker, $file)
|
2017-02-18 19:41:27 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
self::$file_references[$file] = [
|
|
|
|
'a' => $all_file_references,
|
2017-05-27 02:05:57 +02:00
|
|
|
'i' => $inheritance_references,
|
2017-02-18 19:41:27 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
$reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME;
|
2018-09-26 00:37:24 +02:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
file_put_contents($reference_cache_location, serialize(self::$file_references));
|
2018-09-26 00:37:24 +02:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
$method_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_METHOD_CACHE_NAME;
|
|
|
|
|
|
|
|
file_put_contents($method_cache_location, serialize(self::$class_method_references));
|
|
|
|
|
|
|
|
$issues_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::ISSUES_CACHE_NAME;
|
|
|
|
|
|
|
|
file_put_contents($issues_cache_location, serialize(self::$issues));
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 00:37:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, array<string, bool>>
|
|
|
|
*/
|
|
|
|
public static function getCorrectMethodCache(Config $config)
|
|
|
|
{
|
|
|
|
$cache_directory = $config->getCacheDirectory();
|
|
|
|
|
|
|
|
$correct_methods_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CORRECT_METHODS_CACHE_NAME;
|
|
|
|
|
|
|
|
if ($cache_directory
|
|
|
|
&& file_exists($correct_methods_cache_location)
|
|
|
|
&& filemtime($correct_methods_cache_location) > $config->modified_time
|
|
|
|
) {
|
|
|
|
/** @var array<string, array<string, bool>> */
|
|
|
|
return unserialize(file_get_contents($correct_methods_cache_location));
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, array<string, bool>> $correct_methods
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setCorrectMethodCache(array $correct_methods)
|
|
|
|
{
|
|
|
|
$cache_directory = Config::getInstance()->getCacheDirectory();
|
|
|
|
|
|
|
|
if ($cache_directory) {
|
|
|
|
$correct_methods_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CORRECT_METHODS_CACHE_NAME;
|
|
|
|
|
|
|
|
file_put_contents(
|
|
|
|
$correct_methods_cache_location,
|
|
|
|
serialize($correct_methods)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addReferenceToClassMethod(string $calling_method_id, string $referenced_member_id)
|
|
|
|
{
|
|
|
|
if (!isset(self::$class_method_references[$referenced_member_id])) {
|
|
|
|
self::$class_method_references[$referenced_member_id] = [$calling_method_id => true];
|
|
|
|
} else {
|
|
|
|
self::$class_method_references[$referenced_member_id][$calling_method_id] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, array<string,bool>>
|
|
|
|
*/
|
|
|
|
public static function getClassMethodReferences() : array
|
|
|
|
{
|
|
|
|
return self::$class_method_references;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, array<string,bool>> $references
|
|
|
|
* @psalm-suppress MixedTypeCoercion
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addClassMethodReferences(array $references)
|
|
|
|
{
|
|
|
|
foreach ($references as $referenced_member_id => $calling_method_ids) {
|
|
|
|
if (isset(self::$class_method_references[$referenced_member_id])) {
|
|
|
|
self::$class_method_references[$referenced_member_id] = array_merge(
|
|
|
|
self::$class_method_references[$referenced_member_id],
|
|
|
|
$calling_method_ids
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self::$class_method_references[$referenced_member_id] = $calling_method_ids;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, array<int, IssueData>>
|
|
|
|
*/
|
|
|
|
public static function getExistingIssues() : array
|
|
|
|
{
|
|
|
|
return self::$issues;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function clearExistingIssues()
|
|
|
|
{
|
|
|
|
self::$issues = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addIssue(string $file_path, array $issue)
|
|
|
|
{
|
|
|
|
if (!isset(self::$issues[$file_path])) {
|
|
|
|
self::$issues[$file_path] = [$issue];
|
|
|
|
} else {
|
|
|
|
self::$issues[$file_path][] = $issue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$file_references_to_class = [];
|
|
|
|
self::$referencing_files = [];
|
|
|
|
self::$files_inheriting_classes = [];
|
|
|
|
self::$deleted_files = null;
|
|
|
|
self::$file_references = [];
|
|
|
|
self::$class_method_references = [];
|
2018-09-26 22:33:59 +02:00
|
|
|
self::$issues = [];
|
2018-09-26 00:37:24 +02:00
|
|
|
}
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|