False positives with `assertSame` on generics with compatible subtypes
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
Start by reproducing the reported assertSame diagnostic with the CollectionAbstractModel and CollectionPaymentMethods examples. Trace the assertSame rule's handling of generic type arguments, then add a regression test for compatible contained types. Done means valid identity assertions no longer produce a false-positive while genuinely incompatible types remain reported.
Written by the indexing model from the issue text.
Description
I have a Collection container class that uses generics:
/**
* This class represents a list of models.
*
* @template M of AbstractModel
* @extends \SplObjectStorage<M, int>
*/
class Collection extends \SplObjectStorage
{
…
}
(with AbstractModel being the abstract base class for domain models)
Now I get error messages like this:
3069 Call to static method PHPUnit\Framework\Assert::assertSame() with
OliverKlee\Oelib\DataStructures\Collection<OliverKlee\Oelib\Model\Abs
tractModel> and
OliverKlee\Oelib\DataStructures\Collection<Tx_Seminars_Model_Organize
r> will always evaluate to false.
(Organizer is a subclass of AbstractModel, as is PaymentMethods used in the example below.)
for code that basically looks like this (simplified):
class Event
{
/**
* @return Collection<PaymentMethods>
*/
public function getPaymentMethods(): Collection;
/**
* @param Collection<PaymentMethods> $paymentMethods
*/
public function setPaymentMethods(Collection $paymentMethods): void;
}
class EvenTest
{
public function testPaymentMethodsSetsPaymentMethods(): void
{
$paymentMethods = new Collection();
$this->subject->setPaymentMethods($paymentMethods);
self::assertSame($paymentMethods, $this->subject->getPaymentMethods());
}
}
So either the check for assertSame should completely disregard the contained type within generics, or it should allow contained types that are compatible to each other.
- 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
-
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 84/100
pluginsGLPI/datainjection#673 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
octobercms/october#6130 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
getgrav/grav-plugin-form#656 ·