jbboehr/phpstan-array-merge

v0.1.0 2025-06-07 20:36 UTC

This package is auto-updated.

Last update: 2025-06-07 21:07:26 UTC


README

ci License: AGPL v3+ stability-experimental

Installation

To use this extension, require it in Composer:

composer require --dev jbboehr/phpstan-array-merge

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

includes:
    - vendor/jbboehr/phpstan-array-merge/extension.neon

Usage

Have you ever wanted to have a function that performs an array_merge()-like operation but want that generic goodness PHPStan has to offer? WAIT NO MORE!

<?php

class ConstFixture
{
    public const ARRAY = ['foo' => 'bar'];

    /**
     * @template T of array<mixed>
     * @param T $a
     * @phpstan-return array-merge<self::ARRAY, T>
     */
    public static function constMerge(array $a): array
    {
        return array_merge(self::ARRAY, $a);
    }
}
<?php

\PHPStan\dumpType(ConstFixture::constMerge(['baz' => 'bat']));
$ phpstan

 ------ --------------------------------------------
  Line   tmp.php
 ------ --------------------------------------------
  3      Dumped type: array{foo: 'bar', baz: 'bat'}
 ------ --------------------------------------------

If you mix generic arrays and array shapes, you get what is coming to you (or open an issue).

License

This project is licensed under the AGPL v3+ License - see the LICENSE.md file for details.