Skip to content

Commit f49abd3

Browse files
committed
Extract MatrixInterface::lowestAndHighest into static class Versions
1 parent e8b84da commit f49abd3

File tree

7 files changed

+136
-82
lines changed

7 files changed

+136
-82
lines changed

src/Console/Command.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Console\Input\InputInterface;
1313
use Symfony\Component\Console\Input\InputOption;
1414
use Symfony\Component\Console\Output\OutputInterface;
15+
use TypistTech\PhpMatrix\Versions;
1516

1617
#[AsCommand(
1718
name: 'php-matrix',
@@ -118,14 +119,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
118119
);
119120
}
120121

121-
[$lowest, $highest] = $matrix->lowestAndHighest(...$versions);
122-
123122
$result = json_encode(
124123
(object) [
125124
self::CONSTRAINT_ARGUMENT_NAME => $constraint,
126-
'versions' => $versions,
127-
'lowest' => $lowest,
128-
'highest' => $highest,
125+
'versions' => Versions::sort(...$versions),
126+
'lowest' => Versions::lowest(...$versions),
127+
'highest' => Versions::highest(...$versions),
129128
],
130129
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
131130
);

src/Matrix.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,4 @@ public function satisfiedBy(string $constraint): array
2222
$constraint
2323
);
2424
}
25-
26-
public function lowestAndHighest(string $version, string ...$versions): array
27-
{
28-
if (empty($versions)) {
29-
return [$version, $version];
30-
}
31-
32-
$sorted = Semver::sort([$version, ...$versions]);
33-
$count = count($sorted);
34-
35-
return [$sorted[0], $sorted[$count - 1]];
36-
}
3725
}

src/MatrixInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@ interface MatrixInterface
1010
* @return string[]
1111
*/
1212
public function satisfiedBy(string $constraint): array;
13-
14-
/**
15-
* @return string[]
16-
*/
17-
public function lowestAndHighest(string $version, string ...$versions): array;
1813
}

src/Versions.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypistTech\PhpMatrix;
6+
7+
use Composer\Semver\Semver;
8+
use UnexpectedValueException;
9+
10+
readonly class Versions
11+
{
12+
public static function sort(string ...$versions): array
13+
{
14+
if (empty($versions)) {
15+
throw new UnexpectedValueException('Argument #1 ($versions) must not be empty');
16+
}
17+
18+
return Semver::sort($versions);
19+
}
20+
21+
public static function lowest(string ...$versions): string
22+
{
23+
$sorted = self::sort(...$versions);
24+
return $sorted[0];
25+
}
26+
27+
public static function highest(string ...$versions): string
28+
{
29+
$sorted = self::sort(...$versions);
30+
return $sorted[array_key_last($sorted)];
31+
}
32+
}

tests/Unit/MatrixTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,4 @@
6969
expect($actual)->toBe($expected);
7070
})->with('satisfied_by');
7171
});
72-
73-
describe('::lowestAndHighest()', static function (): void {
74-
dataset('lowest_and_highest', [
75-
[['1.2.3'], '1.2.3', '1.2.3'],
76-
[['1.2.3', '1.2.3'], '1.2.3', '1.2.3'],
77-
[['1.2.3', '2.2.3'], '1.2.3', '2.2.3'],
78-
[['1.2.3', '2.2.3', '3.2.3'], '1.2.3', '3.2.3'],
79-
80-
[['1.2'], '1.2', '1.2'],
81-
[['1.2', '1.2'], '1.2', '1.2'],
82-
[['1.2', '2.2'], '1.2', '2.2'],
83-
[['1.2', '2.2', '3.2'], '1.2', '3.2'],
84-
]);
85-
86-
it(
87-
'returns lowest and highest versions',
88-
function (array $versions, string $expectedLowest, string $expectedHighest): void {
89-
$releases = Mockery::mock(ReleasesInterface::class);
90-
91-
$matrix = new Matrix($releases);
92-
93-
shuffle($versions);
94-
95-
[$actualLowest, $actualHighest] = $matrix->lowestAndHighest(...$versions);
96-
97-
expect($actualLowest)->toBe($expectedLowest);
98-
expect($actualHighest)->toBe($expectedHighest);
99-
}
100-
)->with('lowest_and_highest');
101-
});
10272
});

