Skip to content

auto-fix "Only booleans are allowed in an if condition" #316

Open

Description

@staabm

Feature request

when the native type of a property is known, we can auto-fix the "Only booleans are allowed in an if condition" phpstan-strict-error.

same is true for method-calls/function-calls etc. which have a native return-type.
I think this applies to any expression for which we know a non-union native type.

e.g.

class CoffeeBreak
{
	private array $arr;
	
	public function getDuration(): int {
		if ($this->arr) {
			
		}
	}
}

can get fixed to

class CoffeeBreak
{
	private array $arr;
	
	public function getDuration(): int {
		if (count($this->arr) !== 0) {
			
		}
	}
}

https://phpstan.org/r/5a9de50b-cc0c-47c9-81f8-fbe4d609ed4d

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. VincentLanglet commented on May 8, 2026

    @VincentLanglet
    Contributor

    I dunno if we should prefer

    if (count($this->arr) !== 0) {
    

    or a !== [] check.

    I think this applies to any expression for which we know a non-union native type.

    This could work for some union too. The one I saw a lot

    if ($objectOrNull) {
    
    }
    
  2. staabm commented on May 8, 2026

    @staabm
    ContributorAuthor

    I don't care much about which expression we use (as projects will run code style fixing on it most of the time).

    As soon as the expression is stricter we fix the phpstan error - thats most important

  3. VincentLanglet commented on May 8, 2026

    @VincentLanglet
    Contributor

    Btw should be a feature request for the strict-rules repository no ?

  4. transferred this issue fromphpstan/phpstanon May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions