Skip to content

Bug: AddArrowFunctionParamArrayWhereDimFetchRector incorrectly adds 'array' type to string parameter #9330

@antoniovj1

Description

@antoniovj1

Bug Report

Subject Details
Rector version 2.1.4

The AddArrowFunctionParamArrayWhereDimFetchRector rule incorrectly adds an array type hint to a parameter that is actually a string. This happens when a string offset is accessed (e.g., $string[0]) inside an array_map callback.

The rule seems to assume that any variable with a dimension fetch ($var[key]) must be an array, but this is also valid syntax for accessing characters in a string. This incorrect assumption introduces a TypeError at runtime.

Minimal PHP Code Causing Issue

Here is a minimal reproducible example on the playground:

https://getrector.com/demo/eed23d9a-8ac2-4c49-98e7-29700e8c2dc3

<?php

$username = 'john.doe';

$initials = implode(
    '',
    array_map(
        static fn ($name): string => strtoupper((string) $name[0]),
        explode('.', $username),
    ),
);

Rector changes it to:

PHP

<?php

$username = 'john.doe';

$initials = implode(
    '',
    array_map(
        static fn (array $name): string => strtoupper((string) $name[0]),
        explode('.', $username),
    ),
);

Expected Behaviour

Rector should not type as array.

It should correctly identify that the $name parameter in the array_map callback is a string, as it iterates over the array of strings returned by explode(). The string offset access $name[0] is valid for strings, and the parameter should not be incorrectly typed as array.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions