Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpstan bleeding edge cannot assert that JWTAuthenticationFailureResponse extends from Response #7157

Closed
GErpeldinger opened this issue Apr 29, 2022 · 15 comments

Comments

@GErpeldinger
Copy link

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 :D

@mergeable
Copy link

mergeable bot commented Apr 29, 2022

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Yeah, PHPStan most likely doesn't see the eval() definition...

What you can try is to put this file https://github.com/lexik/LexikJWTAuthenticationBundle/blob/2.x/Response/JWTAuthenticationFailureResponse.php in phpstan.neon bootstrapFiles...

@GErpeldinger
Copy link
Author

Oh yes, it works, thank you for your answer! Will this be fixed in future versions?

@ondrejmirtes
Copy link
Member

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.

@ondrejmirtes
Copy link
Member

...or maybe we actually can :) phpstan/phpstan-src@691e89a

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!

@GErpeldinger
Copy link
Author

The error is still present unfortunately

@GErpeldinger
Copy link
Author

But why does it work without the bleeding edge functionalities and not with? Is this the new fully static reflection engine?

@ondrejmirtes ondrejmirtes reopened this Apr 29, 2022
@ondrejmirtes
Copy link
Member

Yes. Could you please create a small reproducing repository that shows this problem? I want to crack it :)

@GErpeldinger
Copy link
Author

@ondrejmirtes
Copy link
Member

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.

@ondrejmirtes
Copy link
Member

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

@GErpeldinger
Copy link
Author

Thank you for your work!

@chalasr
Copy link

chalasr commented May 3, 2022

Thanks a lot. I'm going to fix that in LexikJWTAuthenticationBundle.

@ondrejmirtes
Copy link
Member

I managed to make PHPStan discover Lexik\Bundle\JWTAuthenticationBundle\Response\JWTCompatAuthenticationFailureResponse even with the current release of lexik/jwt-authentication-bundle: phpstan/phpstan-src@e40474b

@github-actions
Copy link

github-actions bot commented Jun 5, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants