1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-09 22:49:31 +01:00
psalm/src/Psalm/Internal/PluginManager/PluginListFactory.php
orklah 8c7423505a
add native param types (#4137)
* add native param types

* redundant phpdoc

* add more param types and adds "?" to nullable types

* remove redundant phpdoc

* add more param types and remove redundant phpdoc

* add more param types and remove redundant phpdoc
2020-09-06 19:36:47 -04:00

68 lines
2.3 KiB
PHP

<?php
namespace Psalm\Internal\PluginManager;
use function array_filter;
use const DIRECTORY_SEPARATOR;
use function json_encode;
use function rtrim;
use function urlencode;
class PluginListFactory
{
/** @var string */
private $project_root;
/** @var string */
private $psalm_root;
public function __construct(string $project_root, string $psalm_root)
{
$this->project_root = $project_root;
$this->psalm_root = $psalm_root;
}
public function __invoke(string $current_dir, ?string $config_file_path = null): PluginList
{
$config_file = new ConfigFile($current_dir, $config_file_path);
$composer_lock = new ComposerLock($this->findLockFiles());
return new PluginList($config_file, $composer_lock);
}
/** @return non-empty-array<int,string> */
private function findLockFiles(): array
{
// use cases
// 1. plugins are installed into project vendors - composer.lock is PROJECT_ROOT/composer.lock
// 2. plugins are installed into separate composer environment (either global or bamarni-bin)
// - composer.lock is PSALM_ROOT/../../../composer.lock
// 3. plugins are installed into psalm vendors - composer.lock is PSALM_ROOT/composer.lock
// 4. none of the above - use stub (empty virtual composer.lock)
if ($this->psalm_root === $this->project_root) {
// managing plugins for psalm itself
$composer_lock_filenames = [
rtrim($this->psalm_root, DIRECTORY_SEPARATOR) . '/composer.lock',
];
} else {
$composer_lock_filenames = [
rtrim($this->project_root, DIRECTORY_SEPARATOR) . '/composer.lock',
rtrim($this->psalm_root, DIRECTORY_SEPARATOR) . '/../../../composer.lock',
rtrim($this->psalm_root, DIRECTORY_SEPARATOR) . '/composer.lock',
];
}
$composer_lock_filenames = array_filter($composer_lock_filenames, 'is_readable');
if (empty($composer_lock_filenames)) {
$stub_composer_lock = (object)[
'packages' => [],
'packages-dev' => [],
];
$composer_lock_filenames[] = 'data:application/json,' . urlencode(json_encode($stub_composer_lock));
}
return $composer_lock_filenames;
}
}