Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

assertNotContains appears to be messing something up.

Đang mở
#262 0 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
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
php
Lĩnh vực
testing

Hướng nghiên cứu

Start with the shortened OmegaTag and testBasic examples in the issue, focusing on the interaction between assertNotContains() and the later assertCount() call. Compare the analysis with and without assertNotContains(); done when the collection is no longer inferred as NEVER and the impossibleType warning is resolved.

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

Mô tả

I've got a weird issue that I can't seem to figure out. I think it is a bug in PHPStan, but I'm not certain.
I appreciate if you can point me in a way on how to debug this or help you figure out what is wrong.

Some context first:

  • popped up after upgrading PHPStan yesterday, it was not there before. I update very often, so it was a recent change.
  • Happens on both PHP8.4 and PHP8.5alpha
  • It involves the PHPUnit method "assetNotContains", so I don't think I can create a playground example.
  • I tried creating a small repo with an example, but I can't reproduce the error there. I'm not sure why. I used the same phpstan.neon file (just paths changed) and the contents of the classes appear identical to me (except namespaces).

Given this (a bit shortened) test class:

class OmegaTag
{
    /**
     * @var Collection<array-key, OmegaProduct>
     */
    private Collection $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * @return Collection<array-key, OmegaProduct>
     */
    public function getProducts(): Collection
    {
        return $this->products;
    }
}

This (shortened) test:

    public function testBasic(): void
    {
        $product = new OmegaProduct();
        $tag = new OmegaTag();

        self::assertNotContains($product, $tag->getProducts());
        self::assertCount(0, $tag->getProducts());
    }

This gives me this error:

Call to static method PHPUnit\Framework\Assert::assertCount() with 0 and *NEVER* will always evaluate to false.                                   
🪪  staticMethod.impossibleType

I'm not sure how PHPStan got to the type *NEVER*.

Observations:

  • When I remove the line with the method assertNotContains, the error goes away.
  • When I refactor the 2 $tag->getProducts() calls to a single variable and use that in the test, the error is still there.

It seems that assertNotContains narrows the type to never, while I think it should not do that.

Is there any direction you can point me in to find out what is wrong here?
Or is there any more info I can give if you think this is a bug?

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.