Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Extract Version #392

Merged
merged 1 commit into from
Dec 2, 2023
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
10 changes: 5 additions & 5 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
use PHPUnit\TextUI;
use PHPUnit\Util;

if (1 !== \preg_match('/(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/', Runner\Version::id(), $matches)) {
try {
$phpUnitVersion = Version\Version::fromString(Runner\Version::id());
} catch (\InvalidArgumentException $exception) {

Check warning on line 22 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L21-L22

Added lines #L21 - L22 were not covered by tests
throw new \RuntimeException(\sprintf(
'Unable to determine PHPUnit version from version identifier "%s".',
Runner\Version::id(),
));
}

$major = (int) $matches['major'];

if (9 === $major) {
if ($phpUnitVersion->major()->equals(Version\Major::fromInt(9))) {

Check warning on line 29 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L29

Added line #L29 was not covered by tests
/**
* @internal
*/
Expand Down Expand Up @@ -166,7 +166,7 @@
return $this->maximumDuration;
}
}
} elseif (10 <= $major) {
} elseif ($phpUnitVersion->major()->equals(Version\Major::fromInt(10))) {

Check warning on line 169 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L169

Added line #L169 was not covered by tests
/**
* @internal
*/
Expand Down
52 changes: 52 additions & 0 deletions src/Version/Major.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Version;

/**
* @internal
*/
final class Major
{
private int $value;

private function __construct(int $value)
{
$this->value = $value;
}

/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value "%d" does not appear to be a valid value for a major version.',
$value,
));
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
}

public function equals(self $other): bool
{
return $this->value === $other->value;
}
}
47 changes: 47 additions & 0 deletions src/Version/Minor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Version;

/**
* @internal
*/
final class Minor
{
private int $value;

private function __construct(int $value)
{
$this->value = $value;
}

/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value "%d" does not appear to be a valid value for a minor version.',
$value,
));
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
}
}
47 changes: 47 additions & 0 deletions src/Version/Patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Version;

/**
* @internal
*/
final class Patch
{
private int $value;

private function __construct(int $value)
{
$this->value = $value;
}

/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value "%d" does not appear to be a valid value for a patch version.',
$value,
));
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
}
}
183 changes: 183 additions & 0 deletions src/Version/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Version;

/**
* @internal
*/
final class Version
{
private Major $major;
private ?Minor $minor;
private ?Patch $patch;

private function __construct(
Major $major,
?Minor $minor,
?Patch $patch
) {
$this->major = $major;
$this->minor = $minor;
$this->patch = $patch;
}

/**
* @throws \InvalidArgumentException
*/
public static function create(
Major $major,
?Minor $minor = null,
?Patch $patch = null
): self {
if (
$patch instanceof Patch
&& !$minor instanceof Minor
) {
throw new \InvalidArgumentException('Patch version requires minor version.');
}

return new self(
$major,
$minor,
$patch,
);
}

/**
* @throws \InvalidArgumentException
*/
public static function fromString(string $value): self
{
if (0 === \preg_match('/^(?P<major>(0|[1-9]\d*))(\.(?P<minor>(0|[1-9]\d*))(\.(?P<patch>(0|[1-9]\d*)))?)?$/', $value, $matches)) {
throw new \InvalidArgumentException(\sprintf(
'Value "%s" does not appear to be a valid value for a semantic version.',
$value,
));
}

$major = Major::fromInt((int) $matches['major']);
$minor = null;
$patch = null;

if (\array_key_exists('minor', $matches)) {
$minor = Minor::fromInt((int) $matches['minor']);
}

if (\array_key_exists('patch', $matches)) {
$patch = Patch::fromInt((int) $matches['patch']);
}

return self::create(
$major,
$minor,
$patch,
);
}

public function major(): Major
{
return $this->major;
}

public function minor(): ?Minor
{
return $this->minor;
}

public function patch(): ?Patch
{
return $this->patch;
}

public function toString(): string
{
if (!$this->minor instanceof Minor) {
return (string) $this->major->toInt();
}

if (!$this->patch instanceof Patch) {
return \sprintf(
'%d.%d',
$this->major->toInt(),
$this->minor->toInt(),
);
}

return \sprintf(
'%d.%d.%d',
$this->major->toInt(),
$this->minor->toInt(),
$this->patch->toInt(),
);
}

public function compare(self $other): int
{
$normalizedThis = self::normalize($this);
$normalizedOther = self::normalize($other);

if ($normalizedThis->major->toInt() < $normalizedOther->major->toInt()) {
return -1;
}

if ($normalizedThis->major->toInt() > $normalizedOther->major->toInt()) {
return 1;
}

\assert($normalizedThis->minor instanceof Minor);
\assert($normalizedOther->minor instanceof Minor);

if ($normalizedThis->minor->toInt() < $normalizedOther->minor->toInt()) {
return -1;
}

if ($normalizedThis->minor->toInt() > $normalizedOther->minor->toInt()) {
return 1;
}

\assert($normalizedThis->patch instanceof Patch);
\assert($normalizedOther->patch instanceof Patch);

if ($normalizedThis->patch->toInt() < $normalizedOther->patch->toInt()) {
return -1;
}

if ($normalizedThis->patch->toInt() > $normalizedOther->patch->toInt()) {
return 1;
}

return 0;
}

private static function normalize(self $version): self
{
if (!$version->minor instanceof Minor) {
return new self(
$version->major,
Minor::fromInt(0),
Patch::fromInt(0),
);
}

if (!$version->patch instanceof Patch) {
return new self(
$version->major,
$version->minor,
Patch::fromInt(0),
);
}

return $version;
}
}
Loading