Skip to content

Incorrect behavior of PreparedValueToEarlyReturnRector #8064

@kkmuffme

Description

@kkmuffme

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/d265976d-715f-457e-a632-c07f64481eed

<?php

final class DemoFile
{
    public function run( $arg )
    {
		$arg = preg_replace_callback(
			'/([a-z])?([a-z])([a-z]*)/',
			static function ( $match ) {
				$replace = '';
				if ( !empty( $match[1] ) ) {
					$replace = 'abc';
				}
				$replace .= $match[2];
				if ( !empty( $match[3] ) ) {
					$replace .= 'abc';
				}
				return $replace;
			},
			$arg
		);

		return $arg;
    }
}

Responsible rules

  • PreparedValueToEarlyReturnRector

Expected Behavior

There are some options to change this to early return, however I'm not sure if this is wanted here (since it would mean you'd have to concat in the return), so I suggested to not change the code at all (instead of creating invalid code as it does now)

See rectorphp/rector-src#4528

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