Skip to content

Incorrect behavior of ArrayDimFetchToMethodCallRector #9289

@calebdw

Description

@calebdw

Bug Report

Hello!

When an array access is in an isset, changing to a method call breaks the application:

Image
Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/ca9a0b4d-9941-4412-8c66-a3fca9ca4593

<?php

class Model implements ArrayAccess
{
    public function getAttribute(string $attribute): mixed {}
}

final class DemoFile
{
    public function run(Model $model): void
    {
        if (isset($model['id'])) {
            unset($model['id']);
        }
    }
}

should probably be:

<?php

class Model implements ArrayAccess
{
    public function getAttribute(string $attribute): mixed {}
}

final class DemoFile
{
    public function run(Model $model): void
    {
        if ($model->getAttribute('id') !== null) {
            $model->offsetUnset('id');
        }
    }
}

Responsible rules

  • ArrayDimFetchToMethodCallRector

Expected Behavior

I would expect either:

  • the array access not to be updated
  • or the isset to be removed and replaced with $model->getAttribute(...) !== null

Additionally, when replacing array access that inside an unset, the ArrayAccess offsetUnset is no longer called for method calls---I'm not really sure what the behaviour should be in this case---it should probably be updated to call offsetUnset directly instead of the configured method

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