Skip to content

Commit 7a4a978

Browse files
authored
Fix createFromMutable error (#298)
1 parent 5328ace commit 7a4a978

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

.github/workflows/continuous-integration.yml

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ jobs:
6666
- name: "Install dependencies with composer in root directory"
6767
run: "composer install --no-interaction"
6868

69+
- name: "Run tests with phpunit/phpunit in root directory"
70+
run: "vendor/bin/phpunit"
71+
6972
- name: "Run tests with phpunit/phpunit in generator/ directory"
7073
run: "vendor/bin/phpunit"
7174
working-directory: "generator"

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
"require-dev": {
105105
"phpstan/phpstan": "^0.12",
106106
"thecodingmachine/phpstan-strict-rules": "^0.12",
107-
"squizlabs/php_codesniffer": "^3.2"
107+
"squizlabs/php_codesniffer": "^3.2",
108+
"phpunit/phpunit": "^9.5"
108109
},
109110
"scripts": {
110111
"phpstan": "phpstan analyse lib -c phpstan.neon --level=max --no-progress -vvv",
@@ -116,4 +117,4 @@
116117
"dev-master": "0.1-dev"
117118
}
118119
}
119-
}
120+
}

lib/DateTimeImmutable.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
namespace Safe;
44

5-
use DateInterval;
6-
use DateTime;
7-
use DateTimeInterface;
8-
use DateTimeZone;
95
use Safe\Exceptions\DatetimeException;
106

117
/**
@@ -23,7 +19,7 @@ class DateTimeImmutable extends \DateTimeImmutable
2319
/**
2420
* DateTimeImmutable constructor.
2521
* @param string $time
26-
* @param DateTimeZone|null $timezone
22+
* @param \DateTimeZone|null $timezone
2723
* @throws \Exception
2824
*/
2925
public function __construct($time = 'now', $timezone = null)
@@ -52,7 +48,7 @@ public function getInnerDateTime(): \DateTimeImmutable
5248
/**
5349
* @param string $format
5450
* @param string $time
55-
* @param DateTimeZone|null $timezone
51+
* @param \DateTimeZone|null $timezone
5652
* @throws DatetimeException
5753
*/
5854
public static function createFromFormat($format, $time, $timezone = null)
@@ -80,12 +76,12 @@ public function format($format): string
8076
}
8177

8278
/**
83-
* @param DateTimeInterface $datetime2
79+
* @param \DateTimeInterface $datetime2
8480
* @param bool $absolute
85-
* @return DateInterval
81+
* @return \DateInterval
8682
* @throws DatetimeException
8783
*/
88-
public function diff($datetime2, $absolute = false): DateInterval
84+
public function diff($datetime2, $absolute = false): \DateInterval
8985
{
9086
/** @var \DateInterval|false $result */
9187
$result = $this->innerDateTime->diff($datetime2, $absolute);
@@ -178,7 +174,7 @@ public function setTimestamp($unixtimestamp): self
178174
}
179175

180176
/**
181-
* @param DateTimeZone $timezone
177+
* @param \DateTimeZone $timezone
182178
* @return DateTimeImmutable
183179
* @throws DatetimeException
184180
*/
@@ -193,7 +189,7 @@ public function setTimezone($timezone): self
193189
}
194190

195191
/**
196-
* @param DateInterval $interval
192+
* @param \DateInterval $interval
197193
* @return DateTimeImmutable
198194
* @throws DatetimeException
199195
*/
@@ -224,7 +220,7 @@ public function getOffset(): int
224220
//overload getters to use the inner datetime immutable instead of itself
225221

226222
/**
227-
* @param DateInterval $interval
223+
* @param \DateInterval $interval
228224
* @return DateTimeImmutable
229225
*/
230226
public function add($interval): self
@@ -233,12 +229,14 @@ public function add($interval): self
233229
}
234230

235231
/**
236-
* @param DateTime $dateTime
232+
* @param \DateTime $dateTime
237233
* @return DateTimeImmutable
238234
*/
239235
public static function createFromMutable($dateTime): self
240236
{
241-
return self::createFromRegular(parent::createFromMutable($dateTime));
237+
$date = \DateTimeImmutable::createFromMutable($dateTime);
238+
239+
return self::createFromRegular($date);
242240
}
243241

244242
/**
@@ -250,7 +248,7 @@ public static function __set_state($array): self
250248
return self::createFromRegular(parent::__set_state($array));
251249
}
252250

253-
public function getTimezone(): DateTimeZone
251+
public function getTimezone(): \DateTimeZone
254252
{
255253
return $this->innerDateTime->getTimezone();
256254
}

phpunit.xml.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<testsuites>
10+
<testsuite name="Main test Suite">
11+
<directory>./tests/</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

tests/DateTimeImmutableTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class DateTimeImmutableTest extends TestCase
8+
{
9+
public function testCreateFromMutable(): void
10+
{
11+
$unsafeDate = new \DateTime('2021-10-16T12:30:16+00:00');
12+
$safeImmutableDate = \Safe\DateTimeImmutable::createFromMutable($unsafeDate);
13+
14+
self::assertSame($unsafeDate->format(\DateTimeInterface::ATOM), $safeImmutableDate->format(\DateTimeInterface::ATOM));
15+
16+
$safeDate = new \Safe\DateTime('2021-10-16T12:30:16+00:00');
17+
$safeImmutableDate = \Safe\DateTimeImmutable::createFromMutable($safeDate);
18+
19+
self::assertSame($safeDate->format(\DateTimeInterface::ATOM), $safeImmutableDate->format(\DateTimeInterface::ATOM));
20+
}
21+
}

0 commit comments

Comments
 (0)