Skip to content

[12.x] fix method dependencies order #56062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/Illuminate/Routing/ResolvesRouteDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,23 @@ protected function resolveClassMethodDependencies(array $parameters, $instance,
*/
public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector)
{
$instanceCount = 0;

$keys = array_keys($parameters);
$values = array_values($parameters);

$skippableValue = new stdClass;

foreach ($reflector->getParameters() as $key => $parameter) {
$instance = $this->transformDependency($parameter, $parameters, $skippableValue);

if ($instance !== $skippableValue) {
$instanceCount++;

if (false !== ($position = array_search($parameter->name, $keys, true))) {
$instance = $parameters[$parameter->name];
unset($keys[$position], $values[$position], $parameters[$parameter->name]);
$this->spliceIntoParameters($parameters, $key, $instance, $parameter->name);
} elseif ($skippableValue !== ($instance = $this->transformDependency($parameter, $values, $skippableValue))) {
$this->spliceIntoParameters($parameters, $key, $instance);
} elseif (! isset($values[$key - $instanceCount]) &&
$parameter->isDefaultValueAvailable()) {
} elseif (empty($values) && $parameter->isDefaultValueAvailable()) {
$this->spliceIntoParameters($parameters, $key, $parameter->getDefaultValue());
} else {
array_shift($keys);
array_shift($values);
}

$this->container->fireAfterResolvingAttributeCallbacks($parameter->getAttributes(), $instance);
Expand Down Expand Up @@ -113,12 +114,17 @@ protected function alreadyInParameters($class, array $parameters)
* @param array $parameters
* @param string $offset
* @param mixed $value
* @param string|null $key
* @return void
*/
protected function spliceIntoParameters(array &$parameters, $offset, $value)
protected function spliceIntoParameters(array &$parameters, $offset, $value, ?string $key = null)
{
array_splice(
$parameters, $offset, 0, [$value]
);
if (null === $key) {
array_splice($parameters, $offset, 0, [$value]);
} else {
$parameters = array_slice($parameters, 0, $offset, true)
+ [$key => $value]
+ array_slice($parameters, $offset, null, true);
}
}
}
10 changes: 5 additions & 5 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,31 +684,31 @@ public function testControllerCallActionMethodParameters()
unset($_SERVER['__test.controller_callAction_parameters']);
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@oneArgument');
$router->dispatch(Request::create($str.'/one/two', 'GET'));
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
$this->assertSame(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);

// Has two arguments and receives two
unset($_SERVER['__test.controller_callAction_parameters']);
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@twoArguments');
$router->dispatch(Request::create($str.'/one/two', 'GET'));
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
$this->assertSame(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);

// Has two arguments but with different names from the ones passed from the route
unset($_SERVER['__test.controller_callAction_parameters']);
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@differentArgumentNames');
$router->dispatch(Request::create($str.'/one/two', 'GET'));
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
$this->assertSame(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);

// Has two arguments with same name but argument order is reversed
unset($_SERVER['__test.controller_callAction_parameters']);
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@reversedArguments');
$router->dispatch(Request::create($str.'/one/two', 'GET'));
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
$this->assertSame(['two' => 'two', 'one' => 'one'], $_SERVER['__test.controller_callAction_parameters']);
Copy link
Author

Choose a reason for hiding this comment

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

I replaced assertEquals by assertSame because assertEquals is NOT checking the order of array items.
While assertSame is checking that both values and order are identical.

When we call the controller action, the order of param is important so we must check it.
In that example, the order of params must be reversed if they are reversed in the controller action.


// No route parameters while method has parameters
unset($_SERVER['__test.controller_callAction_parameters']);
$router->get(($str = Str::random()).'', RouteTestAnotherControllerWithParameterStub::class.'@oneArgument');
$router->dispatch(Request::create($str, 'GET'));
$this->assertEquals([], $_SERVER['__test.controller_callAction_parameters']);
$this->assertSame([], $_SERVER['__test.controller_callAction_parameters']);

// With model bindings
unset($_SERVER['__test.controller_callAction_parameters']);
Expand Down