Repository navigation
fix(types): quoted keys of an array shape are read as PHP string literals - #14600
Conversation
…rals vimeo#11573 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Behaviour change
|
✅ Performance: unchanged · Types: unchanged
How to read this table
✅ drupal: issues +4 −4Memory 4 840 → 4 840 MB (+0.0 %) · Wall 240.3 → 240.8 s (+0.2 %)
Changed in place (4) @@ core/modules/comment/src/CommentForm.php:251 MixedArgument @@
- …states'?: array{visible: array{':input[name=\"uid\"]': array{empty: true}}}, '#title': TranslatableMarkup…
+ …states'?: array{visible: array{':input[name="uid"]': array{empty: true}}}, '#title': TranslatableMarkup…
@@ 2 places InvalidNullableReturnType @@
core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php:29
core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeFieldItemList.php:21
- …states': array{visible: array{':input[id=\"edit-default-value-input-default-date-type\"]': array{value: mixed}}}, '#title': TranslatableMarkup…
+ …states': array{visible: array{':input[id="edit-default-value-input-default-date-type"]': array{value: mixed}}}, '#title': TranslatableMarkup…
@@ core/modules/migrate_drupal_ui/src/Form/CredentialForm.php:85 InvalidReturnType @@
- …states': array{visible: array{':input[name=\"version\"]': array{value: '6'}}}, '#title': TranslatableMarkup…states': array{visible: array{':input[name=\"version\"]': array{value: '7'}}}, '#title': TranslatableMarkup…states': array{visible: array{':input[name=\"version\"]': array{value: '7'}}}, '#title'…
+ …states': array{visible: array{':input[name="version"]': array{value: '6'}}}, '#title': TranslatableMarkup…states': array{visible: array{':input[name="version"]': array{value: '7'}}}, '#title': TranslatableMarkup…states': array{visible: array{':input[name="version"]': array{value: '7'}}}, '#title': Drup…head 00141db · base 9028ade (merge-base) · workflow run |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
orklah
left a comment
There was a problem hiding this comment.
Thanks! Reading quoted keys as PHP string literals is the right fix. The printer needs the same rule, though: TKeyedArray::escapeAndQuote() (used by getId() and toNamespacedString(), so also by --alter) still writes ' . addslashes($name) . ', and addslashes escapes " and NUL, which are no escapes in a single-quoted string. With this PR, keys Psalm prints no longer read back as the same key:
| key | printed | read back on master | read back on this PR |
|---|---|---|---|
" |
'\"' |
" |
\" |
"nul\0" |
'nul\0' |
"nul\0" |
nul\0 (backslash, zero) |
"a\nb" |
'a\nb' |
anb |
a\nb (backslash, n) |
Could you make escapeAndQuote() print a literal that unescapeStringLiteral() reads back unchanged, e.g. single quotes escaping only \ and ', and double quotes with escapes when the key holds control characters (watch addcslashes(), which writes \a/\b that PHP doesn't read, see TLiteralString::getId())? And add a round-trip test, something like:
public function testTKeyedArrayKeysRoundTrip(): void
{
foreach (['"', "a\nb", "a\r\nb", "nul\0", 'Foo\\Bar', "it's"] as $key) {
$printed = TKeyedArray::make([$key => Type::getInt()])->getId();
$type = Type::parseString($printed)->getSingleAtomic();
$this->assertInstanceOf(TKeyedArray::class, $type);
$this->assertSame([$key], array_keys($type->properties), $printed);
}
}Also, the two-line key part of #11573 ("some⏎key3") is left as is, so Fixes #11573 would close the issue with that part open: please use Part of #11573, or say in the issue why it won't be supported.
…same key Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Quoted keys of an array shape lose their escape sequences:
"a\r\nb"is read as the keyarnb, so the key can't be used.Before
After
Part of #11573