Skip to content

Control symbols in array shape keys definition incorrectly inferred by psalm #11573

Open

Description

@alexandrmazur96

Hi,

I'm not sure if it's me who did something wrong or if it's wrong Psalm behavior, but take a look at the following example - https://psalm.dev/r/dc2c9fe8e7

I have a CSV that contains headers with line breaks, and I can't/don't know how to correctly define the Psalm array shape for the records from that CSV.

From my perspective, it looks like I can't define an array shape key that has line breaks - Psalm just strips the escape symbol (\) from the array shape definition, making it unable to define. I tried to define it with an actual line break, but it seems that Psalm treats it as a space character instead of a line break sequence.

Activity

  1. psalm-github-bot commented on Oct 23, 2025

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/dc2c9fe8e7
    <?php
    
    /**
     * @psalm-type _Row=array{
     *     "some\r\nkey1"?: string,
     *     "some\nkey2"?: string,
     *	   "some
     key3": string
     * }
     */
    class A {
        /** @param list<_Row> $rows */
    	public function a(array $rows): void {
    		$key1 = "some\r\nkey1";
            $key2 = "some\nkey2";
            $key3 = "some\nkey3";
            
            foreach ($rows as $row) {
                echo (string)($row[$key1] ?? $row[$key2] ?? $row[$key3]);
            }
        }
    }
    Psalm output (using commit cdceda0):
    
    ERROR: InvalidArrayOffset - 19:27 - Cannot access value on variable $row using offset value of 'some
    key1', expecting 'somernkey1', 'somenkey2' or 'some key3'
    
    ERROR: InvalidArrayOffset - 19:42 - Cannot access value on variable $row using offset value of 'some
    key2', expecting 'somernkey1', 'somenkey2' or 'some key3'
    
    ERROR: InvalidArrayOffset - 19:57 - Cannot access value on variable $row using offset value of 'some
    key3', expecting 'somernkey1', 'somenkey2' or 'some key3'
    
  2. added
    arrays
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    array shapesarraysbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions