Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#158 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
php
Ambito
testing

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
PHP
Stelle
544
Fork
58
Merge medio
2g 1h
PR unite (30g)
5

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di phpstan/phpstan-phpunit

Tutte le issue di phpstan/phpstan-phpunit

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.