tests/Unit/MinorOnlyMatrixTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,4 @@
6464
expect($actual)->toBe($expected);
6565
})->with('satisfied_by');
6666
});
67-
68-
describe('::lowestAndHighest()', static function (): void {
69-
dataset('lowest_and_highest', [
70-
[['1.2.3'], '1.2.3', '1.2.3'],
71-
[['1.2.3', '1.2.3'], '1.2.3', '1.2.3'],
72-
[['1.2.3', '2.2.3'], '1.2.3', '2.2.3'],
73-
[['1.2.3', '2.2.3', '3.2.3'], '1.2.3', '3.2.3'],
74-
75-
[['1.2'], '1.2', '1.2'],
76-
[['1.2', '1.2'], '1.2', '1.2'],
77-
[['1.2', '2.2'], '1.2', '2.2'],
78-
[['1.2', '2.2', '3.2'], '1.2', '3.2'],
79-
]);
80-
81-
it(
82-
'returns lowest and highest versions',
83-
function (array $versions, string $expectedLowest, string $expectedHighest): void {
84-
$releases = Mockery::mock(ReleasesInterface::class);
85-
86-
$matrix = new MinorOnlyMatrix($releases);
87-
88-
shuffle($versions);
89-
90-
[$actualLowest, $actualHighest] = $matrix->lowestAndHighest(...$versions);
91-
92-
expect($actualLowest)->toBe($expectedLowest);
93-
expect($actualHighest)->toBe($expectedHighest);
94-
}
95-
)->with('lowest_and_highest');
96-
});
9767
});

tests/Unit/VersionsTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature\Releases;
6+
7+
use http\Exception\InvalidArgumentException;
8+
use Mockery;
9+
use TypistTech\PhpMatrix\Matrix;
10+
use TypistTech\PhpMatrix\MatrixInterface;
11+
use TypistTech\PhpMatrix\ReleasesInterface;
12+
use TypistTech\PhpMatrix\Versions;
13+
use UnexpectedValueException;
14+
15+
covers(Versions::class);
16+
17+
describe(Versions::class, static function (): void {
18+
describe('::sort()', static function (): void {
19+
dataset('sort', [
20+
[['1.2.3']],
21+
[['1.2.3', '1.2.3']],
22+
[['1.2.3', '2.2.3']],
23+
[['1.2.3', '2.2.3', '3.2.3']],
24+
25+
[['1.2']],
26+
[['1.2', '1.2']],
27+
[['1.2', '2.2']],
28+
[['1.2', '2.2', '3.2']],
29+
]);
30+
31+
it('sorts the versions', function (array $versions): void {
32+
$expected = $versions;
33+
34+
shuffle($versions);
35+
36+
$actual = Versions::sort(...$versions);
37+
38+
expect($actual)->toBe($expected);
39+
}
40+
)->with('sort');
41+
42+
it('throws exception when argument is empty', function (): void {
43+
Versions::sort();
44+
})->throws(UnexpectedValueException::class);
45+
});
46+
47+
describe('::lowest()', static function (): void {
48+
dataset('lowest', [
49+
[['1.2.3'], '1.2.3'],
50+
[['1.2.3', '1.2.3'], '1.2.3'],
51+
[['1.2.3', '2.2.3'], '1.2.3'],
52+
[['1.2.3', '2.2.3', '3.2.3'], '1.2.3'],
53+
54+
[['1.2'], '1.2'],
55+
[['1.2', '1.2'], '1.2'],
56+
[['1.2', '2.2'], '1.2'],
57+
[['1.2', '2.2', '3.2'], '1.2'],
58+
]);
59+
60+
it('returns the lowest version', function (array $versions, string $expected): void {
61+
shuffle($versions);
62+
63+
$actual = Versions::lowest(...$versions);
64+
65+
expect($actual)->toBe($expected);
66+
}
67+
)->with('lowest');
68+
69+
it('throws exception when argument is empty', function (): void {
70+
Versions::lowest();
71+
})->throws(UnexpectedValueException::class);
72+
});
73+
74+
describe('::highest()', static function (): void {
75+
dataset('highest', [
76+
[['1.2.3'], '1.2.3'],
77+
[['1.2.3', '1.2.3'], '1.2.3'],
78+
[['1.2.3', '2.2.3'], '2.2.3'],
79+
[['1.2.3', '2.2.3', '3.2.3'], '3.2.3'],
80+
81+
[['1.2'], '1.2'],
82+
[['1.2', '1.2'], '1.2'],
83+
[['1.2', '2.2'], '2.2'],
84+
[['1.2', '2.2', '3.2'], '3.2'],
85+
]);
86+
87+
it('return the highest version', function (array $versions, string $expected): void {
88+
shuffle($versions);
89+
90+
$actual = Versions::highest(...$versions);
91+
92+
expect($actual)->toBe($expected);
93+
}
94+
)->with('highest');
95+
96+
it('throws exception when argument is empty', function (): void {
97+
Versions::highest();
98+
})->throws(UnexpectedValueException::class);
99+
});
100+
});

0 commit comments

Comments
 (0)