Skip to content

Blink: match PHP-CS-Fixer on escapes and dollar signs in SingleQuote - #418

Merged
TomasVotruba merged 1 commit into
ecsphp:mainfrom
Arkounay:blink-single-quote-escaped-backslash
Nov 20, 2026
Merged

TomasVotruba merged 1 commit into
ecsphp:mainfrom
Arkounay:blink-single-quote-escaped-backslash

Conversation

@Arkounay

@Arkounay Arkounay commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem: SingleQuote kept every double-quoted string holding a $ or a backslash. PHP-CS-Fixer converts the ones whose only escapes are \\, \" and \$:

-$pattern = "/^\\d{2,}$/";
-$format = "Y-m-d\\TH:i:s";
-$quoted = "say \"hi\"";
+$pattern = '/^\\d{2,}$/';
+$format = 'Y-m-d\\TH:i:s';
+$quoted = 'say "hi"';

SingleQuoteFixer only looks at T_CONSTANT_ENCAPSED_STRING tokens (L89-L92), so PHP's tokenizer has already decided that no $ in them starts a variable. It keeps double quotes when the string holds a ' (unless strings_containing_single_quote_chars is on) or when (?<!\\)(?:\\{2})*\\(?!["$\\]) finds a backslash escaping anything but \, " or $ (L102-L107), then unescapes \" and \$ and escapes ' (L108-L110).

Change: the rule now gates on aliasIsConstantString, which also rules out "${name}" (one String token in blink, not a constant string in PHP), mirrors the regex with a scan of the backslashes, and applies the same replacements.

Tests: the 32 cases of PHP-CS-Fixer's SingleQuoteFixerTest and 9 more (a $ that starts no variable, \$, \", \\ before escape letters, the other escape sequences, interpolation, the binary prefix, an unterminated literal). Expected output from PHP-CS-Fixer 3.95.32; 11 of the 41 fail on main.

Parity: on a private Symfony codebase (10,390 files, plus 792 files of it before it was formatted), blink and the PHP engine differed in 7 files for this rule; with this change, 0, and no new difference with the rule alone or the full @Symfony set. On a 26,000-file vendor/ tree: 99 files, now 0.

🤖 Generated with Claude Code (Opus 5.5)

SingleQuote kept every double-quoted string holding a $ or a backslash.
PHP-CS-Fixer's SingleQuoteFixer converts any T_CONSTANT_ENCAPSED_STRING
whose only escape sequences are \\, \" and \$ (and that holds no single
quote, by default), unescaping \" and \$, so "/^\\d{2,}$/" becomes
'/^\\d{2,}$/'. Blink now applies the same checks, and ports the fixer's
upstream test cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TomasVotruba
TomasVotruba merged commit ae98cb5 into ecsphp:main Nov 20, 2026
12 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

Thank you 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants