Skip to content

Incorrect behavior of SimplifyUselessVariableRector with return-by-ref functions #6401

@webmaster777

Description

@webmaster777

Bug Report

Subject Details
Rector version last dev-master
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.org/demo/fa9b69b8-58ba-4f8c-b06b-ff59b18f795c

<?php
interface ValueAccessor
{
    public function & accessValue($object, $memberName);
}
final class DemoFile implements ValueAccessor
{
    public function & accessValue($object, $memberName)
    {
        $value = count($object);
        return $value;
    }
}

NB: I am completely aware that return-by-ref and param-by-ref is a snake pit and that this kind of code should be abolished. Dealing with legacy code here.

Responsible rules

  • SimplifyUselessVariableRector

Expected Behavior

return by ref must return a var or use a function that returns by-ref. count() does not return by-ref. If the change is applied this will result in PHP warnings: https://3v4l.org/f8GDO

Rector should skip this kind of return statements for simplifying.

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