Skip to content

Incorrect behavior of ChangeAndIfToEarlyReturnRector #7413

@shinsenter

Description

@shinsenter

Bug Report

After running rector, the $nl variable in the below example is used before it is defined.
I created a fixture here: rectorphp/rector-src#2823

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

Minimal PHP Code Causing Issue

See https://getrector.org/demo/5cf285f8-b9ba-40f6-bd8c-7c325bb2458d

<?php

function dummy_wrap($maxWidth, $text)
{
    $wmax = ($maxWidth - 2 * MARGIN);
    $cw   = &get_font()['cw'];
    $s    = str_replace("\r", '', (string) $text);

    if (get_font_set()) {
        $nb = mb_strlen($s, 'utf-8');
        while ($nb > 0 && mb_substr($s, $nb - 1, 1, 'utf-8') == "\n") {
            $nb--;
        }
    } else {
        $nb = strlen($s);

        if ($nb > 0 && $s[$nb - 1] == "\n") {
            $nb--;
        }
    }

    $sep = -1;
    $i   = 0;
    $j   = 0;
    $l   = 0;
    $nl  = 1;

    while ($i < $nb) {
        $c = $s[$i];

        if ($c == "\n") {
            $i++;
            $sep = -1;
            $j   = $i;
            $l   = 0;

            $nl++;

            continue;
        }

        if ($c == ' ') {
            $sep = $i;
        }

        $l += $cw[$c] * FONT_SIZE / 1000;

        if ($l > $wmax) {
            if ($sep == -1) {
                if ($i == $j) {
                    $i++;
                }
            } else {
                $i = $sep + 1;
            }
            $sep = -1;
            $j   = $i;
            $l   = 0;

            $nl++;
        } else {
            $i++;
        }
    }

    return $nl;
}

Responsible rules

  • ChangeAndIfToEarlyReturnRector

Expected Behavior

No code error.

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