Skip to content

Bug: false positive in RemoveExtraParametersRector for ReflectionMethod::invoke #1625

@Levivb

Description

@Levivb
Subject Details
PHP version PHP 7.2
Full Command vendor/bin/rector --config ci/config/rector.yml process Tests/Unit/Http/Controllers/Traits/SupportsSetSourceControllerTest.php -n
parameters:
  php_version_features: '7.2'

services:
  Rector\Php\Rector\FuncCall\RemoveExtraParametersRector: ~

Current Behaviour

Rector v0.5.5
Config file: /Users/Levi/Documents/Work/repositories/modulestest/packages/set/ci/config/rector.yml

 3/3 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

1 file with changes
===================

1) Tests/Unit/Http/Controllers/Traits/SupportsSetSourceControllerTest.php

    ---------- begin diff ----------
--- Original
+++ New
@@ -13,6 +13,6 @@
         $reflectionClass = new ReflectionClass('SomeClass');
         $method = $reflectionClass->getMethod('getSetItems');
         $method->setAccessible(true);
-        $method->invoke($this->traitMock, 'some-param', 4, 5, 6);
+        $method->invoke($this->traitMock, 'some-param');
     }
 }
    ----------- end diff -----------

Applied rectors:

 * Rector\Php\Rector\FuncCall\RemoveExtraParametersRector

 [OK] Rector is done! 1 changed files

Minimal PHP Code Causing Issue

<?php
declare(strict_types=1);

namespace Modules\Set\Tests\Unit\Http\Controllers\Traits;

use Modules\Core\Tests\TestCase;
use ReflectionClass;

class SupportsSetSourceControllerTest extends TestCase
{
    public function testGetSetItems(): void
    {
        $reflectionClass = new ReflectionClass('SomeClass');
        $method = $reflectionClass->getMethod('getSetItems');
        $method->setAccessible(true);
        $method->invoke($this->traitMock, 'some-param', 4, 5, 6);
    }
}

Expected Behaviour

Since the method signature of ReflectionMethod::invoke is public ReflectionMethod::invoke ( object $object [, mixed $... ] ) : mixed and the doc (https://php.net/manual/en/reflectionmethod.invoke.php) states Zero or more parameters to be passed to the method, it accepts a variable number of parameters which are passed to the method. You'd expect multiple parameters (beyond the two from the signature) to be accepted. In this case though, any parameter beyond the first two is reported as unnecessary and removed.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions