Skip to content

False positive in PHPStan 1.4.8 with assertNotSame with instances of the same class #120

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

Closed
oliverklee opened this issue Mar 4, 2022 · 12 comments · Fixed by #123
Closed

Comments

@oliverklee
Copy link

Using phpstan/phpstan-phpunit 1.0.0 and phpstan/phpstan 1.4.8, the following code creates a false positive:

namespace OliverKlee\Oelib\Tests\Unit\Templating;

use Nimut\TestingFramework\TestCase\UnitTestCase;
use OliverKlee\Oelib\Templating\Template;
use OliverKlee\Oelib\Templating\TemplateRegistry;

class TemplateRegistryTest extends UnitTestCase
{
    /**
     * @test
     */
    public function getForExistingTemplateFileNameCalledTwoTimesReturnsNewInstance(): void
    {
        self::assertNotSame(
            TemplateRegistry::get('EXT:oelib/Tests/Functional/Fixtures/Template.html'),
            TemplateRegistry::get('EXT:oelib/Tests/Functional/Fixtures/Template.html')
        );
    }
}

This is the error (with different line numbers):

 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   Tests/Unit/Templating/TemplateRegistryTest.php                                                                                                           
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------- 
  69     Call to static method PHPUnit\Framework\Assert::assertNotSame() with OliverKlee\Oelib\Templating\Template and OliverKlee\Oelib\Templating\Template will  
         always evaluate to false.                                                                                                                                
  91     Call to static method PHPUnit\Framework\Assert::assertNotSame() with OliverKlee\Oelib\Templating\Template and OliverKlee\Oelib\Templating\Template will  
         always evaluate to false.                                                                                                                                
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------- 

This problem is new with PHPStan 1.4.8 (as it did no occur with PHPStan 1.4.7).

You can find the complete build with the problem at oliverklee/ext-oelib#904.

(I was not able to use the PHPStan playground for this as I currently do not know of any way of including PHPUnit in it: https://phpstan.org/r/c7fa7bda-1024-45c3-957d-7b91de41b844)

@ondrejmirtes
Copy link
Member

/cc @herndlm Probably worth changing the code to only work for Variable node?

@herndlm
Copy link
Contributor

herndlm commented Mar 4, 2022

🤦‍♂️ weird, I thought this should not happen because very similar looking comparisons in https://github.com/phpstan/phpstan-src/blob/1.4.8/tests/PHPStan/Rules/Comparison/data/impossible-method-call.php#L75 and https://github.com/phpstan/phpstan-src/blob/1.4.8/tests/PHPStan/Rules/Comparison/data/impossible-method-call.php#L87 are fine

/cc @herndlm Probably worth changing the code to only work for Variable node?

sounds like a solution. somehow reproducing that in PHPStan directly would be nice too when I try to fix it ;)

@herndlm
Copy link
Contributor

herndlm commented Mar 4, 2022

Oh look at that, apparently only those static factory-like methods are problems. See phpstan/phpstan-webmozart-assert#122. I'm not sure anymore if limiting it to Variable nodes is the right solution. Maybe there's a bug hidden somewhere else.
I can continue debugging this later today.

@ondrejmirtes
Copy link
Member

It's not a problem for New_ thanks to: https://github.com/phpstan/phpstan-src/blob/156cb69be747b7821275803717ef840fae636602/src/Analyser/TypeSpecifier.php#L976-L978

But it's a problem for anything that can be remembered in SpecifiedTypes by TypeSpecifier.

@herndlm
Copy link
Contributor

herndlm commented Mar 4, 2022

Right, while I was doing a quick test I was actually comparing the type with a node class and thought something is weird. Limiting it to Variable nodes seems to be doing the trick. Give me a couple of minutes..

@oliverklee
Copy link
Author

@herndlm Thanks for working on this! ❤️

@herndlm
Copy link
Contributor

herndlm commented Mar 4, 2022

Looks like the same problem existed in a truthy scope with === already. I'm trying to fix that too, and I'm having a hard time creating failing tests for the falsey scope. Which is why this will take a bit longer than a couple of minutes :)

@herndlm
Copy link
Contributor

herndlm commented Mar 9, 2022

@ondrejmirtes do we need a regression test here? or better: do you want one? :)

@ondrejmirtes
Copy link
Member

Yes, please send it 😊

@ondrejmirtes
Copy link
Member

Fixed in PHPStan 1.4.9 https://github.com/phpstan/phpstan/releases/tag/1.4.9

@oliverklee
Copy link
Author

Thanks, @ondrejmirtes and @herndlm! ❤️

reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Mar 10, 2022
This update gets rid of a false positive:

phpstan/phpstan-phpunit#120

Resolves: #97163
Releases: main, 11.5
Change-Id: I6f7ecd909a127b5940b928d81c94ab7b3404cd49
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/73870
Tested-by: core-ci <[email protected]>
Tested-by: Benni Mack <[email protected]>
Tested-by: Oliver Bartsch <[email protected]>
Reviewed-by: Benni Mack <[email protected]>
Reviewed-by: Oliver Bartsch <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Mar 10, 2022
This update gets rid of a false positive:

phpstan/phpstan-phpunit#120

Resolves: #97163
Releases: main, 11.5
Change-Id: I6f7ecd909a127b5940b928d81c94ab7b3404cd49
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/73896
Reviewed-by: Oliver Klee <[email protected]>
Reviewed-by: Oliver Bartsch <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Oliver Bartsch <[email protected]>
@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 11, 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

Successfully merging a pull request may close this issue.

3 participants