Skip to content

Commit 554132a

Browse files
committed
lazy init min/max version
1 parent 181777f commit 554132a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/Php/ComposerPhpVersionFactory.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,27 @@ final class ComposerPhpVersionFactory
2424

2525
private ?PhpVersion $maxVersion = null;
2626

27+
private bool $initialized = false;
28+
2729
/**
2830
* @param string[] $composerAutoloaderProjectPaths
2931
* @param int|array{min: int, max: int}|null $phpVersion
3032
*/
3133
public function __construct(
3234
private array $composerAutoloaderProjectPaths,
33-
int|array|null $phpVersion,
35+
private int|array|null $phpVersion,
3436
)
3537
{
38+
}
39+
40+
private function initVersions(): void
41+
{
42+
$this->initialized = true;
43+
44+
$phpVersion = $this->phpVersion;
45+
3646
if (is_int($phpVersion)) {
37-
return;
47+
throw new ShouldNotHappenException();
3848
}
3949

4050
if (is_array($phpVersion)) {
@@ -73,11 +83,27 @@ public function __construct(
7383

7484
public function getMinVersion(): ?PhpVersion
7585
{
86+
if (is_int($this->phpVersion)) {
87+
return null;
88+
}
89+
90+
if ($this->initialized === false) {
91+
$this->initVersions();
92+
}
93+
7694
return $this->minVersion;
7795
}
7896

7997
public function getMaxVersion(): ?PhpVersion
8098
{
99+
if (is_int($this->phpVersion)) {
100+
return null;
101+
}
102+
103+
if ($this->initialized === false) {
104+
$this->initVersions();
105+
}
106+
81107
return $this->maxVersion;
82108
}
83109

0 commit comments

Comments
 (0)