`assertTrue` seems to lose inferred type

Open
#229 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
php
Domain
testing

Research direction

Start with the reproduction in tests/MockServerTrait.php, using the listed phpstan/phpstan and phpstan/phpstan-phpunit versions, and compare the dumped types before and after self::assertTrue. Trace how the PHPUnit assertTrue call is handled, then verify that the non-null MockWebServer type is preserved and the return-type error disappears.

Written by the indexing model from the issue text.

Description

  • phpstan/phpstan v2.1.13
  • phpstan/phpstan-phpunit v2.0.6

Given the following trait:

trait MockServerTrait
{
    private static ?MockWebServer\MockWebServer $mockServer = null;

    protected static function startMockServer(
        string $host = '127.0.0.1',
        int $port = 2083,
    ): MockWebServer\MockWebServer {
        self::$mockServer?->stop();
        self::$mockServer = new MockWebServer\MockWebServer($port, $host);
        self::$mockServer->start();

        \PHPStan\dumpType(self::$mockServer); // line 55
        self::assertTrue(self::$mockServer->isRunning());
        \PHPStan\dumpType(self::$mockServer); // line 57

        return self::$mockServer; // line 59
    }
}

When running PHPStan, I get the following errors:

------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/MockServerTrait.php (in context of class EliasHaeussler\CpanelRequests\Tests\MockServerAwareTestCase)                                                                                  
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  55     Dumped type: donatj\MockWebServer\MockWebServer                                                                                                                                              
         ✏️  tests/MockServerTrait.php:55                                                                                                                                                             
  57     Dumped type: donatj\MockWebServer\MockWebServer|null                                                                                                                                         
         ✏️  tests/MockServerTrait.php:57                                                                                                                                                             
  59     Method EliasHaeussler\CpanelRequests\Tests\MockServerAwareTestCase::startMockServer() should return donatj\MockWebServer\MockWebServer but returns donatj\MockWebServer\MockWebServer|null.  
         🪪  return.type                                                                                                                                                                              
         ✏️  tests/MockServerTrait.php:59                                                                                                                                                             
 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

When I omit the last self::assertTrue, no errors are reported anymore.

Dominant language
PHP
Stars
544
Forks
58
Avg merge
2d 1h
Merged PRs (30d)
5

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-phpunit

All issues in phpstan/phpstan-phpunit

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.