-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce value object to represent version_compare() operator
- Loading branch information
1 parent
d440690
commit c4e1e53
Showing
3 changed files
with
64 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Util; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
* @psalm-immutable | ||
*/ | ||
final class VersionComparisonOperator | ||
{ | ||
/** | ||
* @var '<'|'lt'|'<='|'le'|'>'|'gt'|'>='|'ge'|'=='|'='|'eq'|'!='|'<>'|'ne' | ||
*/ | ||
private $operator; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
public function __construct(string $operator) | ||
{ | ||
$this->ensureOperatorIsValid($operator); | ||
|
||
$this->operator = $operator; | ||
} | ||
|
||
/** | ||
* @return '<'|'lt'|'<='|'le'|'>'|'gt'|'>='|'ge'|'=='|'='|'eq'|'!='|'<>'|'ne' | ||
*/ | ||
public function asString(): string | ||
{ | ||
return $this->operator; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
* | ||
* @psalm-assert '<'|'lt'|'<='|'le'|'>'|'gt'|'>='|'ge'|'=='|'='|'eq'|'!='|'<>'|'ne' $operator | ||
*/ | ||
private function ensureOperatorIsValid(string $operator): void | ||
{ | ||
if (!\in_array($operator, ['<', 'lt', '<=', 'le', '>', 'gt', '>=', 'ge', '==', '=', 'eq', '!=', '<>', 'ne'])) { | ||
throw new Exception( | ||
\sprintf( | ||
'"%s" is not a valid version_compare() operator', | ||
$operator | ||
) | ||
); | ||
} | ||
} | ||
} |
If this is internal, the property doesn't need to be private – all immutable classes have typechecker-ensured readonly properties, allowing you to remove the getter method (if you wish)