From 0deda469dfcfb3e9de2341f8c01341a531075c2e Mon Sep 17 00:00:00 2001 From: Pietro Campagnano Date: Mon, 15 Jan 2024 13:44:55 +0100 Subject: [PATCH] ignore release patch number when checking php version --- src/CLI/TargetPhpVersion.php | 21 +++++++++++---------- tests/Unit/CLI/TargetPhpVersionTest.php | 7 +++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/CLI/TargetPhpVersion.php b/src/CLI/TargetPhpVersion.php index 784072a0..27424359 100644 --- a/src/CLI/TargetPhpVersion.php +++ b/src/CLI/TargetPhpVersion.php @@ -22,22 +22,23 @@ class TargetPhpVersion /** @var string|null */ private $version; - private function __construct(?string $version) + private function __construct(string $version) { - $this->version = $version; - } - - public static function create(?string $version): self - { - if (null === $version) { - return new self(phpversion()); + $versionNumbers = explode('.', $version); + if (count($versionNumbers) == 3) { + $version = $versionNumbers[0].'.'.$versionNumbers[1]; } - if (!\in_array($version, (new self(null))::VALID_PHP_VERSIONS)) { + if (!\in_array($version, self::VALID_PHP_VERSIONS)) { throw new PhpVersionNotValidException($version); } - return new self($version); + $this->version = $version; + } + + public static function create(?string $version): self + { + return new self($version ?? phpversion()); } public function get(): ?string diff --git a/tests/Unit/CLI/TargetPhpVersionTest.php b/tests/Unit/CLI/TargetPhpVersionTest.php index e70441a7..68b02024 100644 --- a/tests/Unit/CLI/TargetPhpVersionTest.php +++ b/tests/Unit/CLI/TargetPhpVersionTest.php @@ -17,6 +17,13 @@ public function test_it_should_return_passed_php_version(): void $this->assertEquals('8.3', $targetPhpVersion->get()); } + public function test_it_should_ignore_the_patch_number(): void + { + $targetPhpVersion = TargetPhpVersion::create('8.3.2'); + + $this->assertEquals('8.3', $targetPhpVersion->get()); + } + public function test_it_should_throw_exception_if_not_valid_php_version(): void { $this->expectException(PhpVersionNotValidException::class);