2018-01-21 19:38:51 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function array_combine;
|
|
|
|
use function array_merge;
|
|
|
|
use function count;
|
|
|
|
use function error_log;
|
|
|
|
use function explode;
|
|
|
|
use function in_array;
|
|
|
|
use function krsort;
|
|
|
|
use function ksort;
|
|
|
|
use LanguageServerProtocol\Command;
|
|
|
|
use LanguageServerProtocol\Position;
|
|
|
|
use LanguageServerProtocol\Range;
|
|
|
|
use const PHP_MAJOR_VERSION;
|
|
|
|
use const PHP_MINOR_VERSION;
|
2018-01-21 19:38:51 +01:00
|
|
|
use PhpParser;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function preg_match;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
|
2019-02-18 21:40:47 +01:00
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
2019-07-19 05:30:44 +02:00
|
|
|
use Psalm\Internal\Codebase\CallMap;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Provider\ClassLikeStorageProvider;
|
|
|
|
use Psalm\Internal\Provider\FileProvider;
|
|
|
|
use Psalm\Internal\Provider\FileReferenceProvider;
|
|
|
|
use Psalm\Internal\Provider\FileStorageProvider;
|
|
|
|
use Psalm\Internal\Provider\Providers;
|
|
|
|
use Psalm\Internal\Provider\StatementsProvider;
|
2019-05-30 16:30:41 +02:00
|
|
|
use Psalm\Progress\Progress;
|
|
|
|
use Psalm\Progress\VoidProgress;
|
2018-01-21 19:38:51 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
|
|
|
use Psalm\Storage\FileStorage;
|
|
|
|
use Psalm\Storage\FunctionLikeStorage;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function strlen;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strpos;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function strrpos;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
|
|
|
use function substr;
|
|
|
|
use function substr_count;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
class Codebase
|
|
|
|
{
|
|
|
|
/**
|
2018-04-05 20:40:41 +02:00
|
|
|
* @var Config
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
|
|
|
public $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A map of fully-qualified use declarations to the files
|
|
|
|
* that reference them (keyed by filename)
|
|
|
|
*
|
2019-04-13 00:28:07 +02:00
|
|
|
* @var array<string, array<int, \Psalm\CodeLocation>>
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
|
|
|
public $use_referencing_locations = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A map of file names to the classes that they contain explicit references to
|
|
|
|
* used in collaboration with use_referencing_locations
|
|
|
|
*
|
|
|
|
* @var array<string, array<string, bool>>
|
|
|
|
*/
|
|
|
|
public $use_referencing_files = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FileStorageProvider
|
|
|
|
*/
|
|
|
|
public $file_storage_provider;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ClassLikeStorageProvider
|
|
|
|
*/
|
|
|
|
public $classlike_storage_provider;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $collect_references = false;
|
|
|
|
|
2019-04-13 00:28:07 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $collect_locations = false;
|
|
|
|
|
2019-04-17 19:15:06 +02:00
|
|
|
/**
|
|
|
|
* @var null|'always'|'auto'
|
|
|
|
*/
|
|
|
|
public $find_unused_code = null;
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
|
|
|
* @var FileProvider
|
|
|
|
*/
|
2018-10-26 06:59:14 +02:00
|
|
|
public $file_provider;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
/**
|
|
|
|
* @var FileReferenceProvider
|
|
|
|
*/
|
|
|
|
public $file_reference_provider;
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
|
|
|
* @var StatementsProvider
|
|
|
|
*/
|
2018-02-19 17:53:30 +01:00
|
|
|
public $statements_provider;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
/**
|
2019-05-30 16:30:41 +02:00
|
|
|
* @var Progress
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2019-05-30 16:30:41 +02:00
|
|
|
private $progress;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @var array<string, Type\Union>
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
private static $stubbed_constants = [];
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
/**
|
|
|
|
* Whether to register autoloaded information
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $register_autoload_files = false;
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* Whether to log functions just at the file level or globally (for stubs)
|
|
|
|
*
|
|
|
|
* @var bool
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-06-30 21:29:37 +02:00
|
|
|
public $register_stub_files = false;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2019-03-05 21:45:09 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $find_unused_variables = false;
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\Reflection
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
private $reflection;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\Scanner
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public $scanner;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\Analyzer
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public $analyzer;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\Functions
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public $functions;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\ClassLikes
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public $classlikes;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-01-31 22:08:52 +01:00
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\Methods
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public $methods;
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\Properties
|
2018-02-09 00:14:28 +01:00
|
|
|
*/
|
|
|
|
public $properties;
|
|
|
|
|
2018-01-31 22:08:52 +01:00
|
|
|
/**
|
2018-11-12 16:46:55 +01:00
|
|
|
* @var Internal\Codebase\Populator
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public $populator;
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $server_mode = false;
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $store_node_types = false;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
/**
|
|
|
|
* Whether or not to infer types from usage. Computationally expensive, so turned off by default
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $infer_types_from_usage = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $alter_code = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $diff_methods = false;
|
|
|
|
|
2019-06-01 06:56:54 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2019-06-02 18:02:32 +02:00
|
|
|
public $methods_to_move = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $methods_to_rename = [];
|
2019-06-01 06:56:54 +02:00
|
|
|
|
2019-06-04 06:32:19 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $properties_to_move = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $properties_to_rename = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $class_constants_to_move = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $class_constants_to_rename = [];
|
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $classes_to_move = [];
|
|
|
|
|
2019-06-01 06:56:54 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $call_transforms = [];
|
|
|
|
|
2019-06-04 06:32:19 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $property_transforms = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $class_constant_transforms = [];
|
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $class_transforms = [];
|
|
|
|
|
2019-05-13 20:38:18 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $allow_backwards_incompatible_changes = true;
|
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $php_major_version = PHP_MAJOR_VERSION;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $php_minor_version = PHP_MINOR_VERSION;
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
public function __construct(
|
|
|
|
Config $config,
|
2018-09-28 22:18:45 +02:00
|
|
|
Providers $providers,
|
2019-05-30 16:30:41 +02:00
|
|
|
Progress $progress = null
|
2018-01-21 19:38:51 +01:00
|
|
|
) {
|
2019-05-30 16:30:41 +02:00
|
|
|
if ($progress === null) {
|
|
|
|
$progress = new VoidProgress();
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$this->config = $config;
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->file_storage_provider = $providers->file_storage_provider;
|
|
|
|
$this->classlike_storage_provider = $providers->classlike_storage_provider;
|
2019-05-30 16:30:41 +02:00
|
|
|
$this->progress = $progress;
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->file_provider = $providers->file_provider;
|
2018-10-07 04:58:21 +02:00
|
|
|
$this->file_reference_provider = $providers->file_reference_provider;
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->statements_provider = $providers->statements_provider;
|
2018-01-21 19:38:51 +01:00
|
|
|
|
|
|
|
self::$stubbed_constants = [];
|
|
|
|
|
2018-11-12 16:46:55 +01:00
|
|
|
$this->reflection = new Internal\Codebase\Reflection($providers->classlike_storage_provider, $this);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-11-12 16:46:55 +01:00
|
|
|
$this->scanner = new Internal\Codebase\Scanner(
|
2018-02-04 00:52:35 +01:00
|
|
|
$this,
|
|
|
|
$config,
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers->file_storage_provider,
|
|
|
|
$providers->file_provider,
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->reflection,
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers->file_reference_provider,
|
2019-05-30 16:30:41 +02:00
|
|
|
$progress
|
2018-02-04 00:52:35 +01:00
|
|
|
);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->loadAnalyzer();
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-11-12 16:46:55 +01:00
|
|
|
$this->functions = new Internal\Codebase\Functions($providers->file_storage_provider, $this->reflection);
|
2018-12-21 17:32:44 +01:00
|
|
|
|
2018-11-12 16:46:55 +01:00
|
|
|
$this->properties = new Internal\Codebase\Properties(
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers->classlike_storage_provider,
|
|
|
|
$providers->file_reference_provider
|
|
|
|
);
|
2018-10-07 02:11:19 +02:00
|
|
|
|
2018-11-12 16:46:55 +01:00
|
|
|
$this->classlikes = new Internal\Codebase\ClassLikes(
|
2018-10-07 02:11:19 +02:00
|
|
|
$this->config,
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers->classlike_storage_provider,
|
2019-04-13 00:28:07 +02:00
|
|
|
$providers->file_reference_provider,
|
2018-12-21 17:32:44 +01:00
|
|
|
$this->scanner
|
2018-02-04 00:52:35 +01:00
|
|
|
);
|
2018-12-21 17:32:44 +01:00
|
|
|
|
|
|
|
$this->methods = new Internal\Codebase\Methods(
|
|
|
|
$providers->classlike_storage_provider,
|
|
|
|
$providers->file_reference_provider,
|
|
|
|
$this->classlikes
|
|
|
|
);
|
|
|
|
|
2018-11-12 16:46:55 +01:00
|
|
|
$this->populator = new Internal\Codebase\Populator(
|
2018-02-04 00:52:35 +01:00
|
|
|
$config,
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers->classlike_storage_provider,
|
|
|
|
$providers->file_storage_provider,
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->classlikes,
|
2018-09-28 22:18:45 +02:00
|
|
|
$providers->file_reference_provider,
|
2019-05-30 16:30:41 +02:00
|
|
|
$progress
|
2018-02-04 00:52:35 +01:00
|
|
|
);
|
2018-10-07 02:11:19 +02:00
|
|
|
|
|
|
|
$this->loadAnalyzer();
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function loadAnalyzer()
|
|
|
|
{
|
2018-11-12 16:46:55 +01:00
|
|
|
$this->analyzer = new Internal\Codebase\Analyzer(
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->config,
|
|
|
|
$this->file_provider,
|
|
|
|
$this->file_storage_provider,
|
2019-05-30 16:30:41 +02:00
|
|
|
$this->progress
|
2018-09-28 22:18:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-16 21:59:11 +02:00
|
|
|
* @param array<string> $candidate_files
|
2018-09-28 22:18:45 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-11 18:01:14 +01:00
|
|
|
public function reloadFiles(ProjectAnalyzer $project_analyzer, array $candidate_files)
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
|
|
|
$this->loadAnalyzer();
|
|
|
|
|
2019-06-30 03:32:26 +02:00
|
|
|
$this->file_reference_provider->loadReferenceCache(false);
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2018-10-16 21:59:11 +02:00
|
|
|
if (!$this->statements_provider->parser_cache_provider) {
|
|
|
|
$diff_files = $candidate_files;
|
|
|
|
} else {
|
|
|
|
$diff_files = [];
|
|
|
|
|
|
|
|
$parser_cache_provider = $this->statements_provider->parser_cache_provider;
|
|
|
|
|
|
|
|
foreach ($candidate_files as $candidate_file_path) {
|
|
|
|
if ($parser_cache_provider->loadExistingFileContentsFromCache($candidate_file_path)
|
|
|
|
!== $this->file_provider->getContents($candidate_file_path)
|
|
|
|
) {
|
|
|
|
$diff_files[] = $candidate_file_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$referenced_files = $project_analyzer->getReferencedFilesFromDiff($diff_files, false);
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
foreach ($diff_files as $diff_file_path) {
|
|
|
|
$this->invalidateInformationForFile($diff_file_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($referenced_files as $referenced_file_path) {
|
2019-07-05 22:24:00 +02:00
|
|
|
if (in_array($referenced_file_path, $diff_files, true)) {
|
2018-09-28 22:18:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_storage = $this->file_storage_provider->get($referenced_file_path);
|
|
|
|
|
|
|
|
foreach ($file_storage->classlikes_in_file as $fq_classlike_name) {
|
|
|
|
$this->classlike_storage_provider->remove($fq_classlike_name);
|
|
|
|
$this->classlikes->removeClassLike($fq_classlike_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->file_storage_provider->remove($referenced_file_path);
|
|
|
|
$this->scanner->removeFile($referenced_file_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$referenced_files = array_combine($referenced_files, $referenced_files);
|
|
|
|
|
|
|
|
$this->scanner->addFilesToDeepScan($referenced_files);
|
2019-01-02 18:10:52 +01:00
|
|
|
$this->addFilesToAnalyze(array_combine($candidate_files, $candidate_files));
|
2019-06-30 03:32:26 +02:00
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->scanner->scanFiles($this->classlikes);
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
$this->file_reference_provider->updateReferenceCache($this, $referenced_files);
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
$this->populator->populateCodebase($this);
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
/** @return void */
|
|
|
|
public function enterServerMode()
|
|
|
|
{
|
|
|
|
$this->server_mode = true;
|
2019-02-24 07:33:25 +01:00
|
|
|
$this->store_node_types = true;
|
2018-10-17 21:52:26 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 00:28:07 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function collectLocations()
|
|
|
|
{
|
|
|
|
$this->collect_locations = true;
|
|
|
|
$this->classlikes->collect_locations = true;
|
|
|
|
$this->methods->collect_locations = true;
|
|
|
|
$this->properties->collect_locations = true;
|
|
|
|
}
|
|
|
|
|
2018-02-17 23:45:30 +01:00
|
|
|
/**
|
2019-04-17 19:15:06 +02:00
|
|
|
* @param 'always'|'auto' $find_unused_code
|
|
|
|
*
|
2018-02-17 23:45:30 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-04-17 19:15:06 +02:00
|
|
|
public function reportUnusedCode(string $find_unused_code = 'auto')
|
2018-02-17 23:45:30 +01:00
|
|
|
{
|
2019-04-17 19:15:06 +02:00
|
|
|
$this->collect_references = true;
|
|
|
|
$this->classlikes->collect_references = true;
|
|
|
|
$this->find_unused_code = $find_unused_code;
|
2019-03-05 21:45:09 +01:00
|
|
|
$this->find_unused_variables = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function reportUnusedVariables()
|
|
|
|
{
|
|
|
|
$this->collect_references = true;
|
|
|
|
$this->find_unused_variables = true;
|
2018-02-17 23:45:30 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param array<string, string> $files_to_analyze
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function addFilesToAnalyze(array $files_to_analyze)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->scanner->addFilesToDeepScan($files_to_analyze);
|
|
|
|
$this->analyzer->addFiles($files_to_analyze);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 07:10:27 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* Scans all files their related files
|
2018-02-01 07:10:27 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-10-11 19:58:39 +02:00
|
|
|
public function scanFiles(int $threads = 1)
|
2018-02-01 07:10:27 +01:00
|
|
|
{
|
2018-10-11 19:58:39 +02:00
|
|
|
$has_changes = $this->scanner->scanFiles($this->classlikes, $threads);
|
2018-02-01 07:10:27 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if ($has_changes) {
|
2018-06-28 03:53:25 +02:00
|
|
|
$this->populator->populateCodebase($this);
|
2018-02-01 07:10:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $file_path
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return string
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function getFileContents($file_path)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->file_provider->getContents($file_path);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $file_path
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return array<int, PhpParser\Node\Stmt>
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function getStatementsForFile($file_path)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->statements_provider->getStatementsForFile(
|
|
|
|
$file_path,
|
2019-05-30 16:30:41 +02:00
|
|
|
$this->progress
|
2018-02-04 00:52:35 +01:00
|
|
|
);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $fq_classlike_name
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return ClassLikeStorage
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function createClassLikeStorage($fq_classlike_name)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlike_storage_provider->create($fq_classlike_name);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function cacheClassLikeStorage(ClassLikeStorage $classlike_storage, $file_path)
|
|
|
|
{
|
|
|
|
$file_contents = $this->file_provider->getContents($file_path);
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
if ($this->classlike_storage_provider->cache) {
|
|
|
|
$this->classlike_storage_provider->cache->writeToCache($classlike_storage, $file_path, $file_contents);
|
|
|
|
}
|
2018-02-19 06:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
* @param string $file_path
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function exhumeClassLikeStorage($fq_classlike_name, $file_path)
|
|
|
|
{
|
|
|
|
$file_contents = $this->file_provider->getContents($file_path);
|
|
|
|
$storage = $this->classlike_storage_provider->exhume($fq_classlike_name, $file_path, $file_contents);
|
|
|
|
|
|
|
|
if ($storage->is_trait) {
|
|
|
|
$this->classlikes->addFullyQualifiedTraitName($fq_classlike_name, $file_path);
|
|
|
|
} elseif ($storage->is_interface) {
|
|
|
|
$this->classlikes->addFullyQualifiedInterfaceName($fq_classlike_name, $file_path);
|
|
|
|
} else {
|
|
|
|
$this->classlikes->addFullyQualifiedClassName($fq_classlike_name, $file_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 00:31:40 +01:00
|
|
|
/**
|
|
|
|
* @param ?\ReflectionType $type
|
|
|
|
*/
|
|
|
|
public static function getPsalmTypeFromReflection($type) : Type\Union
|
2019-01-27 23:26:32 +01:00
|
|
|
{
|
|
|
|
return \Psalm\Internal\Codebase\Reflection::getPsalmTypeFromReflectionType($type);
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $file_path
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return FileStorage
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function createFileStorageForPath($file_path)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->file_storage_provider->create($file_path);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $symbol
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2019-04-13 00:28:07 +02:00
|
|
|
* @return \Psalm\CodeLocation[]
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function findReferencesToSymbol($symbol)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2019-04-13 00:28:07 +02:00
|
|
|
if (!$this->collect_locations) {
|
2018-02-04 00:52:35 +01:00
|
|
|
throw new \UnexpectedValueException('Should not be checking references');
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:10:37 +01:00
|
|
|
if (strpos($symbol, '::$') !== false) {
|
|
|
|
return $this->findReferencesToProperty($symbol);
|
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if (strpos($symbol, '::') !== false) {
|
|
|
|
return $this->findReferencesToMethod($symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->findReferencesToClassLike($symbol);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $method_id
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2019-04-13 00:28:07 +02:00
|
|
|
* @return \Psalm\CodeLocation[]
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function findReferencesToMethod($method_id)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2019-04-13 00:28:07 +02:00
|
|
|
return $this->file_reference_provider->getClassMethodLocations(strtolower($method_id));
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 17:10:37 +01:00
|
|
|
/**
|
2019-04-13 00:28:07 +02:00
|
|
|
* @return \Psalm\CodeLocation[]
|
2019-01-22 17:10:37 +01:00
|
|
|
*/
|
2019-04-13 00:28:07 +02:00
|
|
|
public function findReferencesToProperty(string $property_id)
|
2019-01-22 17:10:37 +01:00
|
|
|
{
|
2019-04-13 02:30:28 +02:00
|
|
|
list($fq_class_name, $property_name) = explode('::', $property_id);
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2019-04-13 00:28:07 +02:00
|
|
|
return $this->file_reference_provider->getClassPropertyLocations(
|
|
|
|
strtolower($fq_class_name) . '::' . $property_name
|
|
|
|
);
|
2019-01-22 17:10:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $fq_class_name
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2019-04-13 00:28:07 +02:00
|
|
|
* @return \Psalm\CodeLocation[]
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function findReferencesToClassLike($fq_class_name)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
$fq_class_name_lc = strtolower($fq_class_name);
|
2019-04-13 00:28:07 +02:00
|
|
|
$locations = $this->file_reference_provider->getClassLocations($fq_class_name_lc);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if (isset($this->use_referencing_locations[$fq_class_name_lc])) {
|
2019-04-13 00:28:07 +02:00
|
|
|
$locations = array_merge($locations, $this->use_referencing_locations[$fq_class_name_lc]);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 00:28:07 +02:00
|
|
|
return $locations;
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 23:09:09 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $closure_id
|
|
|
|
*
|
|
|
|
* @return FunctionLikeStorage
|
|
|
|
*/
|
|
|
|
public function getClosureStorage($file_path, $closure_id)
|
|
|
|
{
|
|
|
|
$file_storage = $this->file_storage_provider->get($file_path);
|
|
|
|
|
|
|
|
// closures can be returned here
|
|
|
|
if (isset($file_storage->functions[$closure_id])) {
|
|
|
|
return $file_storage->functions[$closure_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Expecting ' . $closure_id . ' to have storage in ' . $file_path
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
/**
|
|
|
|
* @param string $const_id
|
|
|
|
* @param Type\Union $type
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-06-30 21:29:37 +02:00
|
|
|
public function addGlobalConstantType($const_id, Type\Union $type)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
|
|
|
self::$stubbed_constants[$const_id] = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $const_id
|
|
|
|
*
|
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
|
|
|
public function getStubbedConstantType($const_id)
|
|
|
|
{
|
|
|
|
return isset(self::$stubbed_constants[$const_id]) ? self::$stubbed_constants[$const_id] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $file_path
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return bool
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function fileExists($file_path)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->file_provider->fileExists($file_path);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* Check whether a class/interface exists
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param CodeLocation $code_location
|
|
|
|
*
|
|
|
|
* @return bool
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function classOrInterfaceExists($fq_class_name, CodeLocation $code_location = null)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlikes->classOrInterfaceExists($fq_class_name, $code_location);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param string $possible_parent
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return bool
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function classExtendsOrImplements($fq_class_name, $possible_parent)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlikes->classExtends($fq_class_name, $possible_parent)
|
|
|
|
|| $this->classlikes->classImplements($fq_class_name, $possible_parent);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* Determine whether or not a given class exists
|
|
|
|
*
|
|
|
|
* @param string $fq_class_name
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return bool
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function classExists($fq_class_name)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlikes->classExists($fq_class_name);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* Determine whether or not a class extends a parent
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param string $possible_parent
|
|
|
|
*
|
2019-03-16 23:03:37 +01:00
|
|
|
* @throws \Psalm\Exception\UnpopulatedClasslikeException when called on unpopulated class
|
|
|
|
* @throws \InvalidArgumentException when class does not exist
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function classExtends($fq_class_name, $possible_parent)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2019-03-17 22:11:04 +01:00
|
|
|
return $this->classlikes->classExtends($fq_class_name, $possible_parent, true);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* Check whether a class implements an interface
|
|
|
|
*
|
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param string $interface
|
|
|
|
*
|
|
|
|
* @return bool
|
2018-01-21 19:38:51 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function classImplements($fq_class_name, $interface)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlikes->classImplements($fq_class_name, $interface);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $fq_interface_name
|
2018-01-21 19:38:51 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function interfaceExists($fq_interface_name)
|
2018-01-21 19:38:51 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlikes->interfaceExists($fq_interface_name);
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $interface_name
|
|
|
|
* @param string $possible_parent
|
2018-01-31 22:08:52 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return bool
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function interfaceExtends($interface_name, $possible_parent)
|
2018-01-31 22:08:52 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlikes->interfaceExtends($interface_name, $possible_parent);
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* @param string $fq_interface_name
|
|
|
|
*
|
|
|
|
* @return array<string> all interfaces extended by $interface_name
|
|
|
|
*/
|
|
|
|
public function getParentInterfaces($fq_interface_name)
|
|
|
|
{
|
|
|
|
return $this->classlikes->getParentInterfaces($fq_interface_name);
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* Determine whether or not a class has the correct casing
|
|
|
|
*
|
|
|
|
* @param string $fq_class_name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function classHasCorrectCasing($fq_class_name)
|
|
|
|
{
|
|
|
|
return $this->classlikes->classHasCorrectCasing($fq_class_name);
|
2018-01-31 22:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $fq_interface_name
|
2018-01-31 22:08:52 +01:00
|
|
|
*
|
2018-02-04 00:52:35 +01:00
|
|
|
* @return bool
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function interfaceHasCorrectCasing($fq_interface_name)
|
2018-01-31 22:08:52 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->classlikes->interfaceHasCorrectCasing($fq_interface_name);
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* @param string $fq_trait_name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function traitHasCorrectCase($fq_trait_name)
|
|
|
|
{
|
|
|
|
return $this->classlikes->traitHasCorrectCase($fq_trait_name);
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* Whether or not a given method exists
|
|
|
|
*
|
|
|
|
* @param string $method_id
|
|
|
|
* @param CodeLocation|null $code_location
|
2018-09-27 19:32:08 +02:00
|
|
|
* @param string $calling_method_id
|
2018-02-04 00:52:35 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-04-17 06:17:28 +02:00
|
|
|
public function methodExists(
|
|
|
|
string $method_id,
|
|
|
|
CodeLocation $code_location = null,
|
|
|
|
$calling_method_id = null,
|
|
|
|
string $file_path = null
|
|
|
|
) {
|
|
|
|
return $this->methods->methodExists(
|
|
|
|
$method_id,
|
|
|
|
$calling_method_id,
|
|
|
|
$code_location,
|
|
|
|
null,
|
|
|
|
$file_path
|
|
|
|
);
|
2018-01-31 22:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $method_id
|
|
|
|
*
|
2018-05-12 00:35:02 +02:00
|
|
|
* @return array<int, \Psalm\Storage\FunctionLikeParameter>
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function getMethodParams($method_id)
|
2018-01-31 22:08:52 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->methods->getMethodParams($method_id);
|
2018-01-31 22:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $method_id
|
|
|
|
*
|
|
|
|
* @return bool
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function isVariadic($method_id)
|
2018-01-31 22:08:52 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->methods->isVariadic($method_id);
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param string $self_class
|
2019-03-04 04:57:40 +01:00
|
|
|
* @param array<int, PhpParser\Node\Arg> $call_args
|
2018-02-04 00:52:35 +01:00
|
|
|
*
|
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
2019-03-04 04:57:40 +01:00
|
|
|
public function getMethodReturnType($method_id, &$self_class, array $call_args = [])
|
2018-02-04 00:52:35 +01:00
|
|
|
{
|
|
|
|
return $this->methods->getMethodReturnType($method_id, $self_class);
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getMethodReturnsByRef($method_id)
|
|
|
|
{
|
|
|
|
return $this->methods->getMethodReturnsByRef($method_id);
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param CodeLocation|null $defined_location
|
|
|
|
*
|
|
|
|
* @return CodeLocation|null
|
|
|
|
*/
|
|
|
|
public function getMethodReturnTypeLocation(
|
|
|
|
$method_id,
|
|
|
|
CodeLocation &$defined_location = null
|
|
|
|
) {
|
|
|
|
return $this->methods->getMethodReturnTypeLocation($method_id, $defined_location);
|
2018-01-31 22:08:52 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 23:09:09 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $method_id
|
|
|
|
*
|
|
|
|
* @return string|null
|
2018-01-31 23:09:09 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function getDeclaringMethodId($method_id)
|
2018-01-31 23:09:09 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->methods->getDeclaringMethodId($method_id);
|
|
|
|
}
|
2018-01-31 23:09:09 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* Get the class this method appears in (vs is declared in, which could give a trait)
|
|
|
|
*
|
|
|
|
* @param string $method_id
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getAppearingMethodId($method_id)
|
|
|
|
{
|
|
|
|
return $this->methods->getAppearingMethodId($method_id);
|
2018-01-31 23:09:09 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 22:08:52 +01:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $method_id
|
|
|
|
*
|
|
|
|
* @return array<string>
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function getOverriddenMethodIds($method_id)
|
2018-01-31 22:08:52 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->methods->getOverriddenMethodIds($method_id);
|
2018-01-31 22:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $method_id
|
|
|
|
*
|
|
|
|
* @return string
|
2018-01-31 22:08:52 +01:00
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
public function getCasedMethodId($method_id)
|
2018-01-31 22:08:52 +01:00
|
|
|
{
|
2018-02-04 00:52:35 +01:00
|
|
|
return $this->methods->getCasedMethodId($method_id);
|
2018-01-31 22:08:52 +01:00
|
|
|
}
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-09 16:41:51 +01:00
|
|
|
public function invalidateInformationForFile(string $file_path)
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
|
|
|
$this->scanner->removeFile($file_path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file_storage = $this->file_storage_provider->get($file_path);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($file_storage->classlikes_in_file as $fq_classlike_name) {
|
|
|
|
$this->classlike_storage_provider->remove($fq_classlike_name);
|
|
|
|
$this->classlikes->removeClassLike($fq_classlike_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->file_storage_provider->remove($file_path);
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ?string
|
|
|
|
*/
|
|
|
|
public function getSymbolInformation(string $file_path, string $symbol)
|
|
|
|
{
|
2019-07-02 00:48:33 +02:00
|
|
|
if (\is_numeric($symbol[0])) {
|
|
|
|
return \preg_replace('/[^:]*:/', '', $symbol);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (strpos($symbol, '::')) {
|
|
|
|
if (strpos($symbol, '()')) {
|
|
|
|
$symbol = substr($symbol, 0, -2);
|
|
|
|
|
|
|
|
$declaring_method_id = $this->methods->getDeclaringMethodId($symbol);
|
|
|
|
|
|
|
|
if (!$declaring_method_id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $this->methods->getStorage($declaring_method_id);
|
|
|
|
|
2019-06-22 21:59:16 +02:00
|
|
|
return '<?php ' . $storage->getSignature(true);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
list(, $symbol_name) = explode('::', $symbol);
|
|
|
|
|
|
|
|
if (strpos($symbol, '$') !== false) {
|
|
|
|
$storage = $this->properties->getStorage($symbol);
|
|
|
|
|
|
|
|
return '<?php ' . $storage->getInfo() . ' ' . $symbol_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($fq_classlike_name, $const_name) = explode('::', $symbol);
|
|
|
|
|
|
|
|
$class_constants = $this->classlikes->getConstantsForClass(
|
|
|
|
$fq_classlike_name,
|
|
|
|
\ReflectionProperty::IS_PRIVATE
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset($class_constants[$const_name])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<?php ' . $const_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($symbol, '()')) {
|
|
|
|
$file_storage = $this->file_storage_provider->get($file_path);
|
|
|
|
|
|
|
|
$function_id = strtolower(substr($symbol, 0, -2));
|
|
|
|
|
|
|
|
if (isset($file_storage->functions[$function_id])) {
|
|
|
|
$function_storage = $file_storage->functions[$function_id];
|
|
|
|
|
2019-06-22 21:59:16 +02:00
|
|
|
return '<?php ' . $function_storage->getSignature(true);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $this->classlike_storage_provider->get($symbol);
|
|
|
|
|
|
|
|
return '<?php ' . ($storage->abstract ? 'abstract ' : '') . 'class ' . $storage->name;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
error_log($e->getMessage());
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ?CodeLocation
|
|
|
|
*/
|
|
|
|
public function getSymbolLocation(string $file_path, string $symbol)
|
|
|
|
{
|
2019-07-02 00:48:33 +02:00
|
|
|
if (\is_numeric($symbol[0])) {
|
|
|
|
$symbol = \preg_replace('/:.*/', '', $symbol);
|
|
|
|
$symbol_parts = explode('-', $symbol);
|
|
|
|
|
|
|
|
$file_contents = $this->getFileContents($file_path);
|
|
|
|
|
|
|
|
return new CodeLocation\Raw($file_contents, $file_path, (int) $symbol_parts[0], (int) $symbol_parts[1]);
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
try {
|
|
|
|
if (strpos($symbol, '::')) {
|
|
|
|
if (strpos($symbol, '()')) {
|
|
|
|
$symbol = substr($symbol, 0, -2);
|
|
|
|
|
|
|
|
$declaring_method_id = $this->methods->getDeclaringMethodId($symbol);
|
|
|
|
|
|
|
|
if (!$declaring_method_id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $this->methods->getStorage($declaring_method_id);
|
|
|
|
|
|
|
|
return $storage->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($symbol, '$') !== false) {
|
|
|
|
$storage = $this->properties->getStorage($symbol);
|
|
|
|
|
|
|
|
return $storage->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($fq_classlike_name, $const_name) = explode('::', $symbol);
|
|
|
|
|
|
|
|
$class_constants = $this->classlikes->getConstantsForClass(
|
|
|
|
$fq_classlike_name,
|
|
|
|
\ReflectionProperty::IS_PRIVATE
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset($class_constants[$const_name])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_const_storage = $this->classlike_storage_provider->get($fq_classlike_name);
|
|
|
|
|
|
|
|
return $class_const_storage->class_constant_locations[$const_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($symbol, '()')) {
|
|
|
|
$file_storage = $this->file_storage_provider->get($file_path);
|
|
|
|
|
|
|
|
$function_id = strtolower(substr($symbol, 0, -2));
|
|
|
|
|
|
|
|
if (isset($file_storage->functions[$function_id])) {
|
|
|
|
return $file_storage->functions[$function_id]->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $this->classlike_storage_provider->get($symbol);
|
|
|
|
|
|
|
|
return $storage->location;
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
error_log($e->getMessage());
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
return null;
|
2019-05-15 05:49:15 +02:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
return null;
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array{0: string, 1: Range}|null
|
|
|
|
*/
|
|
|
|
public function getReferenceAtPosition(string $file_path, Position $position)
|
|
|
|
{
|
2018-11-20 22:32:40 +01:00
|
|
|
$is_open = $this->file_provider->isOpen($file_path);
|
|
|
|
|
|
|
|
if (!$is_open) {
|
|
|
|
throw new \Psalm\Exception\UnanalyzedFileException($file_path . ' is not open');
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
$file_contents = $this->getFileContents($file_path);
|
|
|
|
|
|
|
|
$offset = $position->toOffset($file_contents);
|
|
|
|
|
|
|
|
list($reference_map, $type_map) = $this->analyzer->getMapsForFile($file_path);
|
|
|
|
|
|
|
|
$reference = null;
|
|
|
|
|
|
|
|
if (!$reference_map && !$type_map) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$start_pos = null;
|
|
|
|
$end_pos = null;
|
|
|
|
|
|
|
|
ksort($reference_map);
|
|
|
|
|
|
|
|
foreach ($reference_map as $start_pos => list($end_pos, $possible_reference)) {
|
|
|
|
if ($offset < $start_pos) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($offset > $end_pos) {
|
2018-10-26 22:17:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reference = $possible_reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reference === null || $start_pos === null || $end_pos === null) {
|
2019-07-02 00:48:33 +02:00
|
|
|
return null;
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$range = new Range(
|
|
|
|
self::getPositionFromOffset($start_pos, $file_contents),
|
|
|
|
self::getPositionFromOffset($end_pos, $file_contents)
|
|
|
|
);
|
|
|
|
|
|
|
|
return [$reference, $range];
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:54:33 +02:00
|
|
|
/**
|
|
|
|
* @return array{0: string, 1: int, 2: Range}|null
|
|
|
|
*/
|
|
|
|
public function getFunctionArgumentAtPosition(string $file_path, Position $position)
|
|
|
|
{
|
|
|
|
$is_open = $this->file_provider->isOpen($file_path);
|
|
|
|
|
|
|
|
if (!$is_open) {
|
|
|
|
throw new \Psalm\Exception\UnanalyzedFileException($file_path . ' is not open');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_contents = $this->getFileContents($file_path);
|
|
|
|
|
|
|
|
$offset = $position->toOffset($file_contents);
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
list(, , $argument_map) = $this->analyzer->getMapsForFile($file_path);
|
2019-07-01 21:54:33 +02:00
|
|
|
|
|
|
|
$reference = null;
|
|
|
|
$argument_number = null;
|
|
|
|
|
|
|
|
if (!$argument_map) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$start_pos = null;
|
|
|
|
$end_pos = null;
|
|
|
|
|
|
|
|
ksort($argument_map);
|
|
|
|
|
|
|
|
foreach ($argument_map as $start_pos => list($end_pos, $possible_reference, $possible_argument_number)) {
|
|
|
|
if ($offset < $start_pos) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset > $end_pos) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reference = $possible_reference;
|
|
|
|
$argument_number = $possible_argument_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reference === null || $start_pos === null || $end_pos === null || $argument_number === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$range = new Range(
|
|
|
|
self::getPositionFromOffset($start_pos, $file_contents),
|
|
|
|
self::getPositionFromOffset($end_pos, $file_contents)
|
|
|
|
);
|
|
|
|
|
|
|
|
return [$reference, $argument_number, $range];
|
|
|
|
}
|
|
|
|
|
2019-07-19 05:08:54 +02:00
|
|
|
/**
|
2019-07-19 05:30:44 +02:00
|
|
|
* @param string $function_symbol
|
2019-07-19 05:08:54 +02:00
|
|
|
*/
|
2019-07-19 05:30:44 +02:00
|
|
|
public function getSignatureInformation(string $function_symbol) : ?\LanguageServerProtocol\SignatureInformation
|
2019-07-19 05:08:54 +02:00
|
|
|
{
|
2019-07-19 05:30:44 +02:00
|
|
|
$params = null;
|
2019-07-19 05:08:54 +02:00
|
|
|
|
|
|
|
if (strpos($function_symbol, '::') !== false) {
|
|
|
|
$declaring_method_id = $this->methods->getDeclaringMethodId($function_symbol);
|
|
|
|
|
|
|
|
if ($declaring_method_id === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_storage = $this->methods->getStorage($declaring_method_id);
|
|
|
|
$params = $method_storage->params;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$function_storage = $this->functions->getStorage(null, $function_symbol);
|
2019-07-19 05:30:44 +02:00
|
|
|
|
|
|
|
$params = $function_storage->params;
|
2019-07-19 05:08:54 +02:00
|
|
|
} catch (\Exception $exception) {
|
2019-07-19 05:30:44 +02:00
|
|
|
if (CallMap::inCallMap($function_symbol)) {
|
|
|
|
$callables = CallMap::getCallablesFromCallMap($function_symbol);
|
2019-07-19 05:08:54 +02:00
|
|
|
|
2019-07-19 05:30:44 +02:00
|
|
|
if (!$callables || !$callables[0]->params) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = $callables[0]->params;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 05:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$signature_label = '(';
|
|
|
|
$parameters = [];
|
|
|
|
|
|
|
|
foreach ($params as $i => $param) {
|
|
|
|
$parameter_label = ($param->type ?: 'mixed') . ' $' . $param->name;
|
|
|
|
$parameters[] = new \LanguageServerProtocol\ParameterInformation([
|
|
|
|
strlen($signature_label),
|
|
|
|
strlen($signature_label) + strlen($parameter_label),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$signature_label .= $parameter_label;
|
|
|
|
|
|
|
|
if ($i < (count($params) - 1)) {
|
|
|
|
$signature_label .= ', ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$signature_label .= ')';
|
|
|
|
|
|
|
|
return new \LanguageServerProtocol\SignatureInformation(
|
|
|
|
$signature_label,
|
|
|
|
$parameters
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
/**
|
2019-06-30 03:32:26 +02:00
|
|
|
* @return array{0: string, 1: '->'|'::'|'symbol', 2: int}|null
|
2018-10-26 22:17:15 +02:00
|
|
|
*/
|
|
|
|
public function getCompletionDataAtPosition(string $file_path, Position $position)
|
|
|
|
{
|
2018-11-20 22:32:40 +01:00
|
|
|
$is_open = $this->file_provider->isOpen($file_path);
|
|
|
|
|
|
|
|
if (!$is_open) {
|
|
|
|
throw new \Psalm\Exception\UnanalyzedFileException($file_path . ' is not open');
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
$file_contents = $this->getFileContents($file_path);
|
|
|
|
|
|
|
|
$offset = $position->toOffset($file_contents);
|
|
|
|
|
2019-06-30 03:32:26 +02:00
|
|
|
list($reference_map, $type_map) = $this->analyzer->getMapsForFile($file_path);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-06-21 23:10:35 +02:00
|
|
|
if (!$reference_map && !$type_map) {
|
2018-10-26 22:17:15 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$recent_type = null;
|
|
|
|
|
|
|
|
krsort($type_map);
|
|
|
|
|
2019-06-12 12:30:48 +02:00
|
|
|
foreach ($type_map as $start_pos => list($end_pos_excluding_whitespace, $possible_type)) {
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($offset < $start_pos) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-12 12:30:48 +02:00
|
|
|
$num_whitespace_bytes = preg_match('/\G\s+/', $file_contents, $matches, 0, $end_pos_excluding_whitespace)
|
|
|
|
? strlen($matches[0])
|
|
|
|
: 0;
|
|
|
|
$end_pos = $end_pos_excluding_whitespace + $num_whitespace_bytes;
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($offset - $end_pos === 2 || $offset - $end_pos === 3) {
|
2019-05-17 18:38:29 +02:00
|
|
|
$candidate_gap = substr($file_contents, $end_pos, 2);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-05-17 18:38:29 +02:00
|
|
|
if ($candidate_gap === '->' || $candidate_gap === '::') {
|
|
|
|
$gap = $candidate_gap;
|
|
|
|
$recent_type = $possible_type;
|
|
|
|
|
2019-06-21 23:10:35 +02:00
|
|
|
if ($recent_type === 'mixed') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-30 03:32:26 +02:00
|
|
|
return [$recent_type, $gap, $offset];
|
2019-05-17 18:38:29 +02:00
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 23:10:35 +02:00
|
|
|
foreach ($reference_map as $start_pos => list($end_pos, $possible_reference)) {
|
|
|
|
if ($offset < $start_pos || $possible_reference[0] !== '*') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset - $end_pos === 0) {
|
|
|
|
$recent_type = $possible_reference;
|
|
|
|
|
2019-06-30 03:32:26 +02:00
|
|
|
return [$recent_type, 'symbol', $offset];
|
2019-06-21 23:10:35 +02:00
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-21 23:10:35 +02:00
|
|
|
return null;
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2019-05-17 18:11:21 +02:00
|
|
|
/**
|
|
|
|
* @return array<int, \LanguageServerProtocol\CompletionItem>
|
|
|
|
*/
|
|
|
|
public function getCompletionItemsForClassishThing(string $type_string, string $gap) : array
|
|
|
|
{
|
|
|
|
$instance_completion_items = [];
|
|
|
|
$static_completion_items = [];
|
|
|
|
|
|
|
|
$type = Type::parseString($type_string);
|
|
|
|
|
|
|
|
foreach ($type->getTypes() as $atomic_type) {
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
try {
|
|
|
|
$class_storage = $this->classlike_storage_provider->get($atomic_type->value);
|
|
|
|
|
|
|
|
foreach ($class_storage->appearing_method_ids as $declaring_method_id) {
|
|
|
|
$method_storage = $this->methods->getStorage($declaring_method_id);
|
|
|
|
|
2019-06-13 15:41:42 +02:00
|
|
|
$completion_item = new \LanguageServerProtocol\CompletionItem(
|
2019-06-21 23:09:01 +02:00
|
|
|
$method_storage->cased_name,
|
2019-05-17 18:11:21 +02:00
|
|
|
\LanguageServerProtocol\CompletionItemKind::METHOD,
|
2019-06-21 23:09:01 +02:00
|
|
|
(string)$method_storage,
|
2019-05-17 18:11:21 +02:00
|
|
|
null,
|
2019-06-13 15:43:08 +02:00
|
|
|
(string)$method_storage->visibility,
|
2019-06-13 15:45:39 +02:00
|
|
|
$method_storage->cased_name,
|
2019-07-01 23:50:00 +02:00
|
|
|
$method_storage->cased_name . (count($method_storage->params) !== 0 ? '($0)' : '()'),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new Command('Trigger parameter hints', 'editor.action.triggerParameterHints')
|
2019-05-17 18:11:21 +02:00
|
|
|
);
|
2019-06-24 13:51:12 +02:00
|
|
|
$completion_item->insertTextFormat = \LanguageServerProtocol\InsertTextFormat::SNIPPET;
|
2019-06-13 15:41:42 +02:00
|
|
|
|
|
|
|
if ($method_storage->is_static) {
|
|
|
|
$static_completion_items[] = $completion_item;
|
|
|
|
} else {
|
|
|
|
$instance_completion_items[] = $completion_item;
|
|
|
|
}
|
2019-05-17 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class_storage->declaring_property_ids as $property_name => $declaring_class) {
|
|
|
|
$property_storage = $this->properties->getStorage(
|
|
|
|
$declaring_class . '::$' . $property_name
|
|
|
|
);
|
|
|
|
|
2019-06-13 15:41:42 +02:00
|
|
|
$completion_item = new \LanguageServerProtocol\CompletionItem(
|
2019-06-21 23:09:01 +02:00
|
|
|
'$' . $property_name,
|
2019-05-17 18:11:21 +02:00
|
|
|
\LanguageServerProtocol\CompletionItemKind::PROPERTY,
|
2019-06-21 23:09:01 +02:00
|
|
|
$property_storage->getInfo(),
|
2019-05-17 18:11:21 +02:00
|
|
|
null,
|
2019-06-13 15:43:08 +02:00
|
|
|
(string)$property_storage->visibility,
|
2019-06-13 15:45:39 +02:00
|
|
|
$property_name,
|
2019-05-17 18:11:21 +02:00
|
|
|
($gap === '::' ? '$' : '') . $property_name
|
|
|
|
);
|
2019-06-13 15:41:42 +02:00
|
|
|
|
|
|
|
if ($property_storage->is_static) {
|
|
|
|
$static_completion_items[] = $completion_item;
|
|
|
|
} else {
|
|
|
|
$instance_completion_items[] = $completion_item;
|
|
|
|
}
|
2019-05-17 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class_storage->class_constant_locations as $const_name => $_) {
|
|
|
|
$static_completion_items[] = new \LanguageServerProtocol\CompletionItem(
|
2019-06-21 23:10:35 +02:00
|
|
|
$const_name,
|
2019-05-17 18:11:21 +02:00
|
|
|
\LanguageServerProtocol\CompletionItemKind::VARIABLE,
|
2019-06-21 23:10:35 +02:00
|
|
|
'const ' . $const_name,
|
2019-05-17 18:11:21 +02:00
|
|
|
null,
|
|
|
|
null,
|
2019-06-13 15:45:39 +02:00
|
|
|
$const_name,
|
2019-05-17 18:11:21 +02:00
|
|
|
$const_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
error_log($e->getMessage());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 15:42:38 +02:00
|
|
|
}
|
2019-05-17 18:11:21 +02:00
|
|
|
|
2019-06-13 15:42:38 +02:00
|
|
|
if ($gap === '->') {
|
|
|
|
$completion_items = $instance_completion_items;
|
|
|
|
} else {
|
|
|
|
$completion_items = array_merge(
|
|
|
|
$instance_completion_items,
|
|
|
|
$static_completion_items
|
|
|
|
);
|
2019-05-17 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $completion_items;
|
|
|
|
}
|
|
|
|
|
2019-06-21 23:10:35 +02:00
|
|
|
/**
|
|
|
|
* @return array<int, \LanguageServerProtocol\CompletionItem>
|
|
|
|
*/
|
2019-06-30 03:32:26 +02:00
|
|
|
public function getCompletionItemsForPartialSymbol(
|
|
|
|
string $type_string,
|
|
|
|
int $offset,
|
|
|
|
string $file_path
|
|
|
|
) : array {
|
2019-06-21 23:10:35 +02:00
|
|
|
$matching_classlike_names = $this->classlikes->getMatchingClassLikeNames($type_string);
|
|
|
|
|
|
|
|
$completion_items = [];
|
|
|
|
|
2019-06-30 03:32:26 +02:00
|
|
|
$file_storage = $this->file_storage_provider->get($file_path);
|
|
|
|
|
|
|
|
$aliases = null;
|
|
|
|
|
|
|
|
foreach ($file_storage->classlikes_in_file as $fq_class_name) {
|
|
|
|
try {
|
|
|
|
$class_storage = $this->classlike_storage_provider->get($fq_class_name);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_storage->stmt_location) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset > $class_storage->stmt_location->raw_file_start
|
|
|
|
&& $offset < $class_storage->stmt_location->raw_file_end
|
|
|
|
) {
|
|
|
|
$aliases = $class_storage->aliases;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$aliases) {
|
|
|
|
foreach ($file_storage->namespace_aliases as $namespace_start => $namespace_aliases) {
|
|
|
|
if ($namespace_start < $offset) {
|
|
|
|
$aliases = $namespace_aliases;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$aliases) {
|
|
|
|
$aliases = $file_storage->aliases;
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 23:10:35 +02:00
|
|
|
|
2019-07-18 04:50:57 +02:00
|
|
|
foreach ($matching_classlike_names as $fq_class_name) {
|
2019-06-30 17:12:50 +02:00
|
|
|
$extra_edits = [];
|
|
|
|
|
|
|
|
$insertion_text = Type::getStringFromFQCLN(
|
2019-07-18 04:50:57 +02:00
|
|
|
$fq_class_name,
|
2019-06-30 17:12:50 +02:00
|
|
|
$aliases && $aliases->namespace ? $aliases->namespace : null,
|
|
|
|
$aliases ? $aliases->uses_flipped : [],
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($aliases
|
|
|
|
&& $aliases->namespace
|
2019-07-18 04:50:57 +02:00
|
|
|
&& $insertion_text === '\\' . $fq_class_name
|
2019-06-30 17:12:50 +02:00
|
|
|
&& $aliases->namespace_first_stmt_start
|
|
|
|
) {
|
|
|
|
$file_contents = $this->getFileContents($file_path);
|
|
|
|
|
2019-07-18 04:50:57 +02:00
|
|
|
$class_name = \preg_replace('/^.*\\\/', '', $fq_class_name);
|
2019-06-30 17:12:50 +02:00
|
|
|
|
|
|
|
if ($aliases->uses_end) {
|
|
|
|
$position = self::getPositionFromOffset($aliases->uses_end, $file_contents);
|
|
|
|
$extra_edits[] = new \LanguageServerProtocol\TextEdit(
|
|
|
|
new Range(
|
|
|
|
$position,
|
|
|
|
$position
|
|
|
|
),
|
2019-07-18 04:50:57 +02:00
|
|
|
"\n" . 'use ' . $fq_class_name . ';'
|
2019-06-30 17:12:50 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$position = self::getPositionFromOffset($aliases->namespace_first_stmt_start, $file_contents);
|
|
|
|
$extra_edits[] = new \LanguageServerProtocol\TextEdit(
|
|
|
|
new Range(
|
|
|
|
$position,
|
|
|
|
$position
|
|
|
|
),
|
2019-07-18 04:50:57 +02:00
|
|
|
'use ' . $fq_class_name . ';' . "\n" . "\n"
|
2019-06-30 17:12:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$insertion_text = $class_name;
|
|
|
|
}
|
|
|
|
|
2019-06-21 23:10:35 +02:00
|
|
|
$completion_items[] = new \LanguageServerProtocol\CompletionItem(
|
2019-07-18 04:50:57 +02:00
|
|
|
$fq_class_name,
|
2019-06-21 23:10:35 +02:00
|
|
|
\LanguageServerProtocol\CompletionItemKind::CLASS_,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2019-07-18 04:50:57 +02:00
|
|
|
$fq_class_name,
|
2019-06-30 17:12:50 +02:00
|
|
|
$insertion_text,
|
|
|
|
null,
|
|
|
|
$extra_edits
|
2019-06-21 23:10:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $completion_items;
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
private static function getPositionFromOffset(int $offset, string $file_contents) : Position
|
|
|
|
{
|
|
|
|
$file_contents = substr($file_contents, 0, $offset);
|
2019-06-30 17:12:50 +02:00
|
|
|
|
|
|
|
$before_newline_count = strrpos($file_contents, "\n", $offset - strlen($file_contents));
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
return new Position(
|
2018-11-21 19:53:39 +01:00
|
|
|
substr_count($file_contents, "\n"),
|
2019-06-30 17:12:50 +02:00
|
|
|
$offset - (int)$before_newline_count - 1
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-09 16:41:51 +01:00
|
|
|
public function addTemporaryFileChanges(string $file_path, string $new_content)
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
2018-11-09 16:41:51 +01:00
|
|
|
$this->file_provider->addTemporaryFileChanges($file_path, $new_content);
|
|
|
|
}
|
2018-11-02 02:52:39 +01:00
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function removeTemporaryFileChanges(string $file_path)
|
|
|
|
{
|
2018-11-20 21:51:47 +01:00
|
|
|
$this->file_provider->removeTemporaryFileChanges($file_path);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
2019-02-20 12:54:01 +01:00
|
|
|
|
2019-02-18 21:54:23 +01:00
|
|
|
/**
|
2019-02-20 16:59:33 +01:00
|
|
|
* Checks if type is a subtype of other
|
|
|
|
*
|
|
|
|
* Given two types, checks if `$input_type` is a subtype of `$container_type`.
|
|
|
|
* If you consider `Type\Union` as a set of types, this will tell you
|
|
|
|
* if `$input_type` is fully contained in `$container_type`,
|
|
|
|
*
|
|
|
|
* $input_type ⊆ $container_type
|
|
|
|
*
|
|
|
|
* Useful for emitting issues like InvalidArgument, where argument at the call site
|
|
|
|
* should be a subset of the function parameter type.
|
2019-02-18 21:54:23 +01:00
|
|
|
*/
|
2019-02-18 21:40:47 +01:00
|
|
|
public function isTypeContainedByType(
|
|
|
|
Type\Union $input_type,
|
|
|
|
Type\Union $container_type
|
|
|
|
): bool {
|
|
|
|
return TypeAnalyzer::isContainedBy($this, $input_type, $container_type);
|
|
|
|
}
|
2019-02-20 12:54:01 +01:00
|
|
|
|
|
|
|
/**
|
2019-02-20 16:59:33 +01:00
|
|
|
* Checks if type has any part that is a subtype of other
|
|
|
|
*
|
|
|
|
* Given two types, checks if *any part* of `$input_type` is a subtype of `$container_type`.
|
|
|
|
* If you consider `Type\Union` as a set of types, this will tell you if intersection
|
|
|
|
* of `$input_type` with `$container_type` is not empty.
|
|
|
|
*
|
2019-02-20 17:06:51 +01:00
|
|
|
* $input_type ∩ $container_type ≠ ∅ , e.g. they are not disjoint.
|
2019-02-20 16:59:33 +01:00
|
|
|
*
|
|
|
|
* Useful for emitting issues like PossiblyInvalidArgument, where argument at the call
|
|
|
|
* site should be a subtype of the function parameter type, but it's has some types that are
|
|
|
|
* not a subtype of the required type.
|
2019-02-20 12:54:01 +01:00
|
|
|
*/
|
|
|
|
public function canTypeBeContainedByType(
|
|
|
|
Type\Union $input_type,
|
|
|
|
Type\Union $container_type
|
|
|
|
): bool {
|
|
|
|
return TypeAnalyzer::canBeContainedBy($this, $input_type, $container_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-20 16:59:33 +01:00
|
|
|
* Extracts key and value types from a traversable object (or iterable)
|
|
|
|
*
|
|
|
|
* Given an iterable type (*but not TArray*) returns a tuple of it's key/value types.
|
|
|
|
* First element of the tuple holds key type, second has the value type.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* ```php
|
|
|
|
* $codebase->getKeyValueParamsForTraversableObject(Type::parseString('iterable<int,string>'))
|
|
|
|
* // returns [Union(TInt), Union(TString)]
|
|
|
|
* ```
|
|
|
|
*
|
2019-02-20 12:54:01 +01:00
|
|
|
* @return array{Type\Union,Type\Union}
|
|
|
|
*/
|
|
|
|
public function getKeyValueParamsForTraversableObject(Type\Atomic $type): array
|
|
|
|
{
|
|
|
|
$key_type = null;
|
|
|
|
$value_type = null;
|
|
|
|
|
|
|
|
ForeachAnalyzer::getKeyValueParamsForTraversableObject($type, $this, $key_type, $value_type);
|
|
|
|
|
|
|
|
return [
|
|
|
|
$key_type ?? Type::getMixed(),
|
|
|
|
$value_type ?? Type::getMixed(),
|
|
|
|
];
|
|
|
|
}
|
2018-01-21 19:38:51 +01:00
|
|
|
}
|