Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperatorType Exception: improve documentation, duplicate code and unit test #62

Merged
merged 1 commit into from
May 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Whip_VersionRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ private function validateParameters( $component, $version, $operator ) {
throw new Whip_InvalidType( 'Operator', 'string', $operator );
}

if ( ! in_array( $operator, array( '=', '==', '===', '<', '>', '<=', '>=' ), true ) ) {
throw new Whip_InvalidOperatorType( $operator );
$validOperators = array( '=', '==', '===', '<', '>', '<=', '>=' );
if ( ! in_array( $operator, $validOperators, true ) ) {
throw new Whip_InvalidOperatorType( $operator, $validOperators );
}
}
}
14 changes: 4 additions & 10 deletions src/exceptions/Whip_InvalidOperatorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@
*/
class Whip_InvalidOperatorType extends Exception {

/**
* Valid comparison operators as strings.
*
* @var array
*/
private $validOperators = array( '=', '==', '===', '<', '>', '<=', '>=' );

/**
* InvalidOperatorType constructor.
*
* @param string $value Invalid operator.
* @param string $value Invalid operator.
* @param array $validOperators Valid operators.
*/
public function __construct( $value ) {
public function __construct( $value, $validOperators = array( '=', '==', '===', '<', '>', '<=', '>=' ) ) {
parent::__construct(
sprintf(
'Invalid operator of %s used. Please use one of the following operators: %s',
$value,
implode( ', ', $this->validOperators )
implode( ', ', $validOperators )
)
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/VersionRequirementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function testOperatorMustBeString() {

/**
* @expectedException Whip_InvalidOperatorType
* @expectedExceptionMessage Invalid operator of -> used. Please use one of the following operators: =, ==, ===, <, >, <=, >=
*/
public function testOperatorMustBeValid() {
new Whip_VersionRequirement( 'php', '5.2', '->' );
Expand Down