Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/Carbon/Traits/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -905,22 +905,15 @@ 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;
}

/**
* {@inheritdoc}
*/
public static function getLastErrors(): array|false
{
return static::$lastErrors ?? false;
return static::$lastErrors;
}

private static function monthToInt(mixed $value, string $unit = 'month'): mixed
Expand Down
16 changes: 2 additions & 14 deletions tests/Carbon/CreateFromFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,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' => ['10' => 'The parsed date was invalid'],
Expand Down Expand Up @@ -143,12 +136,7 @@ public function testCreateFromFormatWithTestNow()

public function testCreateLastErrorsCanBeAccessedByExtendingClass()
{
$this->assertSame([
'warning_count' => 0,
'warnings' => [],
'error_count' => 0,
'errors' => [],
], MyCarbon::getLastErrors());
$this->assertFalse(MyCarbon::getLastErrors());
}

public function testCreateFromFormatHandlesLastErrors()
Expand All @@ -166,6 +154,6 @@ public function testCreateFromFormatResetLastErrors()
$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());
}
}
23 changes: 8 additions & 15 deletions tests/Carbon/LastErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,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'],
Expand All @@ -59,7 +52,7 @@ public function testCreateHandlesLastErrors()

$carbon = new Carbon('2017-02-15');

$this->assertSame($this->noErrors, $carbon->getLastErrors());
$this->assertFalse($carbon->getLastErrors());
}

public function testLastErrorsInitialization()
Expand All @@ -74,16 +67,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());
}
}
16 changes: 2 additions & 14 deletions tests/CarbonImmutable/CreateFromFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,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' => ['10' => 'The parsed date was invalid'],
Expand Down Expand Up @@ -89,12 +82,7 @@ public function testCreateFromFormatWithTestNow()

public function testCreateLastErrorsCanBeAccessedByExtendingClass()
{
$this->assertSame([
'warning_count' => 0,
'warnings' => [],
'error_count' => 0,
'errors' => [],
], MyCarbon::getLastErrors());
$this->assertFalse(MyCarbon::getLastErrors());
}

public function testCreateFromFormatHandlesLastErrors()
Expand All @@ -112,7 +100,7 @@ public function testCreateFromFormatResetLastErrors()
$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()
Expand Down
23 changes: 8 additions & 15 deletions tests/CarbonImmutable/LastErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,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'],
Expand All @@ -59,7 +52,7 @@ public function testCreateHandlesLastErrors()

$carbon = new Carbon('2017-02-15');

$this->assertSame($this->noErrors, $carbon->getLastErrors());
$this->assertFalse($carbon->getLastErrors());
}

public function testLastErrorsInitialization()
Expand All @@ -74,16 +67,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());
}
}
Loading