Skip to content

A first sweep of PHPStan 2.3 unused variable/param warning cleanups - #1687

Merged
cpeel merged 9 commits into
DistributedProofreaders:masterfrom
bpfoley:phpstan-2.3-prep
Oct 16, 2026
Merged

cpeel merged 9 commits into
DistributedProofreaders:masterfrom
bpfoley:phpstan-2.3-prep

Conversation

@bpfoley

@bpfoley bpfoley commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

These are mostly obvious and local changes, and let us do a sprinkling of simplification and dead code elimination.

They're unlikely to be wrong, as if they remove variables that are actually used, PHPStan would warn about the use of uninitalized variables.

In the cases where the correctness of the removal isn't obvious, I've pulled them out into separate commits with their own descriptions.

It should have been removed with commit 26b077d, but was missed.
Quite a few of these are from destructuring where we use some
but not all of the elements from the array. Others are just simple
dead code.

@cpeel cpeel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but one question about a possible readability improvement.

Comment thread api/index.php Outdated
Found by PHPstan 2.3's `foreach.unusedKey` warning.

Mildly surprisingly, with an associative array `$a`,
```foreach ($a as $k => $v) { ... }``` iterates over keys & values, but
```foreach ($a as $v) { ... }``` iterates over values.
In other words, it's equivalent to
```foreach(array_values($a) as $v) { ... }```

Python makes the 'opposite' choice: iterating over a dict yields the
keys:
```for k in a: ...``` is equivent to
```for k in a.keys(): ...```
and
```for v in a.values(): ...```
is equivalent to the PHP.
This is a slightly trickier analysis, but you can see that
$prev_text_column_name assigned to at the end of a loop body can
only be used in the next iteration of the loop, and it's always
assigned to in the 3-way at the start of that loop.
Note that some of these assign a default value to a variable in case
there is a path through the if blocks following where it isn't
explicitly assigned to. However that's caught by PHPStan's
`variable.undefined` warning at level >= 1.
Did this tidyup to avoid PHPStan 2.3's `function.unusedParameter`
warning
Found by PHPstan 2.3's new `function.unusedParameter` warning.

When we remove the function parameter, PHPstan helpfully shows us the
callers that need the extra parameter removed too.
if `empty($datax)`, then `$datax` must be `[]` (because `array_column()`
above returns the empty array if the specified column index isn't found.

Found by PHPStan 2.3's `assign.overwrite` warning
@cpeel
cpeel merged commit 2f71032 into DistributedProofreaders:master Oct 16, 2026
12 checks passed
@bpfoley
bpfoley deleted the phpstan-2.3-prep branch October 16, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants