Skip to content

@assert =T $x should be supported #10665

Open

Description

@mvorisek

Feature request

repro: https://phpstan.org/r/9b3e8609-2d32-452c-aef3-79cca1a66569

Currently TestCase::assertTrue(true) is reported as always true, but TestCase::assertSame('x', 'x') is not.

assertions were introduced in phpstan/phpstan-src#1317 and this was already discussed in phpstan/phpstan-src#1317 (comment).

At least = should be supported for strict type equality to make the assertion more consistent with psalm.

Activity

  1. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Contributor

    @mvorisek After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    +PHP 8.0 – 8.3 (2 errors)
    +==========
    +
     29: Call to static method Foo::assertTrue() with true will always evaluate to true.
    -30: Call to static method Foo::assertSame() with true and true will always evaluate to true.
    +30: Call to static method Foo::assertSame() with true and true will always evaluate to true.
    +
    +PHP 7.2 – 7.4 (12 errors)
    +==========
    +
    +10: Asserted type true for $condition with type mixed can never happen.
    +10: Parameter $condition of method Foo::assertTrue() has invalid type mixed.
    +23: PHPDoc tag @param for parameter $expected with type ExpectedType is not subtype of native type mixed.
    +23: Parameter $actual of method Foo::assertSame() has invalid type mixed.
    +23: Parameter $expected of method Foo::assertSame() has invalid type mixed.
    +23: Template type ExpectedType of method Foo::assertSame() is not referenced in a parameter.
    +29: Call to static method Foo::assertTrue() with true will always evaluate to true.
    +29: Parameter #1 $condition of static method Foo::assertTrue() expects mixed|null, true given.
    +30: Parameter #1 $expected of static method Foo::assertSame() expects mixed|null, true given.
    +30: Parameter #2 $actual of static method Foo::assertSame() expects mixed|null, true given.
    +31: Parameter #1 $expected of static method Foo::assertSame() expects mixed|null, string given.
    +31: Parameter #2 $actual of static method Foo::assertSame() expects mixed|null, string given.
    Full report

    PHP 8.0 – 8.3 (2 errors)

    Line Error
    29 Call to static method Foo::assertTrue() with true will always evaluate to true.
    30 Call to static method Foo::assertSame() with true and true will always evaluate to true.

    PHP 7.2 – 7.4 (12 errors)

    Line Error
    10 Asserted type true for $condition with type mixed can never happen.
    10 Parameter $condition of method Foo::assertTrue() has invalid type mixed.
    23 PHPDoc tag @param for parameter $expected with type ExpectedType is not subtype of native type mixed.
    23 Parameter $actual of method Foo::assertSame() has invalid type mixed.
    23 Parameter $expected of method Foo::assertSame() has invalid type mixed.
    23 Template type ExpectedType of method Foo::assertSame() is not referenced in a parameter.
    29 Call to static method Foo::assertTrue() with true will always evaluate to true.
    29 `Parameter #1 $condition of static method Foo::assertTrue() expects mixed
    30 `Parameter #1 $expected of static method Foo::assertSame() expects mixed
    30 `Parameter #2 $actual of static method Foo::assertSame() expects mixed
    31 `Parameter #1 $expected of static method Foo::assertSame() expects mixed
    31 `Parameter #2 $actual of static method Foo::assertSame() expects mixed
  2. ondrejmirtes commented on Dec 10, 2024

    @ondrejmirtes
    Member

    I'd say this already works.

  3. mvorisek commented on Dec 10, 2024

    @mvorisek
    ContributorAuthor

    @ondrejmirtes this issue is about l31 which does not emit a PHPStan error.

  4. phpstan-bot commented on Sep 16, 2025

    @phpstan-bot
    Contributor

    @mvorisek After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -PHP 8.0 – 8.4 (2 errors)
    +PHP 8.0 – 8.4 (1 error)
     ==========
     
     29: Call to static method Foo::assertTrue() with true will always evaluate to true.
    -30: Call to static method Foo::assertSame() with true and true will always evaluate to true.
     
     PHP 7.2 – 7.4 (12 errors)
     ==========
    Full report

    PHP 8.0 – 8.4 (1 error)

    Line Error
    29 Call to static method Foo::assertTrue() with true will always evaluate to true.

    PHP 7.2 – 7.4 (12 errors)

    Line Error
    10 Asserted type true for $condition with type mixed can never happen.
    10 Parameter $condition of method Foo::assertTrue() has invalid type mixed.
    23 PHPDoc tag @param for parameter $expected with type ExpectedType is not subtype of native type mixed.
    23 Parameter $actual of method Foo::assertSame() has invalid type mixed.
    23 Parameter $expected of method Foo::assertSame() has invalid type mixed.
    23 Template type ExpectedType of method Foo::assertSame() is not referenced in a parameter.
    29 Call to static method Foo::assertTrue() with true will always evaluate to true.
    29 `Parameter #1 $condition of static method Foo::assertTrue() expects mixed
    30 `Parameter #1 $expected of static method Foo::assertSame() expects mixed
    30 `Parameter #2 $actual of static method Foo::assertSame() expects mixed
    31 `Parameter #1 $expected of static method Foo::assertSame() expects mixed
    31 `Parameter #2 $actual of static method Foo::assertSame() expects mixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions