Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHP 8.3 compatibility #2770

Merged
merged 3 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ parameters:
- lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php
- lazy/Carbon/PHPStan/MacroStrongType.php
- lazy/Carbon/TranslatorStrongType.php
- tests/Fixtures/DateMalformedIntervalStringException.php
- tests/Fixtures/DateMalformedStringException.php
- vendor/autoload.php
level: 3
paths:
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Latest Stable Version](https://img.shields.io/packagist/v/nesbot/carbon.svg?style=flat-square)](https://packagist.org/packages/nesbot/carbon)
[![Total Downloads](https://img.shields.io/packagist/dt/nesbot/carbon.svg?style=flat-square)](https://packagist.org/packages/nesbot/carbon)
[![GitHub Actions](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fbriannesbitt%2FCarbon%2Fbadge&style=flat-square&label=Build&logo=none)](https://actions-badge.atrox.dev/briannesbitt/Carbon/goto)
[![GitHub Actions](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fbriannesbitt%2FCarbon%2Fbadge&style=flat-square&label=Build&logo=none)](https://github.com/briannesbitt/Carbon/actions)
[![codecov.io](https://img.shields.io/codecov/c/github/briannesbitt/Carbon.svg?style=flat-square)](https://codecov.io/github/briannesbitt/Carbon?branch=master)
[![Tidelift](https://tidelift.com/badges/github/briannesbitt/Carbon)](https://tidelift.com/subscription/pkg/packagist-nesbot-carbon?utm_source=packagist-nesbot-carbon&utm_medium=referral&utm_campaign=readme)

Expand Down
11 changes: 9 additions & 2 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Carbon\Traits\ToStringFormat;
use Closure;
use DateInterval;
use DateMalformedIntervalStringException;
use DateTimeInterface;
use DateTimeZone;
use Exception;
Expand Down Expand Up @@ -1004,8 +1005,14 @@ protected static function makeFromString(string $interval)
return static::fromString($interval);
}

/** @var static $interval */
$interval = static::createFromDateString($interval);
// @codeCoverageIgnoreStart
try {
/** @var static $interval */
$interval = static::createFromDateString($interval);
} catch (DateMalformedIntervalStringException $e) {
return null;
}
// @codeCoverageIgnoreEnd

return !$interval || $interval->isEmpty() ? null : $interval;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Carbon/Traits/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Carbon\Exceptions\OutOfRangeException;
use Carbon\Translator;
use Closure;
use DateMalformedStringException;
use DateTimeInterface;
use DateTimeZone;
use Exception;
Expand Down Expand Up @@ -184,7 +185,13 @@ public static function rawParse($time = null, $tz = null)
try {
return new static($time, $tz);
} catch (Exception $exception) {
$date = @static::now($tz)->change($time);
// @codeCoverageIgnoreStart
try {
$date = @static::now($tz)->change($time);
} catch (DateMalformedStringException $ignoredException) {
$date = null;
}
// @codeCoverageIgnoreEnd

if (!$date) {
throw new InvalidFormatException("Could not parse '$time': ".$exception->getMessage(), 0, $exception);
Expand Down
16 changes: 11 additions & 5 deletions src/Carbon/Traits/Units.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Carbon\Exceptions\UnitException;
use Closure;
use DateInterval;
use DateMalformedStringException;
use ReturnTypeWillChange;

/**
Expand Down Expand Up @@ -304,12 +305,17 @@ public function addUnit($unit, $value = 1, $overflow = null)
$unit = 'second';
$value = $second;
}
$date = $date->modify("$value $unit");

if (isset($timeString)) {
$date = $date->setTimeFromTimeString($timeString);
} elseif (isset($canOverflow, $day) && $canOverflow && $day !== $date->day) {
$date = $date->modify('last day of previous month');
try {
$date = $date->modify("$value $unit");

if (isset($timeString)) {
$date = $date->setTimeFromTimeString($timeString);
} elseif (isset($canOverflow, $day) && $canOverflow && $day !== $date->day) {
$date = $date->modify('last day of previous month');
}
} catch (DateMalformedStringException $ignoredException) { // @codeCoverageIgnore
$date = null; // @codeCoverageIgnore
}

if (!$date) {
Expand Down
33 changes: 30 additions & 3 deletions tests/Carbon/ModifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace Tests\Carbon;

use Carbon\Carbon;
use Closure;
use DateMalformedStringException;
use InvalidArgumentException;
use Tests\AbstractTestCase;

Expand Down Expand Up @@ -256,9 +258,34 @@ public function testNextAndPrevious()

public function testInvalidModifier(): void
{
$this->assertFalse(@Carbon::parse('2000-01-25')->change('invalid'));
$this->assertFalse(@Carbon::now()->next('invalid'));
$this->assertFalse(@Carbon::now()->previous('invalid'));
$this->checkInvalid('invalid', static function () {
return @Carbon::parse('2000-01-25')->change('invalid');
});
$this->checkInvalid('next invalid', static function () {
return @Carbon::now()->next('invalid');
});
$this->checkInvalid('last invalid', static function () {
return @Carbon::now()->previous('invalid');
});
}

private function checkInvalid(string $message, Closure $callback): void
{
if (PHP_VERSION < 8.3) {
$this->assertFalse($callback());

return;
}

try {
$callback();
} catch (DateMalformedStringException $exception) {
$this->assertStringContainsString("Failed to parse time string ($message)", $exception->getMessage());

return;
}

$this->fail('This should throw a DateMalformedStringException in PHP 8.3');
}

public function testImplicitCast(): void
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/DateMalformedIntervalStringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class DateMalformedIntervalStringException extends Exception
{
}
14 changes: 14 additions & 0 deletions tests/Fixtures/DateMalformedStringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class DateMalformedStringException extends Exception
{
}