phpstan: Phpstan bleeding edge cannot assert that JWTAuthenticationFailureResponse extends from Response

Bug report

Phpstan version 1.6.3

Hello, i have updated today phpstan to last version, wanting to try the bleeding edge features, I activated them and it seems that it is a problem to recognize that a class of LexikJWTAuthenticationBundle extends from JsonResonse and therefore from Response.

This class have a comptibility layer, can this be the problem? https://github.com/lexik/LexikJWTAuthenticationBundle/blob/2.x/Response/JWTAuthenticationFailureResponse.php

Code snippet that reproduces the problem

I don’t think i can reproduce it on https://phpstan.org/try, but i think it can be easy to reproduce it in a symfony controller:

<?php

declare(strict_types=1);

namespace App\Controller;

use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class TestController extends AbstractController
{
    #[Route(data: '/test'), method: 'POST']
    public function index(): JsonResponse
    {
        return new JWTAuthenticationFailureResponse();
    }
}
<?php

declare(strict_types=1);

namespace App\Tests\Functional\Controller;

use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;

class TestControllerTest extends WebTestCase
{
    // The test works, and when i dd($client->getResponse() instanceof JsonResponse) the result is true
    public function testIndex(): void
    {
        $client = static::createClient();
        $client->request(method: Request::METHOD_POST, uri: '/test');

        self::assertInstanceOf(JWTAuthenticationFailureResponse::class, $client->getResponse());
    }
}

Expected output

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/Controller/TestController.php                                                                                                                                                                      
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  17     Method App\Controller\TestController::index() should return Symfony\Component\HttpFoundation\JsonResponse but returns Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse.  
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/Functional/Controller/TestControllerTest.php                                                                                                                                                                                         
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  18     Call to static method PHPUnit\Framework\Assert::assertInstanceOf() with 'Lexik\\Bundle\\JWTAuthenticationBundle\\Response\\JWTAuthenticationFailureResponse' and Symfony\Component\HttpFoundation\Response will always evaluate to false.  
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

Did PHPStan help you today? Did it make you happy in any way?

Yes, it certainly changed my way of coding for the better, thanks for your work 😄

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

I managed to make PHPStan discover Lexik\Bundle\JWTAuthenticationBundle\Response\JWTCompatAuthenticationFailureResponse even with the current release of lexik/jwt-authentication-bundle: https://github.com/phpstan/phpstan-src/commit/e40474b3fcd27e5a2b37b8f6add8e7ad906db019

Right now you can use the bootstrapFiles workaround, but I’d urge you to have that fixed in that package too 😃 Thanks.

The problem isn’t in eval - the problem is that Lexik\Bundle\JWTAuthenticationBundle\Response\JWTCompatAuthenticationFailureResponse isn’t defined acording to PSR-4 rules configured in that package’s composer.json. If you put it to a separate file with the right filename, it’s going to work.

…or maybe we actually can 😃 https://github.com/phpstan/phpstan-src/commit/691e89adfb4f605588e8a357abbbaec79814c8fa

Can you please try the latest 1.6.x-dev and remove the bootstrapFiles entry? composer require --dev phpstan/phpstan:1.6.x-dev I’d really appreciate it!

Nope, you need to fix it yourself like that, PHPStan does not have a crystal ball to know which file needs to be loaded in order to work 😃 Thanks.