Skip to content

fix(types): quoted keys of an array shape are read as PHP string literals - #14600

Merged
orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/11573-shape-key-escapes
Nov 23, 2026
Merged

orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/11573-shape-key-escapes

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Quoted keys of an array shape lose their escape sequences: "a\r\nb" is read as the key arnb, so the key can't be used.

Before

<?php
$type = Type::parseString('array{"a\r\nb": int, \'c\nd\': string}')->getSingleAtomic();
array_keys($type->properties); // ['arnb', 'cnd']

After

array_keys($type->properties); // ["a\r\nb", 'c\nd']

Part of #11573

…rals vimeo#11573

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • A quoted key of an array shape is read as PHP reads the string literal (TypeParser::unescapeStringLiteral(), which literal string types already use), not through stripslashes(). In double quotes, \n, \r, \t, \$, \", \\, octal, hex and \u{…} are escapes, and any other backslash is kept ("Foo\Bar" stays Foo\Bar, it was FooBar). In single quotes, only \' and \\ are escapes ('c\nd' keeps its backslash, it was cnd).
  • Changed expectation in TypeParseTest::testTKeyedArrayWithQuotedKeys: "\'" is now the two-character key \', as in PHP, where \' is no escape in a double-quoted string. Before, it was '.
  • A key written over two docblock lines still has its line break collapsed into a space by the docblock parser. That part of the issue ("some⏎key3") is unchanged.
  • Verified: testTKeyedArrayWithEscapeSequencesInKeys and the updated testTKeyedArrayWithQuotedKeys fail on master and pass on the branch. testTKeyedArrayWithQuotedClassConstantKey, testTKeyedArrayWithSpace, AnnotationTest::arrayWithKeySlashesAndNewline and ReturnTypeTest::spacedShapesThatAreNotWordPressHashNotation pass.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 768 72.2 $\color{gray}{\textsf{-1.6\%}}$ 25 691 52 996 682 0
✅ symfony 3 596 265.2 $\color{gray}{\textsf{-0.2\%}}$ 50 610 98 403 1 528 0
✅ doctrine-orm 380 21.7 $\color{gray}{\textsf{+1.6\%}}$ 3 481 6 143 0 0
✅ psl 617 27.2 $\color{gray}{\textsf{-1.9\%}}$ 1 375 5 615 1 0
✅ phpunit 455 19.0 $\color{gray}{\textsf{+2.1\%}}$ 1 731 3 410 18 0
✅ php-parser 226 12.4 $\color{gray}{\textsf{+0.5\%}}$ 17 154 21 245 0 0
✅ tempest 895 34.5 $\color{gray}{\textsf{+1.6\%}}$ 5 047 10 528 252 0
✅ amphp-amp 45 1.1 $\color{gray}{\textsf{-1.8\%}}$ 29 49 0 0
✅ webmozarts-assert 272 3.0 $\color{gray}{\textsf{-1.0\%}}$ 577 2 720 0 0
✅ guzzle-promises 44 1.1 $\color{gray}{\textsf{-4.2\%}}$ 155 434 2 0
✅ monolog 135 3.8 $\color{gray}{\textsf{-5.5\%}}$ 913 2 479 10 0
✅ drupal 4 840 240.8 $\color{gray}{\textsf{+0.2\%}}$ 97 591 $\color{red}{\textsf{+4}}$ $\color{green}{\textsf{-4}}$ 169 293 583 0
✅ wordpress 2 038 270.9 $\color{gray}{\textsf{+0.9\%}}$ 142 704 676 684 461 1
Total 15 311 973.0 $\color{gray}{\textsf{+0.2\%}}$ 347 058 $\color{red}{\textsf{+4}}$ $\color{green}{\textsf{-4}}$ 1 049 999 3 537 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.
✅ drupal: issues +4 −4

Memory 4 840 → 4 840 MB (+0.0 %) · Wall 240.3 → 240.8 s (+0.2 %)

Issue type added removed
InvalidNullableReturnType 2 2
InvalidReturnType 1 1
MixedArgument 1 1

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 orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

jirkace and others added 2 commits November 23, 2026 18:36
…same key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jirkace!

@orklah
orklah merged commit e1c97f3 into vimeo:master Nov 23, 2026
72 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants