From 6d248da83df9504faafe28d376fc8e0bcdd9b8d5 Mon Sep 17 00:00:00 2001 From: Christian Blank Date: Fri, 25 Sep 2020 06:40:43 +0200 Subject: [PATCH] 22 datetime error (#24) * [#22] Add PHP 7.4 for testing environment * [#22] Add better type hints --- .gitignore | 1 + .travis.yml | 6 +----- composer.json | 8 ++++---- spec/CheckerSpec.php | 2 +- spec/Type/DatetimeTypeSpec.php | 6 +++--- spec/Type/OptionalTypeSpec.php | 5 +++-- spec/Type/RegexTypeSpec.php | 8 ++++---- spec/Type/StringTypeSpec.php | 6 +++--- src/Check/CountCheck.php | 26 +++++++------------------- src/Error.php | 4 ++-- src/Type/AnyType.php | 4 ---- src/Type/BoolType.php | 2 +- src/Type/DatetimeType.php | 20 ++++++-------------- src/Type/EnumType.php | 24 +++++++++++++----------- src/Type/ExactValueType.php | 7 +------ src/Type/FloatType.php | 2 +- src/Type/IntType.php | 2 +- src/Type/ListType.php | 7 ++----- src/Type/NullableType.php | 5 +---- src/Type/NumericType.php | 2 +- src/Type/ObjectType.php | 10 +++------- src/Type/OptionalType.php | 2 +- src/Type/RegexType.php | 10 ++-------- src/Type/StringType.php | 2 +- src/Type/TypeInterface.php | 4 ---- 25 files changed, 63 insertions(+), 112 deletions(-) diff --git a/.gitignore b/.gitignore index 0a60947..3d4b9b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ vendor composer.lock +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index ba5c4df..5774099 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,15 +3,11 @@ language: php matrix: include: - - php: 7.1 - - php: 7.2 - - php: 7.3 + - php: 7.4 allow_failures: - php: nightly fast_finish: true -before_install: - install: - composer install diff --git a/composer.json b/composer.json index 98f08f6..5ff644f 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "cubicl/php-structure-check", - "description": "Structural check of arrays for PHP 7.1+", + "description": "Structural check of arrays for PHP 7.3+", "keywords": ["array", "structure", "types"], "homepage": "https://github.com/cubicldev/php-structure-check", "type": "library", @@ -19,9 +19,9 @@ "tests-spec": "phpspec run --no-interaction" }, "require-dev": { - "phpspec/phpspec": "^5.1.0", - "phpunit/phpunit": "^7", - "phpstan/phpstan": "^0.11.4" + "phpspec/phpspec": "^6.2", + "phpunit/phpunit": "^9.3", + "phpstan/phpstan": "^0.12" }, "autoload": { "psr-4": { diff --git a/spec/CheckerSpec.php b/spec/CheckerSpec.php index 548ec51..b91780e 100644 --- a/spec/CheckerSpec.php +++ b/spec/CheckerSpec.php @@ -9,7 +9,7 @@ class CheckerSpec extends ObjectBehavior { - function it_is_initializable() + function it_is_initializable(): void { $this->shouldHaveType(Checker::class); } diff --git a/spec/Type/DatetimeTypeSpec.php b/spec/Type/DatetimeTypeSpec.php index b54efd7..05f1231 100644 --- a/spec/Type/DatetimeTypeSpec.php +++ b/spec/Type/DatetimeTypeSpec.php @@ -8,20 +8,20 @@ class DatetimeTypeSpec extends ObjectBehavior { - function it_is_initializable() + public function it_is_initializable(): void { $this->beConstructedWith('d-m-Y h:m:s', 'UTC'); $this->shouldHaveType(DatetimeType::class); } - function it_should_return_valid_for_correct_values() + public function it_should_return_valid_for_correct_values(): void { $this->beConstructedWith('d-m-Y h:m:s', 'Europe/Berlin'); $this->check('', '12-12-2012 12:12:10')->isValid()->shouldBe(true); } - function it_should_return_invalid_for_others() + public function it_should_return_invalid_for_others(): void { $this->beConstructedWith('d-m-Y h:m:s', 'Europe/Berlin'); diff --git a/spec/Type/OptionalTypeSpec.php b/spec/Type/OptionalTypeSpec.php index 61f4a38..0786dba 100644 --- a/spec/Type/OptionalTypeSpec.php +++ b/spec/Type/OptionalTypeSpec.php @@ -9,13 +9,14 @@ class OptionalTypeSpec extends ObjectBehavior { - function it_is_initializable(TypeInterface $childType) + function it_is_initializable(TypeInterface $childType): void { $this->beConstructedWith($childType); $this->shouldHaveType(OptionalType::class); } - function it_should_return_the_value_from_the_child(TypeInterface $childType) { + function it_should_return_the_value_from_the_child(TypeInterface $childType): void + { $this->beConstructedWith($childType); $childType->check('', false)->willReturn(new Result(false, [])); $this->check('', false)->isValid()->shouldBe(false); diff --git a/spec/Type/RegexTypeSpec.php b/spec/Type/RegexTypeSpec.php index 904ee25..30c3250 100644 --- a/spec/Type/RegexTypeSpec.php +++ b/spec/Type/RegexTypeSpec.php @@ -8,28 +8,28 @@ class RegexTypeSpec extends ObjectBehavior { - function it_is_initializable() + function it_is_initializable(): void { $this->beConstructedWith('/^def/'); $this->shouldHaveType(RegexType::class); } - function it_should_return_valid_for_matching_strings() + function it_should_return_valid_for_matching_strings(): void { $this->beConstructedWith('/^def/'); $this->check('', 'definitive')->isValid()->shouldBe(true); } - function it_should_return_invalid_for_not_matching_strings() + function it_should_return_invalid_for_not_matching_strings(): void { $this->beConstructedWith('/^def/'); $this->check('', 'developers')->isValid()->shouldBe(false); } - function it_should_return_invalid_for_others() + function it_should_return_invalid_for_others(): void { $this->beConstructedWith('/^def/'); diff --git a/spec/Type/StringTypeSpec.php b/spec/Type/StringTypeSpec.php index f514c5f..4d95b5d 100644 --- a/spec/Type/StringTypeSpec.php +++ b/spec/Type/StringTypeSpec.php @@ -8,19 +8,19 @@ class StringTypeSpec extends ObjectBehavior { - function it_is_initializable() + function it_is_initializable(): void { $this->shouldHaveType(StringType::class); } - function it_should_return_valid_for_strings() + function it_should_return_valid_for_strings(): void { $this->check('', '')->isValid()->shouldBe(true); $this->check('', 'fooo')->isValid()->shouldBe(true); $this->check('', 'adadsad asd a')->isValid()->shouldBe(true); } - function it_should_return_invalid_for_others() + function it_should_return_invalid_for_others(): void { $this->check('', null)->isValid()->shouldBe(false); $this->check('', 12.3)->isValid()->shouldBe(false); diff --git a/src/Check/CountCheck.php b/src/Check/CountCheck.php index 6f09a2d..6dd09ef 100644 --- a/src/Check/CountCheck.php +++ b/src/Check/CountCheck.php @@ -10,25 +10,13 @@ class CountCheck implements TypeInterface { - /** - * @var string - */ - private static $countErrorMessage = 'The given countable %s has not the expected count %d.'; - - /** - * @var string - */ - private static $countableErrorMessage = 'The given value %s is not a countable'; - - /** - * @var TypeInterface - */ - private $child; - - /** - * @var int - */ - private $count; + private static string $countErrorMessage = 'The given countable %s has not the expected count %d.'; + + private static string $countableErrorMessage = 'The given value %s is not a countable'; + + private TypeInterface $child; + + private int $count; public function __construct(TypeInterface $child, int $count) { diff --git a/src/Error.php b/src/Error.php index d21a104..19e45e5 100644 --- a/src/Error.php +++ b/src/Error.php @@ -4,9 +4,9 @@ class Error implements ErrorInterface { - private $key; + private string $key; - private $message; + private string $message; public function __construct(string $key, string $message) { diff --git a/src/Type/AnyType.php b/src/Type/AnyType.php index dc2b925..ce52d93 100644 --- a/src/Type/AnyType.php +++ b/src/Type/AnyType.php @@ -5,10 +5,6 @@ use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; -/** - * Class AnyType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class AnyType implements TypeInterface { /** diff --git a/src/Type/BoolType.php b/src/Type/BoolType.php index 10bede9..3181a5f 100644 --- a/src/Type/BoolType.php +++ b/src/Type/BoolType.php @@ -8,7 +8,7 @@ class BoolType implements TypeInterface { - private static $errorMessage = 'The value %s is not a boolean.'; + private static string $errorMessage = 'The value %s is not a boolean.'; public function check(string $key, $value): ResultInterface { diff --git a/src/Type/DatetimeType.php b/src/Type/DatetimeType.php index f4e212b..0926244 100644 --- a/src/Type/DatetimeType.php +++ b/src/Type/DatetimeType.php @@ -7,23 +7,15 @@ use DateTimeZone; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; +use JsonException; class DatetimeType implements TypeInterface { - /** - * @var string - */ - private static $errorMessage = 'The value %s is not a valid datetime.'; - - /** - * @var string - */ - private $datetimeFormat; - - /** - * @var string - */ - private $datetimeZone; + private static string $errorMessage = 'The value %s is not a valid datetime.'; + + private string $datetimeFormat; + + private string $datetimeZone; public function __construct(string $format, string $datetimeZone) { diff --git a/src/Type/EnumType.php b/src/Type/EnumType.php index f63f96d..fa570e9 100644 --- a/src/Type/EnumType.php +++ b/src/Type/EnumType.php @@ -6,28 +6,30 @@ use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; +/** + * @template T + */ class EnumType implements TypeInterface { - /** - * @var string - */ - private static $errorMessage = 'The value %s is not in the allowed values (%s).'; + private static string $errorMessage = 'The value %s is not in the allowed values (%s).'; - /** - * @var array - */ - private $values; + /** @var array */ + private array $values; /** - * EnumType constructor. - * - * @param array $values + * @param array $values */ public function __construct(array $values) { $this->values = $values; } + /** + * @param string $key + * @param T $value + * + * @return ResultInterface + */ public function check(string $key, $value): ResultInterface { $checkResult = in_array($value, $this->values, true); diff --git a/src/Type/ExactValueType.php b/src/Type/ExactValueType.php index 91c908d..971e4ce 100644 --- a/src/Type/ExactValueType.php +++ b/src/Type/ExactValueType.php @@ -8,17 +8,12 @@ class ExactValueType implements TypeInterface { - /** - * @var string - */ - private static $errorMessage = 'The value %s is not the same value as %s.'; + private static string $errorMessage = 'The value %s is not the same value as %s.'; /** @var mixed */ private $value; /** - * ExactValueType constructor. - * * @param mixed $value */ public function __construct($value) diff --git a/src/Type/FloatType.php b/src/Type/FloatType.php index 87c7eb0..2739d5e 100644 --- a/src/Type/FloatType.php +++ b/src/Type/FloatType.php @@ -8,7 +8,7 @@ class FloatType implements TypeInterface { - private static $errorMessage = 'The value %s is not a float.'; + private static string $errorMessage = 'The value %s is not a float.'; public function check(string $key, $value): ResultInterface { diff --git a/src/Type/IntType.php b/src/Type/IntType.php index 7e4e036..ae15b82 100644 --- a/src/Type/IntType.php +++ b/src/Type/IntType.php @@ -8,7 +8,7 @@ class IntType implements TypeInterface { - private static $errorMessage = 'The value %s is not an integer.'; + private static string $errorMessage = 'The value %s is not an integer.'; public function check(string $key, $value): ResultInterface { diff --git a/src/Type/ListType.php b/src/Type/ListType.php index 0c5af64..c0d0a0a 100644 --- a/src/Type/ListType.php +++ b/src/Type/ListType.php @@ -8,12 +8,9 @@ class ListType implements TypeInterface { - private static $isNotAnArrayMessage = 'The given value %s is not an array.'; + private static string $isNotAnArrayMessage = 'The given value %s is not an array.'; - /** - * @var TypeInterface - */ - private $child; + private TypeInterface $child; public function __construct(TypeInterface $child) { diff --git a/src/Type/NullableType.php b/src/Type/NullableType.php index c66f5a4..287756b 100644 --- a/src/Type/NullableType.php +++ b/src/Type/NullableType.php @@ -7,10 +7,7 @@ class NullableType implements TypeInterface { - /** - * @var TypeInterface - */ - private $child; + private TypeInterface $child; public function __construct(TypeInterface $child) { diff --git a/src/Type/NumericType.php b/src/Type/NumericType.php index 796f092..e3797d1 100644 --- a/src/Type/NumericType.php +++ b/src/Type/NumericType.php @@ -8,7 +8,7 @@ class NumericType implements TypeInterface { - private static $errorMessage = 'The value %s is not a numeric value.'; + private static string $errorMessage = 'The value %s is not a numeric value.'; public function check(string $key, $value): ResultInterface { diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 49a9fb6..fedb850 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -8,16 +8,12 @@ class ObjectType implements TypeInterface { - private static $missingKeyErrorMessage = 'The key "%s" does not exists'; + private static string $missingKeyErrorMessage = 'The key "%s" does not exists'; - /** - * @var TypeInterface[] - */ - private $children; + /** @var TypeInterface[] */ + private array $children; /** - * ObjectType constructor. - * * @param TypeInterface[] $children */ public function __construct(array $children) diff --git a/src/Type/OptionalType.php b/src/Type/OptionalType.php index 7b68ba4..7c0a26f 100644 --- a/src/Type/OptionalType.php +++ b/src/Type/OptionalType.php @@ -6,7 +6,7 @@ class OptionalType implements TypeInterface { - private $child; + private TypeInterface $child; public function __construct(TypeInterface $child) { diff --git a/src/Type/RegexType.php b/src/Type/RegexType.php index 84d5256..4d93d74 100644 --- a/src/Type/RegexType.php +++ b/src/Type/RegexType.php @@ -8,15 +8,9 @@ class RegexType implements TypeInterface { - /** - * @var string - */ - private static $errorMessage = 'The value %s does not match the regex %s'; + private static string $errorMessage = 'The value %s does not match the regex %s'; - /** - * @var string - */ - private $regex; + private string $regex; public function __construct(string $regex) { diff --git a/src/Type/StringType.php b/src/Type/StringType.php index 4e2441d..e26addb 100644 --- a/src/Type/StringType.php +++ b/src/Type/StringType.php @@ -8,7 +8,7 @@ class StringType implements TypeInterface { - private static $errorMessage = 'The value %s is not a string.'; + private static string $errorMessage = 'The value %s is not a string.'; public function check(string $key, $value): ResultInterface { diff --git a/src/Type/TypeInterface.php b/src/Type/TypeInterface.php index 450989c..59f311a 100644 --- a/src/Type/TypeInterface.php +++ b/src/Type/TypeInterface.php @@ -4,10 +4,6 @@ use Cubicl\StructureCheck\ResultInterface; -/** - * Interface TypeInterface - * @package Cubicl\Cubicl\StructureCheck\Type - */ interface TypeInterface { /**