Skip to content

Filter scope when using PHPUnit's assertions #9

@lcobucci

Description

@lcobucci

I'm not sure if this is the correct repo to open this issue, but it seems to be the best place...

What I'd like to implement (with some help, sure) is the ability to do the same thing done when assert() is called but when using PHPUnit's assertions:

https://github.com/phpstan/phpstan/blob/a50b95cb29fe720a3f8b35297fe3b559d6616d5a/src/Analyser/NodeScopeResolver.php#L197-L204

Some context:

We create contexts for Behat using PHPUnit's assertions and sometimes we have this kind of code:

public function thenSomethingMustHaveHappened(): void
{
    $something = $this->lovelyService->doSomething(); // which has `?Something` as return type hint

    Assert::assertNotNull($something);
    Assert::assertSame('Something', $something->getSomething());    
}

Then PHPstan complains:

------ -------------------------------------------------------------------------------------------------- 
  Line   SomethingContext.php                                                                     
 ------ -------------------------------------------------------------------------------------------------- 
  XX     Calling method getSomething() on possibly null value of type Something|null.  
 ------ -------------------------------------------------------------------------------------------------- 

However if assert($something !== null) is used, things just work...

Of course we could simply use assert() everywhere but we rather use a more friendly tool. So this leads us to two questions:

  1. is this interesting for phpstan?
  2. how can we implement this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions