Error Identifier: new.void
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 Logger
{
public function __construct()
{
}
}
function getLogger(): Logger
{
$result = new Logger();
echo $result;
return $result;
}
Why is it reported? #
The result of a new expression is being used in a context where it evaluates to void. This typically occurs when PHPStan determines that the constructor call produces a void result in the current context, for example when the new expression is used inside an attribute declaration or another specialized context where the return value is not meaningful.
How to fix it #
Ensure the result of the new expression is not used as a value when it is not expected to produce one. If the object is needed, assign it to a variable and use it separately:
<?php declare(strict_types = 1);
-echo new Logger();
+$logger = new Logger();
How to ignore this error #
You can use the identifier new.void to ignore this error using a comment:
// @phpstan-ignore new.void
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.void
Rules that report this error #
- PHPStan\Rules\Classes\ClassAttributesRule [1]
- PHPStan\Rules\Classes\ClassConstantAttributesRule [1]
- PHPStan\Rules\Classes\InstantiationRule [1]
- PHPStan\Rules\Constants\ConstantAttributesRule [1]
- PHPStan\Rules\EnumCases\EnumCaseAttributesRule [1]
- PHPStan\Rules\Functions\ArrowFunctionAttributesRule [1]
- PHPStan\Rules\Functions\CallCallablesRule [1]
- PHPStan\Rules\Functions\CallToFunctionParametersRule [1]
- PHPStan\Rules\Functions\CallUserFuncRule [1]
- PHPStan\Rules\Functions\ClosureAttributesRule [1]
- PHPStan\Rules\Functions\FunctionAttributesRule [1]
- PHPStan\Rules\Functions\ParamAttributesRule [1]
- PHPStan\Rules\Methods\CallMethodsRule [1]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1]
- PHPStan\Rules\Methods\MethodAttributesRule [1]
- PHPStan\Rules\Properties\PropertyAttributesRule [1]
- PHPStan\Rules\Properties\PropertyHookAttributesRule [1]
- PHPStan\Rules\Traits\TraitAttributesRule [1]