Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Internal/ComposerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ final class ComposerHelper

private static ?string $phpstanVersion = null;

/** @var array<string, mixed[]> */
private static array $decodedCache = [];

/** @return array<string, mixed>|null */
public static function getComposerConfig(string $root): ?array
{
if (isset(self::$decodedCache[$root])) {
return self::$decodedCache[$root];
}

$composerJsonPath = self::getComposerJsonPath($root);
if ($composerJsonPath === null) {
return null;
Expand All @@ -32,7 +39,7 @@ public static function getComposerConfig(string $root): ?array
try {
$composerJsonContents = FileReader::read($composerJsonPath);

return Json::decode($composerJsonContents, Json::FORCE_ARRAY);
return self::$decodedCache[$root] ??= Json::decode($composerJsonContents, Json::FORCE_ARRAY);
} catch (CouldNotReadFileException | JsonException) {
return null;
}
Expand Down
23 changes: 7 additions & 16 deletions src/Php/PhpVersionFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

namespace PHPStan\Php;

use Nette\Utils\Json;
use Nette\Utils\JsonException;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\CouldNotReadFileException;
use PHPStan\File\FileReader;
use PHPStan\Internal\ComposerHelper;
use function count;
use function end;
use function is_array;
use function is_file;
use function is_int;
use function is_string;

Expand All @@ -36,17 +32,12 @@ public function create(): PhpVersionFactory
{
$composerPhpVersion = null;
if (count($this->composerAutoloaderProjectPaths) > 0) {
$composerJsonPath = end($this->composerAutoloaderProjectPaths) . '/composer.json';
if (is_file($composerJsonPath)) {
try {
$composerJsonContents = FileReader::read($composerJsonPath);
$composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY);
$platformVersion = $composer['config']['platform']['php'] ?? null;
if (is_string($platformVersion)) {
$composerPhpVersion = $platformVersion;
}
} catch (CouldNotReadFileException | JsonException) {
// pass
$composerJsonPath = end($this->composerAutoloaderProjectPaths);
$composer = ComposerHelper::getComposerConfig($composerJsonPath);
if ($composer !== null) {
$platformVersion = $composer['config']['platform']['php'] ?? null;
if (is_string($platformVersion)) {
$composerPhpVersion = $platformVersion;
}
}
}
Expand Down
Loading