Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidDocblock error when comparing array with special characters in string keys #7222

Closed
theodorejb opened this issue Dec 26, 2021 · 2 comments · Fixed by #7225
Closed
Labels

Comments

@theodorejb
Copy link
Contributor

I have a PHPUnit test that asserts that an array<string, int> array in an object property matches the expected value. However, if the expected value contains special characters in its string keys, Psalm reports an error saying the array cannot be used in an assertion:

https://psalm.dev/r/d3c81c61d3

This worked fine in Psalm 4.14 and then broke starting in Psalm 4.15.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/d3c81c61d3
<?php

class Foo {
    /** @var array<string, int> */
    public array $bar;
    
    /**
     * @param array<string, int> $bar
     */
    public function __construct(array $bar) {
        $this->bar = $bar;
    }
}

$expected = [
    '#[]' => 21,
    '<<>>' => 6,
];

$foo = new Foo($expected);
assertSame($expected, $foo->bar);

/**
 * @psalm-template ExpectedType
 * @psalm-param ExpectedType $expected
 * @psalm-assert =ExpectedType $actual
 */
function assertSame($expected, $actual): void {
    if ($expected !== $actual) {
        throw new Exception("Expected doesn't match actual");
    }
}
Psalm output (using commit aa7e400):

ERROR: InvalidDocblock - 21:1 - array{#[]: 21, <<>>: 6} cannot be used in an assertion

INFO: MissingParamType - 28:32 - Parameter $actual has no provided type

@orklah
Copy link
Collaborator

orklah commented Dec 26, 2021

This is related to #7076 that drastically improved the way assertions for arrays works. This seems related to escaping some chars in the string representation of the array. I'll try to check that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants