Skip to content

Commit

Permalink
PresenterComponentReflection::combineArgs() should retype variables i…
Browse files Browse the repository at this point in the history
…n the $res array. [Fixes #99]

Use foreach's automatic indexing instead of manual one.
  • Loading branch information
peteruhnak authored and dg committed Oct 13, 2015
1 parent 68db80c commit 8e87ae8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/Application/UI/PresenterComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,14 @@ public function hasCallableMethod($method)
public static function combineArgs(\ReflectionFunctionAbstract $method, $args)
{
$res = array();
$i = 0;
foreach ($method->getParameters() as $param) {
foreach ($method->getParameters() as $i => $param) {
$name = $param->getName();
if (!isset($args[$name]) && $param->isDefaultValueAvailable()) {
$res[$i++] = $param->getDefaultValue();
$res[$i] = $param->getDefaultValue();
} else {
$res[$i++] = $arg = isset($args[$name]) ? $args[$name] : NULL;
$res[$i] = $arg = isset($args[$name]) ? $args[$name] : NULL;
list($type, $isClass) = self::getParameterType($param);
if (!self::convertType($arg, $type, $isClass)) {
if (!self::convertType($res[$i], $type, $isClass)) {
throw new BadRequestException(sprintf(
'Argument $%s passed to %s() must be %s, %s given.',
$name,
Expand Down
24 changes: 24 additions & 0 deletions tests/UI/PresenterComponentReflection.combineArgs.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Test: PresenterComponentReflection::combineArgs()
*/

use Nette\Application\UI\PresenterComponentReflection;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

class MyPresenter
{

public function myMethod($intParam = 0, $strParam = '')
{
}

}

$reflection = new ReflectionMethod('MyPresenter', 'myMethod');

Assert::same(array(10, 'str'), PresenterComponentReflection::combineArgs($reflection, array('intParam' => '10', 'strParam' => 'str')));
Assert::same(array(0, ''), PresenterComponentReflection::combineArgs($reflection, array()));

0 comments on commit 8e87ae8

Please sign in to comment.