Skip to content

Commit 3189c17

Browse files
authored
Cleanup for new major (required by Cake6) (#497)
* Remove deprecated toQuarter() $range parameter - Remove $range parameter from toQuarter() (deprecated in 3.3.0) - Update tests to remove deprecated usage - Remove outdated PHPStan ignore for createFromTimestamp() * Widen $toStringFormat type for CakePHP I18n compatibility Allow subclasses to use IntlDateFormatter constants by widening the $toStringFormat property type from string to array|string|int. This enables CakePHP I18n classes to rename $_toStringFormat to $toStringFormat without type conflicts.
1 parent ebde951 commit 3189c17

7 files changed

Lines changed: 58 additions & 33 deletions

File tree

.github/workflows/ci.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- 2.x
77
- 2.next
88
- 3.x
9+
- 4.x
910
pull_request:
1011
branches:
1112
- '*'
@@ -19,5 +20,34 @@ jobs:
1920
secrets: inherit
2021

2122
cs-stan:
22-
uses: cakephp/.github/.github/workflows/cs-stan.yml@5.x
23-
secrets: inherit
23+
name: Coding Standard & Static Analysis
24+
runs-on: ubuntu-24.04
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
with:
29+
persist-credentials: false
30+
31+
- name: Setup PHP
32+
uses: shivammathur/setup-php@v2
33+
with:
34+
php-version: '8.4'
35+
extensions: mbstring, intl
36+
coverage: none
37+
tools: phive, cs2pr
38+
env:
39+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40+
41+
- name: Composer install
42+
uses: ramsey/composer-install@v3
43+
44+
- name: Install PHP tools with phive.
45+
run: "phive install --trust-gpg-keys 'CF1A108D0E7AE720,51C67305FFC2E5C0,12CE0F1D262429A5,99BF4D9A33D65E1E'"
46+
47+
- name: Run phpcs
48+
if: always()
49+
run: vendor/bin/phpcs --report=checkstyle | cs2pr
50+
51+
- name: Run phpstan
52+
if: always()
53+
run: tools/phpstan analyse --error-format=github

phpstan.neon

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ parameters:
99
- identifier: missingType.iterableValue
1010
- identifier: property.readOnlyByPhpDocDefaultValue
1111
- identifier: property.readOnlyByPhpDocAssignNotInConstructor
12-
-
13-
message: "#^Call to an undefined static method DateTimeImmutable\\:\\:createFromTimestamp\\(\\)\\.$#"
14-
count: 1
15-
path: src/Chronos.php
1612
-
1713
message: "#with generic class DatePeriod but does not specify its types: TDate, TEnd, TRecurrences$#"
1814
count: 1

src/Chronos.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,12 @@ class Chronos extends DateTimeImmutable implements Stringable
163163
/**
164164
* Format to use for __toString method when type juggling occurs.
165165
*
166-
* @var string
166+
* The widened type allows subclasses (like CakePHP I18n classes) to use
167+
* IntlDateFormatter constants while maintaining backward compatibility.
168+
*
169+
* @var array|string|int
167170
*/
168-
protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
171+
protected static array|string|int $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
169172

170173
/**
171174
* Days of weekend

src/ChronosDate.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ class ChronosDate implements Stringable
5858
/**
5959
* Format to use for __toString method when type juggling occurs.
6060
*
61-
* @var string
61+
* The widened type allows subclasses (like CakePHP I18n classes) to use
62+
* IntlDateFormatter constants while maintaining backward compatibility.
63+
*
64+
* @var array|string|int
6265
*/
63-
protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
66+
protected static array|string|int $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
6467

6568
/**
6669
* Names of days of the week.

src/ChronosTime.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ class ChronosTime implements Stringable
6060
/**
6161
* Format to use for __toString method.
6262
*
63-
* @var string
63+
* The widened type allows subclasses to use IntlDateFormatter constants
64+
* while maintaining backward compatibility.
65+
*
66+
* @var array|string|int
6467
*/
65-
protected static string $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
68+
protected static array|string|int $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
6669

6770
/**
6871
* @var int
@@ -381,6 +384,8 @@ public static function setToStringFormat(string $format): void
381384
*/
382385
public function __toString(): string
383386
{
387+
assert(is_string(static::$toStringFormat));
388+
384389
return $this->format(static::$toStringFormat);
385390
}
386391

src/FormattingTrait.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
/**
2020
* Provides string formatting methods for datetime instances.
2121
*
22-
* Expects implementing classes to define static::$toStringFormat
22+
* Expects implementing classes to define static::$toStringFormat as `array|string|int`.
23+
* The widened type allows subclasses (like CakePHP I18n classes) to use
24+
* IntlDateFormatter constants while maintaining backward compatibility.
2325
*
2426
* @internal
2527
*/
@@ -54,6 +56,8 @@ public static function setToStringFormat(string $format): void
5456
*/
5557
public function __toString(): string
5658
{
59+
assert(is_string(static::$toStringFormat));
60+
5761
return $this->format(static::$toStringFormat);
5862
}
5963

@@ -236,24 +240,11 @@ public function toUnixString(): string
236240
/**
237241
* Returns the quarter
238242
*
239-
* Deprecated 3.3.0: The $range parameter is deprecated. Use toQuarterRange() for quarter ranges.
240-
*
241-
* @param bool $range Range.
242-
* @return array|int 1, 2, 3, or 4 quarter of year or array if $range true
243+
* @return int 1, 2, 3, or 4 quarter of year
243244
*/
244-
public function toQuarter(bool $range = false): int|array
245+
public function toQuarter(): int
245246
{
246-
$quarter = (int)ceil((int)$this->format('m') / 3);
247-
if ($range === false) {
248-
return $quarter;
249-
}
250-
251-
trigger_error(
252-
'Using toQuarter() with `$range=true` is deprecated. Use `toQuarterRange()` instead.',
253-
E_USER_DEPRECATED,
254-
);
255-
256-
return $this->toQuarterRange();
247+
return (int)ceil((int)$this->format('m') / 3);
257248
}
258249

259250
/**

tests/TestCase/DateTime/StringsTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public static function toQuarterProvider()
186186
* @return void
187187
*/
188188
#[DataProvider('toQuarterProvider')]
189-
public function testToQuarter($date, $expected, $range = false)
189+
public function testToQuarter($date, $expected)
190190
{
191191
$this->assertSame($expected, (new Chronos($date))->toQuarter());
192192
}
@@ -205,9 +205,6 @@ public static function toQuarterRangeProvider()
205205
public function testToQuarterRange($date, $expected)
206206
{
207207
$this->assertSame($expected, (new Chronos($date))->toQuarterRange());
208-
$this->deprecated(function () use ($date, $expected) {
209-
$this->assertSame($expected, (new Chronos($date))->toQuarter(true));
210-
});
211208
}
212209

213210
/**

0 commit comments

Comments
 (0)