diff --git a/src/Carbon/Traits/Creator.php b/src/Carbon/Traits/Creator.php index 9509e5ba..33fa9f5b 100644 --- a/src/Carbon/Traits/Creator.php +++ b/src/Carbon/Traits/Creator.php @@ -50,7 +50,7 @@ trait Creator /** * The errors that can occur. */ - protected static ?array $lastErrors = null; + protected static array|bool $lastErrors = false; /** * Create a new Carbon instance. @@ -905,14 +905,7 @@ public static function make($var, DateTimeZone|string|null $timezone = null): ?s */ private static function setLastErrors($lastErrors): void { - if (\is_array($lastErrors) || $lastErrors === false) { - static::$lastErrors = \is_array($lastErrors) ? $lastErrors : [ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ]; - } + static::$lastErrors = $lastErrors; } /** @@ -920,7 +913,7 @@ private static function setLastErrors($lastErrors): void */ public static function getLastErrors(): array|false { - return static::$lastErrors ?? false; + return static::$lastErrors; } private static function monthToInt(mixed $value, string $unit = 'month'): mixed diff --git a/tests/Carbon/CreateFromFormatTest.php b/tests/Carbon/CreateFromFormatTest.php index 54714cc1..27e2aac4 100644 --- a/tests/Carbon/CreateFromFormatTest.php +++ b/tests/Carbon/CreateFromFormatTest.php @@ -16,6 +16,7 @@ use Carbon\Carbon; use DateTime; use DateTimeZone; +use PHPUnit\Framework\Attributes\RequiresPhp; use Tests\AbstractTestCase; use Tests\Carbon\Fixtures\MyCarbon; @@ -35,13 +36,6 @@ protected function setUp(): void { parent::setUp(); - $this->noErrors = [ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ]; - $this->lastErrors = [ 'warning_count' => 1, 'warnings' => ['10' => 'The parsed date was invalid'], @@ -141,14 +135,10 @@ public function testCreateFromFormatWithTestNow() $this->assertSame($mockedDate->micro === 0, $nativeDate->micro === 0); } + #[RequiresPhp('>=8.2')] public function testCreateLastErrorsCanBeAccessedByExtendingClass() { - $this->assertSame([ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ], MyCarbon::getLastErrors()); + $this->assertFalse(MyCarbon::getLastErrors()); } public function testCreateFromFormatHandlesLastErrors() @@ -160,12 +150,13 @@ public function testCreateFromFormatHandlesLastErrors() $this->assertSame($carbon->getLastErrors(), $datetime->getLastErrors()); } + #[RequiresPhp('>=8.2')] public function testCreateFromFormatResetLastErrors() { $carbon = Carbon::createFromFormat('d/m/Y', '41/02/1900'); $this->assertSame($this->lastErrors, $carbon->getLastErrors()); $carbon = Carbon::createFromFormat('d/m/Y', '11/03/2016'); - $this->assertSame($this->noErrors, $carbon->getLastErrors()); + $this->assertFalse($carbon->getLastErrors()); } } diff --git a/tests/Carbon/LastErrorTest.php b/tests/Carbon/LastErrorTest.php index dd795092..8290aa22 100644 --- a/tests/Carbon/LastErrorTest.php +++ b/tests/Carbon/LastErrorTest.php @@ -16,6 +16,7 @@ use Carbon\Carbon; use Carbon\Traits\Creator; use DateTime; +use PHPUnit\Framework\Attributes\RequiresPhp; use Tests\AbstractTestCase; class LastErrorTest extends AbstractTestCase @@ -34,13 +35,6 @@ protected function setUp(): void { parent::setUp(); - $this->noErrors = [ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ]; - $this->lastErrors = [ 'warning_count' => 1, 'warnings' => ['11' => 'The parsed date was invalid'], @@ -49,6 +43,7 @@ protected function setUp(): void ]; } + #[RequiresPhp('>=8.2')] public function testCreateHandlesLastErrors() { $carbon = new Carbon('2017-02-30'); @@ -59,7 +54,7 @@ public function testCreateHandlesLastErrors() $carbon = new Carbon('2017-02-15'); - $this->assertSame($this->noErrors, $carbon->getLastErrors()); + $this->assertFalse($carbon->getLastErrors()); } public function testLastErrorsInitialization() @@ -74,16 +69,16 @@ public function __construct($time = null, $tz = null) public function triggerError() { - self::setLastErrors(false); + self::setLastErrors([ + 'warning_count' => 1, + 'warnings' => ['11' => 'The parsed date was invalid'], + 'error_count' => 0, + 'errors' => [], + ]); } }; $this->assertFalse($obj::getLastErrors()); $obj->triggerError(); - $this->assertSame([ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ], $obj::getLastErrors()); + $this->assertSame($this->lastErrors, $obj::getLastErrors()); } } diff --git a/tests/CarbonImmutable/CreateFromFormatTest.php b/tests/CarbonImmutable/CreateFromFormatTest.php index 5c87899f..2b877d0a 100644 --- a/tests/CarbonImmutable/CreateFromFormatTest.php +++ b/tests/CarbonImmutable/CreateFromFormatTest.php @@ -16,6 +16,7 @@ use Carbon\CarbonImmutable as Carbon; use DateTime; use DateTimeZone; +use PHPUnit\Framework\Attributes\RequiresPhp; use Tests\AbstractTestCase; use Tests\Carbon\Fixtures\MyCarbon; @@ -35,13 +36,6 @@ protected function setUp(): void { parent::setUp(); - $this->noErrors = [ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ]; - $this->lastErrors = [ 'warning_count' => 1, 'warnings' => ['10' => 'The parsed date was invalid'], @@ -87,14 +81,10 @@ public function testCreateFromFormatWithTestNow() $this->assertSame($mockedDate->micro === 0, $nativeDate->micro === 0); } + #[RequiresPhp('>=8.2')] public function testCreateLastErrorsCanBeAccessedByExtendingClass() { - $this->assertSame([ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ], MyCarbon::getLastErrors()); + $this->assertFalse(MyCarbon::getLastErrors()); } public function testCreateFromFormatHandlesLastErrors() @@ -106,13 +96,14 @@ public function testCreateFromFormatHandlesLastErrors() $this->assertSame($carbon->getLastErrors(), $datetime->getLastErrors()); } + #[RequiresPhp('>=8.2')] public function testCreateFromFormatResetLastErrors() { $carbon = Carbon::createFromFormat('d/m/Y', '41/02/1900'); $this->assertSame($this->lastErrors, $carbon->getLastErrors()); $carbon = Carbon::createFromFormat('d/m/Y', '11/03/2016'); - $this->assertSame($this->noErrors, $carbon->getLastErrors()); + $this->assertFalse($carbon->getLastErrors()); } public function testCreateFromFormatWithDollar() diff --git a/tests/CarbonImmutable/LastErrorTest.php b/tests/CarbonImmutable/LastErrorTest.php index 12fff5d1..02e8f139 100644 --- a/tests/CarbonImmutable/LastErrorTest.php +++ b/tests/CarbonImmutable/LastErrorTest.php @@ -16,6 +16,7 @@ use Carbon\CarbonImmutable as Carbon; use Carbon\Traits\Creator; use DateTime; +use PHPUnit\Framework\Attributes\RequiresPhp; use Tests\AbstractTestCase; class LastErrorTest extends AbstractTestCase @@ -34,13 +35,6 @@ protected function setUp(): void { parent::setUp(); - $this->noErrors = [ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ]; - $this->lastErrors = [ 'warning_count' => 1, 'warnings' => ['11' => 'The parsed date was invalid'], @@ -49,6 +43,7 @@ protected function setUp(): void ]; } + #[RequiresPhp('>=8.2')] public function testCreateHandlesLastErrors() { $carbon = new Carbon('2017-02-30'); @@ -59,7 +54,7 @@ public function testCreateHandlesLastErrors() $carbon = new Carbon('2017-02-15'); - $this->assertSame($this->noErrors, $carbon->getLastErrors()); + $this->assertFalse($carbon->getLastErrors()); } public function testLastErrorsInitialization() @@ -74,16 +69,16 @@ public function __construct($time = null, $tz = null) public function triggerError() { - self::setLastErrors(false); + self::setLastErrors([ + 'warning_count' => 1, + 'warnings' => ['11' => 'The parsed date was invalid'], + 'error_count' => 0, + 'errors' => [], + ]); } }; $this->assertFalse($obj::getLastErrors()); $obj->triggerError(); - $this->assertSame([ - 'warning_count' => 0, - 'warnings' => [], - 'error_count' => 0, - 'errors' => [], - ], $obj::getLastErrors()); + $this->assertSame($this->lastErrors, $obj::getLastErrors()); } }