From ea089d9696cb31a64d31e8a707607d35e21e196c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 20 Nov 2020 15:54:14 +0100 Subject: [PATCH] feature: allow plugin manager to work without config file (#4639) --- .../Internal/PluginManager/PluginList.php | 21 ++++++++++++++----- .../PluginManager/PluginListFactory.php | 6 +++++- tests/Config/PluginListTest.php | 17 +++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Psalm/Internal/PluginManager/PluginList.php b/src/Psalm/Internal/PluginManager/PluginList.php index c642558ef..312b92edc 100644 --- a/src/Psalm/Internal/PluginManager/PluginList.php +++ b/src/Psalm/Internal/PluginManager/PluginList.php @@ -1,6 +1,7 @@ [pluginClass => ?packageName] */ 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->composer_lock = $composer_lock; @@ -34,9 +35,11 @@ class PluginList { if (!$this->enabled_plugins) { $this->enabled_plugins = []; - foreach ($this->config_file->getConfig()->getPluginClasses() as $plugin_entry) { - $plugin_class = $plugin_entry['class']; - $this->enabled_plugins[$plugin_class] = $this->findPluginPackage($plugin_class); + if ($this->config_file) { + foreach ($this->config_file->getConfig()->getPluginClasses() as $plugin_entry) { + $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 { + if (!$this->config_file) { + throw new RuntimeException('Cannot find Psalm config'); + } + $this->config_file->addPlugin($class); } public function disable(string $class): void { + if (!$this->config_file) { + throw new RuntimeException('Cannot find Psalm config'); + } + $this->config_file->removePlugin($class); } } diff --git a/src/Psalm/Internal/PluginManager/PluginListFactory.php b/src/Psalm/Internal/PluginManager/PluginListFactory.php index 18a6a8039..b3e913eb4 100644 --- a/src/Psalm/Internal/PluginManager/PluginListFactory.php +++ b/src/Psalm/Internal/PluginManager/PluginListFactory.php @@ -24,7 +24,11 @@ class PluginListFactory 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()); return new PluginList($config_file, $composer_lock); diff --git a/tests/Config/PluginListTest.php b/tests/Config/PluginListTest.php index c4c4b3d60..50e478503 100644 --- a/tests/Config/PluginListTest.php +++ b/tests/Config/PluginListTest.php @@ -118,6 +118,23 @@ class PluginListTest extends \Psalm\Tests\TestCase $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 */