From 7e80aefcb78285171131a348e7763539416ceaf9 Mon Sep 17 00:00:00 2001 From: Xenofon Spafaridis Date: Fri, 10 May 2019 10:01:14 +0300 Subject: [PATCH 1/2] Add nullable types --- src/ArrayValidator.php | 5 ++--- src/BooleanValidator.php | 5 +++-- src/DateValidator.php | 4 ++-- src/DatetimeValidator.php | 4 ++-- src/EmailValidator.php | 6 +++--- src/EnumValidator.php | 2 +- src/IntegerValidator.php | 6 +++--- src/NumberValidator.php | 8 ++++---- src/ObjectValidator.php | 2 +- src/Result/Result.php | 2 +- src/StringValidator.php | 4 ++-- src/URLValidator.php | 2 +- src/UnsignedIntegerValidator.php | 4 ++-- src/UsernameValidator.php | 2 +- tests/src/ArrayValidatorTest.php | 4 ++-- 15 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/ArrayValidator.php b/src/ArrayValidator.php index 9515494..8248f02 100644 --- a/src/ArrayValidator.php +++ b/src/ArrayValidator.php @@ -74,8 +74,8 @@ class ArrayValidator extends \Phramework\Validate\BaseValidator */ public function __construct( int $minItems = 0, - int $maxItems = null, - $items = null, + ?int $maxItems = null, + ?BaseValidator $items = null, bool $uniqueItems = false ) { parent::__construct(); @@ -98,7 +98,6 @@ public function validate($value) $return = new Result($value, false); if (!is_array($value)) { - $return->exception = 'properties validation'; //error $return->exception = new IncorrectParameterException( 'type', diff --git a/src/BooleanValidator.php b/src/BooleanValidator.php index e37e089..f0d93b3 100644 --- a/src/BooleanValidator.php +++ b/src/BooleanValidator.php @@ -38,8 +38,9 @@ class BooleanValidator extends \Phramework\Validate\BaseValidator * BooleanValidator constructor. * @param bool|null $default */ - public function __construct(bool $default = null) - { + public function __construct( + ?bool $default = null + ) { parent::__construct(); $this->default = $default; diff --git a/src/DateValidator.php b/src/DateValidator.php index 78a5997..6afffe6 100644 --- a/src/DateValidator.php +++ b/src/DateValidator.php @@ -51,8 +51,8 @@ class DateValidator extends \Phramework\Validate\StringValidator * ``` */ public function __construct( - string $formatMinimum = null, - string $formatMaximum = null + ?string $formatMinimum = null, + ?string $formatMaximum = null ) { parent::__construct(); diff --git a/src/DatetimeValidator.php b/src/DatetimeValidator.php index c076e8c..b19ad4e 100644 --- a/src/DatetimeValidator.php +++ b/src/DatetimeValidator.php @@ -51,8 +51,8 @@ class DatetimeValidator extends \Phramework\Validate\StringValidator * ``` */ public function __construct( - string $formatMinimum = null, - string $formatMaximum = null + ?string $formatMinimum = null, + ?string $formatMaximum = null ) { parent::__construct(); diff --git a/src/EmailValidator.php b/src/EmailValidator.php index 30795ca..8138bb6 100644 --- a/src/EmailValidator.php +++ b/src/EmailValidator.php @@ -41,8 +41,8 @@ class EmailValidator extends \Phramework\Validate\StringValidator protected static $type = 'email'; public function __construct( - $minLength = 0, - $maxLength = null + int $minLength = 0, + ?int $maxLength = null ) { parent::__construct( $minLength, @@ -62,7 +62,7 @@ public function validate($value) $return = parent::validate($value); //Apply additional rules - if ($return->status == true) { + if ($return->status === true) { if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { //error $return->exception = new IncorrectParameterException( diff --git a/src/EnumValidator.php b/src/EnumValidator.php index 06b4ab8..0d73ed2 100644 --- a/src/EnumValidator.php +++ b/src/EnumValidator.php @@ -46,7 +46,7 @@ class EnumValidator extends \Phramework\Validate\BaseValidator public function __construct( array $enum = [], - $validateType = false, + bool $validateType = false, $default = null ) { parent::__construct(); diff --git a/src/IntegerValidator.php b/src/IntegerValidator.php index 4843a40..c6f8bbb 100644 --- a/src/IntegerValidator.php +++ b/src/IntegerValidator.php @@ -23,7 +23,7 @@ * Integer validator * @uses \Phramework\Validate\Number As base implementation's rules to * validate that the value is a number and then applies additional rules - * to validate that this is a interger + * to validate that this is a integer * @property integer|null minimum * @property integer|null maximum * @property boolean|null exclusiveMinimum @@ -52,8 +52,8 @@ class IntegerValidator extends \Phramework\Validate\NumberValidator * @throws \Exception */ public function __construct( - int $minimum = null, - int $maximum = null, + ?int $minimum = null, + ?int $maximum = null, bool $exclusiveMinimum = null, bool $exclusiveMaximum = null, int $multipleOf = 1 diff --git a/src/NumberValidator.php b/src/NumberValidator.php index 2d38a2d..21ea984 100644 --- a/src/NumberValidator.php +++ b/src/NumberValidator.php @@ -61,10 +61,10 @@ class NumberValidator extends \Phramework\Validate\BaseValidator * @throws \Exception */ public function __construct( - float $minimum = null, - float $maximum = null, - bool $exclusiveMinimum = null, - bool $exclusiveMaximum = null, + ?float $minimum = null, + ?float $maximum = null, + ?bool $exclusiveMinimum = null, + ?bool $exclusiveMaximum = null, float $multipleOf = null ) { parent::__construct(); diff --git a/src/ObjectValidator.php b/src/ObjectValidator.php index 960c74b..63b81a8 100644 --- a/src/ObjectValidator.php +++ b/src/ObjectValidator.php @@ -79,7 +79,7 @@ public function __construct( array $required = [], $additionalProperties = true, int $minProperties = 0, - int $maxProperties = null, + ?int $maxProperties = null, \stdClass $dependencies = null, \stdClass $xVisibility = null, \stdClass $patternProperties = null diff --git a/src/Result/Result.php b/src/Result/Result.php index dd10503..9d2b017 100644 --- a/src/Result/Result.php +++ b/src/Result/Result.php @@ -49,7 +49,7 @@ class Result public function __construct( $value, bool $status = false, - \Exception $exception = null + ?\Exception $exception = null ) { $this->value = $value; $this->status = $status; diff --git a/src/StringValidator.php b/src/StringValidator.php index 0634cdb..cc390b6 100644 --- a/src/StringValidator.php +++ b/src/StringValidator.php @@ -59,8 +59,8 @@ class StringValidator extends \Phramework\Validate\BaseValidator */ public function __construct( int $minLength = 0, - int $maxLength = null, - string $pattern = null, + ?int $maxLength = null, + ?string $pattern = null, bool $raw = false ) { parent::__construct(); diff --git a/src/URLValidator.php b/src/URLValidator.php index f64d188..7aa339c 100644 --- a/src/URLValidator.php +++ b/src/URLValidator.php @@ -46,7 +46,7 @@ class URLValidator extends \Phramework\Validate\StringValidator */ public function __construct( int $minLength = 0, - int $maxLength = null + ?int $maxLength = null ) { parent::__construct( $minLength, diff --git a/src/UnsignedIntegerValidator.php b/src/UnsignedIntegerValidator.php index d2f1083..23201fa 100644 --- a/src/UnsignedIntegerValidator.php +++ b/src/UnsignedIntegerValidator.php @@ -52,10 +52,10 @@ class UnsignedIntegerValidator extends \Phramework\Validate\IntegerValidator */ public function __construct( int $minimum = 0, - int $maximum = null, + ?int $maximum = null, bool $exclusiveMinimum = null, bool $exclusiveMaximum = null, - int $multipleOf = 1 + ?int $multipleOf = 1 ) { if ($minimum < 0) { throw new \Exception('Minimum cannot be negative'); diff --git a/src/UsernameValidator.php b/src/UsernameValidator.php index 789454d..ee3ae68 100644 --- a/src/UsernameValidator.php +++ b/src/UsernameValidator.php @@ -64,7 +64,7 @@ public static function getUsernamePattern() : string */ public function __construct( int $minLength = 0, - int $maxLength = null + ?int $maxLength = null ) { parent::__construct( $minLength, diff --git a/tests/src/ArrayValidatorTest.php b/tests/src/ArrayValidatorTest.php index 359fb4e..52cf920 100644 --- a/tests/src/ArrayValidatorTest.php +++ b/tests/src/ArrayValidatorTest.php @@ -29,7 +29,7 @@ protected function tearDown() } /** - * @expectedException \Exception + * @expectedException \TypeError */ public function testConstructFailure1() { @@ -52,7 +52,7 @@ public function testConstructFailure3() } /** - * @expectedException \Exception + * @expectedException \TypeError */ public function testConstructFailure4() { From 08ca20acef6473e090a40d354c4651273fb54a7c Mon Sep 17 00:00:00 2001 From: Xenofon Spafaridis Date: Fri, 10 May 2019 10:27:27 +0300 Subject: [PATCH 2/2] Improve NumberValidator and IntegerValidator checks Remove unessesary type checks Set coverage to 100% for Number and IntegerValidator --- src/IntegerValidator.php | 20 ++------- src/NumberValidator.php | 31 +++----------- src/UnsignedIntegerValidator.php | 10 ++--- tests/src/IntegerValidatorTest.php | 15 ++++++- tests/src/NumberValidatorTest.php | 67 +++++++++--------------------- 5 files changed, 45 insertions(+), 98 deletions(-) diff --git a/src/IntegerValidator.php b/src/IntegerValidator.php index c6f8bbb..20cd52e 100644 --- a/src/IntegerValidator.php +++ b/src/IntegerValidator.php @@ -44,12 +44,8 @@ class IntegerValidator extends \Phramework\Validate\NumberValidator protected static $type = 'integer'; /** - * @param integer|null $minimum - * @param integer|null $maximum - * @param boolean|null $exclusiveMinimum - * @param boolean|null $exclusiveMaximum - * @param integer|null $multipleOf - * @throws \Exception + * @throws \InvalidArgumentException When multipleOf is not positive integer + * @throws \DomainException */ public function __construct( ?int $minimum = null, @@ -58,16 +54,8 @@ public function __construct( bool $exclusiveMaximum = null, int $multipleOf = 1 ) { - if ($minimum !== null && !is_int($minimum)) { - throw new \Exception('Minimum must be integer'); - } - - if ($maximum !== null && !is_int($maximum)) { - throw new \Exception('Maximum must be integer'); - } - - if (!is_int($multipleOf) || $multipleOf < 0) { - throw new \Exception('multipleOf must be a positive integer'); + if ($multipleOf !== null && $multipleOf <= 0) { + throw new \InvalidArgumentException('multipleOf must be a positive integer'); } parent::__construct( diff --git a/src/NumberValidator.php b/src/NumberValidator.php index 21ea984..1a8fde0 100644 --- a/src/NumberValidator.php +++ b/src/NumberValidator.php @@ -53,44 +53,23 @@ class NumberValidator extends \Phramework\Validate\BaseValidator protected static $type = 'number'; /** - * @param float|null $minimum - * @param float|null $maximum - * @param bool|null $exclusiveMinimum - * @param bool|null $exclusiveMaximum - * @param float|null $multipleOf - * @throws \Exception + * @throws \DomainException When minimum is not less than the maximum */ public function __construct( ?float $minimum = null, ?float $maximum = null, ?bool $exclusiveMinimum = null, ?bool $exclusiveMaximum = null, - float $multipleOf = null + ?float $multipleOf = null ) { parent::__construct(); - if ($minimum !== null && !is_numeric($minimum)) { - throw new \Exception('Minimum must be numeric'); - } - - if ($maximum !== null && !is_numeric($maximum)) { - throw new \Exception('Maximum must be numeric'); - } - if ($maximum !== null && $minimum !== null && $maximum < $minimum) { - throw new \Exception('maximum cant be less than minimum'); - } - - if ($exclusiveMinimum !== null && !is_bool($exclusiveMinimum)) { - throw new \Exception('exclusiveMinimum must be boolean'); - } - - if ($exclusiveMaximum !== null && !is_bool($exclusiveMaximum)) { - throw new \Exception('exclusiveMaximum must be boolean'); + throw new \DomainException('maximum cant be less than minimum'); } - if ($multipleOf !== null && !is_numeric($multipleOf)) { - throw new \Exception('multipleOf must be numeric'); + if ($multipleOf !== null && $multipleOf <= 0) { + throw new \InvalidArgumentException('multipleOf must be a positive number'); } $this->minimum = $minimum; diff --git a/src/UnsignedIntegerValidator.php b/src/UnsignedIntegerValidator.php index 23201fa..296475b 100644 --- a/src/UnsignedIntegerValidator.php +++ b/src/UnsignedIntegerValidator.php @@ -43,12 +43,8 @@ class UnsignedIntegerValidator extends \Phramework\Validate\IntegerValidator protected static $type = 'unsignedinteger'; /** - * @param int $minimum - * @param int|null $maximum - * @param int|null $exclusiveMinimum - * @param bool|null $exclusiveMaximum - * @param int $multipleOf - * @throws \Exception + * @throws \InvalidArgumentException + * @throws \DomainException */ public function __construct( int $minimum = 0, @@ -58,7 +54,7 @@ public function __construct( ?int $multipleOf = 1 ) { if ($minimum < 0) { - throw new \Exception('Minimum cannot be negative'); + throw new \InvalidArgumentException('Minimum cannot be negative'); } parent::__construct( diff --git a/tests/src/IntegerValidatorTest.php b/tests/src/IntegerValidatorTest.php index 4a8a155..ab6ba09 100644 --- a/tests/src/IntegerValidatorTest.php +++ b/tests/src/IntegerValidatorTest.php @@ -9,7 +9,6 @@ */ class IntegerValidatorTest extends TestCase { - /** * @var IntegerValidator */ @@ -188,4 +187,18 @@ public function testForOverflow() $this->assertSame($value, $parsed); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testShouldFailWhenMultipleOfIsNotPositiveInteger() + { + new IntegerValidator( + null, + null, + null, + null, + -1 + ); + } } diff --git a/tests/src/NumberValidatorTest.php b/tests/src/NumberValidatorTest.php index 76a0540..064e72e 100644 --- a/tests/src/NumberValidatorTest.php +++ b/tests/src/NumberValidatorTest.php @@ -24,13 +24,6 @@ protected function setUp() $this->object = new NumberValidator(-1000, 1000, true); } - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } public function validateSuccessProvider() { @@ -57,7 +50,7 @@ public function validateFailureProvider() ['abc'], ['+xyz'], ['++30'], - [-1000], //should fail becaus of exclusiveMinimum + [-1000], //should fail because of exclusiveMinimum [-10000000], [10000000], ['-1000000000'] @@ -65,43 +58,7 @@ public function validateFailureProvider() } /** - * @expectedException \TypeError - */ - public function testConstructFailure1() - { - $validator = new NumberValidator( - 'a', - 1 - ); - } - - /** - * @expectedException \Exception - */ - /*public function testConstructFailure3() - { - $validator = new NumberValidator( - 1, - 2, - 'a' - ); - }*/ - - /** - * @expectedException \Exception - */ - /*public function testConstructFailure4() - { - $validator = new NumberValidator( - 1, - 2, - true, - 'a' - ); - }*/ - - /** - * @expectedException \Exception + * @expectedException \DomainException */ public function testConstructFailure6() { @@ -162,7 +119,7 @@ private function validateSuccess(NumberValidator $object, $input, $expected) } /** - * @dataProvider validateSuccessProvider + * @dataProvider validateSuccessProvider */ public function testValidateSuccess($input, $expected) { @@ -170,7 +127,7 @@ public function testValidateSuccess($input, $expected) } /** - * @dataProvider validateSuccessProvider + * @dataProvider validateSuccessProvider */ public function testValidateNumberSuccess($input, $expected) { @@ -178,7 +135,7 @@ public function testValidateNumberSuccess($input, $expected) } /** - * @dataProvider validateFailureProvider + * @dataProvider validateFailureProvider */ public function testValidateNumberFailure($input) { @@ -225,4 +182,18 @@ public function testGetType() { $this->assertEquals('number', $this->object->getType()); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testShouldFailWhenMultipleOfIsNotPositiveNumber() + { + new NumberValidator( + null, + null, + null, + null, + -1 + ); + } }