File tree Expand file tree Collapse file tree 2 files changed +15
-17
lines changed
Expand file tree Collapse file tree 2 files changed +15
-17
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 22
33namespace PHPStan \Php ;
44
5- use Nette \Utils \Json ;
6- use Nette \Utils \JsonException ;
75use PHPStan \DependencyInjection \AutowiredParameter ;
86use PHPStan \DependencyInjection \AutowiredService ;
9- use PHPStan \File \CouldNotReadFileException ;
10- use PHPStan \File \FileReader ;
7+ use PHPStan \Internal \ComposerHelper ;
118use function count ;
129use function end ;
1310use function is_array ;
14- use function is_file ;
1511use function is_int ;
1612use 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 }
You can’t perform that action at this time.
0 commit comments