Skip to content

Commit 50e49fd

Browse files
committed
Presenter: invalid action throws BadRequestException only during startup
This will make possible to have type hint 'string' in changeAction()
1 parent 7de3acb commit 50e49fd

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/Application/UI/Presenter.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,13 @@ public function getAction($fullyQualified = FALSE)
381381

382382

383383
/**
384-
* Changes current action. Only alphanumeric characters are allowed.
384+
* Changes current action.
385385
* @param string
386386
* @return void
387387
*/
388388
public function changeAction($action)
389389
{
390-
if (is_string($action) && Nette\Utils\Strings::match($action, '#^[a-zA-Z0-9][a-zA-Z0-9_\x7f-\xff]*\z#')) {
391-
$this->action = $action;
392-
$this->view = $action;
393-
394-
} else {
395-
$this->error('Action name is not alphanumeric string.');
396-
}
390+
$this->action = $this->view = (string) $action;
397391
}
398392

399393

@@ -1231,7 +1225,11 @@ private function initGlobalParameters()
12311225
}
12321226

12331227
// init & validate $this->action & $this->view
1234-
$this->changeAction(isset($selfParams[self::ACTION_KEY]) ? $selfParams[self::ACTION_KEY] : self::DEFAULT_ACTION);
1228+
$action = $selfParams[self::ACTION_KEY] ?? self::DEFAULT_ACTION;
1229+
if (!is_string($action) || !Nette\Utils\Strings::match($action, '#^[a-zA-Z0-9][a-zA-Z0-9_\x7f-\xff]*\z#')) {
1230+
$this->error('Action name is not valid.');
1231+
}
1232+
$this->changeAction($action);
12351233

12361234
// init $this->signalReceiver and key 'signal' in appropriate params array
12371235
$this->signalReceiver = $this->getUniqueId();

tests/UI/Presenter.paramChecking.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $presenter->injectPrimary(
3838
Assert::exception(function () use ($presenter) {
3939
$request = new Application\Request('Test', Http\Request::GET, ['action' => []]);
4040
$presenter->run($request);
41-
}, Nette\Application\BadRequestException::class, 'Action name is not alphanumeric string.');
41+
}, Nette\Application\BadRequestException::class, 'Action name is not valid.');
4242

4343

4444
Assert::exception(function () use ($presenter) {

0 commit comments

Comments
 (0)