Skip to content

Execution time increases exponentially when merging arrays #4308

@JoyceBabu

Description

@JoyceBabu

Bug report

Analysis time increases exponentially with the number of optional array keys.

Number of optional array items Execution Time
< 6 0.5s
6 1.06s
7 2.35s
8 9.38s
9 41.57s

Code snippet that reproduces the problem

<?php

class Test
{
    /**
     * @var (string|int|null)[]
     * @phpstan-var array{
     *  prop1?: string, prop2?: string, prop3?: string,
     *  prop4?: string, prop5?: string, prop6?: string,
     *  prop7?: string, prop8?: int, prop9?: int
     * }
     */
    protected array $updateData = [];

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

I was unable to reproduce the issue with PHPStan.org, since the analysis was timing out when there were more than 7 optional keys.

Expected output

Execution time to increase linearly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions