diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b93c1e4..8049626 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,6 +6,7 @@ on: - 2.x - 2.next - 3.x + - 4.x pull_request: branches: - '*' @@ -19,5 +20,34 @@ jobs: secrets: inherit cs-stan: - uses: cakephp/.github/.github/workflows/cs-stan.yml@5.x - secrets: inherit + name: Coding Standard & Static Analysis + runs-on: ubuntu-24.04 + + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.4' + extensions: mbstring, intl + coverage: none + tools: phive, cs2pr + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Composer install + uses: ramsey/composer-install@v3 + + - name: Install PHP tools with phive. + run: "phive install --trust-gpg-keys 'CF1A108D0E7AE720,51C67305FFC2E5C0,12CE0F1D262429A5,99BF4D9A33D65E1E'" + + - name: Run phpcs + if: always() + run: vendor/bin/phpcs --report=checkstyle | cs2pr + + - name: Run phpstan + if: always() + run: tools/phpstan analyse --error-format=github 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 53b198a..57b6f66 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)); - }); } /**