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 29, 2023
1 parent 61d0a73 commit f7b6c27
Show file tree
Hide file tree
Showing 40 changed files with 1,673 additions and 48 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
1 change: 1 addition & 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": {
"composer/semver": "^3.4.0",
"ergebnis/composer-normalize": "^2.41.1",
"ergebnis/data-provider": "^3.2.0",
"ergebnis/license": "^2.4.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

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

93 changes: 92 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19"/>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="test/DataGenerator/AggregateGenerator.php">
<PropertyTypeCoercion>
<code>$generators</code>
</PropertyTypeCoercion>
</file>
<file src="test/DataGenerator/Composer/StabilityFlagGenerator.php">
<UnusedClass>
<code>StabilityFlagGenerator</code>
</UnusedClass>
</file>
<file src="test/DataGenerator/Composer/TildeVersionRangeGenerator.php">
<MixedArgument>
<code>$otherValue</code>
<code>$otherValue</code>
<code>$otherValue</code>
<code>$valuesWithMajor</code>
<code>$valuesWithMajorAndMinor</code>
<code>$valuesWithMajorMinorAndPatch</code>
<code>$valuesWithMajorMinorPatchAndFourth</code>
</MixedArgument>
<MixedAssignment>
<code>$otherValue</code>
<code>$otherValue</code>
<code>$otherValue</code>
</MixedAssignment>
<UndefinedVariable>
<code>$valuesWithMajor</code>
<code>$valuesWithMajorAndMinor</code>
<code>$valuesWithMajorMinorAndPatch</code>
<code>$valuesWithMajorMinorPatchAndFourth</code>
</UndefinedVariable>
<UnusedMethod>
<code>foo</code>
</UnusedMethod>
</file>
<file src="test/DataGenerator/SemanticVersionGenerator.php">
<UnusedClass>
<code>SemanticVersionGenerator</code>
</UnusedClass>
</file>
<file src="test/DataGenerator/ValueGenerator.php">
<PropertyTypeCoercion>
<code>$values</code>
</PropertyTypeCoercion>
</file>
<file src="test/DataProvider/Composer/CaretVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/ExactVersionConstraintProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/GreaterThanOrEqualVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/GreaterThanVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/HyphenatedVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/LessThanOrEqualVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/LessThanVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/TildeVersionRangeProvider.php">
<PossiblyUnusedMethod>
<code>valid</code>
</PossiblyUnusedMethod>
</file>
<file src="test/DataProvider/Composer/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,
));
}
}
68 changes: 68 additions & 0 deletions src/Composer/TildeVersionRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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 = <<<'PCRE'
/^
~
v?
(
# classic version with up to four parts
(
(0|[1-9]\d*)(\.(0|[1-9]\d*)){0,3}
) |
# classic version with up to four parts, optional separator, and stability flag
(
(0|[1-9]\d*)(\.(0|[1-9]\d*)){0,3}[-._]?(a|alpha|b|beta|dev|p|patch|pl|rc|RC|stable)
) |
# classic version with up to four parts, optional separator, stability flag (except "dev"), optional separator, and build number
(
(0|[1-9]\d*)(\.(0|[1-9]\d*)){0,3}[-._]?(a|alpha|b|beta|p|patch|pl|rc|RC|stable)[.-]?\d+
) |
# version-like branch
(
(0|[1-9]\d*)(\.(0|[1-9]\d*)){0,2}\.x[-._]?dev
)
)
$/xJ
PCRE;

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/DataGenerator/AffixingGenerator.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\DataGenerator;

final class AffixingGenerator implements StringGenerator
{
public function __construct(
private readonly StringGenerator $prefixGenerator,
private readonly StringGenerator $valueGenerator,
private readonly StringGenerator $suffixGenerator,
) {
}

public function generate(): \Generator
{
foreach ($this->prefixGenerator->generate() as $prefix) {
foreach ($this->valueGenerator->generate() as $value) {
foreach ($this->suffixGenerator->generate() as $suffix) {
yield \sprintf(
'%s%s%s',
$prefix,
$value,
$suffix,
);
}
}
}
}
}
36 changes: 36 additions & 0 deletions test/DataGenerator/AggregateGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\DataGenerator;

final class AggregateGenerator implements StringGenerator
{
/**
* @var list<StringGenerator>
*/
private readonly array $generators;

public function __construct(StringGenerator ...$generators)
{
$this->generators = $generators;
}

public function generate(): \Generator
{
foreach ($this->generators as $generator) {
foreach ($generator->generate() as $value) {
yield $value;
}
}
}
}
39 changes: 39 additions & 0 deletions test/DataGenerator/Composer/CaretVersionRangeGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\DataGenerator\Composer;

use Ergebnis\VersionConstraint\Test;

/**
* @see https://getcomposer.org/doc/articles/versions.md#exact-version-constraint
*/
final class CaretVersionRangeGenerator implements Test\DataGenerator\StringGenerator
{
private readonly Test\DataGenerator\StringGenerator $generator;

public function __construct()
{
$this->generator = new Test\DataGenerator\PrefixingGenerator(
new Test\DataGenerator\ValueGenerator('^'),
new Test\DataGenerator\Composer\ExactVersionConstraintGenerator(),
);
}

public function generate(): \Generator
{
foreach ($this->generator->generate() as $caretVersionRange) {
yield $caretVersionRange;
}
}
}
Loading

0 comments on commit f7b6c27

Please sign in to comment.