From c9493eb737793c3f6718e07860e75b8f076803ea Mon Sep 17 00:00:00 2001 From: Victor Welling Date: Fri, 8 Dec 2017 11:25:03 +0100 Subject: [PATCH] Small code quality tweaks --- src/Middleware/PresenterMiddleware.php | 2 +- src/PresentationModel.php | 16 +++++----------- src/View.php | 3 +++ tests/Fixtures/ProductPresentationModel.php | 2 ++ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Middleware/PresenterMiddleware.php b/src/Middleware/PresenterMiddleware.php index 70f3fc0..51470e8 100644 --- a/src/Middleware/PresenterMiddleware.php +++ b/src/Middleware/PresenterMiddleware.php @@ -42,7 +42,7 @@ public function process(View $view, Context $context, callable $next): View { $presentationModel = $view->getPresentationModel(); - if (!$this->hasData($presentationModel) && $presentationModel instanceof HasPresenterInterface) { + if ($presentationModel instanceof HasPresenterInterface && !$this->hasData($presentationModel)) { $presenter = $this->loadPresenter($presentationModel); $presentationModel = $presenter->present($context, $presentationModel); $view = $view->withPresentationModel($presentationModel); diff --git a/src/PresentationModel.php b/src/PresentationModel.php index 8f686e4..a1b8935 100644 --- a/src/PresentationModel.php +++ b/src/PresentationModel.php @@ -52,17 +52,11 @@ final public function withVariables(array $variables): self */ private function setVariables(array $variables) { - if (static::class === self::class) { - foreach ($variables as $variable => $value) { - if (is_string($variable)) { - $this->$variable = $value; - } - } - } else { - foreach ($variables as $variable => $value) { - if (property_exists($this, $variable)) { - $this->$variable = $value; - } + $isGeneric = static::class === self::class; + + foreach ($variables as $variable => $value) { + if (is_string($variable) && ($isGeneric || property_exists($this, $variable))) { + $this->$variable = $value; } } } diff --git a/src/View.php b/src/View.php index e465ff4..e688480 100644 --- a/src/View.php +++ b/src/View.php @@ -5,6 +5,9 @@ use InvalidArgumentException; +/** + * A view is the visual representation of a model. + */ final class View { /** @var callable */ diff --git a/tests/Fixtures/ProductPresentationModel.php b/tests/Fixtures/ProductPresentationModel.php index 883d720..eead4c6 100644 --- a/tests/Fixtures/ProductPresentationModel.php +++ b/tests/Fixtures/ProductPresentationModel.php @@ -10,8 +10,10 @@ final class ProductPresentationModel extends PresentationModel implements HasPre { /** @var bool */ protected $on_stock = false; + /** @var string */ protected $product_name = ''; + /** @var int */ protected $stock_quantity = 0;