Error Identifier: cast.useless
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);
function doFoo(int $value): void
{
$result = (int) $value;
}
Why is it reported? #
The value being cast is already of the target type, making the cast redundant. In the example above, $value is already an int, so casting it to int with (int) has no effect.
This rule is part of phpstan-strict-rules and helps identify unnecessary type casts that add clutter to the code without changing behaviour.
How to fix it #
Remove the unnecessary cast:
<?php declare(strict_types = 1);
function doFoo(int $value): void
{
- $result = (int) $value;
+ $result = $value;
}
How to ignore this error #
You can use the identifier cast.useless to ignore this error using a comment:
// @phpstan-ignore cast.useless
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: cast.useless
Rules that report this error #
- PHPStan\Rules\Cast\UselessCastRule [1] phpstan/phpstan-strict-rules