Skip to content

Commit

Permalink
Enhancement: Implement Composer\TildeVersionRange
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 28, 2023
1 parent a8d8d40 commit 4d91170
Show file tree
Hide file tree
Showing 16 changed files with 630 additions and 47 deletions.
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 [`dc5f5d1...main`][dc5f5d1...main].

### Added

- Added `Composer\TildeVersionRange` as a value object ([#2]), by [@localheinz]

[dc5f5d1...main]: https://github.com/ergebnis/version-constraint/compare/dc5f5d1...main

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

[@localheinz]: https://github.com/localheinz
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,25 @@ composer require ergebnis/version-constraint

## Usage

💡 This is a great place for showing a few usage examples!
This project comes with the following components:

- [`Ergebnis\VersionConstraint\Composer\TildeVersionRange`](#composertildeversionrange)

### `Composer\TildeVersionRange`

#### Create a `Composer\TildeVersionRange` from a string

```php
<?php

declare(strict_types=1);

use Ergebnis\VersionConstraint;

$versionConstraint = VersionConstraint\Composer\TildeVersionRange::fromString('~1.0.0');

echo $versionConstraint->toString(); // ~1.0.0
```

## Changelog

Expand Down
51 changes: 50 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19"/>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="test/DataProvider/CaretVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/GreaterThanOrEqualVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/GreaterThanVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/HyphenatedVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/LessThanOrEqualVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/LessThanVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/TildeVersionRangeProvider.php">
<MixedArgument>
<code>$otherValue</code>
<code>$otherValue</code>
</MixedArgument>
<MixedAssignment>
<code>$otherValue</code>
<code>$otherValue</code>
</MixedAssignment>
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/WildCardVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
</files>
18 changes: 6 additions & 12 deletions src/Example.php → ...er/Exception/InvalidTildeVersionRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@
* @see https://github.com/ergebnis/version-constraint
*/

namespace Ergebnis\VersionConstraint;
namespace Ergebnis\VersionConstraint\Composer\Exception;

final class Example
final class InvalidTildeVersionRange 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 a valid value.',
$value,
));
}
}
40 changes: 40 additions & 0 deletions src/Composer/TildeVersionRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\VersionConstraint\Composer;

final class TildeVersionRange
{
private const REGEX = '/^~(?P<prefix>v)?(?P<major>0|[1-9]\d*)(\.(?P<minor>0|[1-9]\d*)(\.(?P<patch>0|[1-9]\d*)(\.(?P<fourth>0|[1-9]\d*))?)?)?(-(?P<stabilityflag>(alpha|beta|dev|RC|stable)))?$/';

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

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

return new self($value);
}

public function toString(): string
{
return $this->value;
}
}
40 changes: 40 additions & 0 deletions test/DataProvider/CaretVersionRangeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\VersionConstraint\Test\DataProvider;

final class CaretVersionRangeProvider
{
/**
* @see https://getcomposer.org/doc/articles/versions.md#caret-version-range-
*
* @return \Generator<string, array{0: string}>
*/
public static function valid(): \Generator
{
$values = [
'^1',
'^1.0',
'^1.0.0',
'^v1',
'^v1.0',
'^v1.0.0',
];

foreach ($values as $value) {
yield $value => [
$value,
];
}
}
}
40 changes: 40 additions & 0 deletions test/DataProvider/GreaterThanOrEqualVersionRangeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\VersionConstraint\Test\DataProvider;

final class GreaterThanOrEqualVersionRangeProvider
{
/**
* @see https://getcomposer.org/doc/articles/versions.md#version-range
*
* @return \Generator<string, array{0: string}>
*/
public static function valid(): \Generator
{
$values = [
'>=1',
'>=1.0',
'>=1.0.0',
'>=v1',
'>=v1.0',
'>=v1.0.0',
];

foreach ($values as $value) {
yield $value => [
$value,
];
}
}
}
40 changes: 40 additions & 0 deletions test/DataProvider/GreaterThanVersionRangeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\VersionConstraint\Test\DataProvider;

final class GreaterThanVersionRangeProvider
{
/**
* @see https://getcomposer.org/doc/articles/versions.md#version-range
*
* @return \Generator<string, array{0: string}>
*/
public static function valid(): \Generator
{
$values = [
'>1',
'>1.0',
'>1.0.0',
'>v1',
'>v1.0',
'>v1.0.0',
];

foreach ($values as $value) {
yield $value => [
$value,
];
}
}
}
57 changes: 57 additions & 0 deletions test/DataProvider/HyphenatedVersionRangeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\VersionConstraint\Test\DataProvider;

final class HyphenatedVersionRangeProvider
{
/**
* @see https://getcomposer.org/doc/articles/versions.md#hyphenated-version-range-
*
* @return \Generator<string, array{0: string}>
*/
public static function valid(): \Generator
{
$leftValues = [
'1',
'1.0',
'1.0.0',
'v1',
'v1.0',
'v1.0.0',
];

$rightValues = [
'2',
'2.0',
'2.0.0',
'v2',
'v2.0',
'v2.0.0',
];

foreach ($leftValues as $leftValue) {
foreach ($rightValues as $rightValue) {
$value = \sprintf(
'%s - %s',
$leftValue,
$rightValue,
);

yield $value => [
$value,
];
}
}
}
}
40 changes: 40 additions & 0 deletions test/DataProvider/LessThanOrEqualVersionRangeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\VersionConstraint\Test\DataProvider;

final class LessThanOrEqualVersionRangeProvider
{
/**
* @see https://getcomposer.org/doc/articles/versions.md#version-range
*
* @return \Generator<string, array{0: string}>
*/
public static function valid(): \Generator
{
$values = [
'<=1',
'<=1.0',
'<=1.0.0',
'<=v1',
'<=v1.0',
'<=v1.0.0',
];

foreach ($values as $value) {
yield $value => [
$value,
];
}
}
}
Loading

0 comments on commit 4d91170

Please sign in to comment.