Skip to content

Commit

Permalink
Merge pull request #62 from Yoast/JRF/duplicate-code-maintainability
Browse files Browse the repository at this point in the history
InvalidOperatorType Exception: improve documentation, duplicate code and unit test
  • Loading branch information
jcomack authored May 28, 2018
2 parents 76faa73 + de981bf commit e06c8cb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/Whip_VersionRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ private function validateParameters( $component, $version, $operator ) {
throw new Whip_InvalidType( 'Operator', $operator, 'string' );
}

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 @@ -67,6 +67,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

0 comments on commit e06c8cb

Please sign in to comment.