From fc2641473b4023d187c9ebdd2126369f99eac5e1 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Tue, 4 Oct 2016 23:15:25 +0100 Subject: [PATCH 1/2] Refactor out constructor args --- src/CliMenuBuilder.php | 65 +++++++++--------- src/MenuStyle.php | 82 ++++++++++++----------- test/CliMenuBuilderTest.php | 2 +- test/CliMenuTest.php | 14 +--- test/res/testReDrawReDrawsImmediately.txt | 20 +++--- test/res/testSimpleOpenClose.txt | 10 +-- 6 files changed, 91 insertions(+), 102 deletions(-) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index 4f25fca6..f5205934 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -55,9 +55,9 @@ class CliMenuBuilder private $menuItems = []; /** - * @var array + * @var MenuStyle */ - private $style = []; + private $style; /** * @var TerminalInterface @@ -84,27 +84,9 @@ class CliMenuBuilder */ public function __construct(CliMenuBuilder $parent = null) { - $this->parent = $parent; - $this->terminal = TerminalFactory::fromSystem(); - $this->style = $this->getStyleClassDefaults(); - $this->style['terminal'] = $this->terminal; - } - - /** - * Pull the constructor params into an array with default values - * - * @return array - */ - private function getStyleClassDefaults() - { - $styleClassParameters = (new \ReflectionClass(MenuStyle::class))->getConstructor()->getParameters(); - - $defaults = []; - foreach ($styleClassParameters as $parameter) { - $defaults[$parameter->getName()] = $parameter->getDefaultValue(); - } - - return $defaults; + $this->parent = $parent; + $this->terminal = TerminalFactory::fromSystem(); + $this->style = MenuStyle::getDefaultStyleValues(); } /** @@ -389,7 +371,6 @@ public function setTitleSeparator($separator) public function setTerminal(TerminalInterface $terminal) { $this->terminal = $terminal; - $this->style['terminal'] = $this->terminal; return $this; } @@ -436,19 +417,33 @@ private function itemsHaveExtra(array $items) */ private function getMenuStyle() { - $diff = array_udiff_assoc($this->style, $this->getStyleClassDefaults(), function ($current, $default) { - if ($current instanceof TerminalInterface) { - return 0; - } - - return $current === $default ? 0 : 1; - }); + if (null === $this->parent) { + return $this->buildStyle(); + } - if (!$diff && null !== $this->parent) { - return $this->parent->getMenuStyle(); + if ($this->style !== MenuStyle::getDefaultStyleValues()) { + return $this->buildStyle(); } - - return new MenuStyle(...array_values($this->style)); + + return $this->parent->getMenuStyle(); + } + + /** + * @return MenuStyle + */ + private function buildStyle() + { + return (new MenuStyle($this->terminal)) + ->setFg($this->style['fg']) + ->setBg($this->style['bg']) + ->setWidth($this->style['width']) + ->setPadding($this->style['padding']) + ->setMargin($this->style['margin']) + ->setSelectedMarker($this->style['selectedMarker']) + ->setUnselectedMarker($this->style['unselectedMarker']) + ->setItemExtra($this->style['itemExtra']) + ->setDisplaysExtra($this->style['displaysExtra']) + ->setTitleSeparator($this->style['titleSeparator']); } /** diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 6a1b1d43..96c0d380 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -76,6 +76,32 @@ class MenuStyle */ private $titleSeparator; + /** + * Default Values + * + * @var array + */ + private static $defaultStyleValues = [ + 'fg' => 'white', + 'bg' => 'blue', + 'width' => 100, + 'padding' => 2, + 'margin' => 2, + 'selectedMarker' => '●', + 'unselectedMarker' => '○', + 'itemExtra' => '✔', + 'displaysExtra' => false, + 'titleSeparator' => '=', + ]; + + /** + * @return array + */ + public static function getDefaultStyleValues() + { + return static::$defaultStyleValues; + } + /** * @var array */ @@ -121,45 +147,22 @@ class MenuStyle /** * Initialise style * - * @param string $bg - * @param string $fg - * @param int $width - * @param int $padding - * @param int $margin - * @param string $unselectedMarker - * @param string $selectedMarker - * @param string $itemExtra - * @param bool $displaysExtra - * @param string $titleSeparator * @param TerminalInterface $terminal - * @throws InvalidInstantiationException - */ - public function __construct( - $bg = 'blue', - $fg = 'white', - $width = 100, - $padding = 2, - $margin = 2, - $unselectedMarker = '○', - $selectedMarker = '●', - $itemExtra = '✔', - $displaysExtra = false, - $titleSeparator = '=', - TerminalInterface $terminal = null - ) { - $this->terminal = $terminal ?: TerminalFactory::fromSystem(); - $this->bg = $bg; - $this->fg = $fg; - $this->padding = $padding; - $this->margin = $margin; - $this->itemExtra = $itemExtra; - $this->displaysExtra = $displaysExtra; - $this->titleSeparator = $titleSeparator; - - $this->setUnselectedMarker($unselectedMarker); - $this->setSelectedMarker($selectedMarker); - $this->setWidth($width); - $this->calculateContentWidth(); + */ + public function __construct(TerminalInterface $terminal = null) + { + $this->terminal = $terminal ?: TerminalFactory::fromSystem(); + + $this->setFg(static::$defaultStyleValues['fg']); + $this->setBg(static::$defaultStyleValues['bg']); + $this->setWidth(static::$defaultStyleValues['width']); + $this->setPadding(static::$defaultStyleValues['padding']); + $this->setMargin(static::$defaultStyleValues['margin']); + $this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']); + $this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']); + $this->setItemExtra(static::$defaultStyleValues['itemExtra']); + $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); + $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); } /** @@ -432,10 +435,13 @@ public function getMarker($selected) /** * @param string $itemExtra + * @return $this */ public function setItemExtra($itemExtra) { $this->itemExtra = $itemExtra; + + return $this; } /** diff --git a/test/CliMenuBuilderTest.php b/test/CliMenuBuilderTest.php index b49d1c2f..7dc1efa9 100644 --- a/test/CliMenuBuilderTest.php +++ b/test/CliMenuBuilderTest.php @@ -66,7 +66,7 @@ public function testModifyStyles() $terminal = static::createMock(TerminalInterface::class); $terminal - ->expects($this->once()) + ->expects($this->any()) ->method('getWidth') ->will($this->returnValue(200)); diff --git a/test/CliMenuTest.php b/test/CliMenuTest.php index b3809e90..7f86d076 100644 --- a/test/CliMenuTest.php +++ b/test/CliMenuTest.php @@ -105,18 +105,6 @@ private function getTestFile() */ private function getStyle(TerminalInterface $terminal) { - return new MenuStyle( - 'blue', - 'white', - 100, - 2, - 2, - '○', - '●', - '✔', - false, - '=', - $terminal - ); + return new MenuStyle($terminal); } } diff --git a/test/res/testReDrawReDrawsImmediately.txt b/test/res/testReDrawReDrawsImmediately.txt index f2757f80..4ace7f16 100644 --- a/test/res/testReDrawReDrawsImmediately.txt +++ b/test/res/testReDrawReDrawsImmediately.txt @@ -1,18 +1,18 @@ -   -  PHP School FTW  -  ==================================  -  ● Item 1  -   +   +  PHP School FTW  +  ==========================================  +  ● Item 1  +   -   -  PHP School FTW  -  ==================================  -  ● Item 1  -   +   +  PHP School FTW  +  ==========================================  +  ● Item 1  +   diff --git a/test/res/testSimpleOpenClose.txt b/test/res/testSimpleOpenClose.txt index a91118d6..dd97f8c2 100644 --- a/test/res/testSimpleOpenClose.txt +++ b/test/res/testSimpleOpenClose.txt @@ -1,9 +1,9 @@ -   -  PHP School FTW  -  ==================================  -  ● Item 1  -   +   +  PHP School FTW  +  ==========================================  +  ● Item 1  +   From 7924714477cf8130723458f3d87177782be1492c Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Tue, 4 Oct 2016 23:16:19 +0100 Subject: [PATCH 2/2] Fix typehint --- src/CliMenuBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index f5205934..240c4b76 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -55,7 +55,7 @@ class CliMenuBuilder private $menuItems = []; /** - * @var MenuStyle + * @var array */ private $style;