From 79122c8fa823106209ec5998a7dc1e1de70e7a9e Mon Sep 17 00:00:00 2001 From: era269 Date: Sat, 25 Sep 2021 00:09:01 +0300 Subject: [PATCH 1/6] datetime-typehints --- .../AbstractDateTimeImmutableNormalizable.php | 12 +++++ .../DateTimeImmutableRfc3339Normalizable.php | 16 +++++++ src/Object/DateTimeRfc3339Normalizable.php | 16 +++++++ .../AbstractDateTimeNormalizableTrait.php | 19 ++++++++ ...teTimeImmutableRfc3339NormalizableTest.php | 44 +++++++++++++++++++ .../DateTimeRfc3339NormalizableTest.php | 32 ++++++++++++++ 6 files changed, 139 insertions(+) diff --git a/src/Abstraction/AbstractDateTimeImmutableNormalizable.php b/src/Abstraction/AbstractDateTimeImmutableNormalizable.php index 1d19956..8aaaf6b 100644 --- a/src/Abstraction/AbstractDateTimeImmutableNormalizable.php +++ b/src/Abstraction/AbstractDateTimeImmutableNormalizable.php @@ -4,6 +4,7 @@ namespace Era269\Normalizable\Abstraction; +use DateTime; use DateTimeImmutable; use Era269\Normalizable\DenormalizableInterface; use Era269\Normalizable\NormalizableInterface; @@ -12,4 +13,15 @@ abstract class AbstractDateTimeImmutableNormalizable extends DateTimeImmutable implements NormalizableInterface, DenormalizableInterface { use AbstractDateTimeNormalizableTrait; + + /** + * @inheritDoc + */ + public static function createFromMutable(DateTime $object): self + { + /** @var self $dateTimeImmutable */ + $dateTimeImmutable = parent::createFromMutable($object); + + return $dateTimeImmutable; + } } diff --git a/src/Object/DateTimeImmutableRfc3339Normalizable.php b/src/Object/DateTimeImmutableRfc3339Normalizable.php index c2dd16c..8e2b694 100644 --- a/src/Object/DateTimeImmutableRfc3339Normalizable.php +++ b/src/Object/DateTimeImmutableRfc3339Normalizable.php @@ -4,12 +4,28 @@ namespace Era269\Normalizable\Object; +use DateTimeInterface; use Era269\Normalizable\Abstraction\AbstractDateTimeImmutableNormalizable; final class DateTimeImmutableRfc3339Normalizable extends AbstractDateTimeImmutableNormalizable { private const FIELD_NAME_DATE_TIME = 'dateTime'; + /** + * @inheritDoc + */ + public static function createFromInterface(DateTimeInterface $object): self + { + $phpVersion = 80000; + if (PHP_VERSION_ID < $phpVersion) { + self::throwInvalidMethodCallException($phpVersion, __METHOD__); + } + /** @var self $dateTime */ + $dateTime = parent::createFromInterface($object); + + return $dateTime; + } + protected static function getDateTimeFieldName(): string { return self::FIELD_NAME_DATE_TIME; diff --git a/src/Object/DateTimeRfc3339Normalizable.php b/src/Object/DateTimeRfc3339Normalizable.php index de9885a..dfd4d2b 100644 --- a/src/Object/DateTimeRfc3339Normalizable.php +++ b/src/Object/DateTimeRfc3339Normalizable.php @@ -4,12 +4,28 @@ namespace Era269\Normalizable\Object; +use DateTimeInterface; use Era269\Normalizable\Abstraction\AbstractDateTimeNormalizable; final class DateTimeRfc3339Normalizable extends AbstractDateTimeNormalizable { private const FIELD_NAME_DATE_TIME = 'dateTime'; + /** + * @inheritDoc + */ + public static function createFromInterface(DateTimeInterface $object): self + { + $phpVersion = 80000; + if (PHP_VERSION_ID < $phpVersion) { + self::throwInvalidMethodCallException($phpVersion, __METHOD__); + } + /** @var self $dateTime */ + $dateTime = parent::createFromInterface($object); + + return $dateTime; + } + protected static function getDateTimeFieldName(): string { return self::FIELD_NAME_DATE_TIME; diff --git a/src/Traits/AbstractDateTimeNormalizableTrait.php b/src/Traits/AbstractDateTimeNormalizableTrait.php index 3d711bc..8e95790 100644 --- a/src/Traits/AbstractDateTimeNormalizableTrait.php +++ b/src/Traits/AbstractDateTimeNormalizableTrait.php @@ -4,6 +4,7 @@ namespace Era269\Normalizable\Traits; +use BadMethodCallException; use DateTimeInterface; use DateTimeZone; use Exception; @@ -53,4 +54,22 @@ final protected function getObjectForNormalization(): DateTimeInterface } abstract protected function getDateTimeFormat(): string; + + final protected static function throwInvalidMethodCallException(int $phpVersion, string $methodName): void + { + throw new BadMethodCallException(sprintf( + 'Method "%s" is available starting with %s PHP version', + $phpVersion, + $methodName + )); + } + + /** + * @inheritDoc + * @return static|false + */ + public static function createFromFormat($format, $datetime, DateTimeZone $timezone = null) + { + return parent::createFromFormat($format, $datetime, $timezone); + } } diff --git a/tests/Object/DateTimeImmutableRfc3339NormalizableTest.php b/tests/Object/DateTimeImmutableRfc3339NormalizableTest.php index a7b573d..45d2526 100644 --- a/tests/Object/DateTimeImmutableRfc3339NormalizableTest.php +++ b/tests/Object/DateTimeImmutableRfc3339NormalizableTest.php @@ -3,6 +3,9 @@ namespace Era269\Normalizable\Tests\Object; +use BadMethodCallException; +use DateTime; +use DateTimeImmutable; use Era269\Normalizable\Object\DateTimeImmutableRfc3339Normalizable; use PHPUnit\Framework\TestCase; use UnexpectedValueException; @@ -24,6 +27,47 @@ public function testNormalize(): DateTimeImmutableRfc3339Normalizable return $datetime; } + public function testCreateFromInterface(): void + { + $dateTime = new DateTimeImmutable(); + if (PHP_VERSION_ID >= 80000) { + $dateTimeFrom = DateTimeImmutableRfc3339Normalizable::createFromInterface($dateTime); + self::assertInstanceOf(DateTimeImmutableRfc3339Normalizable::class, $dateTimeFrom); + self::assertEquals( + $dateTime->format(DATE_RFC3339), + $dateTimeFrom->format(DATE_RFC3339) + ); + } else { + $this->expectException(BadMethodCallException::class); + DateTimeImmutableRfc3339Normalizable::createFromInterface($dateTime); + } + } + + public function testCreateFromFormat(): void + { + $dateTime = new DateTimeImmutable(); + $dateTimeFrom = DateTimeImmutableRfc3339Normalizable::createFromFormat( + DATE_RFC3339, + $dateTime->format(DATE_RFC3339) + ); + self::assertInstanceOf(DateTimeImmutableRfc3339Normalizable::class, $dateTimeFrom); + self::assertEquals( + $dateTime->format(DATE_RFC3339), + $dateTimeFrom->format(DATE_RFC3339) + ); + } + + public function testCreateFromMutable(): void + { + $dateTime = new DateTime(); + $dateTimeFrom = DateTimeImmutableRfc3339Normalizable::createFromMutable($dateTime); + self::assertInstanceOf(DateTimeImmutableRfc3339Normalizable::class, $dateTimeFrom); + self::assertEquals( + $dateTime->format(DATE_RFC3339), + $dateTimeFrom->format(DATE_RFC3339) + ); + } + /** * @depends testNormalize */ diff --git a/tests/Object/DateTimeRfc3339NormalizableTest.php b/tests/Object/DateTimeRfc3339NormalizableTest.php index 08e18f4..690ad0b 100644 --- a/tests/Object/DateTimeRfc3339NormalizableTest.php +++ b/tests/Object/DateTimeRfc3339NormalizableTest.php @@ -3,6 +3,8 @@ namespace Era269\Normalizable\Tests\Object; +use BadMethodCallException; +use DateTime; use Era269\Normalizable\Object\DateTimeRfc3339Normalizable; use PHPUnit\Framework\TestCase; use UnexpectedValueException; @@ -35,6 +37,36 @@ public function testDenormalize(DateTimeRfc3339Normalizable $dateTime): void ); } + public function testCreateFromInterface(): void + { + $dateTime = new DateTime(); + if (PHP_VERSION_ID >= 80000) { + $dateTimeFrom = DateTimeRfc3339Normalizable::createFromInterface($dateTime); + self::assertInstanceOf(DateTimeRfc3339Normalizable::class, $dateTimeFrom); + self::assertEquals( + $dateTime->format(DATE_RFC3339), + $dateTimeFrom->format(DATE_RFC3339) + ); + } else { + $this->expectException(BadMethodCallException::class); + DateTimeRfc3339Normalizable::createFromInterface($dateTime); + } + } + + public function testCreateFromFormat(): void + { + $dateTime = new DateTime(); + $dateTimeFrom = DateTimeRfc3339Normalizable::createFromFormat( + DATE_RFC3339, + $dateTime->format(DATE_RFC3339) + ); + self::assertInstanceOf(DateTimeRfc3339Normalizable::class, $dateTimeFrom); + self::assertEquals( + $dateTime->format(DATE_RFC3339), + $dateTimeFrom->format(DATE_RFC3339) + ); + } + public function testDenormalizeFail(): void { $this->expectException(UnexpectedValueException::class); From 1d85cec9830465a3a09e562970a44a901f768218 Mon Sep 17 00:00:00 2001 From: era269 Date: Sat, 25 Sep 2021 00:19:24 +0300 Subject: [PATCH 2/6] datetime-typehints --- src/Abstraction/AbstractDateTimeImmutableNormalizable.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Abstraction/AbstractDateTimeImmutableNormalizable.php b/src/Abstraction/AbstractDateTimeImmutableNormalizable.php index 8aaaf6b..3d21f50 100644 --- a/src/Abstraction/AbstractDateTimeImmutableNormalizable.php +++ b/src/Abstraction/AbstractDateTimeImmutableNormalizable.php @@ -4,7 +4,6 @@ namespace Era269\Normalizable\Abstraction; -use DateTime; use DateTimeImmutable; use Era269\Normalizable\DenormalizableInterface; use Era269\Normalizable\NormalizableInterface; @@ -16,10 +15,11 @@ abstract class AbstractDateTimeImmutableNormalizable extends DateTimeImmutable i /** * @inheritDoc + * @return static */ - public static function createFromMutable(DateTime $object): self + public static function createFromMutable($object) { - /** @var self $dateTimeImmutable */ + /** @var static $dateTimeImmutable */ $dateTimeImmutable = parent::createFromMutable($object); return $dateTimeImmutable; From d14a19e74d41a1853556e1b5fe5bbceda7810cce Mon Sep 17 00:00:00 2001 From: era269 Date: Sat, 25 Sep 2021 00:21:36 +0300 Subject: [PATCH 3/6] datetime-typehints --- src/Abstraction/AbstractThrowableToNormalizableAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Abstraction/AbstractThrowableToNormalizableAdapter.php b/src/Abstraction/AbstractThrowableToNormalizableAdapter.php index 622fe9b..55829af 100644 --- a/src/Abstraction/AbstractThrowableToNormalizableAdapter.php +++ b/src/Abstraction/AbstractThrowableToNormalizableAdapter.php @@ -40,7 +40,7 @@ final protected function getNormalized(): array } /** - * @return array + * @return array> */ abstract protected function getTrace(Throwable $throwable): array; From 8bd8119259b2162e45ec24bc2d37da2d06bf8d6a Mon Sep 17 00:00:00 2001 From: era269 Date: Sat, 25 Sep 2021 00:26:54 +0300 Subject: [PATCH 4/6] datetime-typehints --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 80c18aa..ab80c09 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -37,7 +37,7 @@ jobs: run: composer update --prefer-dist --no-progress --no-dev - name: phpStan - run: phpstan analyse + run: vendor/bin/phpstan analyse -c phpstan.neon - name: phpUnit run: phpunit --coverage-clover clover.xml --bootstrap="./tests/bootstrap.php" --whitelist src tests From 2b51c9d295e248256fcba36d0804715975fd3501 Mon Sep 17 00:00:00 2001 From: era269 Date: Sat, 25 Sep 2021 00:31:09 +0300 Subject: [PATCH 5/6] datetime-typehints --- .github/workflows/main.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index ab80c09..99c4632 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -15,14 +15,11 @@ jobs: matrix: php-versions: [ '7.3', '7.4', '8.0' ] phpunit-versions: [ 'latest' ] - phpstan-versions: [ 'latest' ] include: - php-versions: '7.1' phpunit-versions: '7.5.20' - phpstan-versions: [ 'latest' ] - php-versions: '7.2' phpunit-versions: '8.5.15' - phpstan-versions: [ 'latest' ] steps: - uses: actions/checkout@v2 @@ -31,13 +28,13 @@ jobs: with: php-version: ${{ matrix.php-versions }} coverage: xdebug - tools: composer:v2, phpunit:${{ matrix.phpunit-versions }}, phpstan:${{ matrix.phpstan-versions }} + tools: composer:v2, phpunit:${{ matrix.phpunit-versions }}, phpstan - name: Install dependencies run: composer update --prefer-dist --no-progress --no-dev - name: phpStan - run: vendor/bin/phpstan analyse -c phpstan.neon + run: phpstan analyse - name: phpUnit run: phpunit --coverage-clover clover.xml --bootstrap="./tests/bootstrap.php" --whitelist src tests From 928b03efb8a10dec53527453ce9defaac01a646f Mon Sep 17 00:00:00 2001 From: era269 Date: Sat, 25 Sep 2021 00:36:03 +0300 Subject: [PATCH 6/6] datetime-typehints --- src/Traits/AbstractDateTimeNormalizableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/AbstractDateTimeNormalizableTrait.php b/src/Traits/AbstractDateTimeNormalizableTrait.php index 8e95790..0919c0d 100644 --- a/src/Traits/AbstractDateTimeNormalizableTrait.php +++ b/src/Traits/AbstractDateTimeNormalizableTrait.php @@ -68,7 +68,7 @@ final protected static function throwInvalidMethodCallException(int $phpVersion, * @inheritDoc * @return static|false */ - public static function createFromFormat($format, $datetime, DateTimeZone $timezone = null) + public static function createFromFormat($format, $datetime, $timezone = null) { return parent::createFromFormat($format, $datetime, $timezone); }