Skip to content

Commit

Permalink
Merge pull request #172 from ray-di/hotfix
Browse files Browse the repository at this point in the history
Fix getNamedArguments when parameter has default value
  • Loading branch information
koriym authored Feb 18, 2021
2 parents ef6a566 + 85f7ebe commit c04ee5e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 81 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"cs-fix": ["./vendor/bin/phpcbf src tests"],
"clean": ["./vendor/bin/phpstan clear-result-cache", "./vendor/bin/psalm --clear-cache", "rm -rf tests/tmp/*.php"],
"sa": ["./vendor/bin/phpstan analyse -c phpstan.neon", "psalm --show-info=true"],
"metrics": ["./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception --log-junit=build/junit.xml --junit=build/junit.xml src"],
"metrics": ["./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception src"],
"phpmd": ["./vendor/bin/phpmd src text ./phpmd.xml"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
}
Expand Down
7 changes: 5 additions & 2 deletions src/ReflectiveMethodInvocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public function getNamedArguments(): ArrayObject
$params = $this->getMethod()->getParameters();
$namedParams = [];
foreach ($params as $param) {
/** @psalm-suppress MixedAssignment */
$namedParams[$param->getName()] = $args[$param->getPosition()];
$pos = $param->getPosition();
if (isset($args[$pos])) {
/** @psalm-suppress MixedAssignment */
$namedParams[$param->getName()] = $args[$pos];
}
}

return new ArrayObject($namedParams);
Expand Down
4 changes: 4 additions & 0 deletions tests/Fake/FakeClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public function getTriple(int $c): int
{
return $c * 3;
}

public function defaultValue(int $a = 1)
{
}
}
10 changes: 10 additions & 0 deletions tests/ReflectiveMethodInvocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public function testGetNamedArguments(): void
$this->assertSame((array) $args, ['n' => 1]);
}

/**
* @covers ReflectiveMethodInvocation::getNamedArguments
*/
public function testGetNamedArgumentsWithDefaultValue(): void
{
$invocation = new ReflectiveMethodInvocation(new FakeWeavedClass(), 'defaultValue', [], [new FakeInterceptor(), new FakeInterceptor()]);
$args = $invocation->getNamedArguments();
$this->assertSame((array) $args, []);
}

public function testGetAnnotation(): void
{
$fakeMarker = $this->invocation->getMethod()->getAnnotation(FakeMarker::class);
Expand Down
Loading

0 comments on commit c04ee5e

Please sign in to comment.