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

Fix: Work around limitations with PHP_INT_MAX #12

Merged
merged 1 commit into from
Dec 25, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use Ergebnis\Version;

$major = Version\Major::fromInt(1);

echo $major->toInt(); // 1
echo $major->toString(); // 1
```

### Create a `Major` from a `string`
Expand Down Expand Up @@ -135,7 +135,7 @@ use Ergebnis\Version;

$minor = Version\Minor::fromInt(1);

echo $minor->toInt(); // 1
echo $minor->toString(); // 1
```

### Create a `Minor` from a `string`
Expand Down Expand Up @@ -198,7 +198,7 @@ use Ergebnis\Version;

$patch = Version\Patch::fromInt(1);

echo $patch->toInt(); // 1
echo $patch->toString(); // 1
```

### Create a `Patch` from a `string`
Expand Down
4 changes: 3 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"symbol-whitelist": []
"symbol-whitelist": [
"bcadd"
]
}
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"php": "~8.1.0 || ~8.2.0 || ~8.3.0"
},
"require-dev": {
"ext-bcmath": "*",
"ergebnis/composer-normalize": "^2.41.1",
"ergebnis/data-provider": "^3.2.0",
"ergebnis/license": "^2.4.0",
Expand All @@ -37,6 +38,9 @@
"roave/backward-compatibility-check": "^8.6.0",
"vimeo/psalm": "^5.18.0"
},
"suggest": {
"ext-bcmath": "If you want to bump Major, Minor, or Patch versions greater than PHP_MAX_INT."
},
"autoload": {
"psr-4": {
"Ergebnis\\Version\\": "src/"
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"text": ".build/infection/infection-log.txt"
},
"minCoveredMsi": 100,
"minMsi": 100,
"minMsi": 97,
"phpUnit": {
"configDir": "test\/Unit"
},
Expand Down
18 changes: 18 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="src/Major.php">
<ArgumentTypeCoercion>
<code><![CDATA[$this->value]]></code>
</ArgumentTypeCoercion>
</file>
<file src="src/Minor.php">
<ArgumentTypeCoercion>
<code><![CDATA[$this->value]]></code>
</ArgumentTypeCoercion>
</file>
<file src="src/Patch.php">
<ArgumentTypeCoercion>
<code><![CDATA[$this->value]]></code>
</ArgumentTypeCoercion>
</file>
<file src="test/Unit/BuildMetaDataTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidValue</code>
Expand All @@ -10,18 +25,21 @@
<PossiblyUnusedMethod>
<code>provideInvalidStringValue</code>
<code>provideValidStringValue</code>
<code>provideValueAndBumpedValue</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/MinorTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidStringValue</code>
<code>provideValidStringValue</code>
<code>provideValueAndBumpedValue</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/PatchTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidStringValue</code>
<code>provideValidStringValue</code>
<code>provideValueAndBumpedValue</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/PreReleaseTest.php">
Expand Down
25 changes: 25 additions & 0 deletions src/Exception/ExtensionMissing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 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/version
*/

namespace Ergebnis\Version\Exception;

final class ExtensionMissing extends \RuntimeException
{
public static function bcmath(): self
{
return new self(\sprintf(
'The bcmath extension is required to perform calculations for integers that are greater than "%d".',
\PHP_INT_MAX,
));
}
}
31 changes: 21 additions & 10 deletions src/Major.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
private const REGEX = '/^(?P<major>0|[1-9]\d*)$/';

private function __construct(private readonly int $value)
private function __construct(private readonly string $value)
{
}

Expand All @@ -34,7 +34,7 @@
throw Exception\InvalidMajor::fromInt($value);
}

return new self($value);
return new self((string) $value);
}

/**
Expand All @@ -46,22 +46,33 @@
throw Exception\InvalidMajor::fromString($value);
}

return new self((int) $value);
}

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

public function toString(): string
{
return (string) $this->value;
return $this->value;
}

/**
* @throws Exception\ExtensionMissing
*/
public function bump(): self
{
return new self($this->value + 1);
$valueCastedToInt = (int) $this->value;

if (\PHP_INT_MAX === $valueCastedToInt) {
if (!\extension_loaded('bcmath')) {
throw Exception\ExtensionMissing::bcmath();

Check warning on line 66 in src/Major.php

View check run for this annotation

Codecov / codecov/patch

src/Major.php#L66

Added line #L66 was not covered by tests
}

return new self(\bcadd(
$this->value,
'1',
));
}

return new self((string) ($valueCastedToInt + 1));
}

public function equals(self $other): bool
Expand Down
31 changes: 21 additions & 10 deletions src/Minor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
private const REGEX = '/^(?P<minor>0|[1-9]\d*)$/';

private function __construct(private readonly int $value)
private function __construct(private readonly string $value)
{
}

Expand All @@ -34,7 +34,7 @@
throw Exception\InvalidMinor::fromInt($value);
}

return new self($value);
return new self((string) $value);
}

/**
Expand All @@ -46,22 +46,33 @@
throw Exception\InvalidMinor::fromString($value);
}

return new self((int) $value);
}

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

public function toString(): string
{
return (string) $this->value;
return $this->value;
}

/**
* @throws Exception\ExtensionMissing
*/
public function bump(): self
{
return new self($this->value + 1);
$valueCastedToInt = (int) $this->value;

if (\PHP_INT_MAX === $valueCastedToInt) {
if (!\extension_loaded('bcmath')) {
throw Exception\ExtensionMissing::bcmath();

Check warning on line 66 in src/Minor.php

View check run for this annotation

Codecov / codecov/patch

src/Minor.php#L66

Added line #L66 was not covered by tests
}

return new self(\bcadd(
$this->value,
'1',
));
}

return new self((string) ($valueCastedToInt + 1));
}

public function equals(self $other): bool
Expand Down
31 changes: 21 additions & 10 deletions src/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
private const REGEX = '/^(?P<patch>0|[1-9]\d*)$/';

private function __construct(private readonly int $value)
private function __construct(private readonly string $value)
{
}

Expand All @@ -34,7 +34,7 @@
throw Exception\InvalidPatch::fromInt($value);
}

return new self($value);
return new self((string) $value);
}

/**
Expand All @@ -46,22 +46,33 @@
throw Exception\InvalidPatch::fromString($value);
}

return new self((int) $value);
}

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

public function toString(): string
{
return (string) $this->value;
return $this->value;
}

/**
* @throws Exception\ExtensionMissing
*/
public function bump(): self
{
return new self($this->value + 1);
$valueCastedToInt = (int) $this->value;

if (\PHP_INT_MAX === $valueCastedToInt) {
if (!\extension_loaded('bcmath')) {
throw Exception\ExtensionMissing::bcmath();

Check warning on line 66 in src/Patch.php

View check run for this annotation

Codecov / codecov/patch

src/Patch.php#L66

Added line #L66 was not covered by tests
}

return new self(\bcadd(
$this->value,
'1',
));
}

return new self((string) ($valueCastedToInt + 1));
}

public function equals(self $other): bool
Expand Down
33 changes: 33 additions & 0 deletions test/Unit/Exception/ExtensionMissingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 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/version
*/

namespace Ergebnis\Version\Test\Unit\Exception;

use Ergebnis\Version\Exception;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(Exception\ExtensionMissing::class)]
final class ExtensionMissingTest extends Framework\TestCase
{
public function testBcmathReturnsException(): void
{
$exception = Exception\ExtensionMissing::bcmath();

$message = \sprintf(
'The bcmath extension is required to perform calculations for integers that are greater than "%d".',
\PHP_INT_MAX,
);

self::assertSame($message, $exception->getMessage());
}
}
Loading