Skip to content

Commit

Permalink
Presenter, Control: formatTemplateClass() refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 8, 2021
1 parent 55c9d45 commit 0a4aeab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/Application/UI/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ protected function createTemplate(/*string $class = null*/): Template

public function formatTemplateClass(): ?string
{
$class = preg_replace('#Presenter$|Control$#', 'Template', static::class);
if ($class === static::class || !class_exists($class)) {
return $this->checkTemplateClass(preg_replace('#Control$#', '', static::class) . 'Template');
}


/** @internal */
protected function checkTemplateClass(string $class): ?string
{
if (!class_exists($class)) {
return null;
} elseif (!is_a($class, Template::class, true)) {
trigger_error(sprintf(
Expand Down
8 changes: 3 additions & 5 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,9 @@ protected function createTemplate(/*string $class = null*/): Template

public function formatTemplateClass(): ?string
{
$class = preg_replace('#Presenter$#', ucfirst((string) $this->action) . 'Template', static::class);
if (class_exists($class) && is_a($class, Template::class, true)) {
return $class;
}
return parent::formatTemplateClass();
$base = preg_replace('#Presenter$#', '', static::class);
return $this->checkTemplateClass($base . ucfirst((string) $this->action) . 'Template')
?? $this->checkTemplateClass($base . 'Template');
}


Expand Down
2 changes: 1 addition & 1 deletion tests/UI/Presenter.formatTemplateClass.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('with class', function () {
Assert::error(function () {
$presenter = new BPresenter;
Assert::null($presenter->formatTemplateClass());
}, E_USER_NOTICE, '%a% BTemplate was found but does not implement%a%');
}, [E_USER_NOTICE, E_USER_NOTICE]);
});


Expand Down

0 comments on commit 0a4aeab

Please sign in to comment.