IsoWeekCalendarYearMismatch
warning native on by default quick-fixGroup: Probable bugs
Detect a date format literal containing unescaped Y and W in the same year-week label.
Example
<?php
$day=new DateTimeImmutable(); $label=$day->format('Y-W');<?php
$day=new DateTimeImmutable(); $label=$day->format('o-W');Reported:
- line 2: Use the ISO week year with the ISO week number.
Detection
- Report a date format literal containing unescaped Y and W in the same year-week label. The Y counts as a week label only when the format has no other part that belongs to the calendar year (see E1); otherwise Y is the correct year for that part and the format is left alone.
- Resolve builtin calls and named arguments before checking the contract. Project-local callees are considered only when their bodies or immutable summaries establish the required facts. The default is enabled, with positive proof required.
- For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases, recursion or analysis-budget exhaustion cannot establish definite facts.
Exceptions (no report)
- E1. Escaped tokens and formats already using the ISO week-year o are excluded. So is any format with an unescaped calendar-dependent token: a month (
m,n,F,M), a day of the month (d,j), the day of the year (z), the month length (t), the leap-year flag (L), or a composite date (c,r,U). There Y labels the month, day or full date, and swapping it for o would print that part with the wrong year, as inF Y, \w\e\e\k W,Y-m W,Y/z Worc W Y. Weekday tokens (D,l,N,w), time and timezone tokens do not exclude the format. - Unresolved targets, incomplete required facts, suppressed findings and PHP versions lacking the referenced API or language feature produce no report. User declarations shadowing builtin names are not builtin contracts.
Fix
Replace every unescaped Y token with o in the literal; preserve all other format bytes. E1 guarantees that Y solely labels the ISO week in a reported format, so the replacement cannot change the year of a month, day or composite date. The fix is offered only when the format argument is a plain quoted literal whose source text equals its value; a format reached through a variable, constant, concatenation or a literal with escape sequences is reported without a fix.
A fix is offered only when the concrete prerequisites above are proven. Edits use exact source byte ranges, preserve comments and argument evaluation order, and introduce syntax supported by the configured PHP version. Never invent error-handling policy.
PHP versions
Apply only on PHP versions supporting the referenced APIs and syntax (within PHP 5.3–8.5). Builtin behavior and argument contracts follow the configured PHP version. Extension-specific calls require resolved extension API symbols; do not treat same-named application functions as extension calls.
Options
None. Enable or disable this rule through normal rule configuration.
Configure
In custos.json:
{
"rules": {
"IsoWeekCalendarYearMismatch": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
// @custos-ignore IsoWeekCalendarYearMismatch