Skip to content

Commit 4ad2eb5

Browse files
committed
fix: Add Semver conditions proper error handling
1 parent 0f5ca7a commit 4ad2eb5

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/Datafile/Conditions/SemverEqualsCondition.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ final class SemverEqualsCondition implements ConditionInterface
1313
use ContextLookup;
1414

1515
private string $attribute;
16-
private Semver $value;
16+
private string $value;
1717

18-
/**
19-
* @throws InvalidArgumentException
20-
*/
2118
public function __construct(string $attribute, string $value)
2219
{
2320
$this->attribute = $attribute;
24-
$this->value = new Semver($value);
21+
$this->value = $value;
2522
}
2623

2724
public function isSatisfiedBy(array $context): bool
@@ -32,9 +29,13 @@ public function isSatisfiedBy(array $context): bool
3229
}
3330
$comparator = new VersionComparator();
3431

35-
return $comparator(
36-
new Semver($valueFromContext),
37-
$this->value
38-
) === 0;
32+
try {
33+
return $comparator(
34+
new Semver($valueFromContext),
35+
new Semver($this->value)
36+
) === 0;
37+
} catch (InvalidArgumentException $e) {
38+
return false;
39+
}
3940
}
4041
}

src/Datafile/Conditions/SemverGreaterThanCondition.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ final class SemverGreaterThanCondition implements ConditionInterface
1313
use ContextLookup, CompositeCondition;
1414

1515
private string $attribute;
16-
private Semver $value;
16+
private string $value;
1717

18-
/**
19-
* @throws InvalidArgumentException
20-
*/
2118
public function __construct(string $attribute, string $value)
2219
{
2320
$this->attribute = $attribute;
24-
$this->value = new Semver($value);
21+
$this->value = $value;
2522
}
2623

2724
public function isSatisfiedBy(array $context): bool
@@ -32,9 +29,13 @@ public function isSatisfiedBy(array $context): bool
3229
}
3330
$comparator = new VersionComparator();
3431

35-
return $comparator(
36-
new Semver($valueFromContext),
37-
$this->value
38-
) === 1;
32+
try {
33+
return $comparator(
34+
new Semver($valueFromContext),
35+
new Semver($this->value)
36+
) === 1;
37+
} catch (InvalidArgumentException $e) {
38+
return false;
39+
}
3940
}
4041
}

src/Datafile/Conditions/SemverLessThanCondition.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ final class SemverLessThanCondition implements ConditionInterface
1313
use ContextLookup, CompositeCondition;
1414

1515
private string $attribute;
16-
private Semver $value;
16+
private string $value;
1717

1818
/**
1919
* @throws InvalidArgumentException
2020
*/
2121
public function __construct(string $attribute, string $value)
2222
{
2323
$this->attribute = $attribute;
24-
$this->value = new Semver($value);
24+
$this->value = $value;
2525
}
2626

2727
public function isSatisfiedBy(array $context): bool
@@ -32,9 +32,13 @@ public function isSatisfiedBy(array $context): bool
3232
}
3333
$comparator = new VersionComparator();
3434

35-
return $comparator(
36-
new Semver($valueFromContext),
37-
$this->value
38-
) === -1;
35+
try {
36+
return $comparator(
37+
new Semver($valueFromContext),
38+
new Semver($this->value)
39+
) === -1;
40+
} catch (InvalidArgumentException $e) {
41+
return false;
42+
}
3943
}
4044
}

0 commit comments

Comments
 (0)