diff --git a/docs/releases/0.1.5.md b/docs/releases/0.1.5.md new file mode 100644 index 0000000..666a1b8 --- /dev/null +++ b/docs/releases/0.1.5.md @@ -0,0 +1,7 @@ +# QA 0.1.5 + +`static-analysis` and `tests` no longer skip a project whose SymPress packages +Composer installs outside `vendor/`, for example WordPress plugins and MU plugins +placed by `composer/installers` paths. A required `sympress/*` package now counts +as present when it is in `vendor/sympress/` or listed in +`vendor/composer/installed.json`. diff --git a/src/Support/ConfigDiscovery.php b/src/Support/ConfigDiscovery.php index ec4f3bc..464d7fd 100644 --- a/src/Support/ConfigDiscovery.php +++ b/src/Support/ConfigDiscovery.php @@ -70,6 +70,7 @@ public function missingLocalSymPressDependencies(string $packageDir): array $this->stringKeyArray($composer['require-dev'] ?? null), ); $missing = []; + $installed = $this->installedPackageNames($packageDir); foreach (array_keys($requires) as $packageName) { if (!str_starts_with($packageName, 'sympress/')) { @@ -81,7 +82,7 @@ public function missingLocalSymPressDependencies(string $packageDir): array } $vendorName = substr($packageName, strlen('sympress/')); - if (is_dir($packageDir . '/vendor/sympress/' . $vendorName)) { + if (is_dir($packageDir . '/vendor/sympress/' . $vendorName) || in_array($packageName, $installed, true)) { continue; } @@ -91,6 +92,42 @@ public function missingLocalSymPressDependencies(string $packageDir): array return $missing; } + /** + * Packages Composer installed for this package, including those placed outside vendor/ by installer paths + * (WordPress plugins and MU plugins). + * + * @return list + */ + private function installedPackageNames(string $packageDir): array + { + $installedFile = $packageDir . '/vendor/composer/installed.json'; + if (!is_readable($installedFile)) { + return []; + } + + try { + $installed = json_decode((string) file_get_contents($installedFile), true, 512, JSON_THROW_ON_ERROR); + } catch (JsonException) { + return []; + } + + $packages = is_array($installed) ? ($installed['packages'] ?? []) : []; + if (!is_array($packages)) { + return []; + } + + $names = []; + foreach ($packages as $package) { + if (!is_array($package) || !is_string($package['name'] ?? null)) { + continue; + } + + $names[] = $package['name']; + } + + return $names; + } + /** @return array */ private function stringKeyArray(mixed $value): array { diff --git a/tests/Unit/Support/ConfigDiscoveryTest.php b/tests/Unit/Support/ConfigDiscoveryTest.php index cb03455..9794215 100644 --- a/tests/Unit/Support/ConfigDiscoveryTest.php +++ b/tests/Unit/Support/ConfigDiscoveryTest.php @@ -91,6 +91,29 @@ public function testMissingLocalSymPressDependenciesIgnoresQaAndCodingStandards( self::assertSame(['sympress/kernel'], $missing); } + public function testMissingLocalSymPressDependenciesAcceptsPackagesInstalledOutsideVendor(): void + { + file_put_contents($this->workspace . '/composer.json', json_encode([ + 'require' => [ + 'sympress/kernel' => '^1.1', + 'sympress/profiler' => '^1.4', + 'sympress/mailer' => '^0.1', + ], + ], JSON_THROW_ON_ERROR)); + self::assertTrue(mkdir($this->workspace . '/vendor/sympress/kernel', 0777, true)); + self::assertTrue(mkdir($this->workspace . '/vendor/composer', 0777, true)); + file_put_contents($this->workspace . '/vendor/composer/installed.json', json_encode([ + 'packages' => [ + ['name' => 'sympress/kernel', 'install-path' => '../sympress/kernel'], + ['name' => 'sympress/profiler', 'install-path' => '../../public/wp-content/mu-plugins/profiler'], + ], + ], JSON_THROW_ON_ERROR)); + + $missing = (new ConfigDiscovery())->missingLocalSymPressDependencies($this->workspace); + + self::assertSame(['sympress/mailer'], $missing); + } + private function removeDirectory(string $directory): void { if (!is_dir($directory)) {