Error Identifier: property.tooWideBool
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class Foo
{
private bool $active; // ERROR: Property Foo::$active (bool) is never assigned false so the property type can be changed to true.
public function __construct()
{
$this->active = true;
}
public function activate(): void
{
$this->active = true;
}
}
Why is it reported? #
The property is declared with type bool, but PHPStan determined that only one of the two boolean values (true or false) is ever assigned to it. This means the type is wider than necessary. Having a more precise type helps catch bugs where the other boolean value is unexpectedly used.
How to fix it #
Narrow the property type to the specific boolean value that is actually assigned:
<?php declare(strict_types = 1);
class Foo
{
- private bool $active;
+ private true $active;
public function __construct()
{
$this->active = true;
}
}
Alternatively, if the property should support both true and false, add the missing assignment:
<?php declare(strict_types = 1);
class Foo
{
private bool $active;
public function __construct()
{
$this->active = true;
}
+ public function deactivate(): void
+ {
+ $this->active = false;
+ }
}
How to ignore this error #
You can use the identifier property.tooWideBool to ignore this error using a comment:
// @phpstan-ignore property.tooWideBool
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: property.tooWideBool
Rules that report this error #
- PHPStan\Rules\TooWideTypehints\TooWideArrowFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideClosureReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWidePropertyTypeRule [1]