website/errors/closure.unusedUse.md
<?php declare(strict_types = 1);
function doFoo(string $used, string $unused): void
{
$fn = function () use ($used, $unused) {
echo $used;
};
}
The anonymous function (closure) imports the variable $unused via the use clause, but never references it within the closure body. Importing unused variables into a closure is unnecessary and makes the code harder to understand. It may also indicate a mistake where the variable was intended to be used but was forgotten.
Remove the unused variable from the use clause:
<?php declare(strict_types = 1);
function doFoo(string $used, string $unused): void
{
- $fn = function () use ($used, $unused) {
+ $fn = function () use ($used) {
echo $used;
};
}
Or use the variable inside the closure if it was intended to be used:
<?php declare(strict_types = 1);
function doFoo(string $used, string $unused): void
{
$fn = function () use ($used, $unused) {
echo $used;
+ echo $unused;
};
}