Inconsistent behavior with `class_string` of mocks for intersection types
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- php
- Domain
- devtools, testing-qa
Research direction
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.
Written by the indexing model from the issue text.
Description
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.
- 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
-
status/awaiting_triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
WordPress/plugin-check#1486 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
az-digital/az_quickstart#6019 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
grokability/snipe-it#19688 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100