1010
1111readonly class AffectedVersionsParser
1212{
13- private const string UNKNOWN = 'unknown ' ;
14-
1513 public function __construct (
1614 private VersionParser $ parser = new VersionParser ,
1715 ) {}
1816
17+ /**
18+ * @param array{from_version?: mixed, from_inclusive?: mixed, to_version?: mixed, to_inclusive?: mixed}[] $data
19+ */
1920 public function parse (array $ data ): ?ConstraintInterface
2021 {
2122 $ constraints = array_map (function (array $ affected ): ?string {
22- $ fromVersion = (string ) ($ affected ['from_version ' ] ?? self ::UNKNOWN );
23+ $ fromVersion = $ affected ['from_version ' ] ?? null ;
24+ if (! is_string ($ fromVersion )) {
25+ return null ;
26+ }
27+
2328 $ fromInclusive = (bool ) ($ affected ['from_inclusive ' ] ?? false );
24- $ toVersion = (string ) ($ affected ['to_version ' ] ?? self ::UNKNOWN );
29+
30+ $ toVersion = $ affected ['to_version ' ] ?? null ;
31+ if (! is_string ($ toVersion )) {
32+ return null ;
33+ }
34+
2535 $ toInclusive = (bool ) ($ affected ['to_inclusive ' ] ?? false );
2636
2737 if ($ fromVersion === '* ' && $ toVersion === '* ' ) {
@@ -40,7 +50,7 @@ public function parse(array $data): ?ConstraintInterface
4050 }
4151
4252 if ($ toVersion !== '* ' ) {
43- if (! empty ( $ constraint) ) {
53+ if ($ constraint !== '' ) {
4454 $ constraint .= ', ' ;
4555 }
4656
@@ -51,23 +61,18 @@ public function parse(array $data): ?ConstraintInterface
5161 return $ constraint ;
5262 }, $ data );
5363
54- $ constraints = array_filter ($ constraints );
64+ $ constraints = array_filter ($ constraints , static fn (?string $ c ) => $ c !== null );
65+ $ constraints = array_filter ($ constraints , static fn (string $ c ) => $ c !== '' );
5566 $ constraints = array_values ($ constraints );
5667 $ imploded = implode ('|| ' , $ constraints );
5768
58- if (empty ($ imploded )) {
59- return null ;
60- }
61-
62- return $ this ->parser ->parseConstraints ($ imploded );
69+ return $ imploded === ''
70+ ? null
71+ : $ this ->parser ->parseConstraints ($ imploded );
6372 }
6473
6574 private function isValid (string $ version ): bool
6675 {
67- if ($ version === self ::UNKNOWN ) {
68- return false ;
69- }
70-
7176 if ($ version === '* ' ) {
7277 return true ;
7378 }
0 commit comments