Skip to content

Commit

Permalink
Merge pull request #2770 from kylekatarnls/feature/php-8-3
Browse files Browse the repository at this point in the history
Fix PHP 8.3 compatibility
  • Loading branch information
kylekatarnls authored Feb 25, 2023
2 parents 020e560 + 7aa84c6 commit 1aec3f8
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 12 deletions.
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 <brian@nesbot.com>
*
* 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 <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class DateMalformedStringException extends Exception
{
}

0 comments on commit 1aec3f8

Please sign in to comment.