Skip to content

RemoveUnusedPrivateMethodParameterRector copies wrong function parameter types #9181

@CharlemagneLasse

Description

@CharlemagneLasse

Bug Report

Subject Details
Rector version 2.0.16

When I enable dead code elimination and rector removes a parameter then it copies the type of the removed parameter to the next argument. My rector configuration is:

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/public', __DIR__ . '/src'])
    ->withPhpSets(php84: true)
    ->withPreparedSets(
        deadCode: true,
        codeQuality: true,
        codingStyle: true,
        typeDeclarations: true,
        privatization: true
    );

Minimal PHP Code Causing Issue

https://getrector.com/demo/0b532578-8ad1-4597-bc58-1cc18edbf481

<?php

class test {
	private static function just_a_test(string $foo, array $bar, $used_parameter): void
	{
		\external::whatever($foo, $used_parameter);
	}

	public static function trigger(): void
	{
		self::just_a_test('hello', [], 'test');
	}
}

After running rector, I get this completely wrong output:

<?php

class test {
	private static function just_a_test(string $foo, array $used_parameter): void
	{
		\external::whatever($foo, $used_parameter);
	}

	public static function trigger(): void
	{
		self::just_a_test('hello', [], 'test');
	}
}

Expected Behaviour

The $used_parameter should not have the type array (string would be acceptable). And in a perfect world, the call to it should have the second parameter removed automatically. The output should therefore be:

<?php

class test {
	private static function just_a_test(string $foo, $used_parameter): void
	{
		\external::whatever($foo, $used_parameter);
	}

	public static function trigger(): void
	{
		self::just_a_test('hello', 'test');
	}
}

or

<?php

class test {
	private static function just_a_test(string $foo, string $used_parameter): void
	{
		\external::whatever($foo, $used_parameter);
	}

	public static function trigger(): void
	{
		self::just_a_test('hello', 'test');
	}
}

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