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

feature: allow plugin manager to work without config file (#4639)

This commit is contained in:
Dalibor Karlović 2020-11-20 15:54:14 +01:00 committed by Daniil Gentili
parent 12e9a3d2ab
commit ea089d9696
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 38 additions and 6 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Psalm\Internal\PluginManager; namespace Psalm\Internal\PluginManager;
use RuntimeException;
use function array_diff_key; use function array_diff_key;
use function array_flip; use function array_flip;
use function array_key_exists; use function array_key_exists;
@ -9,7 +10,7 @@ use function strpos;
class PluginList class PluginList
{ {
/** @var ConfigFile */ /** @var null|ConfigFile */
private $config_file; private $config_file;
/** @var ComposerLock */ /** @var ComposerLock */
@ -21,7 +22,7 @@ class PluginList
/** @var ?array<string,?string> [pluginClass => ?packageName] */ /** @var ?array<string,?string> [pluginClass => ?packageName] */
private $enabled_plugins = null; private $enabled_plugins = null;
public function __construct(ConfigFile $config_file, ComposerLock $composer_lock) public function __construct(?ConfigFile $config_file, ComposerLock $composer_lock)
{ {
$this->config_file = $config_file; $this->config_file = $config_file;
$this->composer_lock = $composer_lock; $this->composer_lock = $composer_lock;
@ -34,9 +35,11 @@ class PluginList
{ {
if (!$this->enabled_plugins) { if (!$this->enabled_plugins) {
$this->enabled_plugins = []; $this->enabled_plugins = [];
foreach ($this->config_file->getConfig()->getPluginClasses() as $plugin_entry) { if ($this->config_file) {
$plugin_class = $plugin_entry['class']; foreach ($this->config_file->getConfig()->getPluginClasses() as $plugin_entry) {
$this->enabled_plugins[$plugin_class] = $this->findPluginPackage($plugin_class); $plugin_class = $plugin_entry['class'];
$this->enabled_plugins[$plugin_class] = $this->findPluginPackage($plugin_class);
}
} }
} }
@ -96,11 +99,19 @@ class PluginList
public function enable(string $class): void public function enable(string $class): void
{ {
if (!$this->config_file) {
throw new RuntimeException('Cannot find Psalm config');
}
$this->config_file->addPlugin($class); $this->config_file->addPlugin($class);
} }
public function disable(string $class): void public function disable(string $class): void
{ {
if (!$this->config_file) {
throw new RuntimeException('Cannot find Psalm config');
}
$this->config_file->removePlugin($class); $this->config_file->removePlugin($class);
} }
} }

View File

@ -24,7 +24,11 @@ class PluginListFactory
public function __invoke(string $current_dir, ?string $config_file_path = null): PluginList public function __invoke(string $current_dir, ?string $config_file_path = null): PluginList
{ {
$config_file = new ConfigFile($current_dir, $config_file_path); try {
$config_file = new ConfigFile($current_dir, $config_file_path);
} catch (\RuntimeException $exception) {
$config_file = null;
}
$composer_lock = new ComposerLock($this->findLockFiles()); $composer_lock = new ComposerLock($this->findLockFiles());
return new PluginList($config_file, $composer_lock); return new PluginList($config_file, $composer_lock);

View File

@ -118,6 +118,23 @@ class PluginListTest extends \Psalm\Tests\TestCase
$this->assertSame('a\b\c', $plugin_list->resolvePluginClass('vendor/package')); $this->assertSame('a\b\c', $plugin_list->resolvePluginClass('vendor/package'));
} }
/**
* @test
*/
public function canShowAvailablePluginsWithoutAConfigFile(): void
{
$this->composer_lock->getPlugins()->willReturn([
'vendor/package' => 'a\b\c',
'another-vendor/another-package' => 'c\d\e',
]);
$plugin_list = new PluginList(null, $this->composer_lock->reveal());
$this->assertSame([
'a\b\c' => 'vendor/package',
'c\d\e' => 'another-vendor/another-package',
], $plugin_list->getAvailable());
}
/** /**
* @test * @test
*/ */