Skip to content

if ($value > 0) proves that $value is not false #1272

Closed

Description

@iluuu1994

PHPStan 0.10.x

Summary of a problem or a feature request

<?php

function acceptsInt(int $value) {}

/** @var int|false */
$value = false;

if ($value > 0) {
    acceptsInt($value);
}

Parameter #1 $value of function acceptsInt expects int, int|false given.

if ($value > 0) { proves that $value is not false. PHPStan thinks that it still might be false.

Currently, I need to use if ($value !== false && $value > 0) to silence PHPStan.

Code snippet that reproduces the problem

https://phpstan.org/r/0dda040f8e4c229b7fdc68bffae405f8
https://3v4l.org/YuWIQ

Expected output

No error.

Activity

  1. JanTvrdik commented on Jul 23, 2018

    @JanTvrdik
    Contributor

    to silence PHPStan.

    And to make to code readable for humans.

  2. iluuu1994 commented on Jul 23, 2018

    @iluuu1994
    ContributorAuthor

    And to make to code readable for humans.

    Absolutely. That's why "PHPStan-strict-rules" has rules to avoid comparisons of non-number types.

    But I still think PHPStan should avoid false positives.

  3. added this to the 0.10 milestone on Jul 28, 2018
  4. modified the milestones: 0.10, 0.11 on Jul 28, 2018
  5. removed this from the 0.11 milestone on Nov 15, 2018
  6. ondrejmirtes commented on Jan 8, 2021

    @ondrejmirtes
    Member

    Already implemented by phpstan/phpstan-src#390

  7. github-actions commented on May 2, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  8. locked as resolved and limited conversation to collaborators on May 2, 2021
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