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

Array shape with optional parameter assignment issue with coalescing operator #8190

Closed
glaubinix opened this issue Oct 21, 2022 · 4 comments
Closed
Labels
Milestone

Comments

@glaubinix
Copy link

Bug report

There seems to be an issue assigning array shapes with optional parameters using the coalescing operator.

Code snippet that reproduces the problem

/**
 * @phpstan-type OwnerBackup array{name: string, isTest?: bool}
 */
class ClassA
{
    /** @var OwnerBackup */
    public array $ownerBackup;

    /**
     * @param OwnerBackup|null $ownerBackup
     */
    public function __construct(?array $ownerBackup)
    {
        $this->ownerBackup = $ownerBackup ?? [
            'name' => 'Deleted',
        ];
    }
}

https://phpstan.org/r/71144b90-6376-4cc5-b07e-1c5553a9530e

Expected output

No error

Did PHPStan help you today? Did it make you happy in any way?

Yes!

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Oct 26, 2022
@phpstan-bot
Copy link
Contributor

@glaubinix After the latest push in 1.9.x, PHPStan now reports different result with your code snippet:

@@ @@
-16: Property ClassA::$ownerBackup (array{name: string, isTest?: bool}) does not accept array<'isTest'|'name', bool|string>.
-27: Property ClassA::$ownerBackup (array{name: string, isTest?: bool}) does not accept non-empty-array<'isTest'|'name', bool|string>.
+16: Property ClassA::$ownerBackup (array{name: string, isTest?: bool}) does not accept array{}|array{name: string, isTest?: bool}.
Full report
Line Error
16 `Property ClassA::$ownerBackup (array{name: string, isTest?: bool}) does not accept array{}

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2708

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2710

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants