From de981bfff5e19a03cedc28b1b0f1dd7bf908d50e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 26 Mar 2018 12:15:42 +0200 Subject: [PATCH] InvalidOperatorType Exception: improve documentation, duplicate code and unit test The `$validOperators` were defined in two places, making the chance of these two arrays getting out of sync large and lowering maintainability. I've now: * Removed the `$validOperators` property in the `Whip_InvalidOperatorType` class in favour of adding a new parameter to the constructor. * The one call to the Exception will now pass the operators against which it validates. * The message text of the Exception is now unit tested. --- src/Whip_VersionRequirement.php | 5 +++-- src/exceptions/Whip_InvalidOperatorType.php | 14 ++++---------- tests/VersionRequirementTest.php | 1 + 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Whip_VersionRequirement.php b/src/Whip_VersionRequirement.php index 58c91ff..6cc119e 100644 --- a/src/Whip_VersionRequirement.php +++ b/src/Whip_VersionRequirement.php @@ -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 ); } } } diff --git a/src/exceptions/Whip_InvalidOperatorType.php b/src/exceptions/Whip_InvalidOperatorType.php index 1d3ba13..1deb8d9 100644 --- a/src/exceptions/Whip_InvalidOperatorType.php +++ b/src/exceptions/Whip_InvalidOperatorType.php @@ -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 ) ) ); } diff --git a/tests/VersionRequirementTest.php b/tests/VersionRequirementTest.php index bf79b10..f8ead48 100644 --- a/tests/VersionRequirementTest.php +++ b/tests/VersionRequirementTest.php @@ -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', '->' );