Error Identifier: new.resultUnused
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 Validator
{
/** @phpstan-pure */
public function __construct()
{
}
}
function doFoo(): void
{
new Validator(); // ERROR: Call to new Validator() on a separate line has no effect.
}
Why is it reported? #
A new ClassName() expression appears as a standalone statement but the constructor has no side effects. The created object is not assigned to a variable, returned, or used in any way. Since the constructor is pure (or has no impure points), instantiating the object without using it has no observable effect and is likely a mistake.
How to fix it #
Use the result of the instantiation:
<?php declare(strict_types = 1);
function doFoo(): void
{
- new Validator();
+ $validator = new Validator();
}
If the constructor is intended to have side effects, make sure the constructor actually performs a side effect so PHPStan recognizes it as impure:
<?php declare(strict_types = 1);
class Validator
{
- /** @phpstan-pure */
public function __construct()
{
+ // perform some side effect
}
}
How to ignore this error #
You can use the identifier new.resultUnused to ignore this error using a comment:
// @phpstan-ignore new.resultUnused
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: new.resultUnused