diff --git a/phpstan.neon b/phpstan.neon index d956d48..02711ac 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,10 +9,6 @@ parameters: - identifier: missingType.iterableValue - identifier: property.readOnlyByPhpDocDefaultValue - identifier: property.readOnlyByPhpDocAssignNotInConstructor - - - message: "#^Call to an undefined static method DateTimeImmutable\\:\\:createFromTimestamp\\(\\)\\.$#" - count: 1 - path: src/Chronos.php - message: "#with generic class DatePeriod but does not specify its types: TDate, TEnd, TRecurrences$#" count: 1 diff --git a/src/Chronos.php b/src/Chronos.php index a74bfb6..e6906a9 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -163,9 +163,12 @@ class Chronos extends DateTimeImmutable implements Stringable /** * Format to use for __toString method when type juggling occurs. * - * @var string + * The widened type allows subclasses (like CakePHP I18n classes) to use + * IntlDateFormatter constants while maintaining backward compatibility. + * + * @var array|string|int */ - protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; + protected static array|string|int $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; /** * Days of weekend diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 85bad79..329dd13 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -58,9 +58,12 @@ class ChronosDate implements Stringable /** * Format to use for __toString method when type juggling occurs. * - * @var string + * The widened type allows subclasses (like CakePHP I18n classes) to use + * IntlDateFormatter constants while maintaining backward compatibility. + * + * @var array|string|int */ - protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; + protected static array|string|int $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; /** * Names of days of the week. diff --git a/src/ChronosTime.php b/src/ChronosTime.php index 7d65dd2..cc9362b 100644 --- a/src/ChronosTime.php +++ b/src/ChronosTime.php @@ -60,9 +60,12 @@ class ChronosTime implements Stringable /** * Format to use for __toString method. * - * @var string + * The widened type allows subclasses to use IntlDateFormatter constants + * while maintaining backward compatibility. + * + * @var array|string|int */ - protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; + protected static array|string|int $toStringFormat = self::DEFAULT_TO_STRING_FORMAT; /** * @var int @@ -381,6 +384,8 @@ public static function setToStringFormat(string $format): void */ public function __toString(): string { + assert(is_string(static::$toStringFormat)); + return $this->format(static::$toStringFormat); } diff --git a/src/FormattingTrait.php b/src/FormattingTrait.php index 7b4fbae..8878a37 100644 --- a/src/FormattingTrait.php +++ b/src/FormattingTrait.php @@ -19,7 +19,9 @@ /** * Provides string formatting methods for datetime instances. * - * Expects implementing classes to define static::$toStringFormat + * Expects implementing classes to define static::$toStringFormat as `array|string|int`. + * The widened type allows subclasses (like CakePHP I18n classes) to use + * IntlDateFormatter constants while maintaining backward compatibility. * * @internal */ @@ -54,6 +56,8 @@ public static function setToStringFormat(string $format): void */ public function __toString(): string { + assert(is_string(static::$toStringFormat)); + return $this->format(static::$toStringFormat); } @@ -236,24 +240,11 @@ public function toUnixString(): string /** * Returns the quarter * - * Deprecated 3.3.0: The $range parameter is deprecated. Use toQuarterRange() for quarter ranges. - * - * @param bool $range Range. - * @return array|int 1, 2, 3, or 4 quarter of year or array if $range true + * @return int 1, 2, 3, or 4 quarter of year */ - public function toQuarter(bool $range = false): int|array + public function toQuarter(): int { - $quarter = (int)ceil((int)$this->format('m') / 3); - if ($range === false) { - return $quarter; - } - - trigger_error( - 'Using toQuarter() with `$range=true` is deprecated. Use `toQuarterRange()` instead.', - E_USER_DEPRECATED, - ); - - return $this->toQuarterRange(); + return (int)ceil((int)$this->format('m') / 3); } /** diff --git a/tests/TestCase/DateTime/StringsTest.php b/tests/TestCase/DateTime/StringsTest.php index 9e245fe..a9957d8 100644 --- a/tests/TestCase/DateTime/StringsTest.php +++ b/tests/TestCase/DateTime/StringsTest.php @@ -186,7 +186,7 @@ public static function toQuarterProvider() * @return void */ #[DataProvider('toQuarterProvider')] - public function testToQuarter($date, $expected, $range = false) + public function testToQuarter($date, $expected) { $this->assertSame($expected, (new Chronos($date))->toQuarter()); } @@ -205,9 +205,6 @@ public static function toQuarterRangeProvider() public function testToQuarterRange($date, $expected) { $this->assertSame($expected, (new Chronos($date))->toQuarterRange()); - $this->deprecated(function () use ($date, $expected) { - $this->assertSame($expected, (new Chronos($date))->toQuarter(true)); - }); } /**