Skip to content

Commit 0a4aeab

Browse files
committed
Presenter, Control: formatTemplateClass() refactoring
1 parent 55c9d45 commit 0a4aeab

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/Application/UI/Control.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,14 @@ protected function createTemplate(/*string $class = null*/): Template
6666

6767
public function formatTemplateClass(): ?string
6868
{
69-
$class = preg_replace('#Presenter$|Control$#', 'Template', static::class);
70-
if ($class === static::class || !class_exists($class)) {
69+
return $this->checkTemplateClass(preg_replace('#Control$#', '', static::class) . 'Template');
70+
}
71+
72+
73+
/** @internal */
74+
protected function checkTemplateClass(string $class): ?string
75+
{
76+
if (!class_exists($class)) {
7177
return null;
7278
} elseif (!is_a($class, Template::class, true)) {
7379
trigger_error(sprintf(

src/Application/UI/Presenter.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,9 @@ protected function createTemplate(/*string $class = null*/): Template
567567

568568
public function formatTemplateClass(): ?string
569569
{
570-
$class = preg_replace('#Presenter$#', ucfirst((string) $this->action) . 'Template', static::class);
571-
if (class_exists($class) && is_a($class, Template::class, true)) {
572-
return $class;
573-
}
574-
return parent::formatTemplateClass();
570+
$base = preg_replace('#Presenter$#', '', static::class);
571+
return $this->checkTemplateClass($base . ucfirst((string) $this->action) . 'Template')
572+
?? $this->checkTemplateClass($base . 'Template');
575573
}
576574

577575

tests/UI/Presenter.formatTemplateClass.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test('with class', function () {
5959
Assert::error(function () {
6060
$presenter = new BPresenter;
6161
Assert::null($presenter->formatTemplateClass());
62-
}, E_USER_NOTICE, '%a% BTemplate was found but does not implement%a%');
62+
}, [E_USER_NOTICE, E_USER_NOTICE]);
6363
});
6464

6565

0 commit comments

Comments
 (0)