Skip to content

Commit 9764ce8

Browse files
staabmondrejmirtes
authored andcommitted
Prevent reading & json-decoding composer.json multiple times
1 parent 03df34c commit 9764ce8

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/Internal/ComposerHelper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ final class ComposerHelper
2121

2222
private static ?string $phpstanVersion = null;
2323

24+
/** @var array<string, mixed[]> */
25+
private static array $decodedCache = [];
26+
2427
/** @return array<string, mixed>|null */
2528
public static function getComposerConfig(string $root): ?array
2629
{
30+
if (isset(self::$decodedCache[$root])) {
31+
return self::$decodedCache[$root];
32+
}
33+
2734
$composerJsonPath = self::getComposerJsonPath($root);
2835
if ($composerJsonPath === null) {
2936
return null;
@@ -32,7 +39,7 @@ public static function getComposerConfig(string $root): ?array
3239
try {
3340
$composerJsonContents = FileReader::read($composerJsonPath);
3441

35-
return Json::decode($composerJsonContents, Json::FORCE_ARRAY);
42+
return self::$decodedCache[$root] ??= Json::decode($composerJsonContents, Json::FORCE_ARRAY);
3643
} catch (CouldNotReadFileException | JsonException) {
3744
return null;
3845
}

src/Php/PhpVersionFactoryFactory.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
namespace PHPStan\Php;
44

5-
use Nette\Utils\Json;
6-
use Nette\Utils\JsonException;
75
use PHPStan\DependencyInjection\AutowiredParameter;
86
use PHPStan\DependencyInjection\AutowiredService;
9-
use PHPStan\File\CouldNotReadFileException;
10-
use PHPStan\File\FileReader;
7+
use PHPStan\Internal\ComposerHelper;
118
use function count;
129
use function end;
1310
use function is_array;
14-
use function is_file;
1511
use function is_int;
1612
use function is_string;
1713

@@ -36,17 +32,12 @@ public function create(): PhpVersionFactory
3632
{
3733
$composerPhpVersion = null;
3834
if (count($this->composerAutoloaderProjectPaths) > 0) {
39-
$composerJsonPath = end($this->composerAutoloaderProjectPaths) . '/composer.json';
40-
if (is_file($composerJsonPath)) {
41-
try {
42-
$composerJsonContents = FileReader::read($composerJsonPath);
43-
$composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY);
44-
$platformVersion = $composer['config']['platform']['php'] ?? null;
45-
if (is_string($platformVersion)) {
46-
$composerPhpVersion = $platformVersion;
47-
}
48-
} catch (CouldNotReadFileException | JsonException) {
49-
// pass
35+
$composerJsonPath = end($this->composerAutoloaderProjectPaths);
36+
$composer = ComposerHelper::getComposerConfig($composerJsonPath);
37+
if ($composer !== null) {
38+
$platformVersion = $composer['config']['platform']['php'] ?? null;
39+
if (is_string($platformVersion)) {
40+
$composerPhpVersion = $platformVersion;
5041
}
5142
}
5243
}

0 commit comments

Comments
 (0)