From 84e8621730a456065d074915e2547bb3d66fcda7 Mon Sep 17 00:00:00 2001 From: Einenlum Date: Fri, 12 Sep 2025 11:25:16 +0200 Subject: [PATCH 1/2] add a way to get exact installed dependencies from composer lock --- lib/DTO/PhpConfiguration.php | 17 ++++++++++++++ tests/Unit/DetectorTest.php | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/lib/DTO/PhpConfiguration.php b/lib/DTO/PhpConfiguration.php index 3ebe9ee..ca0fa27 100644 --- a/lib/DTO/PhpConfiguration.php +++ b/lib/DTO/PhpConfiguration.php @@ -16,4 +16,21 @@ public function __construct( public ?array $composerLockContent = null, ) { } + + /** @return array */ + public function getExactInstalledDependencies(): array + { + if (null === $this->composerLockContent || !isset($this->composerLockContent['packages'])) { + return []; + } + + $dependencies = []; + foreach ($this->composerLockContent['packages'] as $package) { + if (isset($package['name'], $package['version'])) { + $dependencies[$package['name']] = $package['version']; + } + } + + return $dependencies; + } } diff --git a/tests/Unit/DetectorTest.php b/tests/Unit/DetectorTest.php index 2cb619d..f2b51c2 100644 --- a/tests/Unit/DetectorTest.php +++ b/tests/Unit/DetectorTest.php @@ -451,6 +451,50 @@ public function it_returns_composer_lock_content_if_both_composer_config_files_a ); } + /** @test */ + public function it_returns_no_exact_installed_dependencies_if_no_composer_lock_is_found() + { + $fullConfig = $this->sut->getFullConfiguration( + sprintf('%s/../fixtures/%s', __DIR__, 'composer-config/composer-json') + ); + + $this->assertNotNull($fullConfig); + $this->assertNotNull($fullConfig->phpConfiguration); + $this->assertIsArray( + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + $this->assertCount( + 0, + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + } + + /** @test */ + public function it_returns_exact_installed_dependencies_if_composer_lock_is_found() + { + $fullConfig = $this->sut->getFullConfiguration( + sprintf('%s/../fixtures/%s', __DIR__, 'composer-config/composer-both') + ); + + $this->assertNotNull($fullConfig); + $this->assertNotNull($fullConfig->phpConfiguration); + $this->assertIsArray( + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + $this->assertCount( + 3, + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + $this->assertSame( + [ + 'someframework/foo' => 'v2.4.1', + 'symfony/config' => 'v6.3.2', + 'symfony/framework-bundle' => 'v6.3.5', + ], + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + } + public static function packagesDataProvider(): array { return [ From 7af2a05f55703c569d515114dd3d61674d407e1b Mon Sep 17 00:00:00 2001 From: Einenlum Date: Fri, 12 Sep 2025 11:27:03 +0200 Subject: [PATCH 2/2] add comment --- lib/DTO/PhpConfiguration.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/DTO/PhpConfiguration.php b/lib/DTO/PhpConfiguration.php index ca0fa27..0ec8c52 100644 --- a/lib/DTO/PhpConfiguration.php +++ b/lib/DTO/PhpConfiguration.php @@ -17,7 +17,11 @@ public function __construct( ) { } - /** @return array */ + /** + * This only parses prod dependencies from composer.lock. + * + * @return array + */ public function getExactInstalledDependencies(): array { if (null === $this->composerLockContent || !isset($this->composerLockContent['packages'])) {