Inconsistent behavior with `class_string` of mocks for intersection types

Aperta
#131 2 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
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
php

Direzione di ricerca

Start with the makeInstanceReturnsClassInstance test and reproduce the reported diagnostic involving get_class(), getMock(), and GeneralUtility::makeInstance(). Trace how phpstan-phpunit represents the mock's class-string intersection versus the expected class-string; done means the valid test no longer produces this mismatch.

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

Descrizione

I have not been able to create a working example for this on the PHPStan playground as this bug involves PHPUnit and the phpstan-phpunit package.

We have a class with a generator function that looks like this:

    /**
     * Creates an instance of a class taking into account the class-extensions
     * API of TYPO3. USE THIS method instead of the PHP "new" keyword.
     * Eg. "$obj = new myclass;" should be "$obj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("myclass")" instead!
     *
     * You can also pass arguments for a constructor:
     * \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\myClass::class, $arg1, $arg2, ..., $argN)
     *
     * @template T of object
     * @param class-string<T> $className name of the class to instantiate, must not be empty and not start with a backslash
     * @param array<int, mixed> $constructorArguments Arguments for the constructor
     * @return T the created instance
     */
    public static function makeInstance($className, ...$constructorArguments)
    {…}

One of the tests for this method looks like this:

    /**
     * @test
     */
    public function makeInstanceReturnsClassInstance(): void
    {
        $className = get_class($this->getMockBuilder('foo')->getMock());
        self::assertInstanceOf($className, GeneralUtility::makeInstance($className));
    }

PHPStan (or phpstan-phpunit) now seems to mix up two different representations of a class string for a intersection types (which both look plausible to me, but they're not the same to PHPStan):

  3123   Parameter #1 $className of static method TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() expects                                                       
         class-string<foo&PHPUnit\Framework\MockObject\MockObject>,
         class-string<foo>&class-string<PHPUnit\Framework\MockObject\MockObject> given.   
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.