Skip to content

Commit ad34452

Browse files
committed
ArgumentsNormalizer - keep named arguments for unknown parameters
1 parent f44e8db commit ad34452

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

src/Analyser/ArgumentsNormalizer.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ private static function reorderArgs(ParametersAcceptor $parametersAcceptor, Call
191191

192192
$reorderedArgs = [];
193193
$additionalNamedArgs = [];
194+
$appendArgs = [];
194195
foreach ($callArgs as $i => $arg) {
195196
if ($arg->name === null) {
196197
// add regular args as is
@@ -209,6 +210,15 @@ private static function reorderArgs(ParametersAcceptor $parametersAcceptor, Call
209210
);
210211
} else {
211212
if (!$hasVariadic) {
213+
$attributes = $arg->getAttributes();
214+
$attributes[self::ORIGINAL_ARG_ATTRIBUTE] = $arg;
215+
$appendArgs[] = new Arg(
216+
$arg->value,
217+
$arg->byRef,
218+
$arg->unpack,
219+
$attributes,
220+
null,
221+
);
212222
continue;
213223
}
214224

@@ -235,7 +245,10 @@ private static function reorderArgs(ParametersAcceptor $parametersAcceptor, Call
235245
}
236246

237247
if (count($reorderedArgs) === 0) {
238-
return [];
248+
foreach ($appendArgs as $arg) {
249+
$reorderedArgs[] = $arg;
250+
}
251+
return $reorderedArgs;
239252
}
240253

241254
// fill up all wholes with default values until the last given argument
@@ -269,6 +282,10 @@ private static function reorderArgs(ParametersAcceptor $parametersAcceptor, Call
269282

270283
ksort($reorderedArgs);
271284

285+
foreach ($appendArgs as $arg) {
286+
$reorderedArgs[] = $arg;
287+
}
288+
272289
return $reorderedArgs;
273290
}
274291

tests/PHPStan/Analyser/ArgumentsNormalizerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ public function dataReorderValid(): iterable
222222
[
223223
[new StringType(), 'onee'],
224224
],
225-
[],
225+
[
226+
new StringType(),
227+
],
226228
];
227229

228230
yield [
@@ -237,6 +239,7 @@ public function dataReorderValid(): iterable
237239
],
238240
[
239241
new IntegerType(),
242+
new StringType(),
240243
],
241244
];
242245
}

tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,15 @@ public function testBug10059(): void
304304
$this->analyse([__DIR__ . '/data/bug-10059.php'], []);
305305
}
306306

307+
public function testBug10628(): void
308+
{
309+
if (PHP_VERSION_ID < 80000) {
310+
$this->markTestSkipped('Test requires PHP 8.0.');
311+
}
312+
313+
$this->alwaysWrittenTags = [];
314+
$this->alwaysReadTags = [];
315+
$this->analyse([__DIR__ . '/data/bug-10628.php'], []);
316+
}
317+
307318
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1); // lint >= 8.0
2+
3+
namespace Bug10628;
4+
5+
use stdClass;
6+
7+
interface Bar
8+
{
9+
10+
public function bazName(): string;
11+
12+
}
13+
14+
final class Foo
15+
{
16+
public function __construct(
17+
private Bar $bar,
18+
) {
19+
}
20+
21+
public function __invoke(): stdClass
22+
{
23+
return $this->getMixed()->get(
24+
name: $this->bar->bazName(),
25+
);
26+
}
27+
28+
public function getMixed(): mixed
29+
{
30+
31+
}
32+
}

0 commit comments

Comments
 (0)