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: Implement Version #1

Merged
merged 1 commit into from
Dec 24, 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
2 changes: 1 addition & 1 deletion .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
dependencies: "${{ matrix.dependencies }}"

- name: "Run backward-compatibility analysis with roave/backward-compatibility-check"
run: "vendor/bin/roave-backward-compatibility-check --ansi --format=github-actions --from=64ced12"
run: "vendor/bin/roave-backward-compatibility-check --ansi --format=github-actions --from=6e3389d"

code-coverage:
name: "Code Coverage"
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`64ced12...main`][64ced12...main].

### Added

- Added `Version` as a value object ([#1]), by [@localheinz]

[64ced12...main]: https://github.com/ergebnis/version/compare/64ced12...main

[#1]: https://github.com/ergebnis/version/pull/1

[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ it: refactoring coding-standards security-analysis static-code-analysis tests ##

.PHONY: backward-compatibility-analysis
backward-compatibility-analysis: vendor ## Runs a backward-compatibility analysis with roave/backward-compatibility-check
vendor/bin/roave-backward-compatibility-check --from=64ced12
vendor/bin/roave-backward-compatibility-check --from=6e3389d

.PHONY: code-coverage
code-coverage: vendor ## Collects coverage from running unit tests with phpunit/phpunit
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,34 @@ composer require ergebnis/version

## Usage

💡 This is a great place for showing a few usage examples!
### Create a `Version` from a `string`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$version = Version\Version::fromString('1.2.3');
```

### Compare a `Version` to another `Version`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$one = Version\Version::fromString('1.2.3');
$two = Version\Version::fromString('1.2.3');
$three = Version\Version::fromString('1.2.4');

$one->equals($two); // true
$one->equals($three); // false
```

## Changelog

Expand Down
9 changes: 8 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19"/>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="test/Unit/VersionTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidValue</code>
<code>provideValidValue</code>
</PossiblyUnusedMethod>
</file>
</files>
18 changes: 6 additions & 12 deletions src/Example.php → src/Exception/InvalidVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@
* @see https://github.com/ergebnis/version
*/

namespace Ergebnis\Version;
namespace Ergebnis\Version\Exception;

final class Example
final class InvalidVersion extends \InvalidArgumentException
{
private function __construct(private readonly string $value)
{
}

public static function fromString(string $value): self
{
return new self($value);
}

public function toString(): string
{
return $this->value;
return new self(\sprintf(
'Value "%s" does not appear to be valid.',
$value,
));
}
}
49 changes: 49 additions & 0 deletions src/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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;

final class Version
{
/**
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
* @see https://regex101.com/r/Ly7O1x/3/
*/
private const REGEX = '/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/';

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

/**
* @throws Exception\InvalidVersion
*/
public static function fromString(string $value): self
{
if (1 !== \preg_match(self::REGEX, $value)) {
throw Exception\InvalidVersion::fromString($value);
}

return new self($value);
}

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

public function equals(self $other): bool
{
return $this->value === $other->value;
}
}
33 changes: 0 additions & 33 deletions test/Unit/ExampleTest.php

This file was deleted.

38 changes: 38 additions & 0 deletions test/Unit/Exception/InvalidVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 Ergebnis\Version\Test;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(Exception\InvalidVersion::class)]
final class InvalidVersionTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testFromStringReturnsException(): void
{
$value = self::faker()->word();

$exception = Exception\InvalidVersion::fromString($value);

$message = \sprintf(
'Value "%s" does not appear to be valid.',
$value,
);

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