rector icon indicating copy to clipboard operation
rector copied to clipboard

`LiteralGetToRequestClassConstantRector` won't work on `KernelBrowser` using Symfony 5+

Open mvhirsch opened this issue 1 year ago • 0 comments

Bug Report

Subject Details
Rector version v1.0.4
symfony/http-kernel v6.4.6

Looks like the bugfix in #7135 prevents updating my tests on a Symfony 6.4 project. The HttpKernel\Client class got removed in symfony/http-kernel v5.0.0.. Now it's impossible to reach the needed path of execution.

https://github.com/rectorphp/rector-symfony/blob/b8126e8bf6e239fd53329fcd715ea117a4b4c34c/rules/CodeQuality/Rector/MethodCall/LiteralGetToRequestClassConstantRector.php#L78-L85

By removing the check for that class, it starts refactoring as expected.

         // for client, the transitional dependency to browser-kit might be missing and cause fatal error on PHPStan reflection
         // in most cases that should be skipped, @changelog https://github.com/rectorphp/rector/issues/7135
         if (
-            $this->reflectionProvider->hasClass('Symfony\Component\BrowserKit\AbstractBrowser') &&
-            $this->isObjectType($node->var, new ObjectType('Symfony\Component\HttpKernel\Client'))
+            $this->reflectionProvider->hasClass('Symfony\Component\BrowserKit\AbstractBrowser')
         ) {
             return $this->refactorClientMethodCall($node);
         }

I love using Rector :heart:

Minimal PHP Code Causing Issue

(copied from https://symfony.com/doc/current/testing.html#write-your-first-application-test)

// tests/Controller/PostControllerTest.php
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PostControllerTest extends WebTestCase
{
    public function testSomething(): void
    {
        // This calls KernelTestCase::bootKernel(), and creates a
        // "client" that is acting as the browser
        $client = static::createClient();

        // Request a specific page
        $crawler = $client->request('GET', '/');

        // Validate a successful response and some content
        $this->assertResponseIsSuccessful();
        $this->assertSelectorTextContains('h1', 'Hello World');
    }
}

Expected Behaviour

          // Request a specific page
-         $crawler = $client->request('GET', '/');
+         $crawler = $client->request(Request::METHOD_GET, '/');

mvhirsch avatar Apr 25 '24 06:04 mvhirsch