PhpStan does not consider usage of Phpunit Assert::assertNotEmpty and Assert::assertContainsOnlyInstancesOf when returning an Array
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
Research direction
Reproduce the report with the shown PHPUnit test and its @return Post[] annotation, focusing on the assertNotEmpty() and assertContainsOnlyInstancesOf() calls before return $result. Inspect how the phpstan-phpunit extension handles these assertions and verify that the return type is accepted afterward.
Written by the indexing model from the issue text.
Description
Hello,
I am building a test returning an array of entities to be used in some others tests marked with @depends.
Despite I check that my returned array is not empty an contains only instances of a specific class, phpstan keeps alerting
that my returned value does not match. Here is my code:
/**
* @return Post[]
*
* @throws \Exception
*/
public function testFindForHomePageReturnsCollectionOfPosts(): array
{
$testSubject = self::getContainer()->get(PostRepository::class);
if (!$testSubject instanceof PostRepository) {
throw new ServiceNotFoundException(PostRepository::class);
}
$result = $testSubject->findForHomePage()->toArray();
self::assertNotEmpty($result);
self::assertContainsOnlyInstancesOf(Post::class, $result);
return $result;
}
And the alert thrown by phpstan (line 34 is the return $result statement):

Context:
Symfony 6.2 KernelTestCase
phpunit/phpunit 9.5.25
symfony/phpunit-bridge 6.2.0
PhpStan 1.9.3
extensions:
- phpstan-phpunit 1.3.2
- phpstan-doctrine 1.3.18
- phpstan-symfony 1.2.15
- phpstan/phpstan-webmozart-assert 1.2.1
Thanks a lot for your help :)
- 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
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
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 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
pluginsGLPI/datainjection#673 ·