Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

PhpStan does not consider usage of Phpunit Assert::assertNotEmpty and Assert::assertContainsOnlyInstancesOf when returning an Array

Open
#158 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
testing

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):
image

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

  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.