False positives with `assertSame` on generics with compatible subtypes

Đang mở
#105 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
php
Lĩnh vực
testing, tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
PHP
Star
544
Fork
58
Merge trung bình
2 ngày 1 giờ
Pull request đã merge (30 ngày)
5

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phpstan/phpstan-phpunit

Tất cả issue của phpstan/phpstan-phpunit

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.