Skip to content

Incorrect type for result when merging array with + operator #6965

@JoyceBabu

Description

@JoyceBabu

Bug report

PHPStan is not determining the data type of resultant array when merging array with + operator, when there are values of multiple types.

Code snippet that reproduces the problem

<?php

class Test
{
    /**
     * @phpstan-var array{
     *  prop1?: int, prop2?: bool, prop3?: float,
     *  prop4?: string, prop5?: string, prop6?: string,
	 *  prop7?: string
     * }
     */
    protected array $updateData = [];

    /**
     * @phpstan-param array{
     *  prop1?: int, prop2?: bool, prop3?: float,
     *  prop4?: string, prop5?: string, prop6?: string,
	 *  prop7?: string
     * } $data
     */
    public function update(array $data): void
    {
        $this->updateData = $data + $this->updateData;
    }
}

Expected output

Does Not Work
https://phpstan.org/r/0117678d-c523-442a-8975-7ad2d03fe92c (7+ optional keys)

Works

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions