1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Use composer classmap where available to remove need for reflection of vendor classes

This commit is contained in:
Matthew Brown 2017-12-28 00:56:10 +01:00
parent 57592aca41
commit becce4ae36
2 changed files with 65 additions and 1 deletions

View File

@ -242,6 +242,11 @@ class ProjectChecker
*/ */
public $register_global_functions = false; public $register_global_functions = false;
/**
* @var ?array<string, string>
*/
private $composer_classmap;
const TYPE_CONSOLE = 'console'; const TYPE_CONSOLE = 'console';
const TYPE_JSON = 'json'; const TYPE_JSON = 'json';
const TYPE_EMACS = 'emacs'; const TYPE_EMACS = 'emacs';
@ -1408,7 +1413,6 @@ class ProjectChecker
* @param string $fq_class_name * @param string $fq_class_name
* *
* @return bool * @return bool
* @psalm-suppress MixedMethodCall due to Reflection class weirdness
*/ */
public function fileExistsForClassLike($fq_class_name) public function fileExistsForClassLike($fq_class_name)
{ {
@ -1422,6 +1426,24 @@ class ProjectChecker
throw new \InvalidArgumentException('Why are you asking about a builtin class?'); throw new \InvalidArgumentException('Why are you asking about a builtin class?');
} }
if (!$this->config) {
throw new \UnexpectedValueException('Config should be set here');
}
if ($this->composer_classmap === null) {
$this->composer_classmap = $this->config->getComposerClassMap();
}
if (isset($this->composer_classmap[$fq_class_name_lc])) {
if (file_exists($this->composer_classmap[$fq_class_name_lc])) {
$this->existing_classlikes_lc[$fq_class_name_lc] = true;
$this->existing_classlikes[$fq_class_name] = true;
$this->classlike_files[$fq_class_name_lc] = $this->composer_classmap[$fq_class_name_lc];
return true;
}
}
$old_level = error_reporting(); $old_level = error_reporting();
if (!$this->debug_output) { if (!$this->debug_output) {
@ -1440,6 +1462,7 @@ class ProjectChecker
error_reporting($old_level); error_reporting($old_level);
/** @psalm-suppress MixedMethodCall due to Reflection class weirdness */
$file_path = (string)$reflected_class->getFileName(); $file_path = (string)$reflected_class->getFileName();
// if the file was autoloaded but exists in evaled code only, return false // if the file was autoloaded but exists in evaled code only, return false

View File

@ -713,4 +713,45 @@ class Config
} }
} }
} }
/**
* @return array<string, string>
*/
public function getComposerClassMap()
{
$vendor_dir = $this->base_dir . 'vendor'; // this should ideally not be hardcoded
$autoload_files_classmap =
$vendor_dir . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR . 'autoload_classmap.php';
if (!file_exists($autoload_files_classmap)) {
return [];
}
/**
* @psalm-suppress MixedAssignment
* @psalm-suppress UnresolvableInclude
*/
$class_map = include_once $autoload_files_classmap;
if (is_array($class_map)) {
$composer_classmap = array_change_key_case($class_map);
$composer_classmap = array_filter(
$composer_classmap,
/**
* @param string $file_path
*
* @return bool
*/
function ($file_path) use ($vendor_dir) {
return strpos($file_path, $vendor_dir) === 0;
}
);
} else {
$composer_classmap = [];
}
return $composer_classmap;
}
} }