`assertTrue` seems to lose inferred type
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
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/phpstanv2.1.13phpstan/phpstan-phpunitv2.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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from phpstan/phpstan-phpunit
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 68/100
phpstan/phpstan-phpunit#332 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
phpstan/phpstan-phpunit#321 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
phpstan/phpstan-phpunit#309 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
phpstan/phpstan-phpunit#299 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
phpstan/phpstan-phpunit#292 · 3 comments ·
All issues in phpstan/phpstan-phpunit
Similar issues
-
a11y admissions.uiowa.edu needs grooming SiteImprove best practice
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Save States Menu Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
pluginsGLPI/datainjection#673 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
octobercms/october#6130 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
getgrav/grav-plugin-form#656 ·