Skip to content

Commit 4ba46aa

Browse files
authored
Ignore unused function params prefixed with _ (#6743)
1 parent 0b9a9d1 commit 4ba46aa

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/Rules/UnusedParametersCheck.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function is_string;
1515
use function ltrim;
1616
use function sprintf;
17+
use function str_starts_with;
1718

1819
/**
1920
* The per-parameter half of the unused-parameter rules, on top of the
@@ -61,6 +62,9 @@ public function getUnusedParameterErrors(
6162
if (!$parameter->var instanceof Variable || !is_string($parameter->var->name)) {
6263
continue;
6364
}
65+
if (str_starts_with($parameter->var->name, '_')) {
66+
continue;
67+
}
6468
if (isset($contractParameterNames[$parameter->var->name])) {
6569
continue;
6670
}

‎tests/PHPStan/Rules/Functions/data/unused-function-parameters.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,25 @@ function observedByFuncGetArg(int $x): array
113113

114114
return [func_get_arg(0), $x];
115115
}
116+
117+
function completelyUnusedIgnore(int $used, $_ignoredUnused): int
118+
{
119+
return $used;
120+
}
121+
122+
function byRefUnusedIgnore(int &$_ignoredX): void
123+
{
124+
echo 'no use of the reference';
125+
}
126+
127+
function variadicIgnoredUnusedIgnore(int $used, int ...$_ignoredRest): int
128+
{
129+
return $used;
130+
}
131+
132+
function usedViaResolvedVariableVariableIgnore(int $secret, int $_ignoredOther): int
133+
{
134+
$name = 'secret';
135+
136+
return $$name;
137+
}

‎tests/PHPStan/Rules/Functions/data/unused-input-value-flow.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ function overwrittenParameter(int $input): int
5757
$input = 1;
5858
return $input;
5959
}
60+
61+
function overwrittenParameterIgnore(int $_ignoredInput): int
62+
{
63+
$_ignoredInput = 1;
64+
return $_ignoredInput;
65+
}

0 commit comments

Comments
 (0)