Skip to content

Commit a61d596

Browse files
committed
Presenter: link(), redirect() and forward() more precise recognizes between variadic vs array parameter
1 parent 50e4e8d commit a61d596

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/Application/UI/Presenter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,8 @@ public function forward($destination, $args = [])
653653
$this->sendResponse(new Responses\ForwardResponse($destination));
654654
}
655655

656-
$this->createRequest($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1), 'forward');
656+
$args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1);
657+
$this->createRequest($this, $destination, $args, 'forward');
657658
$this->sendResponse(new Responses\ForwardResponse($this->lastCreatedRequest));
658659
}
659660

src/Application/UI/PresenterComponent.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ public static function formatSignalMethod($signal)
281281
public function link($destination, $args = [])
282282
{
283283
try {
284-
return $this->getPresenter()->createRequest($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1), 'link');
284+
$args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1);
285+
return $this->getPresenter()->createRequest($this, $destination, $args, 'link');
285286

286287
} catch (InvalidLinkException $e) {
287288
return $this->getPresenter()->handleInvalidLink($e);
@@ -297,7 +298,8 @@ public function link($destination, $args = [])
297298
*/
298299
public function lazyLink($destination, $args = [])
299300
{
300-
return new Link($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1));
301+
$args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1);
302+
return new Link($this, $destination, $args);
301303
}
302304

303305

@@ -311,7 +313,8 @@ public function lazyLink($destination, $args = [])
311313
public function isLinkCurrent($destination = NULL, $args = [])
312314
{
313315
if ($destination !== NULL) {
314-
$this->getPresenter()->createRequest($this, $destination, is_array($args) ? $args : array_slice(func_get_args(), 1), 'test');
316+
$args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1);
317+
$this->getPresenter()->createRequest($this, $destination, $args, 'test');
315318
}
316319
return $this->getPresenter()->getLastCreatedRequestFlag('current');
317320
}
@@ -328,11 +331,11 @@ public function isLinkCurrent($destination = NULL, $args = [])
328331
public function redirect($code, $destination = NULL, $args = [])
329332
{
330333
if (!is_numeric($code)) { // first parameter is optional
331-
$args = is_array($destination) ? $destination : array_slice(func_get_args(), 1);
334+
$args = func_num_args() < 3 && is_array($destination) ? $destination : array_slice(func_get_args(), 1);
332335
$destination = $code;
333336
$code = NULL;
334337

335-
} elseif (!is_array($args)) {
338+
} elseif (func_num_args() < 4 && !is_array($args)) {
336339
$args = array_slice(func_get_args(), 2);
337340
}
338341

tests/Application/Presenter.link().phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ class TestPresenter extends Application\UI\Presenter
104104
Assert::same('/index.php?y=2&action=default&do=buy&presenter=Test', $this->link('buy!', 1, 2));
105105
Assert::same('/index.php?y=2&bool=1&str=1&action=default&do=buy&presenter=Test', $this->link('buy!', '1', '2', TRUE, TRUE));
106106
Assert::same('/index.php?y=2&str=0&action=default&do=buy&presenter=Test', $this->link('buy!', '1', '2', FALSE, FALSE));
107-
Assert::same('/index.php?action=default&do=buy&presenter=Test', $this->link('buy!', [1], (object) [1]));
107+
Assert::same('/index.php?action=default&do=buy&presenter=Test', $this->link('buy!', [1]));
108+
Assert::same("#error: Invalid value for parameter 'x' in method TestPresenter::handlebuy(), expected integer.", $this->link('buy!', [1], (object) [1]));
108109
Assert::same('/index.php?y=2&action=default&do=buy&presenter=Test', $this->link('buy!', [1, 'y' => 2]));
109110
Assert::same('/index.php?y=2&action=default&do=buy&presenter=Test', $this->link('buy!', ['x' => 1, 'y' => 2, 'var1' => $this->var1]));
110111
Assert::same('#error: Signal must be non-empty string.', $this->link('!'));
@@ -120,7 +121,8 @@ class TestPresenter extends Application\UI\Presenter
120121
Assert::same('#error: Signal must be non-empty string.', $this['mycontrol']->link('', 0, 1));
121122
Assert::same('/index.php?mycontrol-x=0&mycontrol-y=1&action=default&do=mycontrol-click&presenter=Test', $this['mycontrol']->link('click', 0, 1));
122123
Assert::same('/index.php?mycontrol-x=0a&mycontrol-y=1a&action=default&do=mycontrol-click&presenter=Test', $this['mycontrol']->link('click', '0a', '1a'));
123-
Assert::same('/index.php?mycontrol-x=1&action=default&do=mycontrol-click&presenter=Test', $this['mycontrol']->link('click', [1], (object) [1]));
124+
Assert::same('/index.php?mycontrol-x=1&action=default&do=mycontrol-click&presenter=Test', $this['mycontrol']->link('click', [1]));
125+
Assert::same("#error: Invalid value for parameter 'x' in method TestControl::handleclick(), expected scalar.", $this['mycontrol']->link('click', [1], (object) [1]));
124126
Assert::same('/index.php?mycontrol-x=1&action=default&do=mycontrol-click&presenter=Test', $this['mycontrol']->link('click', TRUE, FALSE));
125127
Assert::same('/index.php?action=default&do=mycontrol-click&presenter=Test', $this['mycontrol']->link('click', NULL, ''));
126128
Assert::same('#error: Passed more parameters than method TestControl::handleClick() expects.', $this['mycontrol']->link('click', 1, 2, 3));

0 commit comments

Comments
 (0)