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 confuses same-name template types defined in different classes #4557

Closed
morozov opened this issue Feb 18, 2021 · 7 comments
Closed

Comments

@morozov
Copy link
Contributor

morozov commented Feb 18, 2021

Bug report

If a template type with the same name is used in two classes, one of which calls a method of the other, PHPStan confuses the type constraint that should be applied to the argument used in a call.

The issue is currently reproducible on doctrine/dbal#4501 but I cannot reproduce it in the playground.

Code snippet that reproduces the problem

<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Platforms;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use PHPUnit\Framework\TestCase;

/**
 * @template T of AbstractPlatform
 */
abstract class AbstractPlatformTestCase extends TestCase
{
    public function testGetCreateTableSqlDispatchEvent(): void
    {
        $listenerMock = $this->createMock(GetCreateTableSqlDispatchEventListener::class);
    }
}

interface GetCreateTableSqlDispatchEventListener
{
}

Expected output

No issue.

Actual output

$ phpstan a tests/Platforms/AbstractPlatformTestCase.php
Note: Using configuration file /home/morozov/Projects/dbal/phpstan.neon.dist.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------ 
  Line   AbstractPlatformTestCase.php                                                        
 ------ ------------------------------------------------------------------------------------ 
  17     Parameter #1 $originalClassName of method PHPUnit\Framework\TestCase::createMock()  
         expects class-string<T of Doctrine\DBAL\Platforms\AbstractPlatform>, string given.  
  17     Unable to resolve the template type T in call to method                             
         PHPUnit\Framework\TestCase::createMock()                                            
         💡 See:                                                                              
         https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type      
 ------ ------------------------------------------------------------------------------------ 

                                                                                                
 [ERROR] Found 2 errors                                                                         

If I rename the template type T to something else (e.g. Z), the problem disappears.

Even if the problem is in the code, the error message is misleading:

Parameter #​1 $originalClassName of method PHPUnit\Framework\TestCase::createMock() expects class-string<T of Doctrine\DBAL\Platforms\AbstractPlatform>.

@mergeable
Copy link

mergeable bot commented Feb 18, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

You're running a really old version of PHPStan, 0.12.57 was released almost 3 months ago.

I'm pretty sure this will work with the latest 0.12.77 release. That's why you can't reproduce it.

@morozov
Copy link
Contributor Author

morozov commented Feb 18, 2021

<!-- Before reporting an issue please check that you are using the latest PHPStan version! -->

Sorry for not mentioning it explicitly. The actual output in the description is taken from the local environment where I had all dependencies updated:

$ composer show | grep phpstan
phpstan/phpdoc-parser                          0.4.9     PHPDoc parser with support for n...
phpstan/phpstan                                0.12.77   PHPStan - PHP Static Analysis Tool
phpstan/phpstan-phpunit                        0.12.17   PHPUnit extensions and rules for...
phpstan/phpstan-strict-rules                   0.12.9    Extra strict and opinionated rul...

$ phpstan a tests/Platforms/AbstractPlatformTestCase.php
Note: Using configuration file /home/morozov/Projects/dbal/phpstan.neon.dist.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------ 
  Line   AbstractPlatformTestCase.php                                                        
 ------ ------------------------------------------------------------------------------------ 
  17     Parameter #1 $originalClassName of method PHPUnit\Framework\TestCase::createMock()  
         expects class-string<T of Doctrine\DBAL\Platforms\AbstractPlatform>, string given.  
  17     Unable to resolve the template type T in call to method                             
         PHPUnit\Framework\TestCase::createMock()                                            
         💡 See:                                                                              
         https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type      
 ------ ------------------------------------------------------------------------------------ 

                                                                                                
 [ERROR] Found 2 errors                                                                         

@ondrejmirtes
Copy link
Member

Playground reproduction: https://phpstan.org/r/b39c6660-2a19-4c41-a54e-b6f4d0c63056

@ondrejmirtes ondrejmirtes reopened this Feb 19, 2021
@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@527269a

@morozov
Copy link
Contributor Author

morozov commented Feb 19, 2021

Thank you, @ondrejmirtes!

@github-actions
Copy link

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 Apr 29, 2021
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

2 participants