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

False positive when using assertNull and assertSame on same method #25

Open
BluePsyduck opened this issue Jul 29, 2018 · 8 comments
Open

False positive when using assertNull and assertSame on same method #25

BluePsyduck opened this issue Jul 29, 2018 · 8 comments

Comments

@BluePsyduck
Copy link

@BluePsyduck BluePsyduck commented Jul 29, 2018

Summary of a problem or a feature request

When asserting a method result to be null, then changing the internal state of the object, and then asserting the method result to be the same as some object, PHPStan currently fails thinking it will still be null, letting assertSame() always fail.

Code snippet that reproduces the problem

<?php

declare(strict_types=1);

class Foo {
    /**
     * @var Bar|null
     */
    protected $bar;

    /**
     * @return Bar|null
     */
    public function getBar(): ?Bar
    {
        return $this->bar;
    }

    /**
     * @param Bar|null $bar
     */
    public function setBar(?Bar $bar): void
    {
        $this->bar = $bar;
    }
}

class Bar
{
}

class FooTest extends TestCase
{
    public function testBar(): void
    {
        $foo = new Foo();
        $bar = new Bar();

        $this->assertNull($foo->getBar());
        $foo->setBar($bar);
        $this->assertSame($bar, $foo->getBar()); // <-- This line triggers the error
    }
}

Used configuration

parameters:
  level: max

includes:
  - vendor/phpstan/phpstan-phpunit/extension.neon
  - vendor/phpstan/phpstan-phpunit/rules.neon
  • phpstan/phpstan @ 0.10.2
  • phpstan/phpstan-phpunit @ 0.10

Actual output

 ------ --------------------------------------------------------------------------------------------------------------------------------------- 
  Line   FooTest.php                                                                                                                            
 ------ --------------------------------------------------------------------------------------------------------------------------------------- 
  18     Call to method PHPUnit\Framework\Assert::assertSame() with FactorioItemBrowser\ExportData\Bar and null will always evaluate to false.  
 ------ --------------------------------------------------------------------------------------------------------------------------------------- 
                                                                                                                       
 [ERROR] Found 1 error      

Expected output

 [OK] No errors   

PHPUnit test does run successfully, and so should PHPStan, as all asserts are valid.

@mhujer
Copy link
Contributor

@mhujer mhujer commented Aug 7, 2018

It started to happen with 0.10.2 (was fine in 0.10.1).

My workaround is to call self::assertNotNull($foo->getBar()); to persuade phpstan that it is no longer null.

@marcospassos
Copy link

@marcospassos marcospassos commented Dec 10, 2018

We're facing the same issue here:

class Foo {
    public static function bar() : object {
         return new \stdClass();
    }
}

self::assertSame(Foo::bar(), Foo::bar());
Call to static method PHPUnit\Framework\Assert::assertSame() with object and object will always evaluate to true.
@fantomas662
Copy link

@fantomas662 fantomas662 commented Mar 6, 2019

Still happening in 0.11

@ro0NL ro0NL mentioned this issue Mar 12, 2019
@Jean85
Copy link

@Jean85 Jean85 commented Mar 13, 2019

Same here. The root issue here seems to stem from the fact that it's deviating from the standard PHPStan behavior, that doesn't lock the return type on a method, only on variables.

@ste93cry
Copy link

@ste93cry ste93cry commented Jul 30, 2019

I have the same error on PHPStan 0.10.8 and phpstan-phpunit 0.10. I have a code similar to the snippet that reproduces the problem posted in the first comment, however the workaround of @mhujer doesn't work for me

@ondrejmirtes
Copy link
Member

@ondrejmirtes ondrejmirtes commented Jul 30, 2019

This will be solved in 0.12.

@josefbenjac
Copy link

@josefbenjac josefbenjac commented Jan 3, 2020

I can confirm, it's resolved

@ondrejmirtes
Copy link
Member

@ondrejmirtes ondrejmirtes commented Jan 3, 2020

It's not perfect right now, the example with setBar() method works only thanks to the fact it returns void.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.