diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index d088a6a2..7e44aa71 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -25,8 +25,8 @@ jobs: - name: Install Dependencies run: composer update --no-interaction --no-progress - - name: Run PHPCS - run: composer run cs + - name: Run cs fixer + run: composer run cs-dry-run - name: Run PHPStan run: composer run static diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cb41efbb..d0914321 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,7 +27,7 @@ jobs: echo "::add-matcher::${{ runner.tool_cache }}/php.json" echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - name: Install dependencies + - name: Install cdependencies run: composer update --no-interaction --no-progress - name: Tests diff --git a/.gitignore b/.gitignore index 99b8f988..df4d9ca1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vendor/ /composer.lock /.phpunit.result.cache /.phpunit.cache +/.php-cs-fixer.cache \ No newline at end of file diff --git a/composer.json b/composer.json index fc604e2e..03ae4038 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,8 @@ ], "require-dev": { "phpunit/phpunit": "^11.5", - "squizlabs/php_codesniffer": "^3.2", - "phpstan/phpstan": "^1.2.0" + "phpstan/phpstan": "^2.1", + "friendsofphp/php-cs-fixer": "*" }, "require": { "php" : ">=8.2", @@ -37,9 +37,13 @@ "psr-4": { "PhpSchool\\CliMenuTest\\": "test/" } }, "scripts" : { + "cs-dry-run" : [ + "php-cs-fixer check src --rules=@PSR12", + "php-cs-fixer check test --rules=@PSR12" + ], "cs" : [ - "phpcs src --standard=PSR2", - "phpcs test --standard=PSR2" + "php-cs-fixer fix src --rules=@PSR12", + "php-cs-fixer fix test --rules=@PSR12" ], "static" : [ "phpstan analyse src --level=7" diff --git a/phpstan.neon b/phpstan.neon index 9d52fd98..e69de29b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,2 +0,0 @@ -parameters: - checkMissingIterableValueType: false diff --git a/src/Action/ExitAction.php b/src/Action/ExitAction.php index e4cdbfa0..b3b9ca60 100644 --- a/src/Action/ExitAction.php +++ b/src/Action/ExitAction.php @@ -1,4 +1,5 @@ close(); } diff --git a/src/Action/GoBackAction.php b/src/Action/GoBackAction.php index 3844d5c4..3ba87966 100644 --- a/src/Action/GoBackAction.php +++ b/src/Action/GoBackAction.php @@ -1,4 +1,5 @@ getParent()) { $menu->closeThis(); diff --git a/src/Builder/CliMenuBuilder.php b/src/Builder/CliMenuBuilder.php index c9a0928e..4246d949 100644 --- a/src/Builder/CliMenuBuilder.php +++ b/src/Builder/CliMenuBuilder.php @@ -1,4 +1,5 @@ , style: ItemStyle}> */ - private $extraItemStyles = []; + private array $extraItemStyles = []; - /** - * @var bool - */ - private $subMenu = false; + private bool $subMenu = false; public function __construct(?Terminal $terminal = null) { @@ -100,7 +74,7 @@ public function __construct(?Terminal $terminal = null) $this->menu = new CliMenu(null, [], $this->terminal, $this->style); } - public static function newSubMenu(Terminal $terminal) : self + public static function newSubMenu(Terminal $terminal): self { $instance = new self($terminal); $instance->subMenu = true; @@ -108,14 +82,14 @@ public static function newSubMenu(Terminal $terminal) : self return $instance; } - public function setTitle(string $title) : self + public function setTitle(string $title): self { $this->menu->setTitle($title); return $this; } - public function addMenuItem(MenuItemInterface $item) : self + public function addMenuItem(MenuItemInterface $item): self { $this->menu->addItem($item); @@ -129,13 +103,16 @@ public function addItem( callable $itemCallable, bool $showItemExtra = false, bool $disabled = false - ) : self { + ): self { $this->addMenuItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); return $this; } - public function addItems(array $items) : self + /** + * @param list $items + */ + public function addItems(array $items): self { foreach ($items as $item) { $this->addItem(...$item); @@ -149,12 +126,15 @@ public function addCheckboxItem( callable $itemCallable, bool $showItemExtra = false, bool $disabled = false - ) : self { + ): self { $this->addMenuItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)); return $this; } + /** + * @param list $items + */ public function addCheckboxItems(array $items): self { foreach ($items as $item) { @@ -169,12 +149,15 @@ public function addRadioItem( callable $itemCallable, bool $showItemExtra = false, bool $disabled = false - ) : self { + ): self { $this->addMenuItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled)); return $this; } + /** + * @param list $items + */ public function addRadioItems(array $items): self { foreach ($items as $item) { @@ -184,28 +167,28 @@ public function addRadioItems(array $items): self return $this; } - public function addStaticItem(string $text) : self + public function addStaticItem(string $text): self { $this->addMenuItem(new StaticItem($text)); return $this; } - public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self + public function addLineBreak(string $breakChar = ' ', int $lines = 1): self { $this->addMenuItem(new LineBreakItem($breakChar, $lines)); return $this; } - public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self + public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = ''): self { $this->addMenuItem(new AsciiArtItem($art, $position, $alt)); return $this; } - public function addSubMenu(string $text, callable $callback) : self + public function addSubMenu(string $text, callable $callback): self { $builder = self::newSubMenu($this->terminal); @@ -233,7 +216,7 @@ public function addSubMenu(string $text, callable $callback) : self return $this; } - public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : self + public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder): self { $menu = $builder->build(); $menu->setParent($this->menu); @@ -249,7 +232,7 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s return $this; } - public function enableAutoShortcuts(?string $regex = null) : self + public function enableAutoShortcuts(?string $regex = null): self { $this->autoShortcuts = true; @@ -260,7 +243,7 @@ public function enableAutoShortcuts(?string $regex = null) : self return $this; } - private function extractShortcut(string $title) : ?string + private function extractShortcut(string $title): ?string { preg_match($this->autoShortcutsRegex, $title, $match); @@ -275,14 +258,14 @@ private function extractShortcut(string $title) : ?string return strtolower($match[1]); } - private function processItemShortcut(MenuItemInterface $item) : void + private function processItemShortcut(MenuItemInterface $item): void { $this->processIndividualShortcut($item, function (CliMenu $menu) use ($item) { $menu->executeAsSelected($item); }); } - private function processSplitItemShortcuts(SplitItem $splitItem) : void + private function processSplitItemShortcuts(SplitItem $splitItem): void { foreach ($splitItem->getItems() as $item) { $this->processIndividualShortcut($item, function (CliMenu $menu) use ($splitItem, $item) { @@ -301,7 +284,7 @@ private function processSplitItemShortcuts(SplitItem $splitItem) : void } } - private function processIndividualShortcut(MenuItemInterface $item, callable $callback) : void + private function processIndividualShortcut(MenuItemInterface $item, callable $callback): void { if (!$this->autoShortcuts) { return; @@ -315,7 +298,7 @@ private function processIndividualShortcut(MenuItemInterface $item, callable $ca } } - public function addSplitItem(callable $callback) : self + public function addSplitItem(callable $callback): self { $builder = new SplitItemBuilder($this->menu); @@ -341,7 +324,7 @@ public function addSplitItem(callable $callback) : self * * @throws \InvalidArgumentException */ - public function disableMenu() : self + public function disableMenu(): self { if (!$this->subMenu) { throw new \InvalidArgumentException( @@ -354,82 +337,82 @@ public function disableMenu() : self return $this; } - public function isMenuDisabled() : bool + public function isMenuDisabled(): bool { return $this->disabled; } - public function setGoBackButtonText(string $goBackButtonTest) : self + public function setGoBackButtonText(string $goBackButtonTest): self { $this->goBackButtonText = $goBackButtonTest; return $this; } - public function setExitButtonText(string $exitButtonText) : self + public function setExitButtonText(string $exitButtonText): self { $this->exitButtonText = $exitButtonText; return $this; } - public function setBackgroundColour(string $colour, ?string $fallback = null) : self + public function setBackgroundColour(string $colour, ?string $fallback = null): self { $this->style->setBg($colour, $fallback); return $this; } - public function setForegroundColour(string $colour, ?string $fallback = null) : self + public function setForegroundColour(string $colour, ?string $fallback = null): self { $this->style->setFg($colour, $fallback); return $this; } - public function setWidth(int $width) : self + public function setWidth(int $width): self { $this->style->setWidth($width); return $this; } - public function setPadding(int $topBottom, ?int $leftRight = null) : self + public function setPadding(int $topBottom, ?int $leftRight = null): self { $this->style->setPadding($topBottom, $leftRight); return $this; } - public function setPaddingTopBottom(int $topBottom) : self + public function setPaddingTopBottom(int $topBottom): self { $this->style->setPaddingTopBottom($topBottom); return $this; } - public function setPaddingLeftRight(int $leftRight) : self + public function setPaddingLeftRight(int $leftRight): self { $this->style->setPaddingLeftRight($leftRight); return $this; } - public function setMarginAuto() : self + public function setMarginAuto(): self { $this->style->setMarginAuto(); return $this; } - public function setMargin(int $margin) : self + public function setMargin(int $margin): self { $this->style->setMargin($margin); return $this; } - public function setItemExtra(string $extra) : self + public function setItemExtra(string $extra): self { $this->style->setItemExtra($extra); $this->getSelectableStyle()->setItemExtra($extra); @@ -440,7 +423,7 @@ public function setItemExtra(string $extra) : self return $this; } - public function setTitleSeparator(string $separator) : self + public function setTitleSeparator(string $separator): self { $this->style->setTitleSeparator($separator); @@ -452,77 +435,80 @@ public function setTitleSeparator(string $separator) : self * @param int|string|null $bottom * @param int|string|null $left */ - public function setBorder(int $top, $right = null, $bottom = null, $left = null, ?string $colour = null) : self + public function setBorder(int $top, $right = null, $bottom = null, $left = null, ?string $colour = null): self { $this->style->setBorder($top, $right, $bottom, $left, $colour); return $this; } - public function setBorderTopWidth(int $width) : self + public function setBorderTopWidth(int $width): self { $this->style->setBorderTopWidth($width); return $this; } - public function setBorderRightWidth(int $width) : self + public function setBorderRightWidth(int $width): self { $this->style->setBorderRightWidth($width); return $this; } - public function setBorderBottomWidth(int $width) : self + public function setBorderBottomWidth(int $width): self { $this->style->setBorderBottomWidth($width); return $this; } - public function setBorderLeftWidth(int $width) : self + public function setBorderLeftWidth(int $width): self { $this->style->setBorderLeftWidth($width); return $this; } - public function setBorderColour(string $colour, ?string $fallback = null) : self + public function setBorderColour(string $colour, ?string $fallback = null): self { $this->style->setBorderColour($colour, $fallback); return $this; } - public function getStyle() : MenuStyle + public function getStyle(): MenuStyle { return $this->style; } - public function getTerminal() : Terminal + public function getTerminal(): Terminal { return $this->terminal; } - private function getDefaultItems() : array + /** + * @return list + */ + private function getDefaultItems(): array { $actions = []; if ($this->subMenu) { - $actions[] = new SelectableItem($this->goBackButtonText, new GoBackAction); + $actions[] = new SelectableItem($this->goBackButtonText, new GoBackAction()); } - $actions[] = new SelectableItem($this->exitButtonText, new ExitAction); + $actions[] = new SelectableItem($this->exitButtonText, new ExitAction()); return $actions; } - public function disableDefaultItems() : self + public function disableDefaultItems(): self { $this->disableDefaultItems = true; return $this; } - public function displayExtra() : self + public function displayExtra(): self { $this->style->setDisplaysExtra(true); $this->getSelectableStyle()->setDisplaysExtra(true); @@ -532,14 +518,17 @@ public function displayExtra() : self return $this; } - private function itemsHaveExtra(array $items) : bool + /** + * @param array $items + */ + private function itemsHaveExtra(array $items): bool { return !empty(array_filter($items, function (MenuItemInterface $item) { return $item->showsItemExtra(); })); } - public function build() : CliMenu + public function build(): CliMenu { if (!$this->disableDefaultItems) { $this->menu->addItems($this->getDefaultItems()); @@ -556,98 +545,101 @@ public function build() : CliMenu return $this->menu; } - public function getDefaultStyle() : DefaultStyle + public function getDefaultStyle(): DefaultStyle { $style = $this->menu->getItemStyle(DefaultStyle::class); assert($style instanceof DefaultStyle); return $style; } - public function setDefaultStyle(DefaultStyle $style) : self + public function setDefaultStyle(DefaultStyle $style): self { $this->menu->setItemStyle($style, DefaultStyle::class); return $this; } - public function modifyDefaultStyle(callable $itemCallable) : self + public function modifyDefaultStyle(callable $itemCallable): self { $itemCallable($this->getDefaultStyle()); return $this; } - public function getSelectableStyle() : SelectableStyle + public function getSelectableStyle(): SelectableStyle { $style = $this->menu->getItemStyle(SelectableStyle::class); assert($style instanceof SelectableStyle); return $style; } - public function setSelectableStyle(SelectableStyle $style) : self + public function setSelectableStyle(SelectableStyle $style): self { $this->menu->setItemStyle($style, SelectableStyle::class); return $this; } - public function modifySelectableStyle(callable $itemCallable) : self + public function modifySelectableStyle(callable $itemCallable): self { $itemCallable($this->getSelectableStyle()); return $this; } - public function getCheckboxStyle() : CheckboxStyle + public function getCheckboxStyle(): CheckboxStyle { $style = $this->menu->getItemStyle(CheckboxStyle::class); assert($style instanceof CheckboxStyle); return $style; } - public function setCheckboxStyle(CheckboxStyle $style) : self + public function setCheckboxStyle(CheckboxStyle $style): self { $this->menu->setItemStyle($style, CheckboxStyle::class); return $this; } - public function modifyCheckboxStyle(callable $itemCallable) : self + public function modifyCheckboxStyle(callable $itemCallable): self { $itemCallable($this->getCheckboxStyle()); return $this; } - public function getRadioStyle() : RadioStyle + public function getRadioStyle(): RadioStyle { $style = $this->menu->getItemStyle(RadioStyle::class); assert($style instanceof RadioStyle); return $style; } - public function setRadioStyle(RadioStyle $style) : self + public function setRadioStyle(RadioStyle $style): self { $this->menu->setItemStyle($style, RadioItem::class); return $this; } - public function modifyRadioStyle(callable $itemCallable) : self + public function modifyRadioStyle(callable $itemCallable): self { $itemCallable($this->getRadioStyle()); return $this; } - public function modifyStyle(string $styleClass, callable $itemCallable) : self + public function modifyStyle(string $styleClass, callable $itemCallable): self { $itemCallable($this->menu->getItemStyle($styleClass)); return $this; } - public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : self + /** + * @param class-string $itemClass + */ + public function registerItemStyle(string $itemClass, ItemStyle $itemStyle): self { $this->menu->getStyleLocator() ->registerItemStyle($itemClass, $itemStyle); diff --git a/src/Builder/SplitItemBuilder.php b/src/Builder/SplitItemBuilder.php index 34a24ba4..fa6ab64e 100644 --- a/src/Builder/SplitItemBuilder.php +++ b/src/Builder/SplitItemBuilder.php @@ -1,4 +1,5 @@ */ class SplitItemBuilder { - /** - * @var CliMenu - */ - private $menu; + private CliMenu $menu; - /** - * @var SplitItem - */ - private $splitItem; + private SplitItem $splitItem; /** - * Whether or not to auto create keyboard shortcuts for items + * Whether to auto create keyboard shortcuts for items * when they contain square brackets. Eg: [M]y item - * - * @var bool */ - private $autoShortcuts = false; + private bool $autoShortcuts = false; /** * Regex to auto match for shortcuts defaults to looking * for a single character encased in square brackets - * - * @var string */ - private $autoShortcutsRegex = '/\[(.)\]/'; + private string $autoShortcutsRegex = '/\[(.)\]/'; /** - * @var array + * @var list, style: ItemStyle}> */ - private $extraItemStyles = []; + private array $extraItemStyles = []; public function __construct(CliMenu $menu) { @@ -62,7 +55,7 @@ public function addItem( callable $itemCallable, bool $showItemExtra = false, bool $disabled = false - ) : self { + ): self { $this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); return $this; @@ -73,7 +66,7 @@ public function addCheckboxItem( callable $itemCallable, bool $showItemExtra = false, bool $disabled = false - ) : self { + ): self { $this->splitItem->addItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)); return $this; @@ -84,27 +77,27 @@ public function addRadioItem( callable $itemCallable, bool $showItemExtra = false, bool $disabled = false - ) : self { + ): self { $this->splitItem->addItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled)); return $this; } - public function addStaticItem(string $text) : self + public function addStaticItem(string $text): self { $this->splitItem->addItem(new StaticItem($text)); return $this; } - public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self + public function addLineBreak(string $breakChar = ' ', int $lines = 1): self { $this->splitItem->addItem(new LineBreakItem($breakChar, $lines)); return $this; } - public function addSubMenu(string $text, \Closure $callback) : self + public function addSubMenu(string $text, \Closure $callback): self { $builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal()); @@ -130,21 +123,21 @@ public function addSubMenu(string $text, \Closure $callback) : self return $this; } - public function addMenuItem(MenuItemInterface $item) : self + public function addMenuItem(MenuItemInterface $item): self { $this->splitItem->addItem($item); return $this; } - public function setGutter(int $gutter) : self + public function setGutter(int $gutter): self { $this->splitItem->setGutter($gutter); return $this; } - public function enableAutoShortcuts(?string $regex = null) : self + public function enableAutoShortcuts(?string $regex = null): self { $this->autoShortcuts = true; @@ -155,14 +148,17 @@ public function enableAutoShortcuts(?string $regex = null) : self return $this; } - public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : self + /** + * @param class-string $itemClass + */ + public function registerItemStyle(string $itemClass, ItemStyle $itemStyle): self { $this->extraItemStyles[] = ['class' => $itemClass, 'style' => $itemStyle]; return $this; } - public function build() : SplitItem + public function build(): SplitItem { return $this->splitItem; } diff --git a/src/CliMenu.php b/src/CliMenu.php index 40891dd7..87a9603d 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -1,4 +1,5 @@ */ - protected $items = []; + protected array $items = []; - /** - * @var int|null - */ - protected $selectedItem; + protected int|null $selectedItem = null; - /** - * @var bool - */ - protected $open = false; + protected bool $open = false; - /** - * @var CliMenu|null - */ - protected $parent; + protected CliMenu|null $parent = null; /** - * @var array + * @var array */ - protected $defaultControlMappings = [ + protected array $defaultControlMappings = [ '^P' => InputCharacter::UP, 'k' => InputCharacter::UP, '^K' => InputCharacter::DOWN, @@ -87,15 +68,15 @@ class CliMenu ]; /** - * @var array + * @var array */ - protected $customControlMappings = []; + protected array $customControlMappings = []; + + private Frame $currentFrame; /** - * @var Frame + * @param list $items */ - private $currentFrame; - public function __construct( ?string $title, array $items, @@ -115,7 +96,7 @@ public function __construct( /** * Configure the terminal to work with CliMenu */ - protected function configureTerminal() : void + protected function configureTerminal(): void { $this->assertTerminalIsValidTTY(); @@ -128,7 +109,7 @@ protected function configureTerminal() : void /** * Revert changes made to the terminal */ - protected function tearDownTerminal() : void + protected function tearDownTerminal(): void { $this->terminal->restoreOriginalConfiguration(); $this->terminal->enableCanonicalMode(); @@ -136,39 +117,39 @@ protected function tearDownTerminal() : void $this->terminal->enableCursor(); } - private function assertTerminalIsValidTTY() : void + private function assertTerminalIsValidTTY(): void { if (!$this->terminal->isInteractive()) { throw new InvalidTerminalException('Terminal is not interactive (TTY)'); } } - public function setTitle(string $title) : void + public function setTitle(string $title): void { $this->title = $title; } - public function getTitle() : ?string + public function getTitle(): ?string { return $this->title; } - public function setParent(CliMenu $parent) : void + public function setParent(CliMenu $parent): void { $this->parent = $parent; } - public function getParent() : ?CliMenu + public function getParent(): ?CliMenu { return $this->parent; } - public function getTerminal() : Terminal + public function getTerminal(): Terminal { return $this->terminal; } - public function isOpen() : bool + public function isOpen(): bool { return $this->open; } @@ -176,7 +157,7 @@ public function isOpen() : bool /** * Add a new Item to the menu */ - public function addItem(MenuItemInterface $item) : void + public function addItem(MenuItemInterface $item): void { $this->items[] = $item; @@ -185,8 +166,10 @@ public function addItem(MenuItemInterface $item) : void /** * Add multiple Items to the menu + * + * @param list $items */ - public function addItems(array $items) : void + public function addItems(array $items): void { foreach ($items as $item) { $this->items[] = $item; @@ -197,8 +180,10 @@ public function addItems(array $items) : void /** * Set Items of the menu + * + * @param list $items */ - public function setItems(array $items) : void + public function setItems(array $items): void { $this->selectedItem = null; $this->items = $items; @@ -209,7 +194,7 @@ public function setItems(array $items) : void /** * Set the selected pointer to the first selectable item */ - private function selectFirstItem() : void + private function selectFirstItem(): void { if (null === $this->selectedItem) { foreach ($this->items as $key => $item) { @@ -224,15 +209,18 @@ private function selectFirstItem() : void /** * Disables the built-in VIM control mappings */ - public function disableDefaultControlMappings() : void + public function disableDefaultControlMappings(): void { $this->defaultControlMappings = []; } /** * Set default control mappings + * + * @param array $defaultControlMappings + * */ - public function setDefaultControlMappings(array $defaultControlMappings) : void + public function setDefaultControlMappings(array $defaultControlMappings): void { $this->defaultControlMappings = $defaultControlMappings; } @@ -240,7 +228,7 @@ public function setDefaultControlMappings(array $defaultControlMappings) : void /** * Adds a custom control mapping */ - public function addCustomControlMapping(string $input, callable $callable) : void + public function addCustomControlMapping(string $input, callable $callable): void { if (isset($this->defaultControlMappings[$input]) || isset($this->customControlMappings[$input])) { throw new \InvalidArgumentException('Cannot rebind this input'); @@ -249,15 +237,20 @@ public function addCustomControlMapping(string $input, callable $callable) : voi $this->customControlMappings[$input] = $callable; } - public function getCustomControlMappings() : array + /** + * @return array + */ + public function getCustomControlMappings(): array { return $this->customControlMappings; } /** * Shorthand function to add multiple custom control mapping at once + * + * @param array $map */ - public function addCustomControlMappings(array $map) : void + public function addCustomControlMappings(array $map): void { foreach ($map as $input => $callable) { $this->addCustomControlMapping($input, $callable); @@ -267,7 +260,7 @@ public function addCustomControlMappings(array $map) : void /** * Removes a custom control mapping */ - public function removeCustomControlMapping(string $input) : void + public function removeCustomControlMapping(string $input): void { if (!isset($this->customControlMappings[$input])) { throw new \InvalidArgumentException('This input is not registered'); @@ -279,7 +272,7 @@ public function removeCustomControlMapping(string $input) : void /** * Display menu and capture input */ - private function display() : void + private function display(): void { $this->draw(); @@ -325,7 +318,7 @@ private function display() : void /** * Move the selection in a given direction, up / down */ - protected function moveSelectionVertically(string $direction) : void + protected function moveSelectionVertically(string $direction): void { $itemKeys = array_keys($this->items); @@ -344,7 +337,7 @@ protected function moveSelectionVertically(string $direction) : void ? $this->selectedItem-- : $this->selectedItem++; - if ($this->selectedItem !== null && !array_key_exists($this->selectedItem, $this->items)) { + if (!array_key_exists($this->selectedItem, $this->items)) { $this->selectedItem = $direction === 'UP' ? (int) end($itemKeys) : (int) reset($itemKeys); @@ -355,7 +348,7 @@ protected function moveSelectionVertically(string $direction) : void /** * Move the selection in a given direction, left / right */ - protected function moveSelectionHorizontally(string $direction) : void + protected function moveSelectionHorizontally(string $direction): void { if (!$this->items[$this->selectedItem] instanceof SplitItem) { return; @@ -396,7 +389,7 @@ protected function moveSelectionHorizontally(string $direction) : void * * @return bool */ - private function canSelect() : bool + private function canSelect(): bool { return $this->items[$this->selectedItem]->canSelect(); } @@ -405,7 +398,7 @@ private function canSelect() : bool * Retrieve the item the user actually selected * */ - public function getSelectedItem() : MenuItemInterface + public function getSelectedItem(): MenuItemInterface { if (null === $this->selectedItem) { throw new \RuntimeException('No selected item'); @@ -417,7 +410,7 @@ public function getSelectedItem() : MenuItemInterface : $item; } - public function setSelectedItem(MenuItemInterface $item) : void + public function setSelectedItem(MenuItemInterface $item): void { $key = array_search($item, $this->items, true); @@ -428,7 +421,7 @@ public function setSelectedItem(MenuItemInterface $item) : void $this->selectedItem = (int) $key; } - public function getSelectedItemIndex() : int + public function getSelectedItemIndex(): int { if (null === $this->selectedItem) { throw new \RuntimeException('No selected item'); @@ -437,7 +430,7 @@ public function getSelectedItemIndex() : int return $this->selectedItem; } - public function getItemByIndex(int $index) : MenuItemInterface + public function getItemByIndex(int $index): MenuItemInterface { if (!isset($this->items[$index])) { throw new \RuntimeException('Item with index does not exist'); @@ -446,7 +439,7 @@ public function getItemByIndex(int $index) : MenuItemInterface return $this->items[$index]; } - public function executeAsSelected(MenuItemInterface $item) : void + public function executeAsSelected(MenuItemInterface $item): void { $current = $this->items[$this->selectedItem]; $this->setSelectedItem($item); @@ -457,7 +450,7 @@ public function executeAsSelected(MenuItemInterface $item) : void /** * Execute the current item */ - protected function executeCurrentItem() : void + protected function executeCurrentItem(): void { $item = $this->getSelectedItem(); @@ -476,7 +469,7 @@ protected function executeCurrentItem() : void * * Redraw the menu */ - public function redraw(bool $clear = false) : void + public function redraw(bool $clear = false): void { if ($clear) { $this->terminal->clear(); @@ -486,19 +479,19 @@ public function redraw(bool $clear = false) : void $this->draw(); } - private function assertOpen() : void + private function assertOpen(): void { if (!$this->isOpen()) { - throw new MenuNotOpenException; + throw new MenuNotOpenException(); } } /** * Draw the menu to STDOUT */ - protected function draw() : void + protected function draw(): void { - $frame = new Frame; + $frame = new Frame(); $frame->newLine(2); @@ -544,8 +537,10 @@ protected function draw() : void /** * Draw a menu item + * + * @return list */ - protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) : array + protected function drawMenuItem(MenuItemInterface $item, bool $selected = false): array { $rows = $item->getRows($this->style, $selected); @@ -588,7 +583,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) /** * @throws InvalidTerminalException */ - public function open() : void + public function open(): void { if ($this->isOpen()) { return; @@ -608,7 +603,7 @@ public function open() : void * * @throws InvalidTerminalException */ - public function close() : void + public function close(): void { $menu = $this; @@ -620,7 +615,7 @@ public function close() : void $this->tearDownTerminal(); } - public function closeThis() : void + public function closeThis(): void { $this->terminal->clean(); $this->terminal->moveCursorToTop(); @@ -630,12 +625,12 @@ public function closeThis() : void /** * @return MenuItemInterface[] */ - public function getItems() : array + public function getItems(): array { return $this->items; } - public function removeItem(MenuItemInterface $item) : void + public function removeItem(MenuItemInterface $item): void { $key = array_search($item, $this->items, true); @@ -652,37 +647,40 @@ public function removeItem(MenuItemInterface $item) : void } } - public function getStyle() : MenuStyle + public function getStyle(): MenuStyle { return $this->style; } - public function setStyle(MenuStyle $style) : void + public function setStyle(MenuStyle $style): void { $this->style = $style; } - public function setItemStyle(ItemStyle $style, string $styleClass) : void + /** + * @param class-string $styleClass + */ + public function setItemStyle(ItemStyle $style, string $styleClass): void { $this->itemStyleLocator->setStyle($style, $styleClass); } - public function getItemStyle(string $styleClass) : ItemStyle + public function getItemStyle(string $styleClass): ItemStyle { return $this->itemStyleLocator->getStyle($styleClass); } - public function getItemStyleForItem(MenuItemInterface $item) : ItemStyle + public function getItemStyleForItem(MenuItemInterface $item): ItemStyle { return $this->itemStyleLocator->getStyleForMenuItem($item); } - public function getStyleLocator() : Locator + public function getStyleLocator(): Locator { return $this->itemStyleLocator; } - public function importStyles(CliMenu $menu) : void + public function importStyles(CliMenu $menu): void { if (!$this->style->hasChangedFromDefaults()) { $this->style = $menu->style; @@ -691,12 +689,12 @@ public function importStyles(CliMenu $menu) : void $this->itemStyleLocator->importFrom($menu->itemStyleLocator); } - public function getCurrentFrame() : Frame + public function getCurrentFrame(): Frame { return $this->currentFrame; } - public function flash(string $text, ?MenuStyle $style = null) : Flash + public function flash(string $text, ?MenuStyle $style = null): Flash { $this->guardSingleLine($text); @@ -707,7 +705,7 @@ public function flash(string $text, ?MenuStyle $style = null) : Flash return new Flash($this, $style, $this->terminal, $text); } - public function confirm(string $text, ?MenuStyle $style = null) : Confirm + public function confirm(string $text, ?MenuStyle $style = null): Confirm { $this->guardSingleLine($text); @@ -718,7 +716,7 @@ public function confirm(string $text, ?MenuStyle $style = null) : Confirm return new Confirm($this, $style, $this->terminal, $text); } - public function cancellableConfirm(string $text, ?MenuStyle $style = null) : CancellableConfirm + public function cancellableConfirm(string $text, ?MenuStyle $style = null): CancellableConfirm { $this->guardSingleLine($text); @@ -729,7 +727,7 @@ public function cancellableConfirm(string $text, ?MenuStyle $style = null) : Can return new CancellableConfirm($this, $style, $this->terminal, $text); } - public function askNumber(?MenuStyle $style = null) : Number + public function askNumber(?MenuStyle $style = null): Number { $this->assertOpen(); @@ -740,7 +738,7 @@ public function askNumber(?MenuStyle $style = null) : Number return new Number(new InputIO($this, $this->terminal), $style); } - public function askText(?MenuStyle $style = null) : Text + public function askText(?MenuStyle $style = null): Text { $this->assertOpen(); @@ -751,7 +749,7 @@ public function askText(?MenuStyle $style = null) : Text return new Text(new InputIO($this, $this->terminal), $style); } - public function askPassword(?MenuStyle $style = null) : Password + public function askPassword(?MenuStyle $style = null): Password { $this->assertOpen(); @@ -762,14 +760,14 @@ public function askPassword(?MenuStyle $style = null) : Password return new Password(new InputIO($this, $this->terminal), $style); } - private function guardSingleLine(string $text) : void + private function guardSingleLine(string $text): void { if (strpos($text, "\n") !== false) { - throw new \InvalidArgumentException; + throw new \InvalidArgumentException(); } } - public function propagateStyles() : void + public function propagateStyles(): void { collect($this->items) ->filter(function (int $k, MenuItemInterface $item) { diff --git a/src/Dialogue/CancellableConfirm.php b/src/Dialogue/CancellableConfirm.php index 3f93abde..e8fbca2a 100644 --- a/src/Dialogue/CancellableConfirm.php +++ b/src/Dialogue/CancellableConfirm.php @@ -1,4 +1,5 @@ drawDialog($confirmText, $cancelText); diff --git a/src/Dialogue/Confirm.php b/src/Dialogue/Confirm.php index 8912b807..f846ea55 100644 --- a/src/Dialogue/Confirm.php +++ b/src/Dialogue/Confirm.php @@ -1,4 +1,5 @@ assertMenuOpen(); diff --git a/src/Dialogue/Dialogue.php b/src/Dialogue/Dialogue.php index 22122c23..ecf3ebdb 100644 --- a/src/Dialogue/Dialogue.php +++ b/src/Dialogue/Dialogue.php @@ -1,4 +1,5 @@ parentMenu->isOpen()) { - throw new MenuNotOpenException; + throw new MenuNotOpenException(); } } /** * Calculate the co-ordinates to write the messages */ - protected function calculateCoordinates() : void + protected function calculateCoordinates(): void { //y $textLines = count(explode("\n", $this->text)) + 2; @@ -91,7 +71,7 @@ protected function calculateCoordinates() : void /** * Write an empty row */ - protected function emptyRow() : void + protected function emptyRow(): void { $this->write( sprintf( @@ -108,13 +88,13 @@ protected function emptyRow() : void /** * Write some text at a particular column */ - protected function write(string $text, ?int $column = null) : void + protected function write(string $text, ?int $column = null): void { $this->terminal->moveCursorToColumn($column ?: $this->x); $this->terminal->write($text); } - public function getStyle() : MenuStyle + public function getStyle(): MenuStyle { return $this->style; } diff --git a/src/Dialogue/Flash.php b/src/Dialogue/Flash.php index 739a8943..6d64a584 100644 --- a/src/Dialogue/Flash.php +++ b/src/Dialogue/Flash.php @@ -1,4 +1,5 @@ assertMenuOpen(); diff --git a/src/Exception/CannotShrinkMenuException.php b/src/Exception/CannotShrinkMenuException.php index 15bd2c47..8a8c5873 100644 --- a/src/Exception/CannotShrinkMenuException.php +++ b/src/Exception/CannotShrinkMenuException.php @@ -8,7 +8,7 @@ class CannotShrinkMenuException extends InvalidArgumentException { - public static function fromMarginAndTerminalWidth(int $margin, int $terminalWidth) : self + public static function fromMarginAndTerminalWidth(int $margin, int $terminalWidth): self { return new self( sprintf( diff --git a/src/Exception/InvalidShortcutException.php b/src/Exception/InvalidShortcutException.php index 815338ef..64cebd5b 100644 --- a/src/Exception/InvalidShortcutException.php +++ b/src/Exception/InvalidShortcutException.php @@ -1,4 +1,5 @@ */ - private $rows = []; + private array $rows = []; - public function newLine(int $count = 1) : void + public function newLine(int $count = 1): void { foreach (range(1, $count) as $i) { $this->rows[] = "\n"; } } - public function addRows(array $rows = []) : void + /** + * @param list $rows + */ + public function addRows(array $rows = []): void { foreach ($rows as $row) { $this->rows[] = $row; } } - public function addRow(string $row) : void + public function addRow(string $row): void { $this->rows[] = $row; } - public function count() : int + public function count(): int { return count($this->rows); } - public function getRows() : array + /** + * @return list + */ + public function getRows(): array { return $this->rows; } diff --git a/src/Input/Input.php b/src/Input/Input.php index 22e94f52..a479082c 100644 --- a/src/Input/Input.php +++ b/src/Input/Input.php @@ -1,4 +1,5 @@ parentMenu = $parentMenu; } - public function collect(Input $input) : InputResult + public function collect(Input $input): InputResult { $this->drawInput($input, $input->getPlaceholderText()); $inputValue = $input->getPlaceholderText(); $havePlaceHolderValue = !empty($inputValue); - + $originalValue = $inputValue; $reader = new NonCanonicalReader($this->terminal); @@ -74,6 +69,7 @@ public function collect(Input $input) : InputResult continue 2; } + // no break case InputCharacter::BACKSPACE: if (!empty($inputValue)) { $inputValue = substr($inputValue, 0, -1); @@ -93,7 +89,7 @@ public function collect(Input $input) : InputResult } } - public function registerControlCallback(string $control, callable $callback) : void + public function registerControlCallback(string $control, callable $callback): void { if (!isset($this->callbacks[$control])) { $this->callbacks[$control] = []; @@ -102,7 +98,10 @@ public function registerControlCallback(string $control, callable $callback) : v $this->callbacks[$control][] = $callback; } - private function getInputWidth(array $lines) : int + /** + * @param non-empty-list $lines + */ + private function getInputWidth(array $lines): int { return max( array_map( @@ -111,24 +110,24 @@ function (string $line) { }, $lines ) - ) ? : 0; + ) ?: 0; } - private function calculateYPosition() : int + private function calculateYPosition(): int { $lines = 5; //1. empty 2. prompt text 3. empty 4. input 5. empty - return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1); + return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines / 2) + 1); } - private function calculateYPositionWithError() : int + private function calculateYPositionWithError(): int { $lines = 7; //1. empty 2. prompt text 3. empty 4. input 5. empty 6. error 7. empty - return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1); + return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines / 2) + 1); } - private function calculateXPosition(Input $input, string $userInput) : int + private function calculateXPosition(Input $input, string $userInput): int { $width = $this->getInputWidth( [ @@ -145,7 +144,7 @@ private function calculateXPosition(Input $input, string $userInput) : int return (int) ($parentHalfWidth - $halfWidth); } - private function drawLine(Input $input, string $userInput, string $text) : void + private function drawLine(Input $input, string $userInput, string $text): void { $this->terminal->moveCursorToColumn($this->calculateXPosition($input, $userInput)); @@ -161,7 +160,7 @@ private function drawLine(Input $input, string $userInput, string $text) : void $this->terminal->write($line); } - private function drawCenteredLine(Input $input, string $userInput, string $text) : void + private function drawCenteredLine(Input $input, string $userInput, string $text): void { $width = $this->getInputWidth( [ @@ -187,7 +186,7 @@ private function drawCenteredLine(Input $input, string $userInput, string $text) ); } - private function drawEmptyLine(Input $input, string $userInput) : void + private function drawEmptyLine(Input $input, string $userInput): void { $width = $this->getInputWidth( [ @@ -204,7 +203,7 @@ private function drawEmptyLine(Input $input, string $userInput) : void ); } - private function drawInput(Input $input, string $userInput) : void + private function drawInput(Input $input, string $userInput): void { $this->terminal->moveCursorToRow($this->calculateYPosition()); @@ -215,7 +214,7 @@ private function drawInput(Input $input, string $userInput) : void $this->drawEmptyLine($input, $userInput); } - private function drawInputWithError(Input $input, string $userInput) : void + private function drawInputWithError(Input $input, string $userInput): void { $this->terminal->moveCursorToRow($this->calculateYPositionWithError()); @@ -232,7 +231,7 @@ private function drawInputWithError(Input $input, string $userInput) : void $this->drawEmptyLine($input, $userInput); } - private function drawTitle(Input $input, string $userInput) : void + private function drawTitle(Input $input, string $userInput): void { $this->drawCenteredLine( @@ -242,7 +241,7 @@ private function drawTitle(Input $input, string $userInput) : void ); } - private function drawInputField(Input $input, string $userInput) : void + private function drawInputField(Input $input, string $userInput): void { $this->drawCenteredLine( $input, diff --git a/src/Input/InputResult.php b/src/Input/InputResult.php index 28cb83ae..0362af25 100644 --- a/src/Input/InputResult.php +++ b/src/Input/InputResult.php @@ -1,4 +1,5 @@ input = $input; } - public function fetch() : string + public function fetch(): string { return $this->input; } diff --git a/src/Input/Number.php b/src/Input/Number.php index c7d741cc..2c9bd351 100644 --- a/src/Input/Number.php +++ b/src/Input/Number.php @@ -1,4 +1,5 @@ style = $style; } - public function setPromptText(string $promptText) : Input + public function setPromptText(string $promptText): Input { $this->promptText = $promptText; return $this; } - public function getPromptText() : string + public function getPromptText(): string { return $this->promptText; } - public function setValidationFailedText(string $validationFailedText) : Input + public function setValidationFailedText(string $validationFailedText): Input { $this->validationFailedText = $validationFailedText; return $this; } - public function getValidationFailedText() : string + public function getValidationFailedText(): string { return $this->validationFailedText; } - public function setPlaceholderText(string $placeholderText) : Input + public function setPlaceholderText(string $placeholderText): Input { $this->placeholderText = $placeholderText; return $this; } - public function getPlaceholderText() : string + public function getPlaceholderText(): string { return $this->placeholderText; } - public function setValidator(callable $validator) : Input + public function setValidator(callable $validator): Input { - $this->validator = $validator; - + if ($validator instanceof \Closure) { + $validator = $validator->bindTo($this); + } + + $this->validator = $validator(...); + return $this; } - public function ask() : InputResult + public function ask(): InputResult { $this->inputIO->registerControlCallback(InputCharacter::UP, function (string $input) { return $this->validate($input) ? (string) ((int) $input + 1) : $input; @@ -103,27 +90,23 @@ public function ask() : InputResult return $this->inputIO->collect($this); } - public function validate(string $input) : bool + public function validate(string $input): bool { if ($this->validator) { $validator = $this->validator; - - if ($validator instanceof \Closure) { - $validator = $validator->bindTo($this); - } - + return $validator($input); } return (bool) preg_match('/^-?\d+$/', $input); } - public function filter(string $value) : string + public function filter(string $value): string { return $value; } - public function getStyle() : MenuStyle + public function getStyle(): MenuStyle { return $this->style; } diff --git a/src/Input/Password.php b/src/Input/Password.php index a259bb0e..560f6b2b 100644 --- a/src/Input/Password.php +++ b/src/Input/Password.php @@ -1,4 +1,5 @@ style = $style; } - public function setPromptText(string $promptText) : Input + public function setPromptText(string $promptText): Input { $this->promptText = $promptText; return $this; } - public function getPromptText() : string + public function getPromptText(): string { return $this->promptText; } - public function setValidationFailedText(string $validationFailedText) : Input + public function setValidationFailedText(string $validationFailedText): Input { $this->validationFailedText = $validationFailedText; return $this; } - public function getValidationFailedText() : string + public function getValidationFailedText(): string { return $this->validationFailedText; } - public function setPlaceholderText(string $placeholderText) : Input + public function setPlaceholderText(string $placeholderText): Input { $this->placeholderText = $placeholderText; return $this; } - public function getPlaceholderText() : string + public function getPlaceholderText(): string { return $this->placeholderText; } - public function setValidator(callable $validator) : Input + public function setValidator(callable $validator): Input { - $this->validator = $validator; + if ($validator instanceof \Closure) { + $validator = $validator->bindTo($this); + } + + $this->validator = $validator(...); return $this; } - public function ask() : InputResult + public function ask(): InputResult { return $this->inputIO->collect($this); } - public function validate(string $input) : bool + public function validate(string $input): bool { if ($this->validator) { $validator = $this->validator; - if ($validator instanceof \Closure) { - $validator = $validator->bindTo($this); - } - return $validator($input); } return mb_strwidth($input) >= $this->passwordLength; } - public function filter(string $value) : string + public function filter(string $value): string { return str_repeat('*', mb_strwidth($value)); } - public function getStyle() : MenuStyle + public function getStyle(): MenuStyle { return $this->style; } - public function setPasswordLength(int $length) : int + public function setPasswordLength(int $length): int { return $this->passwordLength = $length; } diff --git a/src/Input/Text.php b/src/Input/Text.php index 8435892d..38c2bfbc 100644 --- a/src/Input/Text.php +++ b/src/Input/Text.php @@ -1,4 +1,5 @@ style = $style; } - public function setPromptText(string $promptText) : Input + public function setPromptText(string $promptText): Input { $this->promptText = $promptText; return $this; } - public function getPromptText() : string + public function getPromptText(): string { return $this->promptText; } - public function setValidationFailedText(string $validationFailedText) : Input + public function setValidationFailedText(string $validationFailedText): Input { $this->validationFailedText = $validationFailedText; return $this; } - public function getValidationFailedText() : string + public function getValidationFailedText(): string { return $this->validationFailedText; } - public function setPlaceholderText(string $placeholderText) : Input + public function setPlaceholderText(string $placeholderText): Input { $this->placeholderText = $placeholderText; return $this; } - public function getPlaceholderText() : string + public function getPlaceholderText(): string { return $this->placeholderText; } - public function setValidator(callable $validator) : Input + public function setValidator(callable $validator): Input { - $this->validator = $validator; - + if ($validator instanceof \Closure) { + $validator = $validator->bindTo($this); + } + + $this->validator = $validator(...); + return $this; } - public function ask() : InputResult + public function ask(): InputResult { return $this->inputIO->collect($this); } - public function validate(string $input) : bool + public function validate(string $input): bool { if ($this->validator) { $validator = $this->validator; - - if ($validator instanceof \Closure) { - $validator = $validator->bindTo($this); - } - + return $validator($input); } return !empty($input); } - public function filter(string $value) : string + public function filter(string $value): string { return $value; } - public function getStyle() : MenuStyle + public function getStyle(): MenuStyle { return $this->style; } diff --git a/src/MenuItem/AsciiArtItem.php b/src/MenuItem/AsciiArtItem.php index b3d7f47e..16563d48 100644 --- a/src/MenuItem/AsciiArtItem.php +++ b/src/MenuItem/AsciiArtItem.php @@ -1,4 +1,5 @@ artLength > $style->getContentWidth()) { $alternate = new StaticItem($this->alternateText); @@ -89,7 +75,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array /** * Can the item be selected */ - public function canSelect() : bool + public function canSelect(): bool { return false; } @@ -97,7 +83,7 @@ public function canSelect() : bool /** * Execute the items callable if required */ - public function getSelectAction() : ?callable + public function getSelectAction(): ?callable { return null; } @@ -105,7 +91,7 @@ public function getSelectAction() : ?callable /** * Return the raw string of text */ - public function getText() : string + public function getText(): string { return $this->text; } @@ -113,7 +99,7 @@ public function getText() : string /** * Set the raw string of text */ - public function setText(string $text) : void + public function setText(string $text): void { $this->text = implode("\n", array_map(function (string $line) { return rtrim($line, ' '); @@ -125,7 +111,7 @@ public function setText(string $text) : void /** * Calculate the length of the art */ - private function calculateArtLength() : void + private function calculateArtLength(): void { $this->artLength = (int) max(array_map('mb_strwidth', explode("\n", $this->text))); } @@ -133,17 +119,17 @@ private function calculateArtLength() : void /** * Return the length of the art */ - public function getArtLength() : int + public function getArtLength(): int { return $this->artLength; } - public function getPosition() : string + public function getPosition(): string { return $this->position; } - public function getAlternateText() : string + public function getAlternateText(): string { return $this->alternateText; } @@ -151,7 +137,7 @@ public function getAlternateText() : string /** * Whether or not the menu item is showing the menustyle extra value */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return false; } @@ -159,7 +145,7 @@ public function showsItemExtra() : bool /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { //noop } @@ -167,7 +153,7 @@ public function showItemExtra() : void /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { //noop } @@ -175,12 +161,12 @@ public function hideItemExtra() : void /** * @return DefaultStyle */ - public function getStyle() : ItemStyle + public function getStyle(): ItemStyle { return $this->style; } - public function setStyle(DefaultStyle $style) : void + public function setStyle(DefaultStyle $style): void { $this->style = $style; } diff --git a/src/MenuItem/CheckboxItem.php b/src/MenuItem/CheckboxItem.php index 6d0cead7..212d48ac 100644 --- a/src/MenuItem/CheckboxItem.php +++ b/src/MenuItem/CheckboxItem.php @@ -1,4 +1,5 @@ text = $text; - $this->selectAction = $selectAction; + $this->selectAction = $selectAction(...); $this->showItemExtra = $showItemExtra; $this->disabled = $disabled; @@ -59,7 +46,7 @@ public function __construct( /** * The output text for the item */ - public function getRows(MenuStyle $style, bool $selected = false) : array + public function getRows(MenuStyle $style, bool $selected = false): array { return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled); } @@ -67,7 +54,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array /** * Return the raw string of text */ - public function getText() : string + public function getText(): string { return $this->text; } @@ -75,7 +62,7 @@ public function getText() : string /** * Set the raw string of text */ - public function setText(string $text) : void + public function setText(string $text): void { $this->text = $text; } @@ -83,7 +70,7 @@ public function setText(string $text) : void /** * Execute the items callable if required */ - public function getSelectAction() : ?callable + public function getSelectAction(): ?callable { return function (CliMenu $cliMenu) { $this->toggle(); @@ -96,7 +83,7 @@ public function getSelectAction() : ?callable /** * Can the item be selected */ - public function canSelect() : bool + public function canSelect(): bool { return !$this->disabled; } @@ -104,7 +91,7 @@ public function canSelect() : bool /** * Whether or not we are showing item extra */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return $this->showItemExtra; } @@ -112,7 +99,7 @@ public function showsItemExtra() : bool /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { $this->showItemExtra = true; } @@ -120,7 +107,7 @@ public function showItemExtra() : void /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { $this->showItemExtra = false; } @@ -128,7 +115,7 @@ public function hideItemExtra() : void /** * Whether or not the item is checked */ - public function getChecked() : bool + public function getChecked(): bool { return $this->checked; } @@ -136,7 +123,7 @@ public function getChecked() : bool /** * Sets checked state to true */ - public function setChecked() : void + public function setChecked(): void { $this->checked = true; } @@ -144,7 +131,7 @@ public function setChecked() : void /** * Sets checked state to false */ - public function setUnchecked() : void + public function setUnchecked(): void { $this->checked = false; } @@ -152,7 +139,7 @@ public function setUnchecked() : void /** * Toggles checked state */ - public function toggle() : void + public function toggle(): void { $this->checked = !$this->checked; } @@ -160,12 +147,12 @@ public function toggle() : void /** * @return CheckboxStyle */ - public function getStyle() : ItemStyle + public function getStyle(): ItemStyle { return $this->style; } - public function setStyle(CheckboxStyle $style) : void + public function setStyle(CheckboxStyle $style): void { $this->style = $style; } diff --git a/src/MenuItem/LineBreakItem.php b/src/MenuItem/LineBreakItem.php index e9083552..6388bc0c 100644 --- a/src/MenuItem/LineBreakItem.php +++ b/src/MenuItem/LineBreakItem.php @@ -1,4 +1,5 @@ breakChar; } @@ -77,7 +69,7 @@ public function getText() : string /** * Set the raw string of text */ - public function setText(string $text) : void + public function setText(string $text): void { $this->breakChar = $text; } @@ -85,12 +77,12 @@ public function setText(string $text) : void /** * Whether or not the menu item is showing the menustyle extra value */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return false; } - public function getLines() : int + public function getLines(): int { return $this->lines; } @@ -98,7 +90,7 @@ public function getLines() : int /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { //noop } @@ -106,7 +98,7 @@ public function showItemExtra() : void /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { //noop } @@ -114,12 +106,12 @@ public function hideItemExtra() : void /** * @return DefaultStyle */ - public function getStyle() : ItemStyle + public function getStyle(): ItemStyle { return $this->style; } - public function setStyle(DefaultStyle $style) : void + public function setStyle(DefaultStyle $style): void { $this->style = $style; } diff --git a/src/MenuItem/MenuItemInterface.php b/src/MenuItem/MenuItemInterface.php index 81e7d7cc..955c3232 100644 --- a/src/MenuItem/MenuItemInterface.php +++ b/src/MenuItem/MenuItemInterface.php @@ -1,4 +1,5 @@ */ - public function getRows(MenuStyle $style, bool $selected = false) : array; + public function getRows(MenuStyle $style, bool $selected = false): array; /** * Return the raw string of text */ - public function getText() : string; + public function getText(): string; /** * Can the item be selected */ - public function canSelect() : bool; + public function canSelect(): bool; /** * Execute the items callable if required */ - public function getSelectAction() : ?callable; + public function getSelectAction(): ?callable; /** * Whether or not the menu item is showing the menustyle extra value */ - public function showsItemExtra() : bool; + public function showsItemExtra(): bool; /** * Enable showing item extra */ - public function showItemExtra() : void; + public function showItemExtra(): void; /** * Disable showing item extra */ - public function hideItemExtra() : void; + public function hideItemExtra(): void; /** * Get the items style object. Can and * should be subclassed to provide bespoke * behaviour. */ - public function getStyle() : ItemStyle; + public function getStyle(): ItemStyle; } diff --git a/src/MenuItem/MenuMenuItem.php b/src/MenuItem/MenuMenuItem.php index fee929aa..9ac8be0b 100644 --- a/src/MenuItem/MenuMenuItem.php +++ b/src/MenuItem/MenuMenuItem.php @@ -1,4 +1,5 @@ render($style, $this, $selected, $this->disabled); } @@ -61,7 +47,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array /** * Return the raw string of text */ - public function getText() : string + public function getText(): string { return $this->text; } @@ -69,7 +55,7 @@ public function getText() : string /** * Set the raw string of text */ - public function setText(string $text) : void + public function setText(string $text): void { $this->text = $text; } @@ -77,7 +63,7 @@ public function setText(string $text) : void /** * Execute the items callable if required */ - public function getSelectAction() : ?callable + public function getSelectAction(): ?callable { return function (CliMenu $menu) { $this->showSubMenu($menu); @@ -87,7 +73,7 @@ public function getSelectAction() : ?callable /** * Returns the sub menu */ - public function getSubMenu() : CliMenu + public function getSubMenu(): CliMenu { return $this->subMenu; } @@ -95,7 +81,7 @@ public function getSubMenu() : CliMenu /** * Display the sub menu */ - public function showSubMenu(CliMenu $parentMenu) : void + public function showSubMenu(CliMenu $parentMenu): void { $parentMenu->closeThis(); $this->subMenu->open(); @@ -104,7 +90,7 @@ public function showSubMenu(CliMenu $parentMenu) : void /** * Can the item be selected */ - public function canSelect() : bool + public function canSelect(): bool { return !$this->disabled; } @@ -112,7 +98,7 @@ public function canSelect() : bool /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { $this->showItemExtra = true; } @@ -120,7 +106,7 @@ public function showItemExtra() : void /** * Whether or not we are showing item extra */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return $this->showItemExtra; } @@ -128,7 +114,7 @@ public function showsItemExtra() : bool /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { $this->showItemExtra = false; } @@ -136,12 +122,12 @@ public function hideItemExtra() : void /** * @return SelectableStyle */ - public function getStyle() : ItemStyle + public function getStyle(): ItemStyle { return $this->style; } - public function setStyle(SelectableStyle $style) : void + public function setStyle(SelectableStyle $style): void { $this->style = $style; } diff --git a/src/MenuItem/PropagatesStyles.php b/src/MenuItem/PropagatesStyles.php index 5c412069..d9d58209 100644 --- a/src/MenuItem/PropagatesStyles.php +++ b/src/MenuItem/PropagatesStyles.php @@ -12,5 +12,5 @@ interface PropagatesStyles * Push the parents styles to any * child items or menus. */ - public function propagateStyles(CliMenu $parent) : void; + public function propagateStyles(CliMenu $parent): void; } diff --git a/src/MenuItem/RadioItem.php b/src/MenuItem/RadioItem.php index 69fcad94..92c5dfbe 100644 --- a/src/MenuItem/RadioItem.php +++ b/src/MenuItem/RadioItem.php @@ -1,4 +1,5 @@ text = $text; - $this->selectAction = $selectAction; + $this->selectAction = $selectAction(...); $this->showItemExtra = $showItemExtra; $this->disabled = $disabled; @@ -59,7 +43,7 @@ public function __construct( /** * The output text for the item */ - public function getRows(MenuStyle $style, bool $selected = false) : array + public function getRows(MenuStyle $style, bool $selected = false): array { return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled); } @@ -67,7 +51,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array /** * Return the raw string of text */ - public function getText() : string + public function getText(): string { return $this->text; } @@ -75,7 +59,7 @@ public function getText() : string /** * Set the raw string of text */ - public function setText(string $text) : void + public function setText(string $text): void { $this->text = $text; } @@ -83,7 +67,7 @@ public function setText(string $text) : void /** * Execute the items callable if required */ - public function getSelectAction() : ?callable + public function getSelectAction(): ?callable { return function (CliMenu $cliMenu) { $parentItem = $cliMenu->getItemByIndex($cliMenu->getSelectedItemIndex()); @@ -116,7 +100,7 @@ function (RadioItem $item) { /** * Can the item be selected */ - public function canSelect() : bool + public function canSelect(): bool { return !$this->disabled; } @@ -124,7 +108,7 @@ public function canSelect() : bool /** * Whether or not we are showing item extra */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return $this->showItemExtra; } @@ -132,7 +116,7 @@ public function showsItemExtra() : bool /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { $this->showItemExtra = true; } @@ -140,7 +124,7 @@ public function showItemExtra() : void /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { $this->showItemExtra = false; } @@ -148,7 +132,7 @@ public function hideItemExtra() : void /** * Whether or not the item is checked */ - public function getChecked() : bool + public function getChecked(): bool { return $this->checked; } @@ -156,7 +140,7 @@ public function getChecked() : bool /** * Sets checked state to true */ - public function setChecked() : void + public function setChecked(): void { $this->checked = true; } @@ -164,7 +148,7 @@ public function setChecked() : void /** * Sets checked state to false */ - public function setUnchecked() : void + public function setUnchecked(): void { $this->checked = false; } @@ -172,7 +156,7 @@ public function setUnchecked() : void /** * Toggles checked state */ - public function toggle() : void + public function toggle(): void { $this->checked = !$this->checked; } @@ -180,12 +164,12 @@ public function toggle() : void /** * @return RadioStyle */ - public function getStyle() : ItemStyle + public function getStyle(): ItemStyle { return $this->style; } - public function setStyle(RadioStyle $style) : void + public function setStyle(RadioStyle $style): void { $this->style = $style; } diff --git a/src/MenuItem/SelectableItem.php b/src/MenuItem/SelectableItem.php index d5364e8a..cab2a922 100644 --- a/src/MenuItem/SelectableItem.php +++ b/src/MenuItem/SelectableItem.php @@ -1,4 +1,5 @@ text = $text; - $this->selectAction = $selectAction; + $this->selectAction = $selectAction(...); $this->showItemExtra = $showItemExtra; $this->disabled = $disabled; @@ -56,7 +46,7 @@ public function __construct( /** * The output text for the item */ - public function getRows(MenuStyle $style, bool $selected = false) : array + public function getRows(MenuStyle $style, bool $selected = false): array { return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled); } @@ -64,7 +54,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array /** * Return the raw string of text */ - public function getText() : string + public function getText(): string { return $this->text; } @@ -72,7 +62,7 @@ public function getText() : string /** * Set the raw string of text */ - public function setText(string $text) : void + public function setText(string $text): void { $this->text = $text; } @@ -80,7 +70,7 @@ public function setText(string $text) : void /** * Execute the items callable if required */ - public function getSelectAction() : ?callable + public function getSelectAction(): ?callable { return $this->selectAction; } @@ -88,7 +78,7 @@ public function getSelectAction() : ?callable /** * Can the item be selected */ - public function canSelect() : bool + public function canSelect(): bool { return !$this->disabled; } @@ -96,7 +86,7 @@ public function canSelect() : bool /** * Whether or not we are showing item extra */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return $this->showItemExtra; } @@ -104,7 +94,7 @@ public function showsItemExtra() : bool /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { $this->showItemExtra = true; } @@ -112,7 +102,7 @@ public function showItemExtra() : void /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { $this->showItemExtra = false; } @@ -120,12 +110,12 @@ public function hideItemExtra() : void /** * @return SelectableStyle */ - public function getStyle() : ItemStyle + public function getStyle(): ItemStyle { return $this->style; } - public function setStyle(SelectableStyle $style) : void + public function setStyle(SelectableStyle $style): void { $this->style = $style; } diff --git a/src/MenuItem/SelectableItemRenderer.php b/src/MenuItem/SelectableItemRenderer.php index ea301f8a..72bccdb5 100644 --- a/src/MenuItem/SelectableItemRenderer.php +++ b/src/MenuItem/SelectableItemRenderer.php @@ -7,17 +7,21 @@ use PhpSchool\CliMenu\MenuStyle; use PhpSchool\CliMenu\Style\ItemStyle; use PhpSchool\CliMenu\Util\StringUtil as s; + use function PhpSchool\CliMenu\Util\mapWithKeys; class SelectableItemRenderer { - public function render(MenuStyle $menuStyle, MenuItemInterface $item, bool $selected, bool $disabled) : array + /** + * @return list + */ + public function render(MenuStyle $menuStyle, MenuItemInterface $item, bool $selected, bool $disabled): array { $itemStyle = $item->getStyle(); $marker = $itemStyle->getMarker($item, $selected); $availableTextWidth = $this->getAvailableTextWidth($menuStyle, $itemStyle); - return mapWithKeys( + return array_values(mapWithKeys( $this->wrapAndIndentText($marker, $item->getText(), $availableTextWidth), function (int $key, string $row) use ($menuStyle, $item, $availableTextWidth, $disabled) { $text = $disabled ? $menuStyle->getDisabledItemText($row) : $row; @@ -26,10 +30,13 @@ function (int $key, string $row) use ($menuStyle, $item, $availableTextWidth, $d ? $this->lineWithExtra($text, $availableTextWidth, $item->getStyle()) : $text; } - ); + )); } - public function wrapAndIndentText(string $marker, string $text, int $availableWidth) : array + /** + * @return list + */ + public function wrapAndIndentText(string $marker, string $text, int $availableWidth): array { return explode( "\n", @@ -41,7 +48,7 @@ public function wrapAndIndentText(string $marker, string $text, int $availableWi ); } - public function lineWithExtra(string $text, int $availableWidth, ItemStyle $itemStyle) : string + public function lineWithExtra(string $text, int $availableWidth, ItemStyle $itemStyle): string { return sprintf( '%s%s %s', @@ -51,12 +58,12 @@ public function lineWithExtra(string $text, int $availableWidth, ItemStyle $item ); } - public function emptyString(int $numCharacters) : string + public function emptyString(int $numCharacters): string { return str_repeat(' ', $numCharacters); } - public function getAvailableTextWidth(MenuStyle $menuStyle, ItemStyle $itemStyle) : int + public function getAvailableTextWidth(MenuStyle $menuStyle, ItemStyle $itemStyle): int { return $itemStyle->getDisplaysExtra() ? $menuStyle->getContentWidth() - (mb_strwidth($itemStyle->getItemExtra()) + 2) diff --git a/src/MenuItem/SplitItem.php b/src/MenuItem/SplitItem.php index e8abf67a..6bbd1b20 100644 --- a/src/MenuItem/SplitItem.php +++ b/src/MenuItem/SplitItem.php @@ -1,4 +1,5 @@ */ - private $items = []; + private array $items = []; - /** - * @var int|null - */ - private $selectedItemIndex; + private int|null $selectedItemIndex; - /** - * @var bool - */ - private $canBeSelected = true; + private bool $canBeSelected = true; - /** - * @var int - */ - private $gutter = 2; + private int $gutter = 2; - /** - * @var DefaultStyle - */ - private $style; + private DefaultStyle $style; /** - * @var array + * @var list> */ private static $blacklistedItems = [ \PhpSchool\CliMenu\MenuItem\AsciiArtItem::class, @@ -53,6 +43,9 @@ class SplitItem implements MenuItemInterface, PropagatesStyles \PhpSchool\CliMenu\MenuItem\SplitItem::class, ]; + /** + * @param list $items + */ public function __construct(array $items = []) { $this->addItems($items); @@ -61,18 +54,18 @@ public function __construct(array $items = []) $this->style = new DefaultStyle(); } - public function getGutter() : int + public function getGutter(): int { return $this->gutter; } - public function setGutter(int $gutter) : void + public function setGutter(int $gutter): void { Assertion::greaterOrEqualThan($gutter, 0); $this->gutter = $gutter; } - public function addItem(MenuItemInterface $item) : self + public function addItem(MenuItemInterface $item): self { foreach (self::$blacklistedItems as $bl) { if ($item instanceof $bl) { @@ -84,16 +77,22 @@ public function addItem(MenuItemInterface $item) : self return $this; } - public function addItems(array $items) : self + /** + * @param list $items + */ + public function addItems(array $items): self { foreach ($items as $item) { $this->addItem($item); } - + return $this; } - public function setItems(array $items) : self + /** + * @param list $items + */ + public function setItems(array $items): self { $this->items = []; $this->addItems($items); @@ -103,7 +102,7 @@ public function setItems(array $items) : self /** * Select default item */ - private function setDefaultSelectedItem() : void + private function setDefaultSelectedItem(): void { foreach ($this->items as $index => $item) { if ($item->canSelect()) { @@ -118,16 +117,17 @@ private function setDefaultSelectedItem() : void } /** - * The output text for the item + * The output text for the + * @return list */ - public function getRows(MenuStyle $style, bool $selected = false) : array + public function getRows(MenuStyle $style, bool $selected = false): array { $numberOfItems = count($this->items); if ($numberOfItems === 0) { throw new \RuntimeException(sprintf('There should be at least one item added to: %s', __CLASS__)); } - + if (!$selected) { $this->setDefaultSelectedItem(); } @@ -142,7 +142,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array $length = (int) $length; $missingLength = $style->getContentWidth() % $numberOfItems; - + return $this->buildRows( mapWithKeys($this->items, function (int $index, MenuItemInterface $item) use ($selected, $length, $style) { $isSelected = $selected && $index === $this->selectedItemIndex; @@ -178,19 +178,26 @@ public function getRows(MenuStyle $style, bool $selected = false) : array ); } - private function buildRows(array $cells, int $missingLength, int $length, int $largestItemExtra) : array + /** + * @param array> $cells + * @return list + */ + private function buildRows(array $cells, int $missingLength, int $length, int $largestItemExtra): array { $extraPadLength = $largestItemExtra > 0 ? 2 + $largestItemExtra : 0; - + return array_map( function ($i) use ($cells, $length, $missingLength, $extraPadLength) { return $this->buildRow($cells, $i, $length, $missingLength, $extraPadLength); }, - range(0, max(array_map('count', $cells)) - 1) + range(0, max(array_values(array_map(count(...), $cells))) - 1) ); } - private function buildRow(array $cells, int $index, int $length, int $missingLength, int $extraPadLength) : string + /** + * @param array> $cells + */ + private function buildRow(array $cells, int $index, int $length, int $missingLength, int $extraPadLength): string { return sprintf( '%s%s', @@ -207,14 +214,18 @@ function ($cell) use ($index, $length, $extraPadLength) { ); } + /** + * @param array $content + * @return array + */ private function buildCell( array $content, int $length, MenuStyle $style, bool $isSelected, string $itemExtra - ) : array { - return array_map(function ($row, $index) use ($length, $style, $isSelected, $itemExtra) { + ): array { + return array_map(function (string $row, int $index) use ($length, $style, $isSelected, $itemExtra) { $invertedColoursSetCode = $isSelected ? $style->getInvertedColoursSetCode() : ''; @@ -238,7 +249,7 @@ private function buildCell( * Is there an item with this index and can it be * selected? */ - public function canSelectIndex(int $index) : bool + public function canSelectIndex(int $index): bool { return isset($this->items[$index]) && $this->items[$index]->canSelect(); } @@ -247,12 +258,12 @@ public function canSelectIndex(int $index) : bool * Set the item index which should be selected. If the item does * not exist then throw an exception. */ - public function setSelectedItemIndex(int $index) : void + public function setSelectedItemIndex(int $index): void { if (!isset($this->items[$index])) { throw new \InvalidArgumentException(sprintf('Index: "%s" does not exist', $index)); } - + $this->selectedItemIndex = $index; } @@ -260,7 +271,7 @@ public function setSelectedItemIndex(int $index) : void * Get the currently select item index. * May be null in case of no selectable item. */ - public function getSelectedItemIndex() : ?int + public function getSelectedItemIndex(): ?int { return $this->selectedItemIndex; } @@ -269,16 +280,19 @@ public function getSelectedItemIndex() : ?int * Get the currently selected item - if no items are selectable * then throw an exception. */ - public function getSelectedItem() : MenuItemInterface + public function getSelectedItem(): MenuItemInterface { if (null === $this->selectedItemIndex) { throw new \RuntimeException('No item is selected'); } - + return $this->items[$this->selectedItemIndex]; } - public function getItems() : array + /** + * @return list + */ + public function getItems(): array { return $this->items; } @@ -287,7 +301,7 @@ public function getItems() : array * Can the item be selected * In this case, it indicates if at least 1 item inside the SplitItem can be selected */ - public function canSelect() : bool + public function canSelect(): bool { return $this->canBeSelected; } @@ -295,7 +309,7 @@ public function canSelect() : bool /** * Execute the items callable if required */ - public function getSelectAction() : ?callable + public function getSelectAction(): ?callable { return null; } @@ -303,7 +317,7 @@ public function getSelectAction() : ?callable /** * Whether or not the menu item is showing the menustyle extra value */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return false; } @@ -311,7 +325,7 @@ public function showsItemExtra() : bool /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { //noop } @@ -319,7 +333,7 @@ public function showItemExtra() : void /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { //noop } @@ -327,7 +341,7 @@ public function hideItemExtra() : void /** * Nothing to return with SplitItem */ - public function getText() : string + public function getText(): string { throw new \BadMethodCallException(sprintf('Not supported on: %s', __CLASS__)); } @@ -335,16 +349,16 @@ public function getText() : string /** * Finds largest itemExtra value in items */ - private function calculateItemExtra() : int + private function calculateItemExtra(): int { - return max(array_map( + return max(array_values(array_map( function (MenuItemInterface $item) { return mb_strwidth($item->getStyle()->getItemExtra()); }, array_filter($this->items, function (MenuItemInterface $item) { return $item->getStyle()->getDisplaysExtra(); }) - )); + ))); } /** diff --git a/src/MenuItem/StaticItem.php b/src/MenuItem/StaticItem.php index 798101e4..388fbf91 100644 --- a/src/MenuItem/StaticItem.php +++ b/src/MenuItem/StaticItem.php @@ -1,4 +1,5 @@ text, $style->getContentWidth())); } @@ -41,7 +36,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array /** * Return the raw string of text */ - public function getText() : string + public function getText(): string { return $this->text; } @@ -49,7 +44,7 @@ public function getText() : string /** * Set the raw string of text */ - public function setText(string $text) : void + public function setText(string $text): void { $this->text = $text; } @@ -57,7 +52,7 @@ public function setText(string $text) : void /** * Execute the items callable if required */ - public function getSelectAction() : ?callable + public function getSelectAction(): ?callable { return null; } @@ -65,7 +60,7 @@ public function getSelectAction() : ?callable /** * Can the item be selected */ - public function canSelect() : bool + public function canSelect(): bool { return false; } @@ -73,7 +68,7 @@ public function canSelect() : bool /** * Whether or not we are showing item extra */ - public function showsItemExtra() : bool + public function showsItemExtra(): bool { return false; } @@ -81,7 +76,7 @@ public function showsItemExtra() : bool /** * Enable showing item extra */ - public function showItemExtra() : void + public function showItemExtra(): void { //noop } @@ -89,7 +84,7 @@ public function showItemExtra() : void /** * Disable showing item extra */ - public function hideItemExtra() : void + public function hideItemExtra(): void { //noop } @@ -97,12 +92,12 @@ public function hideItemExtra() : void /** * @return DefaultStyle */ - public function getStyle() : ItemStyle + public function getStyle(): ItemStyle { return $this->style; } - public function setStyle(DefaultStyle $style) : void + public function setStyle(DefaultStyle $style): void { $this->style = $style; } diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 0e4fa85c..07f1204c 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -1,4 +1,5 @@ */ - private $paddingTopBottomRows = []; + private array $paddingTopBottomRows = []; - /** - * @var int - */ - protected $contentWidth; + protected int $contentWidth; - /** - * @var string - */ - private $itemExtra; + private string $itemExtra; - /** - * @var bool - */ - private $displaysExtra; + private bool $displaysExtra; - /** - * @var string - */ - private $titleSeparator; + private string $titleSeparator; - /** - * @var string - */ - private $coloursSetCode; + private string $coloursSetCode; - /** - * @var string - */ - private $invertedColoursSetCode = "\033[7m"; + private string $invertedColoursSetCode = "\033[7m"; - /** - * @var string - */ - private $invertedColoursUnsetCode = "\033[27m"; + private string $invertedColoursUnsetCode = "\033[27m"; - /** - * @var string - */ - private $coloursResetCode = "\033[0m"; + private string $coloursResetCode = "\033[0m"; - /** - * @var int - */ - private $borderTopWidth = 0; + private int $borderTopWidth = 0; - /** - * @var int - */ - private $borderRightWidth = 0; + private int $borderRightWidth = 0; - /** - * @var int - */ - private $borderBottomWidth = 0; + private int $borderBottomWidth = 0; - /** - * @var int - */ - private $borderLeftWidth = 0; + private int $borderLeftWidth = 0; - /** - * @var string - */ - private $borderColour = 'white'; + private string $borderColour = 'white'; /** - * @var array + * @var list */ - private $borderTopRows = []; + private array $borderTopRows = []; /** - * @var array + * @var list */ - private $borderBottomRows = []; + private array $borderBottomRows = []; - /** - * @var bool - */ - private $marginAuto = false; + private bool $marginAuto = false; - /** - * @var bool - */ - private $debugMode = false; + private bool $debugMode = false; /** * Default Values * - * @var array - */ - private static $defaultStyleValues = [ + * @var array{ + * fg: string, + * bg: string, + * width: int, + * paddingTopBottom: int, + * paddingLeftRight: int, + * margin: int, + * itemExtra: string, + * displaysExtra: bool, + * titleSeparator: string, + * borderTopWidth: int, + * borderRightWidth: int, + * borderBottomWidth: int, + * borderLeftWidth: int, + * borderColour: string, + * marginAuto: bool + * } + */ + private static array $defaultStyleValues = [ 'fg' => 'white', 'bg' => 'blue', 'width' => 100, @@ -183,9 +134,9 @@ class MenuStyle ]; /** - * @var array + * @var array */ - private static $availableForegroundColors = [ + private static array $availableForegroundColors = [ 'black' => 30, 'red' => 31, 'green' => 32, @@ -198,9 +149,9 @@ class MenuStyle ]; /** - * @var array + * @var array */ - private static $availableBackgroundColors = [ + private static array $availableBackgroundColors = [ 'black' => 40, 'red' => 41, 'green' => 42, @@ -213,9 +164,9 @@ class MenuStyle ]; /** - * @var array + * @var array */ - private static $availableOptions = [ + private static array $availableOptions = [ 'bold' => ['set' => 1, 'unset' => 22], 'dim' => ['set' => 2, 'unset' => 22], 'underscore' => ['set' => 4, 'unset' => 24], @@ -250,7 +201,7 @@ public function __construct(?Terminal $terminal = null) $this->setBorderColour(self::$defaultStyleValues['borderColour']); } - public function hasChangedFromDefaults() : bool + public function hasChangedFromDefaults(): bool { $currentValues = [ $this->fg, @@ -286,7 +237,7 @@ public function hasChangedFromDefaults() : bool * * @return string */ - public function getDisabledItemText(string $text) : string + public function getDisabledItemText(string $text): string { return sprintf( "\033[%sm\033[%sm%s\033[%sm\033[%sm", @@ -305,7 +256,7 @@ public function getDisabledItemText(string $text) : string * * @return string */ - private function getForegroundColourCode(bool $bright = false) : string + private function getForegroundColourCode(bool $bright = false): string { if (!is_numeric($this->fg)) { $fgCode = (int)self::$availableForegroundColors[$this->fg]; @@ -324,7 +275,7 @@ private function getForegroundColourCode(bool $bright = false) : string * * @return string */ - private function getBackgroundColourCode(bool $bright = false) : string + private function getBackgroundColourCode(bool $bright = false): string { if (!is_numeric($this->bg)) { $bgCode = (int)self::$availableBackgroundColors[$this->bg]; @@ -339,7 +290,7 @@ private function getBackgroundColourCode(bool $bright = false) : string /** * Generates the ansi escape sequence to set the colours */ - private function generateColoursSetCode() : void + private function generateColoursSetCode(): void { $this->coloursSetCode = sprintf( "\033[%s;%sm", @@ -351,7 +302,7 @@ private function generateColoursSetCode() : void /** * Get the colour code for Bg and Fg */ - public function getColoursSetCode() : string + public function getColoursSetCode(): string { return $this->coloursSetCode; } @@ -359,7 +310,7 @@ public function getColoursSetCode() : string /** * Get the inverted escape sequence (used for selected elements) */ - public function getInvertedColoursSetCode() : string + public function getInvertedColoursSetCode(): string { return $this->invertedColoursSetCode; } @@ -367,7 +318,7 @@ public function getInvertedColoursSetCode() : string /** * Get the inverted escape sequence (used for selected elements) */ - public function getInvertedColoursUnsetCode() : string + public function getInvertedColoursUnsetCode(): string { return $this->invertedColoursUnsetCode; } @@ -375,7 +326,7 @@ public function getInvertedColoursUnsetCode() : string /** * Get the escape sequence used to reset colours to default */ - public function getColoursResetCode() : string + public function getColoursResetCode(): string { return $this->coloursResetCode; } @@ -385,7 +336,7 @@ public function getColoursResetCode() : string * * The content width is menu width minus borders and padding. */ - protected function calculateContentWidth() : void + protected function calculateContentWidth(): void { $this->contentWidth = $this->width - ($this->paddingLeftRight * 2) @@ -396,12 +347,12 @@ protected function calculateContentWidth() : void } } - public function getFg() : string + public function getFg(): string { return $this->fg; } - public function setFg(string $fg, ?string $fallback = null) : self + public function setFg(string $fg, ?string $fallback = null): self { $this->fg = ColourUtil::validateColour( $this->terminal, @@ -413,12 +364,12 @@ public function setFg(string $fg, ?string $fallback = null) : self return $this; } - public function getBg() : string + public function getBg(): string { return $this->bg; } - public function setBg(string $bg, ?string $fallback = null) : self + public function setBg(string $bg, ?string $fallback = null): self { $this->bg = ColourUtil::validateColour( $this->terminal, @@ -432,12 +383,12 @@ public function setBg(string $bg, ?string $fallback = null) : self return $this; } - public function getWidth() : int + public function getWidth(): int { return $this->width; } - public function setWidth(int $width) : self + public function setWidth(int $width): self { Assertion::greaterOrEqualThan($width, 0); @@ -456,7 +407,7 @@ public function setWidth(int $width) : self return $this; } - private function maybeShrinkWidth(int $margin, int $width) : int + private function maybeShrinkWidth(int $margin, int $width): int { if ($width + ($margin * 2) >= $this->terminal->getWidth()) { $width = $this->terminal->getWidth() - ($margin * 2); @@ -469,17 +420,17 @@ private function maybeShrinkWidth(int $margin, int $width) : int return $width; } - public function getPaddingTopBottom() : int + public function getPaddingTopBottom(): int { return $this->paddingTopBottom; } - public function getPaddingLeftRight() : int + public function getPaddingLeftRight(): int { return $this->paddingLeftRight; } - private function generatePaddingTopBottomRows() : void + private function generatePaddingTopBottomRows(): void { if ($this->borderLeftWidth || $this->borderRightWidth) { $borderColour = $this->getBorderColourCode(); @@ -515,14 +466,14 @@ private function generatePaddingTopBottomRows() : void } /** - * @return array + * @return list */ - public function getPaddingTopBottomRows() : array + public function getPaddingTopBottomRows(): array { return $this->paddingTopBottomRows; } - public function setPadding(int $topBottom, ?int $leftRight = null) : self + public function setPadding(int $topBottom, ?int $leftRight = null): self { if ($leftRight === null) { $leftRight = $topBottom; @@ -537,7 +488,7 @@ public function setPadding(int $topBottom, ?int $leftRight = null) : self return $this; } - public function setPaddingTopBottom(int $topBottom) : self + public function setPaddingTopBottom(int $topBottom): self { Assertion::greaterOrEqualThan($topBottom, 0); $this->paddingTopBottom = $topBottom; @@ -547,7 +498,7 @@ public function setPaddingTopBottom(int $topBottom) : self return $this; } - public function setPaddingLeftRight(int $leftRight) : self + public function setPaddingLeftRight(int $leftRight): self { Assertion::greaterOrEqualThan($leftRight, 0); $this->paddingLeftRight = $leftRight; @@ -558,12 +509,12 @@ public function setPaddingLeftRight(int $leftRight) : self return $this; } - public function getMargin() : int + public function getMargin(): int { return $this->margin; } - public function setMarginAuto() : self + public function setMarginAuto(): self { $this->marginAuto = true; $this->margin = 0; @@ -573,12 +524,12 @@ public function setMarginAuto() : self return $this; } - private function calculateMarginAuto(int $width) : void + private function calculateMarginAuto(int $width): void { $this->margin = (int) floor(($this->terminal->getWidth() - ($width)) / 2); } - public function setMargin(int $margin) : self + public function setMargin(int $margin): self { Assertion::greaterOrEqualThan($margin, 0); @@ -592,7 +543,7 @@ public function setMargin(int $margin) : self return $this; } - public function getContentWidth() : int + public function getContentWidth(): int { return $this->contentWidth; } @@ -600,7 +551,7 @@ public function getContentWidth() : int /** * Get padding for right had side of content */ - public function getRightHandPadding(int $contentLength) : int + public function getRightHandPadding(int $contentLength): int { $rightPadding = $this->getContentWidth() - $contentLength + $this->getPaddingLeftRight(); @@ -611,43 +562,43 @@ public function getRightHandPadding(int $contentLength) : int return $rightPadding; } - public function setItemExtra(string $itemExtra) : self + public function setItemExtra(string $itemExtra): self { $this->itemExtra = $itemExtra; return $this; } - public function getItemExtra() : string + public function getItemExtra(): string { return $this->itemExtra; } - public function getDisplaysExtra() : bool + public function getDisplaysExtra(): bool { return $this->displaysExtra; } - public function setDisplaysExtra(bool $displaysExtra) : self + public function setDisplaysExtra(bool $displaysExtra): self { $this->displaysExtra = $displaysExtra; return $this; } - public function getTitleSeparator() : string + public function getTitleSeparator(): string { return $this->titleSeparator; } - public function setTitleSeparator(string $actionSeparator) : self + public function setTitleSeparator(string $actionSeparator): self { $this->titleSeparator = $actionSeparator; return $this; } - private function generateBorderRows() : void + private function generateBorderRows(): void { $borderRow = sprintf( "%s%s%s%s\n", @@ -673,17 +624,17 @@ private function generateBorderRows() : void } /** - * @return array + * @return list */ - public function getBorderTopRows() : array + public function getBorderTopRows(): array { return $this->borderTopRows; } /** - * @return array + * @return list */ - public function getBorderBottomRows() : array + public function getBorderBottomRows(): array { return $this->borderBottomRows; } @@ -701,7 +652,7 @@ public function setBorder( $bottomWidth = null, $leftWidth = null, ?string $colour = null - ) : self { + ): self { if (!is_int($rightWidth)) { $colour = $rightWidth; $rightWidth = $bottomWidth = $leftWidth = $topWidth; @@ -730,7 +681,7 @@ public function setBorder( return $this; } - public function setBorderTopWidth(int $width) : self + public function setBorderTopWidth(int $width): self { $this->borderTopWidth = $width; @@ -739,7 +690,7 @@ public function setBorderTopWidth(int $width) : self return $this; } - public function setBorderRightWidth(int $width) : self + public function setBorderRightWidth(int $width): self { $this->borderRightWidth = $width; $this->calculateContentWidth(); @@ -749,7 +700,7 @@ public function setBorderRightWidth(int $width) : self return $this; } - public function setBorderBottomWidth(int $width) : self + public function setBorderBottomWidth(int $width): self { $this->borderBottomWidth = $width; @@ -758,7 +709,7 @@ public function setBorderBottomWidth(int $width) : self return $this; } - public function setBorderLeftWidth(int $width) : self + public function setBorderLeftWidth(int $width): self { $this->borderLeftWidth = $width; $this->calculateContentWidth(); @@ -768,7 +719,7 @@ public function setBorderLeftWidth(int $width) : self return $this; } - public function setBorderColour(string $colour, ?string $fallback = null) : self + public function setBorderColour(string $colour, ?string $fallback = null): self { $this->borderColour = ColourUtil::validateColour( $this->terminal, @@ -782,32 +733,32 @@ public function setBorderColour(string $colour, ?string $fallback = null) : self return $this; } - public function getBorderTopWidth() : int + public function getBorderTopWidth(): int { return $this->borderTopWidth; } - public function getBorderRightWidth() : int + public function getBorderRightWidth(): int { return $this->borderRightWidth; } - public function getBorderBottomWidth() : int + public function getBorderBottomWidth(): int { return $this->borderBottomWidth; } - public function getBorderLeftWidth() : int + public function getBorderLeftWidth(): int { return $this->borderLeftWidth; } - public function getBorderColour() : string + public function getBorderColour(): string { return $this->borderColour; } - public function getBorderColourCode() : string + public function getBorderColourCode(): string { if (!is_numeric($this->borderColour)) { $borderColourCode = self::$availableBackgroundColors[$this->borderColour]; @@ -836,7 +787,7 @@ public function getOptionCode(string $string, bool $set = true): string * Get a string of given length consisting of 0-9 * eg $length = 15 : 012345678901234 */ - private function getDebugString(int $length) : string + private function getDebugString(int $length): string { $nums = []; for ($i = 0, $j = 0; $i < $length; $i++, $j++) { diff --git a/src/Style/CheckboxStyle.php b/src/Style/CheckboxStyle.php index a3bc34e8..017bf838 100644 --- a/src/Style/CheckboxStyle.php +++ b/src/Style/CheckboxStyle.php @@ -1,4 +1,5 @@ false, ]; - /** - * @var string - */ - private $checkedMarker; + private string $checkedMarker; - /** - * @var string - */ - private $uncheckedMarker; + private string $uncheckedMarker; - /** - * @var string - */ - private $itemExtra; + private string $itemExtra; - /** - * @var bool - */ - private $displaysExtra; + private bool $displaysExtra; public function __construct() { @@ -43,7 +32,7 @@ public function __construct() $this->displaysExtra = self::DEFAULT_STYLES['displaysExtra']; } - public function hasChangedFromDefaults() : bool + public function hasChangedFromDefaults(): bool { $currentValues = [ $this->checkedMarker, @@ -55,7 +44,7 @@ public function hasChangedFromDefaults() : bool return $currentValues !== array_values(self::DEFAULT_STYLES); } - public function getMarker(MenuItemInterface $item, bool $selected) : string + public function getMarker(MenuItemInterface $item, bool $selected): string { if (!$item instanceof CheckboxItem) { throw new \InvalidArgumentException( @@ -66,36 +55,36 @@ public function getMarker(MenuItemInterface $item, bool $selected) : string return $item->getChecked() ? $this->checkedMarker : $this->uncheckedMarker; } - public function getCheckedMarker() : string + public function getCheckedMarker(): string { return $this->checkedMarker; } - public function setCheckedMarker(string $marker) : self + public function setCheckedMarker(string $marker): self { $this->checkedMarker = $marker; return $this; } - public function getUncheckedMarker() : string + public function getUncheckedMarker(): string { return $this->uncheckedMarker; } - public function setUncheckedMarker(string $marker) : self + public function setUncheckedMarker(string $marker): self { $this->uncheckedMarker = $marker; return $this; } - public function getItemExtra() : string + public function getItemExtra(): string { return $this->itemExtra; } - public function setItemExtra(string $itemExtra) : self + public function setItemExtra(string $itemExtra): self { $this->itemExtra = $itemExtra; @@ -105,12 +94,12 @@ public function setItemExtra(string $itemExtra) : self return $this; } - public function getDisplaysExtra() : bool + public function getDisplaysExtra(): bool { return $this->displaysExtra; } - public function setDisplaysExtra(bool $displaysExtra) : self + public function setDisplaysExtra(bool $displaysExtra): self { $this->displaysExtra = $displaysExtra; diff --git a/src/Style/DefaultStyle.php b/src/Style/DefaultStyle.php index 7fe273ab..9ee204f0 100644 --- a/src/Style/DefaultStyle.php +++ b/src/Style/DefaultStyle.php @@ -8,22 +8,22 @@ class DefaultStyle implements ItemStyle { - public function hasChangedFromDefaults() : bool + public function hasChangedFromDefaults(): bool { return true; } - public function getDisplaysExtra() : bool + public function getDisplaysExtra(): bool { return false; } - public function getItemExtra() : string + public function getItemExtra(): string { return ''; } - public function getMarker(MenuItemInterface $item, bool $isSelected) : string + public function getMarker(MenuItemInterface $item, bool $isSelected): string { return ''; } diff --git a/src/Style/Exception/InvalidStyle.php b/src/Style/Exception/InvalidStyle.php index fde0a505..43eabf88 100644 --- a/src/Style/Exception/InvalidStyle.php +++ b/src/Style/Exception/InvalidStyle.php @@ -8,22 +8,22 @@ class InvalidStyle extends \RuntimeException { - public static function unregisteredStyle(string $styleClass) : self + public static function unregisteredStyle(string $styleClass): self { return new self("Style class: '$styleClass' is not registered"); } - public static function notSubClassOf(string $styleClass) : self + public static function notSubClassOf(string $styleClass): self { return new self("Style instance must be a subclass of: '$styleClass'"); } - public static function unregisteredItem(string $itemClass) : self + public static function unregisteredItem(string $itemClass): self { return new self("Menu item: '$itemClass' does not have a registered style class"); } - public static function itemAlreadyRegistered(string $itemClass) : self + public static function itemAlreadyRegistered(string $itemClass): self { return new self("Menu item: '$itemClass' already has a registered style class"); } diff --git a/src/Style/ItemStyle.php b/src/Style/ItemStyle.php index b819e704..a2c51922 100644 --- a/src/Style/ItemStyle.php +++ b/src/Style/ItemStyle.php @@ -8,11 +8,11 @@ interface ItemStyle { - public function hasChangedFromDefaults() : bool; + public function hasChangedFromDefaults(): bool; - public function getDisplaysExtra() : bool; + public function getDisplaysExtra(): bool; - public function getItemExtra() : string; + public function getItemExtra(): string; - public function getMarker(MenuItemInterface $menuItem, bool $isSelected) : string; + public function getMarker(MenuItemInterface $menuItem, bool $isSelected): string; } diff --git a/src/Style/Locator.php b/src/Style/Locator.php index 47ffa791..5e1a67bc 100644 --- a/src/Style/Locator.php +++ b/src/Style/Locator.php @@ -14,12 +14,13 @@ use PhpSchool\CliMenu\MenuItem\SplitItem; use PhpSchool\CliMenu\MenuItem\StaticItem; use PhpSchool\CliMenu\Style\Exception\InvalidStyle; + use function PhpSchool\CliMenu\Util\mapWithKeys; class Locator { /** - * @var array + * @var array */ private $itemStyleMap = [ StaticItem::class => DefaultStyle::class, @@ -33,7 +34,7 @@ class Locator ]; /** - * @var array + * @var array */ private $styles; @@ -53,7 +54,7 @@ public function __construct() * * @param Locator $other */ - public function importFrom(self $other) : void + public function importFrom(self $other): void { $this->styles = mapWithKeys( $this->styles, @@ -65,7 +66,7 @@ function ($styleClass, ItemStyle $instance) use ($other) { ); } - public function getStyle(string $styleClass) : ItemStyle + public function getStyle(string $styleClass): ItemStyle { if (!isset($this->styles[$styleClass])) { throw InvalidStyle::unregisteredStyle($styleClass); @@ -74,7 +75,10 @@ public function getStyle(string $styleClass) : ItemStyle return $this->styles[$styleClass]; } - public function setStyle(ItemStyle $itemStyle, string $styleClass) : void + /** + * @param class-string $styleClass + */ + public function setStyle(ItemStyle $itemStyle, string $styleClass): void { if (!isset($this->styles[$styleClass])) { throw InvalidStyle::unregisteredStyle($styleClass); @@ -87,12 +91,12 @@ public function setStyle(ItemStyle $itemStyle, string $styleClass) : void $this->styles[$styleClass] = $itemStyle; } - public function hasStyleForMenuItem(MenuItemInterface $item) : bool + public function hasStyleForMenuItem(MenuItemInterface $item): bool { return isset($this->itemStyleMap[get_class($item)]); } - public function getStyleForMenuItem(MenuItemInterface $item) : ItemStyle + public function getStyleForMenuItem(MenuItemInterface $item): ItemStyle { if (!isset($this->itemStyleMap[get_class($item)])) { throw InvalidStyle::unregisteredItem(get_class($item)); @@ -103,7 +107,10 @@ public function getStyleForMenuItem(MenuItemInterface $item) : ItemStyle return $this->getStyle($styleClass); } - public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : void + /** + * @param class-string $itemClass + */ + public function registerItemStyle(string $itemClass, ItemStyle $itemStyle): void { if (isset($this->itemStyleMap[$itemClass])) { throw InvalidStyle::itemAlreadyRegistered($itemClass); diff --git a/src/Style/RadioStyle.php b/src/Style/RadioStyle.php index d053f727..bc8768e0 100644 --- a/src/Style/RadioStyle.php +++ b/src/Style/RadioStyle.php @@ -1,4 +1,5 @@ false, ]; - /** - * @var string - */ - private $checkedMarker; + private string $checkedMarker; - /** - * @var string - */ - private $uncheckedMarker; + private string $uncheckedMarker; - /** - * @var string - */ - private $itemExtra; + private string $itemExtra; - /** - * @var bool - */ - private $displaysExtra; + private bool $displaysExtra; public function __construct() { @@ -43,7 +32,7 @@ public function __construct() $this->displaysExtra = self::DEFAULT_STYLES['displaysExtra']; } - public function hasChangedFromDefaults() : bool + public function hasChangedFromDefaults(): bool { $currentValues = [ $this->checkedMarker, @@ -55,7 +44,7 @@ public function hasChangedFromDefaults() : bool return $currentValues !== array_values(self::DEFAULT_STYLES); } - public function getMarker(MenuItemInterface $item, bool $selected) : string + public function getMarker(MenuItemInterface $item, bool $selected): string { if (!$item instanceof RadioItem) { throw new \InvalidArgumentException( @@ -66,36 +55,36 @@ public function getMarker(MenuItemInterface $item, bool $selected) : string return $item->getChecked() ? $this->checkedMarker : $this->uncheckedMarker; } - public function getCheckedMarker() : string + public function getCheckedMarker(): string { return $this->checkedMarker; } - public function setCheckedMarker(string $marker) : self + public function setCheckedMarker(string $marker): self { $this->checkedMarker = $marker; return $this; } - public function getUncheckedMarker() : string + public function getUncheckedMarker(): string { return $this->uncheckedMarker; } - public function setUncheckedMarker(string $marker) : self + public function setUncheckedMarker(string $marker): self { $this->uncheckedMarker = $marker; return $this; } - public function getItemExtra() : string + public function getItemExtra(): string { return $this->itemExtra; } - public function setItemExtra(string $itemExtra) : self + public function setItemExtra(string $itemExtra): self { $this->itemExtra = $itemExtra; @@ -105,12 +94,12 @@ public function setItemExtra(string $itemExtra) : self return $this; } - public function getDisplaysExtra() : bool + public function getDisplaysExtra(): bool { return $this->displaysExtra; } - public function setDisplaysExtra(bool $displaysExtra) : self + public function setDisplaysExtra(bool $displaysExtra): self { $this->displaysExtra = $displaysExtra; diff --git a/src/Style/SelectableStyle.php b/src/Style/SelectableStyle.php index 3d7bd2f4..d955f6a3 100644 --- a/src/Style/SelectableStyle.php +++ b/src/Style/SelectableStyle.php @@ -1,4 +1,5 @@ false, ]; - /** - * @var string - */ - private $selectedMarker; + private string $selectedMarker; - /** - * @var string - */ - private $unselectedMarker; + private string $unselectedMarker; - /** - * @var string - */ - private $itemExtra; + private string $itemExtra; - /** - * @var bool - */ - private $displaysExtra; + private bool $displaysExtra; public function __construct() { @@ -42,7 +31,7 @@ public function __construct() $this->displaysExtra = self::DEFAULT_STYLES['displaysExtra']; } - public function hasChangedFromDefaults() : bool + public function hasChangedFromDefaults(): bool { $currentValues = [ $this->selectedMarker, @@ -54,41 +43,41 @@ public function hasChangedFromDefaults() : bool return $currentValues !== array_values(self::DEFAULT_STYLES); } - public function getMarker(MenuItemInterface $item, bool $selected) : string + public function getMarker(MenuItemInterface $item, bool $selected): string { return $selected ? $this->selectedMarker : $this->unselectedMarker; } - public function getSelectedMarker() : string + public function getSelectedMarker(): string { return $this->selectedMarker; } - public function setSelectedMarker(string $marker) : self + public function setSelectedMarker(string $marker): self { $this->selectedMarker = $marker; return $this; } - public function getUnselectedMarker() : string + public function getUnselectedMarker(): string { return $this->unselectedMarker; } - public function setUnselectedMarker(string $marker) : self + public function setUnselectedMarker(string $marker): self { $this->unselectedMarker = $marker; return $this; } - public function getItemExtra() : string + public function getItemExtra(): string { return $this->itemExtra; } - public function setItemExtra(string $itemExtra) : self + public function setItemExtra(string $itemExtra): self { $this->itemExtra = $itemExtra; @@ -98,12 +87,12 @@ public function setItemExtra(string $itemExtra) : self return $this; } - public function getDisplaysExtra() : bool + public function getDisplaysExtra(): bool { return $this->displaysExtra; } - public function setDisplaysExtra(bool $displaysExtra) : self + public function setDisplaysExtra(bool $displaysExtra): self { $this->displaysExtra = $displaysExtra; diff --git a/src/Terminal/TerminalFactory.php b/src/Terminal/TerminalFactory.php index 1cb4e03c..7aa76fad 100644 --- a/src/Terminal/TerminalFactory.php +++ b/src/Terminal/TerminalFactory.php @@ -1,4 +1,5 @@ $array + * @param callable(TKey, TItem): TItemNew $callback + * @return array + */ +function mapWithKeys(array $array, callable $callback): array { - $arr = array_combine( + return array_combine( array_keys($array), array_map($callback, array_keys($array), $array) ); - - assert(is_array($arr)); - - return $arr; } -function filter(array $array, callable $callback) : array +/** + * @template TKey of array-key + * @template TItem + * + * @param array $array + * @param callable(TKey, TItem): bool $callback + * @return array + */ +function filter(array $array, callable $callback): array { return array_filter($array, function ($v, $k) use ($callback) { return $callback($k, $v); }, ARRAY_FILTER_USE_BOTH); } -function each(array $array, callable $callback) : void +/** + * @template TKey of array-key + * @template TItem + * + * @param array $array + * @param callable(TKey, TItem): void $callback + */ +function each(array $array, callable $callback): void { foreach ($array as $k => $v) { $callback($k, $v); } } -function max(array $items) : int +/** + * @param list $items + */ +function max(array $items): int { return count($items) > 0 ? \max($items) : 0; } -function collect(array $items) : Collection +/** + * @template TKey of array-key + * @template TItem of mixed + * + * @param array $items + * @return Collection + */ +function collect(array $items): Collection { return new Collection($items); } diff --git a/src/Util/Collection.php b/src/Util/Collection.php index 2e854c0d..b6747602 100644 --- a/src/Util/Collection.php +++ b/src/Util/Collection.php @@ -4,41 +4,66 @@ namespace PhpSchool\CliMenu\Util; +/** + * @template TKey of array-key + * @template TItem of mixed + */ class Collection { /** - * @var array + * @var array */ - private $items; + private array $items; + /** + * @param array $items + */ public function __construct(array $items) { $this->items = $items; } - public function map(callable $cb) : self + /** + * @template TItemNew of mixed + * + * @param callable(TKey, TItem): TItemNew $cb + * @return self + */ + public function map(callable $cb): self { return new self(mapWithKeys($this->items, $cb)); } - public function filter(callable $cb) : self + /** + * @return self + */ + public function filter(callable $cb): self { return new self(filter($this->items, $cb)); } - public function values() : self + /** + * @return self + */ + public function values(): self { return new self(array_values($this->items)); } - public function each(callable $cb) : self + /** + * @return self + */ + public function each(callable $cb): self { each($this->items, $cb); return $this; } - public function all() : array + /** + * @return array + */ + public function all(): array { return $this->items; } diff --git a/src/Util/ColourUtil.php b/src/Util/ColourUtil.php index a73c3901..8305d426 100644 --- a/src/Util/ColourUtil.php +++ b/src/Util/ColourUtil.php @@ -1,4 +1,5 @@ */ - private static $defaultColoursNames = [ + private static array $defaultColoursNames = [ 'black', 'red', 'green', @@ -24,9 +25,9 @@ class ColourUtil ]; /** - * @var array + * @var array */ - private static $coloursMap = [ + private static array $coloursMap = [ 0 => 'black', 1 => 'red', 2 => 'green', @@ -285,7 +286,10 @@ class ColourUtil 255 => 'white', ]; - public static function getDefaultColourNames() : array + /** + * @return list + */ + public static function getDefaultColourNames(): array { return self::$defaultColoursNames; } @@ -294,7 +298,7 @@ public static function getDefaultColourNames() : array * Simple function to transform a 8-bit (256 colours) colour code * to one of the default 8 colors available in the terminal */ - public static function map256To8(int $colourCode) : string + public static function map256To8(int $colourCode): string { if (!isset(self::$coloursMap[$colourCode])) { throw new \InvalidArgumentException('Invalid colour code'); @@ -307,14 +311,14 @@ public static function map256To8(int $colourCode) : string * Check if $colour exists * If it's a 256-colours code and $terminal doesn't support it, returns a fallback value */ - public static function validateColour(Terminal $terminal, string $colour, ?string $fallback = null) : string + public static function validateColour(Terminal $terminal, string $colour, ?string $fallback = null): string { if (!is_numeric($colour)) { return self::validateColourName($colour); } - + Assertion::between($colour, 0, 255, 'Invalid colour code'); - + if ($terminal->getColourSupport() >= 256) { return $colour; } @@ -322,11 +326,11 @@ public static function validateColour(Terminal $terminal, string $colour, ?strin if ($fallback !== null) { return self::validateColourName($fallback); } - + return static::map256To8((int) $colour); } - - private static function validateColourName(string $colourName) : string + + private static function validateColourName(string $colourName): string { Assertion::inArray($colourName, static::getDefaultColourNames()); return $colourName; diff --git a/src/Util/StringUtil.php b/src/Util/StringUtil.php index 560171f9..d8b44dc9 100644 --- a/src/Util/StringUtil.php +++ b/src/Util/StringUtil.php @@ -1,4 +1,5 @@ getMockBuilder(CliMenu::class) ->disableOriginalConstructor() ->getMock(); - - $action = new ExitAction; - + + $action = new ExitAction(); + $menu ->expects($this->once()) ->method('close'); - + $action->__invoke($menu); } } diff --git a/test/Action/GoBackActionTest.php b/test/Action/GoBackActionTest.php index 0a085442..45f4930e 100644 --- a/test/Action/GoBackActionTest.php +++ b/test/Action/GoBackActionTest.php @@ -1,4 +1,5 @@ getMockBuilder(CliMenu::class) ->disableOriginalConstructor() ->getMock(); - + $menu = $this->getMockBuilder(CliMenu::class) ->disableOriginalConstructor() ->getMock(); - - $action = new GoBackAction; - + + $action = new GoBackAction(); + $menu ->expects($this->once()) ->method('getParent') ->willReturn($parent); - + $menu ->expects($this->once()) ->method('closeThis'); - + $parent ->expects($this->once()) ->method('open'); - + $action->__invoke($menu); } } diff --git a/test/Builder/CliMenuBuilderTest.php b/test/Builder/CliMenuBuilderTest.php index ed8170b2..15719f9c 100644 --- a/test/Builder/CliMenuBuilderTest.php +++ b/test/Builder/CliMenuBuilderTest.php @@ -1,4 +1,5 @@ build(); - + $expected = [ [ 'class' => SelectableItem::class, 'text' => 'Exit', ], ]; - + $this->checkMenuItems($menu, $expected); } - public function testModifyExitButtonText() : void + public function testModifyExitButtonText(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setExitButtonText('RELEASE ME'); $menu = $builder->build(); @@ -55,14 +56,14 @@ public function testModifyExitButtonText() : void $this->checkMenuItems($menu, $expected); } - public function testModifyStyles() : void + public function testModifyStyles(): void { $terminal = static::createMock(Terminal::class); $terminal ->expects($this->any()) ->method('getWidth') ->willReturn(200); - + $builder = new CliMenuBuilder($terminal); $builder->setBackgroundColour('red'); $builder->setForegroundColour('red'); @@ -85,7 +86,7 @@ public function testModifyStyles() : void self::assertEquals('-', $style->getTitleSeparator()); } - public function testSetBorderShorthandFunction() : void + public function testSetBorderShorthandFunction(): void { $terminal = static::createMock(Terminal::class); $terminal @@ -182,9 +183,9 @@ public function testSetBorderShorthandFunction() : void self::assertEquals('green', $style->getBorderColour()); } - public function testSetBorderTopWidth() : void + public function testSetBorderTopWidth(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setBorderTopWidth(5); $style = $builder->build()->getStyle(); @@ -192,9 +193,9 @@ public function testSetBorderTopWidth() : void self::assertEquals(5, $style->getBorderTopWidth()); } - public function testSetBorderRightWidth() : void + public function testSetBorderRightWidth(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setBorderRightWidth(6); $style = $builder->build()->getStyle(); @@ -202,9 +203,9 @@ public function testSetBorderRightWidth() : void self::assertEquals(6, $style->getBorderRightWidth()); } - public function testSetBorderBottomWidth() : void + public function testSetBorderBottomWidth(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setBorderBottomWidth(7); $style = $builder->build()->getStyle(); @@ -212,9 +213,9 @@ public function testSetBorderBottomWidth() : void self::assertEquals(7, $style->getBorderBottomWidth()); } - public function testSetBorderLeftWidth() : void + public function testSetBorderLeftWidth(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setBorderLeftWidth(8); $style = $builder->build()->getStyle(); @@ -222,17 +223,17 @@ public function testSetBorderLeftWidth() : void self::assertEquals(8, $style->getBorderLeftWidth()); } - public function testSetBorderColour() : void + public function testSetBorderColour(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setBorderColour('red'); $style = $builder->build()->getStyle(); self::assertEquals('red', $style->getBorderColour()); } - - public function test256ColoursCodes() : void + + public function test256ColoursCodes(): void { $terminal = static::createMock(Terminal::class); $terminal @@ -271,7 +272,7 @@ public function test256ColoursCodes() : void self::assertEquals('red', $style->getFg()); } - public function testSetFgThrowsExceptionWhenColourCodeIsNotInRange() : void + public function testSetFgThrowsExceptionWhenColourCodeIsNotInRange(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid colour code'); @@ -288,7 +289,7 @@ public function testSetFgThrowsExceptionWhenColourCodeIsNotInRange() : void $builder->setForegroundColour('512', 'white'); } - public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange() : void + public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid colour code'); @@ -305,32 +306,32 @@ public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange() : void $builder->setBackgroundColour('257', 'white'); } - public function testDisableDefaultItems() : void + public function testDisableDefaultItems(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); - + $menu = $builder->build(); self::assertEquals([], $menu->getItems()); } - public function testSetTitle() : void + public function testSetTitle(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setTitle('title'); - + $menu = $builder->build(); self::assertEquals('title', $menu->getTitle()); } - public function testAddItem() : void + public function testAddItem(): void { $callable = function () { }; - - $builder = new CliMenuBuilder; + + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addItem('Item 1', $callable); $builder->addItem('Item 2', $callable); @@ -346,16 +347,16 @@ public function testAddItem() : void 'text' => 'Item 2', ], ]; - + $this->checkMenuItems($menu, $expected); } - public function testAddMultipleItems() : void + public function testAddMultipleItems(): void { $callable = function () { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addItems([ ['Item 1', $callable], @@ -377,12 +378,12 @@ public function testAddMultipleItems() : void $this->checkMenuItems($menu, $expected); } - public function testAddMultipleCheckboxItems() : void + public function testAddMultipleCheckboxItems(): void { $callable = function () { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addCheckboxItems([ ['Item 1', $callable], @@ -404,12 +405,12 @@ public function testAddMultipleCheckboxItems() : void $this->checkMenuItems($menu, $expected); } - public function testAddCheckboxItem() : void + public function testAddCheckboxItem(): void { $callable = function () { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addCheckboxItem('Item 1', $callable); $builder->addCheckboxItem('Item 2', $callable); @@ -429,12 +430,12 @@ public function testAddCheckboxItem() : void $this->checkMenuItems($menu, $expected); } - public function testAddRadioItem() : void + public function testAddRadioItem(): void { $callable = function () { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addRadioItem('Item 1', $callable); $builder->addRadioItem('Item 2', $callable); @@ -454,12 +455,12 @@ public function testAddRadioItem() : void $this->checkMenuItems($menu, $expected); } - public function testAddMultipleRadioItems() : void + public function testAddMultipleRadioItems(): void { $callable = function () { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addRadioItems([ ['Item 1', $callable], @@ -481,27 +482,27 @@ public function testAddMultipleRadioItems() : void $this->checkMenuItems($menu, $expected); } - public function testAddStaticItem() : void + public function testAddStaticItem(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addStaticItem('Static Item 1'); $menu = $builder->build(); - + $expected = [ [ 'class' => StaticItem::class, 'text' => 'Static Item 1', ] ]; - + $this->checkMenuItems($menu, $expected); } - public function testAddLineBreakItem() : void + public function testAddLineBreakItem(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addLineBreak('Line Break Item 1'); $menu = $builder->build(); @@ -517,9 +518,9 @@ public function testAddLineBreakItem() : void $this->checkMenuItems($menu, $expected); } - public function testAddLineBreakItemWithNumLines() : void + public function testAddLineBreakItemWithNumLines(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addLineBreak('Line Break Item 1', 3); $menu = $builder->build(); @@ -535,9 +536,9 @@ public function testAddLineBreakItemWithNumLines() : void $this->checkMenuItems($menu, $expected); } - public function testAsciiArtWithDefaultPosition() : void + public function testAsciiArtWithDefaultPosition(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addAsciiArt("//\n//"); $menu = $builder->build(); @@ -553,9 +554,9 @@ public function testAsciiArtWithDefaultPosition() : void $this->checkMenuItems($menu, $expected); } - public function testAsciiArtWithSpecificPosition() : void + public function testAsciiArtWithSpecificPosition(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addAsciiArt("//\n//", AsciiArtItem::POSITION_LEFT); $menu = $builder->build(); @@ -571,9 +572,9 @@ public function testAsciiArtWithSpecificPosition() : void $this->checkMenuItems($menu, $expected); } - public function testAsciiArtWithAlt() : void + public function testAsciiArtWithAlt(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addAsciiArt("//\n//", AsciiArtItem::POSITION_LEFT, 'Some ALT'); $menu = $builder->build(); @@ -590,15 +591,15 @@ public function testAsciiArtWithAlt() : void $this->checkMenuItems($menu, $expected); } - public function testAddSubMenu() : void + public function testAddSubMenu(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addSubMenu('My SubMenu', function () { }); - + $menu = $builder->build(); - + $this->checkMenuItems($menu, [ [ 'class' => MenuMenuItem::class @@ -606,11 +607,11 @@ public function testAddSubMenu() : void ]); } - public function testAddSubMenuWithBuilder() : void + public function testAddSubMenuWithBuilder(): void { - $subMenuBuilder = new CliMenuBuilder; - - $builder = new CliMenuBuilder; + $subMenuBuilder = new CliMenuBuilder(); + + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addSubMenuFromBuilder('My SubMenu', $subMenuBuilder); @@ -623,11 +624,11 @@ public function testAddSubMenuWithBuilder() : void ]); } - public function testAddSubMenuUsesTextParameterAsMenuItemText() : void + public function testAddSubMenuUsesTextParameterAsMenuItemText(): void { - $subMenuBuilder = new CliMenuBuilder; + $subMenuBuilder = new CliMenuBuilder(); - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addSubMenuFromBuilder('My SubMenu', $subMenuBuilder); @@ -636,7 +637,7 @@ public function testAddSubMenuUsesTextParameterAsMenuItemText() : void self::assertEquals('My SubMenu', $menu->getItems()[0]->getText()); } - public function testSubMenuInheritsParentsStyle() : void + public function testSubMenuInheritsParentsStyle(): void { $terminal = self::createMock(Terminal::class); $terminal @@ -666,7 +667,7 @@ public function testSubMenuInheritsParentsStyle() : void self::assertEquals($menu->getStyle(), $subMenu2->getStyle()); } - public function testSplitItemSubMenuInheritsParentsStyle() : void + public function testSplitItemSubMenuInheritsParentsStyle(): void { $terminal = self::createMock(Terminal::class); $terminal @@ -699,7 +700,7 @@ public function testSplitItemSubMenuInheritsParentsStyle() : void self::assertEquals($menu->getStyle(), $subMenu->getStyle()); } - public function testSubMenuIgnoresParentsStyleIfCustomAndPassesToChildren() : void + public function testSubMenuIgnoresParentsStyleIfCustomAndPassesToChildren(): void { $terminal = self::createMock(Terminal::class); $terminal @@ -728,9 +729,9 @@ public function testSubMenuIgnoresParentsStyleIfCustomAndPassesToChildren() : vo self::assertSame('yellow', $subMenu2->getStyle()->getBg()); } - public function testSubMenuDoesNotInheritsParentsStyleWhenSubMenuStyleHasAlterations() : void + public function testSubMenuDoesNotInheritsParentsStyleWhenSubMenuStyleHasAlterations(): void { - $menu = (new CliMenuBuilder) + $menu = (new CliMenuBuilder()) ->setBackgroundColour('green') ->addSubMenu('My SubMenu', function (CliMenuBuilder $b) { $b->addItem('Some Item', function () { @@ -743,9 +744,9 @@ public function testSubMenuDoesNotInheritsParentsStyleWhenSubMenuStyleHasAlterat self::assertSame('green', $menu->getStyle()->getBg()); } - public function testSubMenuDefaultItems() : void + public function testSubMenuDefaultItems(): void { - $menu = (new CliMenuBuilder) + $menu = (new CliMenuBuilder()) ->disableDefaultItems() ->addSubMenu('My SubMenu', function () { }) @@ -765,16 +766,16 @@ public function testSubMenuDefaultItems() : void $this->checkMenuItems($menu->getItems()[0]->getSubMenu(), $expected); } - public function testModifyExitAndGoBackTextOnSubMenu() : void + public function testModifyExitAndGoBackTextOnSubMenu(): void { - $menu = (new CliMenuBuilder) + $menu = (new CliMenuBuilder()) ->disableDefaultItems() ->addSubMenu('My SubMenu', function (CliMenuBuilder $b) { $b->setExitButtonText("Won't you stay a little while longer?") ->setGoBackButtonText("Don't click this - it's definitely not a go back button"); }) ->build(); - + $expected = [ [ @@ -790,42 +791,42 @@ public function testModifyExitAndGoBackTextOnSubMenu() : void $this->checkMenuItems($menu->getItems()[0]->getSubMenu(), $expected); } - public function testDisableDefaultItemsDisablesExitAndGoBackOnSubMenu() : void + public function testDisableDefaultItemsDisablesExitAndGoBackOnSubMenu(): void { - $menu = (new CliMenuBuilder) + $menu = (new CliMenuBuilder()) ->disableDefaultItems() ->addSubMenu('My SubMenu', function (CliMenuBuilder $b) { $b->disableDefaultItems(); }) ->build(); - + self::assertEquals($menu->getItems()[0]->getSubMenu()->getItems(), []); } - public function testThrowsExceptionWhenDisablingRootMenu() : void + public function testThrowsExceptionWhenDisablingRootMenu(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('You can\'t disable the root menu'); - (new CliMenuBuilder)->disableMenu(); + (new CliMenuBuilder())->disableMenu(); } #[DataProvider('marginBelowZeroProvider')] - public function testSetMarginThrowsExceptionIfValueIsNotZeroOrAbove(int $value) : void + public function testSetMarginThrowsExceptionIfValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); - - - (new CliMenuBuilder)->setMargin($value)->build(); + + + (new CliMenuBuilder())->setMargin($value)->build(); } - public static function marginBelowZeroProvider() : array + public static function marginBelowZeroProvider(): array { return [[-1], [-2], [-10]]; } #[DataProvider('marginAboveZeroProvider')] - public function testSetMarginAcceptsZeroAndPositiveIntegers(int $value) : void + public function testSetMarginAcceptsZeroAndPositiveIntegers(int $value): void { $terminal = self::createMock(Terminal::class); $terminal @@ -834,16 +835,16 @@ public function testSetMarginAcceptsZeroAndPositiveIntegers(int $value) : void ->willReturn(200); $menu = (new CliMenuBuilder($terminal))->setMargin($value)->build(); - + self::assertSame($value, $menu->getStyle()->getMargin()); } - public static function marginAboveZeroProvider() : array + public static function marginAboveZeroProvider(): array { return [[0], [1], [10], [50]]; } - public function testSetMarginAutoAutomaticallyCalculatesMarginToCenter() : void + public function testSetMarginAutoAutomaticallyCalculatesMarginToCenter(): void { $terminal = self::createMock(Terminal::class); $terminal @@ -856,11 +857,11 @@ public function testSetMarginAutoAutomaticallyCalculatesMarginToCenter() : void ->setMarginAuto() ->setWidth(100) ->build(); - + self::assertSame(50, $menu->getStyle()->getMargin()); } - public function testSetMarginAutoOverwritesSetMargin() : void + public function testSetMarginAutoOverwritesSetMargin(): void { $terminal = self::createMock(Terminal::class); $terminal @@ -878,7 +879,7 @@ public function testSetMarginAutoOverwritesSetMargin() : void self::assertSame(50, $menu->getStyle()->getMargin()); } - public function testSetMarginManuallyOverwritesSetMarginAuto() : void + public function testSetMarginManuallyOverwritesSetMarginAuto(): void { $terminal = self::createMock(Terminal::class); $terminal @@ -896,20 +897,20 @@ public function testSetMarginManuallyOverwritesSetMarginAuto() : void self::assertSame(10, $menu->getStyle()->getMargin()); } - public function testSetPaddingWithUniversalValue() : void + public function testSetPaddingWithUniversalValue(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setPadding(3); $style = $builder->build()->getStyle(); - + self::assertEquals(3, $style->getPaddingTopBottom()); self::assertEquals(3, $style->getPaddingLeftRight()); } - public function testSetPaddingWithXAndYValues() : void + public function testSetPaddingWithXAndYValues(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setPadding(2, 3); $style = $builder->build()->getStyle(); @@ -918,9 +919,9 @@ public function testSetPaddingWithXAndYValues() : void self::assertEquals(3, $style->getPaddingLeftRight()); } - public function testSetPaddingTopAndBottom() : void + public function testSetPaddingTopAndBottom(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setPaddingTopBottom(2); $style = $builder->build()->getStyle(); @@ -928,9 +929,9 @@ public function testSetPaddingTopAndBottom() : void self::assertEquals(2, $style->getPaddingTopBottom()); } - public function testSetPaddingLeftAndRight() : void + public function testSetPaddingLeftAndRight(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->setPaddingLeftRight(3); $style = $builder->build()->getStyle(); @@ -938,9 +939,9 @@ public function testSetPaddingLeftAndRight() : void self::assertEquals(3, $style->getPaddingLeftRight()); } - public function testAddSubMenuWithClosureBinding() : void + public function testAddSubMenuWithClosureBinding(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addSubMenu('My SubMenu', function (CliMenuBuilder $b) { $b->disableDefaultItems(); @@ -960,9 +961,9 @@ public function testAddSubMenuWithClosureBinding() : void $this->checkMenuItems($menu->getItems()[0]->getSubMenu(), $expected); } - public function testAddSplitItemWithClosureBinding() : void + public function testAddSplitItemWithClosureBinding(): void { - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->disableDefaultItems(); $builder->addSplitItem(function (SplitItemBuilder $b) { $b->addItem('My Item', function () { @@ -981,11 +982,11 @@ public function testAddSplitItemWithClosureBinding() : void $this->checkItems($menu->getItems()[0]->getItems(), $expected); } - public function testDisplayExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void + public function testDisplayExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra(): void { $cb = function () { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->addItem('Item 1', $cb); $builder->addItem('Item 2', $cb); $builder->displayExtra(); @@ -995,11 +996,11 @@ public function testDisplayExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra( self::assertTrue($menu->getStyle()->getDisplaysExtra()); } - public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void + public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra(): void { $cb = function () { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->addItem('Item 1', $cb); $builder->addItem('Item 2', $cb); $builder->setItemExtra('DONE'); @@ -1009,12 +1010,12 @@ public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplay self::assertTrue($menu->getStyle()->getDisplaysExtra()); } - private function checkMenuItems(CliMenu $menu, array $expected) : void + private function checkMenuItems(CliMenu $menu, array $expected): void { $this->checkItems($menu->getItems(), $expected); } - private function checkItems(array $actualItems, array $expected) : void + private function checkItems(array $actualItems, array $expected): void { $propMap = [ 'breakChar' => 'getText', @@ -1040,15 +1041,15 @@ private function checkItems(array $actualItems, array $expected) : void } } - public function testRegisterItemStylePropagatesToSubmenus() : void + public function testRegisterItemStylePropagatesToSubmenus(): void { - $myItem = new class extends LineBreakItem { + $myItem = new class () extends LineBreakItem { }; - $myStyle = new class extends DefaultStyle { + $myStyle = new class () extends DefaultStyle { }; - $builder = new CliMenuBuilder; + $builder = new CliMenuBuilder(); $builder->registerItemStyle(get_class($myItem), $myStyle); $builder ->addSubMenu('My SubMenu', function (CliMenuBuilder $b) use ($myItem) { diff --git a/test/Builder/SplitItemBuilderTest.php b/test/Builder/SplitItemBuilderTest.php index 971f633a..54a59850 100644 --- a/test/Builder/SplitItemBuilderTest.php +++ b/test/Builder/SplitItemBuilderTest.php @@ -1,4 +1,5 @@ checkItemItems($item, $expected); } - public function testAddCheckboxItem() : void + public function testAddCheckboxItem(): void { $callable = function () { }; @@ -68,7 +69,7 @@ public function testAddCheckboxItem() : void $this->checkItemItems($item, $expected); } - public function testAddRadioItem() : void + public function testAddRadioItem(): void { $callable = function () { }; @@ -93,7 +94,7 @@ public function testAddRadioItem() : void $this->checkItemItems($item, $expected); } - public function testAddStaticItem() : void + public function testAddStaticItem(): void { $menu = new CliMenu(null, []); @@ -111,7 +112,7 @@ public function testAddStaticItem() : void $this->checkItemItems($item, $expected); } - public function testAddSubMenu() : void + public function testAddSubMenu(): void { $menu = new CliMenu(null, []); $builder = new SplitItemBuilder($menu); @@ -127,7 +128,7 @@ public function testAddSubMenu() : void ]); } - public function testAddSubMenuUsesTextParameterAsMenuItemText() : void + public function testAddSubMenuUsesTextParameterAsMenuItemText(): void { $menu = new CliMenu(null, []); $builder = new SplitItemBuilder($menu); @@ -139,7 +140,7 @@ public function testAddSubMenuUsesTextParameterAsMenuItemText() : void self::assertEquals('My SubMenu', $item->getItems()[0]->getText()); } - public function testSetGutter() : void + public function testSetGutter(): void { $menu = new CliMenu(null, []); $builder = new SplitItemBuilder($menu); @@ -149,7 +150,7 @@ public function testSetGutter() : void self::assertEquals(4, $item->getGutter()); } - public function testAddSubMenuWithClosureBinding() : void + public function testAddSubMenuWithClosureBinding(): void { $menu = new CliMenu(null, []); $builder = new SplitItemBuilder($menu); @@ -174,12 +175,12 @@ public function testAddSubMenuWithClosureBinding() : void ); } - private function checkItemItems(SplitItem $item, array $expected) : void + private function checkItemItems(SplitItem $item, array $expected): void { $this->checkItems($item->getItems(), $expected); } - private function checkItems(array $actualItems, array $expected) : void + private function checkItems(array $actualItems, array $expected): void { self::assertCount(count($expected), $actualItems); @@ -195,12 +196,12 @@ private function checkItems(array $actualItems, array $expected) : void } } - public function testRegisterItemStylePropagatesToSubmenus() : void + public function testRegisterItemStylePropagatesToSubmenus(): void { - $myItem = new class extends LineBreakItem { + $myItem = new class () extends LineBreakItem { }; - $myStyle = new class extends DefaultStyle { + $myStyle = new class () extends DefaultStyle { }; $menu = new CliMenu(null, []); diff --git a/test/CliMenuTest.php b/test/CliMenuTest.php index 619f11ac..5b23bab3 100644 --- a/test/CliMenuTest.php +++ b/test/CliMenuTest.php @@ -1,4 +1,5 @@ output = new BufferedOutput; + $this->output = new BufferedOutput(); $this->terminal = $this->createMock(Terminal::class); $this->terminal->expects($this->any()) @@ -50,7 +51,7 @@ public function setUp() : void }); } - public function testGetMenuStyle() : void + public function testGetMenuStyle(): void { $menu = new CliMenu('PHP School FTW', []); self::assertInstanceOf(MenuStyle::class, $menu->getStyle()); @@ -60,7 +61,7 @@ public function testGetMenuStyle() : void self::assertSame($style, $menu->getStyle()); } - public function testReDrawThrowsExceptionIfMenuNotOpen() : void + public function testReDrawThrowsExceptionIfMenuNotOpen(): void { $menu = new CliMenu('PHP School FTW', []); @@ -69,7 +70,7 @@ public function testReDrawThrowsExceptionIfMenuNotOpen() : void $menu->redraw(); } - public function testSimpleOpenClose() : void + public function testSimpleOpenClose(): void { $this->terminal->expects($this->once()) ->method('read') @@ -87,7 +88,7 @@ public function testSimpleOpenClose() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSimpleOpenCloseWithBorders() : void + public function testSimpleOpenCloseWithBorders(): void { $this->terminal->expects($this->once()) ->method('read') @@ -106,7 +107,7 @@ public function testSimpleOpenCloseWithBorders() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSimpleOpenCloseWithLeftAndRightBorders() : void + public function testSimpleOpenCloseWithLeftAndRightBorders(): void { $this->terminal->expects($this->once()) ->method('read') @@ -127,7 +128,7 @@ public function testSimpleOpenCloseWithLeftAndRightBorders() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSimpleOpenCloseWithMarginAutoAndBorders() : void + public function testSimpleOpenCloseWithMarginAutoAndBorders(): void { $this->terminal->expects($this->once()) ->method('read') @@ -148,7 +149,7 @@ public function testSimpleOpenCloseWithMarginAutoAndBorders() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSimpleOpenCloseWithPaddingTopAndBottom() : void + public function testSimpleOpenCloseWithPaddingTopAndBottom(): void { $this->terminal->expects($this->once()) ->method('read') @@ -167,7 +168,7 @@ public function testSimpleOpenCloseWithPaddingTopAndBottom() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSimpleOpenCloseWithPaddingLeftAndRight() : void + public function testSimpleOpenCloseWithPaddingLeftAndRight(): void { $this->terminal->expects($this->once()) ->method('read') @@ -186,7 +187,7 @@ public function testSimpleOpenCloseWithPaddingLeftAndRight() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSimpleOpenCloseWithDifferentXAndYPadding() : void + public function testSimpleOpenCloseWithDifferentXAndYPadding(): void { $this->terminal->expects($this->once()) ->method('read') @@ -206,7 +207,7 @@ public function testSimpleOpenCloseWithDifferentXAndYPadding() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testReDrawReDrawsImmediately() : void + public function testReDrawReDrawsImmediately(): void { $this->terminal->expects($this->once()) ->method('read') @@ -226,7 +227,7 @@ public function testReDrawReDrawsImmediately() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testRedrawClearsTerminalFirstIfOptionIsPassed() : void + public function testRedrawClearsTerminalFirstIfOptionIsPassed(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any()) @@ -278,13 +279,13 @@ public function testRedrawClearsTerminalFirstIfOptionIsPassed() : void static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testGetItems() : void + public function testGetItems(): void { $item1 = new LineBreakItem(); $item2 = new LineBreakItem(); - $style = $this->getStyle($terminal = new MockTerminal); + $style = $this->getStyle($terminal = new MockTerminal()); $menu = new CliMenu( 'PHP School FTW', @@ -299,12 +300,12 @@ public function testGetItems() : void self::assertSame([$item1, $item2], $menu->getItems()); } - public function testRemoveItem() : void + public function testRemoveItem(): void { $item1 = new LineBreakItem(); $item2 = new LineBreakItem(); - $style = $this->getStyle($terminal = new MockTerminal); + $style = $this->getStyle($terminal = new MockTerminal()); $menu = new CliMenu( 'PHP School FTW', @@ -324,7 +325,7 @@ public function testRemoveItem() : void self::assertContains($item2, $menu->getItems()); } - public function testRemoveItemThrowsExceptionWhenItemDoesntExistInMenu() : void + public function testRemoveItemThrowsExceptionWhenItemDoesntExistInMenu(): void { $this->expectException(\InvalidArgumentException::class); @@ -335,21 +336,21 @@ public function testRemoveItemThrowsExceptionWhenItemDoesntExistInMenu() : void $menu->removeItem($item1); } - public function testFlashThrowsExceptionIfParameterContainsNewline() : void + public function testFlashThrowsExceptionIfParameterContainsNewline(): void { $this->expectException(\InvalidArgumentException::class); $menu = new CliMenu('PHP School FTW', []); $menu->flash("Foo\nBar"); } - public function testConfirmThrowsExceptionIfParameterContainsNewline() : void + public function testConfirmThrowsExceptionIfParameterContainsNewline(): void { $this->expectException(\InvalidArgumentException::class); $menu = new CliMenu('PHP School FTW', []); $menu->confirm("Foo\nBar"); } - public function testThrowsExceptionIfTerminalIsNotValidTTY() : void + public function testThrowsExceptionIfTerminalIsNotValidTTY(): void { $this->expectException(\PhpSchool\CliMenu\Exception\InvalidTerminalException::class); @@ -367,7 +368,7 @@ public function testThrowsExceptionIfTerminalIsNotValidTTY() : void $menu->open(); } - public function testOpenThrowsExceptionIfNoItemsInMenu() : void + public function testOpenThrowsExceptionIfNoItemsInMenu(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Menu must have at least 1 item before it can be opened'); @@ -375,13 +376,13 @@ public function testOpenThrowsExceptionIfNoItemsInMenu() : void (new CliMenu('PHP School FTW', [], $this->terminal))->open(); } - public function testGetTerminal() : void + public function testGetTerminal(): void { $menu = new CliMenu('PHP School FTW', []); self::assertInstanceOf(UnixTerminal::class, $menu->getTerminal()); } - public function testAddItem() : void + public function testAddItem(): void { $menu = new CliMenu('PHP School FTW', []); @@ -396,7 +397,7 @@ public function testAddItem() : void $this->assertCount(1, $menu->getItems()); } - public function testAddItems() : void + public function testAddItems(): void { $menu = new CliMenu('PHP School FTW', []); @@ -415,7 +416,7 @@ public function testAddItems() : void $this->assertCount(2, $menu->getItems()); } - public function testSetItems() : void + public function testSetItems(): void { $menu = new CliMenu('PHP School FTW', []); @@ -447,7 +448,7 @@ public function testSetItems() : void $this->assertSame([$item3, $item4], $menu->getItems()); } - public function testAskNumberThrowsExceptionIfMenuNotOpen() : void + public function testAskNumberThrowsExceptionIfMenuNotOpen(): void { $menu = new CliMenu('PHP School FTW', []); @@ -456,7 +457,7 @@ public function testAskNumberThrowsExceptionIfMenuNotOpen() : void $menu->askNumber(); } - public function testAskNumberStyle() : void + public function testAskNumberStyle(): void { $terminal = $this->createMock(Terminal::class); @@ -485,7 +486,7 @@ public function testAskNumberStyle() : void self::assertEquals('red', $number->getStyle()->getFg()); } - public function testAskTextThrowsExceptionIfMenuNotOpen() : void + public function testAskTextThrowsExceptionIfMenuNotOpen(): void { $menu = new CliMenu('PHP School FTW', []); @@ -494,7 +495,7 @@ public function testAskTextThrowsExceptionIfMenuNotOpen() : void $menu->askText(); } - public function testAskTextStyle() : void + public function testAskTextStyle(): void { $terminal = $this->createMock(Terminal::class); @@ -523,7 +524,7 @@ public function testAskTextStyle() : void self::assertEquals('red', $text->getStyle()->getFg()); } - public function testAskPasswordThrowsExceptionIfMenuNotOpen() : void + public function testAskPasswordThrowsExceptionIfMenuNotOpen(): void { $menu = new CliMenu('PHP School FTW', []); @@ -532,7 +533,7 @@ public function testAskPasswordThrowsExceptionIfMenuNotOpen() : void $menu->askPassword(); } - public function testAskPasswordStyle() : void + public function testAskPasswordStyle(): void { $terminal = $this->createMock(Terminal::class); @@ -561,7 +562,7 @@ public function testAskPasswordStyle() : void self::assertEquals('red', $password->getStyle()->getFg()); } - public function testAddCustomControlMappingThrowsExceptionWhenOverwritingExistingDefaultControls() : void + public function testAddCustomControlMappingThrowsExceptionWhenOverwritingExistingDefaultControls(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot rebind this input'); @@ -571,7 +572,7 @@ public function testAddCustomControlMappingThrowsExceptionWhenOverwritingExistin }); } - public function testAddCustomControlMappingThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap() : void + public function testAddCustomControlMappingThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot rebind this input'); @@ -583,7 +584,7 @@ public function testAddCustomControlMappingThrowsExceptionWhenAttemptingToOverwr }); } - public function testAddCustomControlMapping() : void + public function testAddCustomControlMapping(): void { $this->terminal->expects($this->once()) ->method('read') @@ -604,7 +605,7 @@ public function testAddCustomControlMapping() : void } - public function testAddCustomControlMappingWithControlChar() : void + public function testAddCustomControlMappingWithControlChar(): void { $this->terminal->expects($this->once()) ->method('read') @@ -624,7 +625,7 @@ public function testAddCustomControlMappingWithControlChar() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testAddCustomControlMappingsThrowsExceptionWhenOverwritingExistingDefaultControls() : void + public function testAddCustomControlMappingsThrowsExceptionWhenOverwritingExistingDefaultControls(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot rebind this input'); @@ -636,7 +637,7 @@ public function testAddCustomControlMappingsThrowsExceptionWhenOverwritingExisti ]); } - public function testAddCustomControlMappingsThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap() : void + public function testAddCustomControlMappingsThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot rebind this input'); @@ -652,7 +653,7 @@ public function testAddCustomControlMappingsThrowsExceptionWhenAttemptingToOverw ]); } - public function testAddCustomControlMappings() : void + public function testAddCustomControlMappings(): void { $this->terminal->expects($this->any()) ->method('read') @@ -678,7 +679,7 @@ public function testAddCustomControlMappings() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testRemoveCustomControlMappingThrowsExceptionIfNoSuchMappingExists() : void + public function testRemoveCustomControlMappingThrowsExceptionIfNoSuchMappingExists(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('This input is not registered'); @@ -687,7 +688,7 @@ public function testRemoveCustomControlMappingThrowsExceptionIfNoSuchMappingExis $menu->removeCustomControlMapping('c'); } - public function testRemoveCustomControlMapping() : void + public function testRemoveCustomControlMapping(): void { $action = function (CliMenu $menu) { $menu->close(); @@ -701,7 +702,7 @@ public function testRemoveCustomControlMapping() : void self::assertSame([], $menu->getCustomControlMappings()); } - public function testSplitItemWithNoSelectableItemsScrollingVertically() : void + public function testSplitItemWithNoSelectableItemsScrollingVertically(): void { $this->terminal->expects($this->exactly(3)) ->method('read') @@ -721,7 +722,7 @@ public function testSplitItemWithNoSelectableItemsScrollingVertically() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSplitItemWithSelectableItemsScrollingVertical() : void + public function testSplitItemWithSelectableItemsScrollingVertical(): void { $this->terminal->expects($this->exactly(4)) ->method('read') @@ -748,7 +749,7 @@ public function testSplitItemWithSelectableItemsScrollingVertical() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSplitItemWithSelectableItemsScrollingRight() : void + public function testSplitItemWithSelectableItemsScrollingRight(): void { $this->terminal->expects($this->exactly(6)) ->method('read') @@ -775,7 +776,7 @@ public function testSplitItemWithSelectableItemsScrollingRight() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSplitItemWithSelectableItemsScrollingLeft() : void + public function testSplitItemWithSelectableItemsScrollingLeft(): void { $this->terminal->expects($this->exactly(6)) ->method('read') @@ -806,7 +807,7 @@ public function testSplitItemWithSelectableItemsScrollingLeft() : void self::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testSplitItemWithSelectableAndStaticItemsScrollingHorizontally() : void + public function testSplitItemWithSelectableAndStaticItemsScrollingHorizontally(): void { $this->terminal->expects($this->exactly(6)) ->method('read') @@ -838,7 +839,7 @@ public function testSplitItemWithSelectableAndStaticItemsScrollingHorizontally() } - public function testSelectableCallableReceivesSelectableAndNotSplitItem() : void + public function testSelectableCallableReceivesSelectableAndNotSplitItem(): void { $this->terminal->expects($this->exactly(1)) ->method('read') @@ -866,7 +867,7 @@ public function testSelectableCallableReceivesSelectableAndNotSplitItem() : void self::assertSame($expectedSelectedItem, $actualSelectedItem); } - public function testAddItemSelectsFirstSelectableItemWhenItemsExistButNoneAreSelectable() : void + public function testAddItemSelectsFirstSelectableItemWhenItemsExistButNoneAreSelectable(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); $menu->addItem(new StaticItem('No Selectable')); @@ -883,7 +884,7 @@ public function testAddItemSelectsFirstSelectableItemWhenItemsExistButNoneAreSel self::assertEquals($item, $menu->getSelectedItem()); } - public function testAddItemsSelectsFirstSelectableItemWhenItemsExistButNoneAreSelectable() : void + public function testAddItemsSelectsFirstSelectableItemWhenItemsExistButNoneAreSelectable(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); $menu->addItem(new StaticItem('No Selectable')); @@ -900,7 +901,7 @@ public function testAddItemsSelectsFirstSelectableItemWhenItemsExistButNoneAreSe self::assertEquals($item, $menu->getSelectedItem()); } - public function testSetItemsReSelectsFirstSelectableItem() : void + public function testSetItemsReSelectsFirstSelectableItem(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); $menu->addItem(new StaticItem('No Selectable')); @@ -915,7 +916,7 @@ public function testSetItemsReSelectsFirstSelectableItem() : void self::assertEquals($item2, $menu->getSelectedItem()); } - public function testRemoveItemReSelectsFirstSelectableItemIfSelectedItemRemoved() : void + public function testRemoveItemReSelectsFirstSelectableItemIfSelectedItemRemoved(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); $menu->addItem(new StaticItem('No Selectable')); @@ -946,7 +947,7 @@ public function testRemoveItemReSelectsFirstSelectableItemIfSelectedItemRemoved( self::assertEquals($item2, $menu->getSelectedItem()); } - public function testGetSelectedItemThrowsExceptionIfNoSelectedItem() : void + public function testGetSelectedItemThrowsExceptionIfNoSelectedItem(): void { self::expectException(\RuntimeException::class); self::expectExceptionMessage('No selected item'); @@ -956,7 +957,7 @@ public function testGetSelectedItemThrowsExceptionIfNoSelectedItem() : void $menu->getSelectedItem(); } - public function testMenuCanOpenAndFunctionWithoutAnySelectableItems() : void + public function testMenuCanOpenAndFunctionWithoutAnySelectableItems(): void { $this->terminal->expects($this->exactly(3)) ->method('read') @@ -970,7 +971,7 @@ public function testMenuCanOpenAndFunctionWithoutAnySelectableItems() : void self::assertCount(1, $menu->getItems()); } - public function testSetSelectedItemThrowsExceptionIfItemDoesNotExistInMenu() : void + public function testSetSelectedItemThrowsExceptionIfItemDoesNotExistInMenu(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); $menu->addItem($item1 = new SelectableItem('Selectable 1', function () { @@ -986,7 +987,7 @@ public function testSetSelectedItemThrowsExceptionIfItemDoesNotExistInMenu() : v $menu->setSelectedItem($item3); } - public function testSetSelectedItem() : void + public function testSetSelectedItem(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); $menu->addItem($item1 = new SelectableItem('Selectable 1', function () { @@ -999,7 +1000,7 @@ public function testSetSelectedItem() : void self::assertSame($item2, $menu->getSelectedItem()); } - public function testGetSelectedItemIndexThrowsExceptionIfNoItemSelected() : void + public function testGetSelectedItemIndexThrowsExceptionIfNoItemSelected(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); @@ -1007,7 +1008,7 @@ public function testGetSelectedItemIndexThrowsExceptionIfNoItemSelected() : void $menu->getSelectedItemIndex(); } - public function testGetSelectedItemIndex() : void + public function testGetSelectedItemIndex(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); $menu->addItem($item1 = new SelectableItem('Selectable 1', function () { @@ -1020,7 +1021,7 @@ public function testGetSelectedItemIndex() : void self::assertSame(1, $menu->getSelectedItemIndex()); } - public function testGetItemByIndexThrowsExceptionIfItemDoesNotExistInMenu() : void + public function testGetItemByIndexThrowsExceptionIfItemDoesNotExistInMenu(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); @@ -1028,7 +1029,7 @@ public function testGetItemByIndexThrowsExceptionIfItemDoesNotExistInMenu() : vo $menu->getItemByIndex(3); } - public function testGetItemByIndex() : void + public function testGetItemByIndex(): void { $menu = new CliMenu('PHP School FTW', [], $this->terminal); @@ -1042,12 +1043,12 @@ public function testGetItemByIndex() : void self::assertSame($item2, $menu->getItemByIndex(1)); } - private function getTestFile() : string + private function getTestFile(): string { return sprintf('%s/res/%s.txt', __DIR__, $this->name()); } - private function getStyle(Terminal $terminal) : MenuStyle + private function getStyle(Terminal $terminal): MenuStyle { return new MenuStyle($terminal); } diff --git a/test/Dialogue/ConfirmTest.php b/test/Dialogue/ConfirmTest.php index 8ba7aa8b..ad3e715f 100644 --- a/test/Dialogue/ConfirmTest.php +++ b/test/Dialogue/ConfirmTest.php @@ -1,4 +1,5 @@ output = new BufferedOutput; + $this->output = new BufferedOutput(); $this->terminal = $this->createMock(Terminal::class); $this->terminal->expects($this->any()) @@ -45,7 +46,7 @@ public function setUp() : void }); } - public function testConfirmWithOddLengthConfirmAndButton() : void + public function testConfirmWithOddLengthConfirmAndButton(): void { $this->terminal ->method('read') @@ -69,7 +70,7 @@ public function testConfirmWithOddLengthConfirmAndButton() : void static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testConfirmWithEvenLengthConfirmAndButton() : void + public function testConfirmWithEvenLengthConfirmAndButton(): void { $this->terminal ->method('read') @@ -93,7 +94,7 @@ public function testConfirmWithEvenLengthConfirmAndButton() : void static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testConfirmWithEvenLengthConfirmAndOddLengthButton() : void + public function testConfirmWithEvenLengthConfirmAndOddLengthButton(): void { $this->terminal ->method('read') @@ -117,7 +118,7 @@ public function testConfirmWithEvenLengthConfirmAndOddLengthButton() : void static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testConfirmWithOddLengthConfirmAndEvenLengthButton() : void + public function testConfirmWithOddLengthConfirmAndEvenLengthButton(): void { $this->terminal ->method('read') @@ -189,7 +190,7 @@ public function testConfirmCancellableWithLongPrompt(): void static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testConfirmCanOnlyBeClosedWithEnter() : void + public function testConfirmCanOnlyBeClosedWithEnter(): void { $this->terminal ->method('read') @@ -288,12 +289,12 @@ public function testConfirmCancelCancellableReturnsFalse() static::assertFalse($return); } - private function getTestFile() : string + private function getTestFile(): string { return sprintf('%s/../res/%s.txt', __DIR__, $this->name()); } - private function getStyle(Terminal $terminal) : MenuStyle + private function getStyle(Terminal $terminal): MenuStyle { return new MenuStyle($terminal); } diff --git a/test/Dialogue/FlashTest.php b/test/Dialogue/FlashTest.php index 91e4bc69..8078ba52 100644 --- a/test/Dialogue/FlashTest.php +++ b/test/Dialogue/FlashTest.php @@ -1,4 +1,5 @@ output = new BufferedOutput; + $this->output = new BufferedOutput(); $this->terminal = $this->createMock(Terminal::class); $this->terminal->expects($this->any()) @@ -46,7 +47,7 @@ public function setUp() : void }); } - public function testFlashWithOddLength() : void + public function testFlashWithOddLength(): void { $this->terminal ->method('read') @@ -70,7 +71,7 @@ public function testFlashWithOddLength() : void static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public function testFlashWithEvenLength() : void + public function testFlashWithEvenLength(): void { $this->terminal ->method('read') @@ -95,7 +96,7 @@ public function testFlashWithEvenLength() : void } #[DataProvider('keyProvider')] - public function testFlashCanBeClosedWithAnyKey(string $key) : void + public function testFlashCanBeClosedWithAnyKey(string $key): void { $this->terminal ->method('read') @@ -116,7 +117,7 @@ public function testFlashCanBeClosedWithAnyKey(string $key) : void static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch()); } - public static function keyProvider() : array + public static function keyProvider(): array { return [ ["\n"], @@ -126,12 +127,12 @@ public static function keyProvider() : array ]; } - private function getTestFile() : string + private function getTestFile(): string { return sprintf('%s/../res/%s.txt', __DIR__, $this->name()); } - private function getStyle(Terminal $terminal) : MenuStyle + private function getStyle(Terminal $terminal): MenuStyle { return new MenuStyle($terminal); } diff --git a/test/Exception/CannotShrinkMenuExceptionTest.php b/test/Exception/CannotShrinkMenuExceptionTest.php index 58e84421..7fd7e91a 100644 --- a/test/Exception/CannotShrinkMenuExceptionTest.php +++ b/test/Exception/CannotShrinkMenuExceptionTest.php @@ -9,7 +9,7 @@ class CannotShrinkMenuExceptionTest extends TestCase { - public function testException() : void + public function testException(): void { $e = CannotShrinkMenuException::fromMarginAndTerminalWidth(10, 15); $this->assertEquals( diff --git a/test/Exception/InvalidTerminalExceptionTest.php b/test/Exception/InvalidTerminalExceptionTest.php index 19d4afe3..3cfdb0ee 100644 --- a/test/Exception/InvalidTerminalExceptionTest.php +++ b/test/Exception/InvalidTerminalExceptionTest.php @@ -1,4 +1,5 @@ assertEquals('error', $e->getMessage()); diff --git a/test/Exception/MenuNotOpenExceptionTest.php b/test/Exception/MenuNotOpenExceptionTest.php index a5ce1bd1..f315d389 100644 --- a/test/Exception/MenuNotOpenExceptionTest.php +++ b/test/Exception/MenuNotOpenExceptionTest.php @@ -1,4 +1,5 @@ assertEquals('error', $e->getMessage()); diff --git a/test/FrameTest.php b/test/FrameTest.php index 39d40527..7b06b439 100644 --- a/test/FrameTest.php +++ b/test/FrameTest.php @@ -1,4 +1,5 @@ newLine(); $this->assertEquals(["\n"], $frame->getRows()); - $frame = new Frame; + $frame = new Frame(); $frame->newLine(1); $this->assertEquals(["\n"], $frame->getRows()); - $frame = new Frame; + $frame = new Frame(); $frame->newLine(2); $this->assertEquals(["\n", "\n"], $frame->getRows()); } - public function testAddRows() : void + public function testAddRows(): void { - $frame = new Frame; + $frame = new Frame(); $frame->addRows(['one', 'two']); $this->assertEquals(['one', 'two'], $frame->getRows()); $frame->addRows(['three']); $this->assertEquals(['one', 'two', 'three'], $frame->getRows()); } - public function testAddRow() : void + public function testAddRow(): void { - $frame = new Frame; + $frame = new Frame(); $frame->addRow('one'); $this->assertEquals(['one'], $frame->getRows()); $frame->addRow('two'); $this->assertEquals(['one', 'two'], $frame->getRows()); } - public function testCount() : void + public function testCount(): void { - $frame = new Frame; + $frame = new Frame(); $frame->addRow('one'); $this->assertEquals(['one'], $frame->getRows()); $this->assertCount(1, $frame); @@ -58,9 +59,9 @@ public function testCount() : void $this->assertCount(2, $frame); } - public function testAll() : void + public function testAll(): void { - $frame = new Frame; + $frame = new Frame(); $frame->addRow('one'); $frame->addRows(["two", "three"]); $frame->newLine(2); diff --git a/test/Input/InputIOTest.php b/test/Input/InputIOTest.php index a31c0366..7398e1cd 100644 --- a/test/Input/InputIOTest.php +++ b/test/Input/InputIOTest.php @@ -1,4 +1,5 @@ terminal = $this->createMock(Terminal::class); $this->terminal ->method('getWidth') ->willReturn(100); - $this->output = new BufferedOutput; + $this->output = new BufferedOutput(); $this->menu = $this->createMock(CliMenu::class); $this->style = new MenuStyle($this->terminal); $this->inputIO = new InputIO($this->menu, $this->terminal); @@ -66,7 +67,7 @@ public function setUp() : void ->willReturn($parentStyle); } - public function testEnterReturnsOutputIfValid() : void + public function testEnterReturnsOutputIfValid(): void { $this->terminal ->expects($this->exactly(2)) @@ -80,7 +81,7 @@ public function testEnterReturnsOutputIfValid() : void echo $this->output->fetch(); } - public function testCustomControlFunctions() : void + public function testCustomControlFunctions(): void { $this->inputIO->registerControlCallback(InputCharacter::UP, function ($input) { $input = (int) $input; @@ -97,7 +98,7 @@ public function testCustomControlFunctions() : void self::assertEquals('11', $result->fetch()); } - public function testBackspaceDeletesPreviousCharacter() : void + public function testBackspaceDeletesPreviousCharacter(): void { $this->terminal ->expects($this->exactly(6)) @@ -109,10 +110,10 @@ public function testBackspaceDeletesPreviousCharacter() : void self::assertEquals('1', $result->fetch()); } - public function testValidationErrorCausesErrorMessageToBeDisplayed() : void + public function testValidationErrorCausesErrorMessageToBeDisplayed(): void { $input = new class ($this->inputIO, $this->style) extends Text { - public function validate(string $input) : bool + public function validate(string $input): bool { return $input[-1] === 'p'; } diff --git a/test/Input/InputResultTest.php b/test/Input/InputResultTest.php index a93dc46d..42fd8d72 100644 --- a/test/Input/InputResultTest.php +++ b/test/Input/InputResultTest.php @@ -1,4 +1,5 @@ fetch()); } diff --git a/test/Input/NumberTest.php b/test/Input/NumberTest.php index 2a0a2793..3b94fc5a 100644 --- a/test/Input/NumberTest.php +++ b/test/Input/NumberTest.php @@ -1,4 +1,5 @@ terminal = $this->createMock(Terminal::class); $menu = $this->createMock(CliMenu::class); @@ -41,7 +42,7 @@ public function setUp() : void $this->input = new Number($this->inputIO, $style); } - public function testGetSetPromptText() : void + public function testGetSetPromptText(): void { static::assertEquals('Enter a number:', $this->input->getPromptText()); @@ -49,7 +50,7 @@ public function testGetSetPromptText() : void static::assertEquals('Number please:', $this->input->getPromptText()); } - public function testGetSetValidationFailedText() : void + public function testGetSetValidationFailedText(): void { static::assertEquals('Not a valid number, try again', $this->input->getValidationFailedText()); @@ -57,7 +58,7 @@ public function testGetSetValidationFailedText() : void static::assertEquals('Failed!', $this->input->getValidationFailedText()); } - public function testGetSetPlaceholderText() : void + public function testGetSetPlaceholderText(): void { static::assertEquals('', $this->input->getPlaceholderText()); @@ -66,12 +67,12 @@ public function testGetSetPlaceholderText() : void } #[DataProvider('validateProvider')] - public function testValidate(string $value, bool $result) : void + public function testValidate(string $value, bool $result): void { static::assertEquals($this->input->validate($value), $result); } - public static function validateProvider() : array + public static function validateProvider(): array { return [ ['10', true], @@ -88,12 +89,12 @@ public static function validateProvider() : array ]; } - public function testFilterReturnsInputAsIs() : void + public function testFilterReturnsInputAsIs(): void { static::assertEquals('9999', $this->input->filter('9999')); } - public function testUpKeyIncrementsNumber() : void + public function testUpKeyIncrementsNumber(): void { $this->terminal ->expects($this->exactly(4)) @@ -103,7 +104,7 @@ public function testUpKeyIncrementsNumber() : void self::assertEquals(11, $this->input->ask()->fetch()); } - public function testDownKeyDecrementsNumber() : void + public function testDownKeyDecrementsNumber(): void { $this->terminal ->expects($this->exactly(4)) diff --git a/test/Input/PasswordTest.php b/test/Input/PasswordTest.php index bf806b05..c8abbbd3 100644 --- a/test/Input/PasswordTest.php +++ b/test/Input/PasswordTest.php @@ -1,4 +1,5 @@ terminal = $this->createMock(Terminal::class); $menu = $this->createMock(CliMenu::class); @@ -41,7 +42,7 @@ public function setUp() : void $this->input = new Password($this->inputIO, $style); } - public function testGetSetPromptText() : void + public function testGetSetPromptText(): void { static::assertEquals('Enter password:', $this->input->getPromptText()); @@ -49,7 +50,7 @@ public function testGetSetPromptText() : void static::assertEquals('Password please:', $this->input->getPromptText()); } - public function testGetSetValidationFailedText() : void + public function testGetSetValidationFailedText(): void { static::assertEquals('Invalid password, try again', $this->input->getValidationFailedText()); @@ -57,7 +58,7 @@ public function testGetSetValidationFailedText() : void static::assertEquals('Failed!', $this->input->getValidationFailedText()); } - public function testGetSetPlaceholderText() : void + public function testGetSetPlaceholderText(): void { static::assertEquals('', $this->input->getPlaceholderText()); @@ -66,12 +67,12 @@ public function testGetSetPlaceholderText() : void } #[DataProvider('validateProvider')] - public function testValidate(string $value, bool $result) : void + public function testValidate(string $value, bool $result): void { static::assertEquals($this->input->validate($value), $result); } - public static function validateProvider() : array + public static function validateProvider(): array { return [ ['10', false], @@ -80,12 +81,12 @@ public static function validateProvider() : array ]; } - public function testFilterConcealsPassword() : void + public function testFilterConcealsPassword(): void { static::assertEquals('****', $this->input->filter('pppp')); } - public function testAskPassword() : void + public function testAskPassword(): void { $this->terminal ->expects($this->exactly(17)) @@ -96,10 +97,10 @@ public function testAskPassword() : void } #[DataProvider('customValidateProvider')] - public function testValidateWithCustomValidator(string $value, bool $result) : void + public function testValidateWithCustomValidator(string $value, bool $result): void { $customValidate = function ($input) { - return preg_match('/\d/', $input) && preg_match('/[a-zA-Z]/', $input); + return preg_match('/\d/', $input) && preg_match('/[a-zA-Z]/', $input); }; $this->input->setValidator($customValidate); @@ -107,7 +108,7 @@ public function testValidateWithCustomValidator(string $value, bool $result) : v static::assertEquals($this->input->validate($value), $result); } - public static function customValidateProvider() : array + public static function customValidateProvider(): array { return [ ['10', false], @@ -118,7 +119,7 @@ public static function customValidateProvider() : array ]; } - public function testWithCustomValidatorAndCustomValidationMessage() : void + public function testWithCustomValidatorAndCustomValidationMessage(): void { $customValidate = function ($input) { if ($input === 'mypassword') { @@ -135,13 +136,13 @@ public function testWithCustomValidatorAndCustomValidationMessage() : void self::assertEquals('Password too generic', $this->input->getValidationFailedText()); } - public function testPasswordValidationWithDefaultLength() : void + public function testPasswordValidationWithDefaultLength(): void { self::assertFalse($this->input->validate(str_pad('a', 15))); self::assertTrue($this->input->validate(str_pad('a', 16))); } - public function testPasswordValidationWithDefinedLength() : void + public function testPasswordValidationWithDefinedLength(): void { $this->input->setPasswordLength(5); diff --git a/test/Input/TextTest.php b/test/Input/TextTest.php index 3f3241ef..869340dd 100644 --- a/test/Input/TextTest.php +++ b/test/Input/TextTest.php @@ -1,4 +1,5 @@ terminal = $this->createMock(Terminal::class); $menu = $this->createMock(CliMenu::class); @@ -41,7 +42,7 @@ public function setUp() : void $this->input = new Text($this->inputIO, $style); } - public function testGetSetPromptText() : void + public function testGetSetPromptText(): void { static::assertEquals('Enter text:', $this->input->getPromptText()); @@ -49,7 +50,7 @@ public function testGetSetPromptText() : void static::assertEquals('Text please:', $this->input->getPromptText()); } - public function testGetSetValidationFailedText() : void + public function testGetSetValidationFailedText(): void { static::assertEquals('Invalid, try again', $this->input->getValidationFailedText()); @@ -57,7 +58,7 @@ public function testGetSetValidationFailedText() : void static::assertEquals('Failed!', $this->input->getValidationFailedText()); } - public function testGetSetPlaceholderText() : void + public function testGetSetPlaceholderText(): void { static::assertEquals('', $this->input->getPlaceholderText()); @@ -66,12 +67,12 @@ public function testGetSetPlaceholderText() : void } #[DataProvider('validateProvider')] - public function testValidate(string $value, bool $result) : void + public function testValidate(string $value, bool $result): void { static::assertEquals($this->input->validate($value), $result); } - public static function validateProvider() : array + public static function validateProvider(): array { return [ ['', false], @@ -80,12 +81,12 @@ public static function validateProvider() : array ]; } - public function testFilterReturnsInputAsIs() : void + public function testFilterReturnsInputAsIs(): void { static::assertEquals('9999', $this->input->filter('9999')); } - public function testAskText() : void + public function testAskText(): void { $this->terminal ->expects($this->exactly(10)) diff --git a/test/MenuItem/AsciiArtItemTest.php b/test/MenuItem/AsciiArtItemTest.php index 17a4b771..94407096 100644 --- a/test/MenuItem/AsciiArtItemTest.php +++ b/test/MenuItem/AsciiArtItemTest.php @@ -1,4 +1,5 @@ assertFalse($item->canSelect()); } - public function testGetSelectActionReturnsNull() : void + public function testGetSelectActionReturnsNull(): void { $item = new AsciiArtItem('////\\\\'); $this->assertNull($item->getSelectAction()); } - public function testShowsItemExtraReturnsFalse() : void + public function testShowsItemExtraReturnsFalse(): void { $item = new AsciiArtItem('////\\\\'); $this->assertFalse($item->showsItemExtra()); } - public function testGetText() : void + public function testGetText(): void { $item = new AsciiArtItem('////\\\\'); $this->assertEquals('////\\\\', $item->getText()); } - public function testGetArtLength() : void + public function testGetArtLength(): void { $item = new AsciiArtItem("//\n//\n///"); $this->assertEquals(3, $item->getArtLength()); } - public function testGetRowsLeftAligned() : void + public function testGetRowsLeftAligned(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -61,7 +62,7 @@ public function testGetRowsLeftAligned() : void ); } - public function testGetRowsRightAligned() : void + public function testGetRowsRightAligned(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -80,7 +81,7 @@ public function testGetRowsRightAligned() : void ); } - public function testGetRowsCenterAligned() : void + public function testGetRowsCenterAligned(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -99,7 +100,7 @@ public function testGetRowsCenterAligned() : void ); } - public function testGetRowsCenterAlignedWithOddWidth() : void + public function testGetRowsCenterAlignedWithOddWidth(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -118,7 +119,7 @@ public function testGetRowsCenterAlignedWithOddWidth() : void ); } - public function testHideAndShowItemExtraHasNoEffect() : void + public function testHideAndShowItemExtraHasNoEffect(): void { $item = new AsciiArtItem("//\n//", AsciiArtItem::POSITION_CENTER); @@ -129,7 +130,7 @@ public function testHideAndShowItemExtraHasNoEffect() : void $this->assertFalse($item->showsItemExtra()); } - public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall() : void + public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -137,13 +138,13 @@ public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall() : void ->expects($this->any()) ->method('getContentWidth') ->willReturn(10); - + $item = new AsciiArtItem('TOO LONG. SO SO LONG.', AsciiArtItem::POSITION_CENTER, 'my alt'); - + self::assertSame(['my alt'], $item->getRows($menuStyle)); } - public function testGetRowsDoesNotReturnsStaticAltItemWhenOnlySpacesOverflow() : void + public function testGetRowsDoesNotReturnsStaticAltItemWhenOnlySpacesOverflow(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -151,13 +152,13 @@ public function testGetRowsDoesNotReturnsStaticAltItemWhenOnlySpacesOverflow() : ->expects($this->any()) ->method('getContentWidth') ->willReturn(15); - + $item = new AsciiArtItem('NOT TOO LONG ', AsciiArtItem::POSITION_LEFT, 'my alt'); - + self::assertSame(['NOT TOO LONG'], $item->getRows($menuStyle)); } - public function testWithRealAsciiArtCenterAligned() : void + public function testWithRealAsciiArtCenterAligned(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -189,7 +190,7 @@ public function testWithRealAsciiArtCenterAligned() : void ); } - public function testWithRealAsciiArtRightAligned() : void + public function testWithRealAsciiArtRightAligned(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -221,7 +222,7 @@ public function testWithRealAsciiArtRightAligned() : void ); } - public function testWithRealAsciiArtCenterAlignedWithWhiteSpaceAtTheEndOfEachLine() : void + public function testWithRealAsciiArtCenterAlignedWithWhiteSpaceAtTheEndOfEachLine(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -229,7 +230,7 @@ public function testWithRealAsciiArtCenterAlignedWithWhiteSpaceAtTheEndOfEachLin ->expects($this->any()) ->method('getContentWidth') ->willReturn(30); - + $art = <<createMock(MenuStyle::class); @@ -260,7 +261,7 @@ public function testSetText() : void ->expects($this->any()) ->method('getContentWidth') ->willReturn(30); - + $art = <<assertTrue($item->canSelect()); } - public function testGetSelectAction() : void + public function testGetSelectAction(): void { $callable = function () { return 'callable is called'; @@ -32,7 +33,7 @@ public function testGetSelectAction() : void $this->assertSame($callable(), $item->getSelectAction()($cliMenu)); } - public function testSelectActionTogglesItem() : void + public function testSelectActionTogglesItem(): void { $callable = function () { }; @@ -50,7 +51,7 @@ public function testSelectActionTogglesItem() : void self::assertTrue($item->getChecked()); } - public function testShowsItemExtra() : void + public function testShowsItemExtra(): void { $item = new CheckboxItem('Item', function () { }); @@ -61,14 +62,14 @@ public function testShowsItemExtra() : void $this->assertTrue($item->showsItemExtra()); } - public function testGetText() : void + public function testGetText(): void { $item = new CheckboxItem('Item', function () { }); $this->assertEquals('Item', $item->getText()); } - public function testGetRows() : void + public function testGetRows(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -88,7 +89,7 @@ public function testGetRows() : void $this->assertEquals(['[✔] Item'], $itemChecked->getRows($menuStyle, true)); } - public function testSetText() : void + public function testSetText(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -110,7 +111,7 @@ public function testSetText() : void $this->assertEquals(['[✔] New Text'], $itemChecked->getRows($menuStyle, true)); } - public function testTogglesMarker() : void + public function testTogglesMarker(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -134,7 +135,7 @@ public function testTogglesMarker() : void $this->assertEquals(['[ ] Item'], $itemChecked->getRows($menuStyle, true)); } - public function testGetRowsWithItemExtra() : void + public function testGetRowsWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -151,7 +152,7 @@ public function testGetRowsWithItemExtra() : void $this->assertEquals(['[ ] Item [EXTRA]'], $item->getRows($menuStyle)); } - public function testGetRowsWithMultipleLinesWithItemExtra() : void + public function testGetRowsWithMultipleLinesWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -174,7 +175,7 @@ public function testGetRowsWithMultipleLinesWithItemExtra() : void ); } - public function testHideAndShowItemExtra() : void + public function testHideAndShowItemExtra(): void { $item = new CheckboxItem('Item', function () { }); diff --git a/test/MenuItem/LineBreakItemTest.php b/test/MenuItem/LineBreakItemTest.php index 68b706b2..2f618e24 100644 --- a/test/MenuItem/LineBreakItemTest.php +++ b/test/MenuItem/LineBreakItemTest.php @@ -1,4 +1,5 @@ assertFalse($item->canSelect()); } - public function testGetSelectActionReturnsNull() : void + public function testGetSelectActionReturnsNull(): void { $item = new LineBreakItem('*'); $this->assertNull($item->getSelectAction()); } - public function testShowsItemExtraReturnsFalse() : void + public function testShowsItemExtraReturnsFalse(): void { $item = new LineBreakItem('*'); $this->assertFalse($item->showsItemExtra()); } - public function testGetText() : void + public function testGetText(): void { $item = new LineBreakItem('*'); $this->assertEquals('*', $item->getText()); } - public function testGetRowsRepeatsCharForMenuWidth() : void + public function testGetRowsRepeatsCharForMenuWidth(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -49,7 +50,7 @@ public function testGetRowsRepeatsCharForMenuWidth() : void $this->assertEquals(['**********'], $item->getRows($menuStyle)); } - public function testGetRowsRepeatsCharForMenuWidthMultiLines() : void + public function testGetRowsRepeatsCharForMenuWidthMultiLines(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -62,7 +63,7 @@ public function testGetRowsRepeatsCharForMenuWidthMultiLines() : void $this->assertEquals(['**********', '**********', '**********'], $item->getRows($menuStyle)); } - public function testGetRowsWithPhraseThatDoesNotFitInWidthEvenlyIsTrimmed() : void + public function testGetRowsWithPhraseThatDoesNotFitInWidthEvenlyIsTrimmed(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -70,7 +71,7 @@ public function testGetRowsWithPhraseThatDoesNotFitInWidthEvenlyIsTrimmed() : vo ->expects($this->any()) ->method('getContentWidth') ->willReturn(5); - + //ABC should be repeated but ABCABC is 6 and the allowed length is 5 //so ABCABC is trimmed to ABCAB @@ -78,7 +79,7 @@ public function testGetRowsWithPhraseThatDoesNotFitInWidthEvenlyIsTrimmed() : vo $this->assertEquals(['ABCAB', 'ABCAB', 'ABCAB'], $item->getRows($menuStyle)); } - public function testGetRowsWithMultiByteChars() : void + public function testGetRowsWithMultiByteChars(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -91,7 +92,7 @@ public function testGetRowsWithMultiByteChars() : void $this->assertEquals(['❅❅❅❅❅', '❅❅❅❅❅'], $item->getRows($menuStyle)); } - public function testSetText() : void + public function testSetText(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -105,7 +106,7 @@ public function testSetText() : void $this->assertEquals(['❅-❅-❅', '❅-❅-❅'], $item->getRows($menuStyle)); } - public function testHideAndShowItemExtraHasNoEffect() : void + public function testHideAndShowItemExtraHasNoEffect(): void { $item = new LineBreakItem('*'); diff --git a/test/MenuItem/MenuMenuItemTest.php b/test/MenuItem/MenuMenuItemTest.php index 7d6eb25a..8331c1bf 100644 --- a/test/MenuItem/MenuMenuItemTest.php +++ b/test/MenuItem/MenuMenuItemTest.php @@ -1,4 +1,5 @@ createMock(CliMenu::class); @@ -22,17 +23,17 @@ public function testCanSelectIsTrue() : void $this->assertTrue($item->canSelect()); } - public function testGetSelectAction() : void + public function testGetSelectAction(): void { $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); - + $action = $item->getSelectAction(); $this->assertIsCallable($action); } - public function testShowsItemExtra() : void + public function testShowsItemExtra(): void { $subMenu = $this->createMock(CliMenu::class); @@ -40,7 +41,7 @@ public function testShowsItemExtra() : void $this->assertFalse($item->showsItemExtra()); } - public function testGetText() : void + public function testGetText(): void { $subMenu = $this->createMock(CliMenu::class); @@ -48,7 +49,7 @@ public function testGetText() : void $this->assertEquals('Item', $item->getText()); } - public function testGetRows() : void + public function testGetRows(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -65,7 +66,7 @@ public function testGetRows() : void $this->assertEquals(['* Item'], $item->getRows($menuStyle)); } - public function testGetRowsWithUnSelectedMarker() : void + public function testGetRowsWithUnSelectedMarker(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -83,7 +84,7 @@ public function testGetRowsWithUnSelectedMarker() : void $this->assertEquals(['* Item'], $item->getRows($menuStyle, false)); } - public function testGetRowsWithSelectedMarker() : void + public function testGetRowsWithSelectedMarker(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -103,7 +104,7 @@ public function testGetRowsWithSelectedMarker() : void } - public function testGetRowsWithMultipleLines() : void + public function testGetRowsWithMultipleLines(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -126,15 +127,15 @@ public function testGetRowsWithMultipleLines() : void ); } - public function testShowSubMenu() : void + public function testShowSubMenu(): void { $mainMenu = $this->createMock(CliMenu::class); $subMenu = $this->createMock(CliMenu::class); - + $mainMenu ->expects($this->once()) ->method('closeThis'); - + $subMenu ->expects($this->once()) ->method('open'); @@ -143,7 +144,7 @@ public function testShowSubMenu() : void $item->showSubMenu($mainMenu); } - public function testHideAndShowItemExtra() : void + public function testHideAndShowItemExtra(): void { $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); @@ -155,11 +156,11 @@ public function testHideAndShowItemExtra() : void $this->assertFalse($item->showsItemExtra()); } - public function testGetSubMenu() : void + public function testGetSubMenu(): void { $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); - + self::assertSame($subMenu, $item->getSubMenu()); } } diff --git a/test/MenuItem/RadioItemTest.php b/test/MenuItem/RadioItemTest.php index 4c0e5085..84a4b767 100644 --- a/test/MenuItem/RadioItemTest.php +++ b/test/MenuItem/RadioItemTest.php @@ -1,4 +1,5 @@ assertTrue($item->canSelect()); } - public function testGetSelectAction() : void + public function testGetSelectAction(): void { $callable = function () { return 'callable is called'; @@ -32,7 +33,7 @@ public function testGetSelectAction() : void $this->assertSame($callable(), $item->getSelectAction()($cliMenu)); } - public function testShowsItemExtra() : void + public function testShowsItemExtra(): void { $item = new RadioItem('Item', function () { }); @@ -43,14 +44,14 @@ public function testShowsItemExtra() : void $this->assertTrue($item->showsItemExtra()); } - public function testGetText() : void + public function testGetText(): void { $item = new RadioItem('Item', function () { }); $this->assertEquals('Item', $item->getText()); } - public function testGetRows() : void + public function testGetRows(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -70,7 +71,7 @@ public function testGetRows() : void $this->assertEquals(['[●] Item'], $itemChecked->getRows($menuStyle, true)); } - public function testSetText() : void + public function testSetText(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -92,7 +93,7 @@ public function testSetText() : void $this->assertEquals(['[●] New Text'], $itemChecked->getRows($menuStyle, true)); } - public function testTogglesMarker() : void + public function testTogglesMarker(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -116,7 +117,7 @@ public function testTogglesMarker() : void $this->assertEquals(['[○] Item'], $itemChecked->getRows($menuStyle, true)); } - public function testTogglesUnselectedMarkers() : void + public function testTogglesUnselectedMarkers(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -168,7 +169,7 @@ public function testTogglesUnselectedMarkers() : void $this->assertEquals(['[●] Item'], $item3->getRows($menuStyle, true)); } - public function testGetRowsWithItemExtra() : void + public function testGetRowsWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -185,7 +186,7 @@ public function testGetRowsWithItemExtra() : void $this->assertEquals(['[○] Item [EXTRA]'], $item->getRows($menuStyle)); } - public function testGetRowsWithMultipleLinesWithItemExtra() : void + public function testGetRowsWithMultipleLinesWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -208,7 +209,7 @@ public function testGetRowsWithMultipleLinesWithItemExtra() : void ); } - public function testHideAndShowItemExtra() : void + public function testHideAndShowItemExtra(): void { $item = new RadioItem('Item', function () { }); diff --git a/test/MenuItem/SelectableItemRendererTest.php b/test/MenuItem/SelectableItemRendererTest.php index 8859b6cf..ed43d5ec 100644 --- a/test/MenuItem/SelectableItemRendererTest.php +++ b/test/MenuItem/SelectableItemRendererTest.php @@ -13,11 +13,11 @@ class SelectableItemRendererTest extends TestCase { - public function testRender() : void + public function testRender(): void { $renderer = new SelectableItemRenderer(); - $menuStyle = new MenuStyle(new MockTerminal); + $menuStyle = new MenuStyle(new MockTerminal()); $menuStyle->setWidth(35); $style = (new SelectableStyle())->setItemExtra('[DONE]'); @@ -34,11 +34,11 @@ public function testRender() : void $renderer->render($menuStyle, $item, true, false) ); } - public function testRenderMultiLine() : void + public function testRenderMultiLine(): void { $renderer = new SelectableItemRenderer(); - $menuStyle = new MenuStyle(new MockTerminal); + $menuStyle = new MenuStyle(new MockTerminal()); $menuStyle->setWidth(35); $style = (new SelectableStyle())->setItemExtra('[DONE]'); @@ -56,11 +56,11 @@ public function testRenderMultiLine() : void $renderer->render($menuStyle, $item, true, false) ); } - public function testRenderUnselected() : void + public function testRenderUnselected(): void { $renderer = new SelectableItemRenderer(); - $menuStyle = new MenuStyle(new MockTerminal); + $menuStyle = new MenuStyle(new MockTerminal()); $menuStyle->setWidth(35); $style = (new SelectableStyle())->setItemExtra('[DONE]'); @@ -76,11 +76,11 @@ public function testRenderUnselected() : void $renderer->render($menuStyle, $item, false, false) ); } - public function testRenderDisabled() : void + public function testRenderDisabled(): void { $renderer = new SelectableItemRenderer(); - $menuStyle = new MenuStyle(new MockTerminal); + $menuStyle = new MenuStyle(new MockTerminal()); $menuStyle->setWidth(35); $style = (new SelectableStyle())->setItemExtra('[DONE]'); @@ -94,7 +94,7 @@ public function testRenderDisabled() : void escapeshellcmd($renderer->render($menuStyle, $item, true, true)[0]) ); } - public function testWrapAndIndentText() : void + public function testWrapAndIndentText(): void { $renderer = new SelectableItemRenderer(); @@ -109,7 +109,7 @@ public function testWrapAndIndentText() : void $renderer->wrapAndIndentText('[ ] ', $text, 20) ); } - public function testLineWithExtra() : void + public function testLineWithExtra(): void { $renderer = new SelectableItemRenderer(); $style = (new SelectableStyle())->setItemExtra('[DONE]'); @@ -119,7 +119,7 @@ public function testLineWithExtra() : void $renderer->lineWithExtra('FIRST LINE', 15, $style) ); } - public function testEmptyString() : void + public function testEmptyString(): void { $renderer = new SelectableItemRenderer(); @@ -127,22 +127,22 @@ public function testEmptyString() : void self::assertEquals(' ', $renderer->emptyString(3)); self::assertEquals(' ', $renderer->emptyString(5)); } - public function testGetAvailableTextWidthWithoutExtra() : void + public function testGetAvailableTextWidthWithoutExtra(): void { $renderer = new SelectableItemRenderer(); - $menuStyle = new MenuStyle(new MockTerminal); + $menuStyle = new MenuStyle(new MockTerminal()); $menuStyle->setWidth(100); $itemStyle = new SelectableStyle(); self::assertEquals(92, $renderer->getAvailableTextWidth($menuStyle, $itemStyle)); } - public function testGetAvailableTextWidthWithExtra() : void + public function testGetAvailableTextWidthWithExtra(): void { $renderer = new SelectableItemRenderer(); - $menuStyle = new MenuStyle(new MockTerminal); + $menuStyle = new MenuStyle(new MockTerminal()); $menuStyle->setWidth(100); $itemStyle = new SelectableStyle(); diff --git a/test/MenuItem/SelectableItemTest.php b/test/MenuItem/SelectableItemTest.php index 0ed4d578..5253348a 100644 --- a/test/MenuItem/SelectableItemTest.php +++ b/test/MenuItem/SelectableItemTest.php @@ -1,4 +1,5 @@ assertTrue($item->canSelect()); } - public function testGetSelectAction() : void + public function testGetSelectAction(): void { $callable = function () { }; @@ -28,7 +29,7 @@ public function testGetSelectAction() : void $this->assertSame($callable, $item->getSelectAction()); } - public function testShowsItemExtra() : void + public function testShowsItemExtra(): void { $item = new SelectableItem('Item', function () { }); @@ -39,14 +40,14 @@ public function testShowsItemExtra() : void $this->assertTrue($item->showsItemExtra()); } - public function testGetText() : void + public function testGetText(): void { $item = new SelectableItem('Item', function () { }); $this->assertEquals('Item', $item->getText()); } - public function testGetRows() : void + public function testGetRows(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -54,7 +55,7 @@ public function testGetRows() : void $menuStyle = new MenuStyle($terminal); $menuStyle->setPaddingLeftRight(0); $menuStyle->setWidth(10); - + $item = new SelectableItem('Item', function () { }); $this->assertEquals(['○ Item'], $item->getRows($menuStyle)); @@ -62,7 +63,7 @@ public function testGetRows() : void $this->assertEquals(['● Item'], $item->getRows($menuStyle, true)); } - public function testSetText() : void + public function testSetText(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -79,7 +80,7 @@ public function testSetText() : void $this->assertEquals(['● New Text'], $item->getRows($menuStyle, true)); } - public function testGetRowsWithUnSelectedMarker() : void + public function testGetRowsWithUnSelectedMarker(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -96,7 +97,7 @@ public function testGetRowsWithUnSelectedMarker() : void $this->assertEquals(['* Item'], $item->getRows($menuStyle, false)); } - public function testGetRowsWithSelectedMarker() : void + public function testGetRowsWithSelectedMarker(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -112,7 +113,7 @@ public function testGetRowsWithSelectedMarker() : void $this->assertEquals(['= Item'], $item->getRows($menuStyle, true)); } - public function testGetRowsWithItemExtra() : void + public function testGetRowsWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -130,7 +131,7 @@ public function testGetRowsWithItemExtra() : void $this->assertEquals(['* Item [EXTRA]'], $item->getRows($menuStyle)); } - public function testGetRowsWithMultipleLinesWithItemExtra() : void + public function testGetRowsWithMultipleLinesWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -154,7 +155,7 @@ public function testGetRowsWithMultipleLinesWithItemExtra() : void ); } - public function testHideAndShowItemExtra() : void + public function testHideAndShowItemExtra(): void { $item = new SelectableItem('Item', function () { }); diff --git a/test/MenuItem/SplitItemTest.php b/test/MenuItem/SplitItemTest.php index 78305e59..41ae1e9c 100644 --- a/test/MenuItem/SplitItemTest.php +++ b/test/MenuItem/SplitItemTest.php @@ -1,4 +1,5 @@ setItems([$menuItem]); } - public static function blacklistedItemProvider() : array + public static function blacklistedItemProvider(): array { return [ [new AsciiArtItem('( ︶︿︶)_╭∩╮')], @@ -69,13 +69,13 @@ public static function blacklistedItemProvider() : array ]; } - public function testAddItem() : void + public function testAddItem(): void { $item1 = new StaticItem('One'); $item2 = new StaticItem('Two'); $splitItem = new SplitItem(); $splitItem->addItem($item1); - + self::assertEquals([$item1], $splitItem->getItems()); $splitItem->addItem($item2); @@ -83,7 +83,7 @@ public function testAddItem() : void self::assertEquals([$item1, $item2], $splitItem->getItems()); } - public function testAddItems() : void + public function testAddItems(): void { $item1 = new StaticItem('One'); $item2 = new StaticItem('Two'); @@ -91,13 +91,13 @@ public function testAddItems() : void $splitItem->addItems([$item1]); self::assertEquals([$item1], $splitItem->getItems()); - + $splitItem->addItems([$item2]); self::assertEquals([$item1, $item2], $splitItem->getItems()); } - public function testSetItems() : void + public function testSetItems(): void { $item1 = new StaticItem('One'); $item2 = new StaticItem('Two'); @@ -108,21 +108,21 @@ public function testSetItems() : void self::assertEquals([$item2, $item3], $splitItem->getItems()); } - public function testGetItems() : void + public function testGetItems(): void { $item = new StaticItem('test'); - + self::assertEquals([], (new SplitItem([]))->getItems()); self::assertEquals([$item], (new SplitItem([$item]))->getItems()); } - public function testGetSelectActionReturnsNull() : void + public function testGetSelectActionReturnsNull(): void { $item = new SplitItem([]); $this->assertNull($item->getSelectAction()); } - public function testHideAndShowItemExtraHasNoEffect() : void + public function testHideAndShowItemExtraHasNoEffect(): void { $item = new SplitItem([]); @@ -133,7 +133,7 @@ public function testHideAndShowItemExtraHasNoEffect() : void $this->assertFalse($item->showsItemExtra()); } - public function testGetRowsWithStaticItems() : void + public function testGetRowsWithStaticItems(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -141,13 +141,13 @@ public function testGetRowsWithStaticItems() : void ->expects($this->any()) ->method('getContentWidth') ->willReturn(30); - + $item = new SplitItem([new StaticItem('One'), new StaticItem('Two')]); self::assertEquals(['One Two '], $item->getRows($menuStyle)); } - public function testSetGutter() : void + public function testSetGutter(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -166,18 +166,18 @@ public function testSetGutter() : void } #[DataProvider('belowZeroProvider')] - public function testSetGutterThrowsExceptionIfValueIsNotZeroOrAbove(int $value) : void + public function testSetGutterThrowsExceptionIfValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); $item = new SplitItem(); $item->setGutter($value); } - public static function belowZeroProvider() : array + public static function belowZeroProvider(): array { return [[-1], [-2], [-10]]; } - public function testGetRowsWithOneItemSelected() : void + public function testGetRowsWithOneItemSelected(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -205,7 +205,7 @@ public function testGetRowsWithOneItemSelected() : void self::assertEquals(['= Item One * Item Two '], $item->getRows($menuStyle, true)); } - public function testGetRowsWithMultipleLineStaticItems() : void + public function testGetRowsWithMultipleLineStaticItems(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -225,7 +225,7 @@ public function testGetRowsWithMultipleLineStaticItems() : void ); } - public function testGetRowsWithMultipleLinesWithUnSelectedMarker() : void + public function testGetRowsWithMultipleLinesWithUnSelectedMarker(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -256,7 +256,7 @@ public function testGetRowsWithMultipleLinesWithUnSelectedMarker() : void ); } - public function testGetRowsWithMultipleLinesWithOneItemSelected() : void + public function testGetRowsWithMultipleLinesWithOneItemSelected(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -278,7 +278,7 @@ public function testGetRowsWithMultipleLinesWithOneItemSelected() : void $item2->setStyle($selectableStyle); $item = new SplitItem([$item1, $item2]); - + $item->setSelectedItemIndex(0); self::assertEquals( @@ -290,7 +290,7 @@ public function testGetRowsWithMultipleLinesWithOneItemSelected() : void ); } - public function testGetRowsWithItemExtra() : void + public function testGetRowsWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -317,7 +317,7 @@ public function testGetRowsWithItemExtra() : void self::assertEquals(['* Item 1 [EXTRA] * Item 2 [EXTRA] '], $item->getRows($menuStyle)); } - public function testGetRowsWithMultipleLinesWithItemExtra() : void + public function testGetRowsWithMultipleLinesWithItemExtra(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -350,7 +350,7 @@ public function testGetRowsWithMultipleLinesWithItemExtra() : void ); } - public function testGetRowsWithMultipleLinesWithItemExtraOnOne() : void + public function testGetRowsWithMultipleLinesWithItemExtraOnOne(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->any())->method('getWidth')->willReturn(100); @@ -382,45 +382,45 @@ public function testGetRowsWithMultipleLinesWithItemExtraOnOne() : void $item->getRows($menuStyle) ); } - - public function testGetTextThrowsAnException() : void + + public function testGetTextThrowsAnException(): void { self::expectException(\BadMethodCallException::class); self::expectExceptionMessage(sprintf('Not supported on: %s', SplitItem::class)); - + (new SplitItem([]))->getText(); } - public function testGetRowsThrowsAnExceptionIfNoItemsWereAdded() : void + public function testGetRowsThrowsAnExceptionIfNoItemsWereAdded(): void { self::expectException(\RuntimeException::class); self::expectExceptionMessage(sprintf('There should be at least one item added to: %s', SplitItem::class)); - + (new SplitItem([]))->getRows($this->createMock(MenuStyle::class)); } - public function testCanBeSelectedReturnsTrueWhenItContainsSelectableItems() : void + public function testCanBeSelectedReturnsTrueWhenItContainsSelectableItems(): void { self::assertTrue((new SplitItem([new SelectableItem('One', 'strlen')]))->canSelect()); } - public function testCanBeSelectedReturnsFalseWhenItContainsNoSelectableItems() : void + public function testCanBeSelectedReturnsFalseWhenItContainsNoSelectableItems(): void { self::assertFalse((new SplitItem([new StaticItem('One')]))->canSelect()); } - public function testGetSelectedItemIndexWhenSelectableItemExists() : void + public function testGetSelectedItemIndexWhenSelectableItemExists(): void { $item1 = new StaticItem('One'); $item2 = new SelectableItem('Two', function () { }); - + $splitItem = new SplitItem([$item1, $item2]); - + self::assertEquals(1, $splitItem->getSelectedItemIndex()); } - public function testGetSelectedItemIndexWhenNoSelectableItemExists() : void + public function testGetSelectedItemIndexWhenNoSelectableItemExists(): void { $item1 = new StaticItem('One'); $splitItem = new SplitItem([$item1]); @@ -428,15 +428,15 @@ public function testGetSelectedItemIndexWhenNoSelectableItemExists() : void self::assertNull($splitItem->getSelectedItemIndex()); } - public function testSetSelectedItemIndexThrowsExceptionIsIndexDoesNotExist() : void + public function testSetSelectedItemIndexThrowsExceptionIsIndexDoesNotExist(): void { self::expectException(\InvalidArgumentException::class); self::expectExceptionMessage('Index: "2" does not exist'); - + (new SplitItem([]))->setSelectedItemIndex(2); } - public function testGetSelectedItemReturnsItem() : void + public function testGetSelectedItemReturnsItem(): void { $item1 = new StaticItem('One'); $item2 = new SelectableItem('Two', function () { @@ -446,31 +446,31 @@ public function testGetSelectedItemReturnsItem() : void self::assertSame($item2, $splitItem->getSelectedItem()); } - public function testGetSelectedItemThrowsExceptionWhenNoSelectableItemExists() : void + public function testGetSelectedItemThrowsExceptionWhenNoSelectableItemExists(): void { self::expectException(\RuntimeException::class); self::expectExceptionMessage('No item is selected'); - + $item1 = new StaticItem('One'); $splitItem = new SplitItem([$item1]); self::assertSame($splitItem, $splitItem->getSelectedItem()); } - public function testCanSelectIndex() : void + public function testCanSelectIndex(): void { $item1 = new StaticItem('One'); $item2 = new SelectableItem('Two', function () { }); $splitItem = new SplitItem([$item1, $item2]); - + self::assertFalse($splitItem->canSelectIndex(0)); self::assertFalse($splitItem->canSelectIndex(5)); self::assertTrue($splitItem->canSelectIndex(1)); } - public function testCheckboxItem() : void + public function testCheckboxItem(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -507,7 +507,7 @@ public function testCheckboxItem() : void self::assertEquals(['[✔] Item One [ ] Item Two '], $item->getRows($menuStyle, true)); } - public function testRadioItem() : void + public function testRadioItem(): void { $menuStyle = $this->createMock(MenuStyle::class); diff --git a/test/MenuItem/StaticItemTest.php b/test/MenuItem/StaticItemTest.php index 5222bb27..f076fde5 100644 --- a/test/MenuItem/StaticItemTest.php +++ b/test/MenuItem/StaticItemTest.php @@ -1,4 +1,5 @@ assertFalse($item->canSelect()); } - public function testGetSelectActionReturnsNull() : void + public function testGetSelectActionReturnsNull(): void { $item = new StaticItem('Item 1'); $this->assertNull($item->getSelectAction()); } - public function testShowsItemExtraReturnsFalse() : void + public function testShowsItemExtraReturnsFalse(): void { $item = new StaticItem('Item 1'); $this->assertFalse($item->showsItemExtra()); } - public function testGetText() : void + public function testGetText(): void { $item = new StaticItem('Item 1'); $this->assertEquals('Item 1', $item->getText()); } - public function testGetRowsWithContentWhichFitsOnOneLine() : void + public function testGetRowsWithContentWhichFitsOnOneLine(): void { $menuStyle = $this->createMock(MenuStyle::class); - + $menuStyle ->expects($this->once()) ->method('getContentWidth') ->willReturn(20); $item = new StaticItem('CONTENT 1 LINE'); - + $this->assertEquals( ['CONTENT 1 LINE'], $item->getRows($menuStyle) ); } - public function testGetRowsWithContentWhichDoesNotFitOnOneLineIsWrapped() : void + public function testGetRowsWithContentWhichDoesNotFitOnOneLineIsWrapped(): void { $menuStyle = $this->createMock(MenuStyle::class); @@ -70,10 +71,10 @@ public function testGetRowsWithContentWhichDoesNotFitOnOneLineIsWrapped() : void ); } - public function testSetText() : void + public function testSetText(): void { $menuStyle = $this->createMock(MenuStyle::class); - + $menuStyle ->expects($this->once()) ->method('getContentWidth') @@ -81,14 +82,14 @@ public function testSetText() : void $item = new StaticItem('CONTENT'); $item->setText('CONTENT 2 LINES'); - + $this->assertEquals( ['CONTENT 2', 'LINES'], $item->getRows($menuStyle) ); } - public function testHideAndShowItemExtraHasNoEffect() : void + public function testHideAndShowItemExtraHasNoEffect(): void { $item = new StaticItem('CONTENT 1 LINE'); diff --git a/test/MenuStyleTest.php b/test/MenuStyleTest.php index 0da53c1f..e841443d 100644 --- a/test/MenuStyleTest.php +++ b/test/MenuStyleTest.php @@ -1,4 +1,5 @@ getMockTerminal($colours, $width)); } - private function getMockTerminal(int $colours, int $width) : MockObject + private function getMockTerminal(int $colours, int $width): MockObject { $terminal = $this ->getMockBuilder(UnixTerminal::class) @@ -41,7 +42,7 @@ private function getMockTerminal(int $colours, int $width) : MockObject return $terminal; } - public function testMenuStyleCanBeInstantiatedByCliMenuBuilder() : void + public function testMenuStyleCanBeInstantiatedByCliMenuBuilder(): void { $builder = new CliMenuBuilder(); $menu = $builder->build(); @@ -56,27 +57,27 @@ public function testMenuStyleCanBeInstantiatedByCliMenuBuilder() : void self::assertSame(MenuStyle::class, get_class($style)); } - public function testGetColoursSetCode() : void + public function testGetColoursSetCode(): void { self::assertSame("\e[37;44m", $this->getMenuStyle()->getColoursSetCode()); } - public function testGetColoursResetCode() : void + public function testGetColoursResetCode(): void { self::assertSame("\e[0m", $this->getMenuStyle()->getColoursResetCode()); } - public function testGetInvertedColoursSetCode() : void + public function testGetInvertedColoursSetCode(): void { self::assertSame("\e[7m", $this->getMenuStyle()->getInvertedColoursSetCode()); } - public function testGetInvertedColoursUnsetCode() : void + public function testGetInvertedColoursUnsetCode(): void { self::assertSame("\e[27m", $this->getMenuStyle()->getInvertedColoursUnsetCode()); } - public function testGetterAndSetters() : void + public function testGetterAndSetters(): void { $style = $this->getMenuStyle(); @@ -126,7 +127,7 @@ public function testGetterAndSetters() : void self::assertSame('green', $style->getBorderColour()); } - public function testSetBorderShorthandFunction() : void + public function testSetBorderShorthandFunction(): void { $style = $this->getMenuStyle(); $style->setBorder(3); @@ -193,7 +194,7 @@ public function testSetBorderShorthandFunction() : void static::assertSame('red', $style->getBorderColour()); } - public function test256ColoursCodes() : void + public function test256ColoursCodes(): void { $style = $this->getMenuStyle(256); $style->setBg('16', 'white'); @@ -210,7 +211,7 @@ public function test256ColoursCodes() : void static::assertSame("\033[31;47m", $style->getColoursSetCode()); } - public function testSetFgThrowsExceptionWhenColourCodeIsNotInRange() : void + public function testSetFgThrowsExceptionWhenColourCodeIsNotInRange(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid colour code'); @@ -219,7 +220,7 @@ public function testSetFgThrowsExceptionWhenColourCodeIsNotInRange() : void $style->setFg('512', 'white'); } - public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange() : void + public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid colour code'); @@ -228,7 +229,7 @@ public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange() : void $style->setBg('257', 'white'); } - public function testWidthCalculation() : void + public function testWidthCalculation(): void { $style = $this->getMenuStyle(); $style->setPadding(0); @@ -249,7 +250,7 @@ public function testWidthCalculation() : void static::assertSame(286, $style->getContentWidth()); } - public function testRightHandPaddingCalculation() : void + public function testRightHandPaddingCalculation(): void { $style = $this->getMenuStyle(); $style->setPadding(0); @@ -269,7 +270,7 @@ public function testRightHandPaddingCalculation() : void static::assertSame(241, $style->getRightHandPadding(50)); } - public function testRightHandPaddingReturnsZeroWhenContentLengthTooLong() : void + public function testRightHandPaddingReturnsZeroWhenContentLengthTooLong(): void { $style = $this->getMenuStyle(); $style->setPadding(0); @@ -282,7 +283,7 @@ public function testRightHandPaddingReturnsZeroWhenContentLengthTooLong() : void self::assertEquals(0, $style->getRightHandPadding(150)); } - public function testRightHandPaddingReturnsZeroWhenContentLengthTooLongBecauseOfBorder() : void + public function testRightHandPaddingReturnsZeroWhenContentLengthTooLongBecauseOfBorder(): void { $style = $this->getMenuStyle(); $style->setPadding(10); @@ -298,7 +299,7 @@ public function testRightHandPaddingReturnsZeroWhenContentLengthTooLongBecauseOf self::assertEquals(0, $style->getRightHandPadding(100)); } - public function testMargin() : void + public function testMargin(): void { $style = $this->getMenuStyle(); @@ -309,7 +310,7 @@ public function testMargin() : void self::assertSame(5, $style->getMargin()); } - public function testSetMarginWhenWidthIsLargerThanTerminal() : void + public function testSetMarginWhenWidthIsLargerThanTerminal(): void { $style = $this->getMenuStyle(); $style->setWidth(600); @@ -323,7 +324,7 @@ public function testSetMarginWhenWidthIsLargerThanTerminal() : void self::assertSame(430, $style->getWidth()); } - public function testMarginAutoCenters() : void + public function testMarginAutoCenters(): void { $style = $this->getMenuStyle(); @@ -335,7 +336,7 @@ public function testMarginAutoCenters() : void self::assertSame(290, $style->getContentWidth()); } - public function testSetMarginAutoWhenWidthIsLargerThanTerminal() : void + public function testSetMarginAutoWhenWidthIsLargerThanTerminal(): void { $style = $this->getMenuStyle(); $style->setWidth(600); @@ -345,7 +346,7 @@ public function testSetMarginAutoWhenWidthIsLargerThanTerminal() : void self::assertSame(500, $style->getWidth()); } - public function testModifyWidthWhenMarginAutoIsEnabledRecalculatesMargin() : void + public function testModifyWidthWhenMarginAutoIsEnabledRecalculatesMargin(): void { $style = $this->getMenuStyle(); @@ -362,7 +363,7 @@ public function testModifyWidthWhenMarginAutoIsEnabledRecalculatesMargin() : voi self::assertSame(390, $style->getContentWidth()); } - public function testSetBgRecalculatesPaddingTopAndBottomRows() : void + public function testSetBgRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -380,7 +381,7 @@ public function testSetBgRecalculatesPaddingTopAndBottomRows() : void ); } - public function testSetWidthRecalculatesPaddingTopAndBottomRows() : void + public function testSetWidthRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -398,7 +399,7 @@ public function testSetWidthRecalculatesPaddingTopAndBottomRows() : void ); } - public function testSetPaddingRecalculatesPaddingTopAndBottomRows() : void + public function testSetPaddingRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -420,7 +421,7 @@ public function testSetPaddingRecalculatesPaddingTopAndBottomRows() : void ); } - public function testSetPaddingTopAndBottomRecalculatesPaddingTopAndBottomRows() : void + public function testSetPaddingTopAndBottomRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -441,7 +442,7 @@ public function testSetPaddingTopAndBottomRecalculatesPaddingTopAndBottomRows() ); } - public function testSetPaddingLeftAndRightRecalculatesPaddingTopAndBottomRows() : void + public function testSetPaddingLeftAndRightRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -459,7 +460,7 @@ public function testSetPaddingLeftAndRightRecalculatesPaddingTopAndBottomRows() ); } - public function testSetMarginRecalculatesPaddingTopAndBottomRows() : void + public function testSetMarginRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -477,7 +478,7 @@ public function testSetMarginRecalculatesPaddingTopAndBottomRows() : void ); } - public function testSetBorderRecalculatesPaddingTopAndBottomRows() : void + public function testSetBorderRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -496,7 +497,7 @@ public function testSetBorderRecalculatesPaddingTopAndBottomRows() : void ); } - public function testSetBorderRightRecalculatesPaddingTopAndBottomRows() : void + public function testSetBorderRightRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -515,7 +516,7 @@ public function testSetBorderRightRecalculatesPaddingTopAndBottomRows() : void ); } - public function testSetBorderLeftRecalculatesPaddingTopAndBottomRows() : void + public function testSetBorderLeftRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -525,7 +526,7 @@ public function testSetBorderLeftRecalculatesPaddingTopAndBottomRows() : void [" \033[37;44m \033[0m\n"], $style->getPaddingTopBottomRows() ); - + $style->setBorderLeftWidth(1); self::assertEquals( @@ -534,7 +535,7 @@ public function testSetBorderLeftRecalculatesPaddingTopAndBottomRows() : void ); } - public function testSetBorderColourRecalculatesPaddingTopAndBottomRows() : void + public function testSetBorderColourRecalculatesPaddingTopAndBottomRows(): void { $style = $this->getMenuStyle(); $style->setWidth(30); @@ -544,7 +545,7 @@ public function testSetBorderColourRecalculatesPaddingTopAndBottomRows() : void [" \033[41m \e[37;44m \033[41m \033[0m\n"], $style->getPaddingTopBottomRows() ); - + $style->setBorderColour('green'); self::assertEquals( @@ -554,32 +555,32 @@ public function testSetBorderColourRecalculatesPaddingTopAndBottomRows() : void } #[DataProvider('belowZeroProvider')] - public function testSetWidthThrowsExceptionIfValueIsNotZeroOrAbove(int $value) : void + public function testSetWidthThrowsExceptionIfValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); $this->getMenuStyle()->setWidth($value); } - public static function belowZeroProvider() : array + public static function belowZeroProvider(): array { return [[-1], [-2], [-10]]; } - public function testSetPaddingWithUniversalValue() : void + public function testSetPaddingWithUniversalValue(): void { $style = $this->getMenuStyle(); self::assertEquals(1, $style->getPaddingTopBottom()); self::assertEquals(2, $style->getPaddingLeftRight()); - + $style->setPadding(10); self::assertEquals(10, $style->getPaddingTopBottom()); self::assertEquals(10, $style->getPaddingLeftRight()); } - public function testSetPaddingWithXAndYValues() : void + public function testSetPaddingWithXAndYValues(): void { $style = $this->getMenuStyle(); @@ -592,7 +593,7 @@ public function testSetPaddingWithXAndYValues() : void self::assertEquals(6, $style->getPaddingLeftRight()); } - public function testSetPaddingTopAndBottom() : void + public function testSetPaddingTopAndBottom(): void { $style = $this->getMenuStyle(); @@ -603,19 +604,19 @@ public function testSetPaddingTopAndBottom() : void self::assertEquals(5, $style->getPaddingTopBottom()); } - public function testSetPaddingLeftAndRight() : void + public function testSetPaddingLeftAndRight(): void { $style = $this->getMenuStyle(); - + self::assertEquals(2, $style->getPaddingLeftRight()); - + $style->setPaddingLeftRight(4); - + self::assertEquals(4, $style->getPaddingLeftRight()); } #[DataProvider('belowZeroProvider')] - public function testSetPaddingTopAndBottomThrowsExceptionIfValueIsNotZeroOrAbove(int $value) : void + public function testSetPaddingTopAndBottomThrowsExceptionIfValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); @@ -623,7 +624,7 @@ public function testSetPaddingTopAndBottomThrowsExceptionIfValueIsNotZeroOrAbove } #[DataProvider('belowZeroProvider')] - public function testSetPaddingLeftAndRightThrowsExceptionIfValueIsNotZeroOrAbove(int $value) : void + public function testSetPaddingLeftAndRightThrowsExceptionIfValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); @@ -631,7 +632,7 @@ public function testSetPaddingLeftAndRightThrowsExceptionIfValueIsNotZeroOrAbove } #[DataProvider('belowZeroProvider')] - public function testSetPaddingThrowsExceptionIfTopBottomValueIsNotZeroOrAbove(int $value) : void + public function testSetPaddingThrowsExceptionIfTopBottomValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); @@ -639,7 +640,7 @@ public function testSetPaddingThrowsExceptionIfTopBottomValueIsNotZeroOrAbove(in } #[DataProvider('belowZeroProvider')] - public function testSetPaddingThrowsExceptionIfLeftRightValueIsNotZeroOrAbove(int $value) : void + public function testSetPaddingThrowsExceptionIfLeftRightValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); @@ -647,14 +648,14 @@ public function testSetPaddingThrowsExceptionIfLeftRightValueIsNotZeroOrAbove(in } #[DataProvider('belowZeroProvider')] - public function testSetMarginThrowsExceptionIfValueIsNotZeroOrAbove(int $value) : void + public function testSetMarginThrowsExceptionIfValueIsNotZeroOrAbove(int $value): void { self::expectException(\Assert\InvalidArgumentException::class); $this->getMenuStyle()->setMargin($value); } - public function testHasChangedFromDefaultsReturnsFalseWhenAutoShrunk() : void + public function testHasChangedFromDefaultsReturnsFalseWhenAutoShrunk(): void { $terminal = $this ->getMockBuilder(UnixTerminal::class) @@ -672,7 +673,7 @@ public function testHasChangedFromDefaultsReturnsFalseWhenAutoShrunk() : void self::assertFalse($menuStyle->hasChangedFromDefaults()); } - public function testDebugMode() : void + public function testDebugMode(): void { $style = $this->getMenuStyle(8, 40); @@ -705,7 +706,7 @@ public function testDebugMode() : void } #[DataProvider('cannotShrinkProvider')] - public function testSetWidthThrowsExceptionIfMenuTooWideAndMarginConsumesTerminal(int $margin) : void + public function testSetWidthThrowsExceptionIfMenuTooWideAndMarginConsumesTerminal(int $margin): void { $this->expectException(CannotShrinkMenuException::class); $this->expectExceptionMessage( @@ -717,7 +718,7 @@ public function testSetWidthThrowsExceptionIfMenuTooWideAndMarginConsumesTermina $style->setWidth(10); } - public static function cannotShrinkProvider() : array + public static function cannotShrinkProvider(): array { return [[50], [51], [100]]; } diff --git a/test/MockTerminal.php b/test/MockTerminal.php index 1b97356b..b1b28ffb 100644 --- a/test/MockTerminal.php +++ b/test/MockTerminal.php @@ -8,7 +8,6 @@ class MockTerminal implements Terminal { - /** * @inheritDoc */ diff --git a/test/Style/CheckboxStyleTest.php b/test/Style/CheckboxStyleTest.php index ed8e2fbb..fa5b9f33 100644 --- a/test/Style/CheckboxStyleTest.php +++ b/test/Style/CheckboxStyleTest.php @@ -10,17 +10,17 @@ class CheckboxStyleTest extends TestCase { - public function testHasChangedFromDefaultsWhenNoStylesChanged() : void + public function testHasChangedFromDefaultsWhenNoStylesChanged(): void { self::assertFalse((new CheckboxStyle())->hasChangedFromDefaults()); } - public function testGetMarker() : void + public function testGetMarker(): void { $item = new CheckboxItem('My Checkbox', 'var_dump'); $item->setChecked(); - $style = new CheckboxStyle; + $style = new CheckboxStyle(); self::assertSame('[✔] ', $style->getMarker($item, false)); @@ -29,9 +29,9 @@ public function testGetMarker() : void self::assertSame('[ ] ', $style->getMarker($item, false)); } - public function testGetSetMarkerOn() : void + public function testGetSetMarkerOn(): void { - $style = new CheckboxStyle; + $style = new CheckboxStyle(); self::assertSame('[✔] ', $style->getCheckedMarker()); @@ -41,9 +41,9 @@ public function testGetSetMarkerOn() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testGetSetMarkerOff() : void + public function testGetSetMarkerOff(): void { - $style = new CheckboxStyle; + $style = new CheckboxStyle(); self::assertSame('[ ] ', $style->getUncheckedMarker()); @@ -53,9 +53,9 @@ public function testGetSetMarkerOff() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testGetSetItemExtra() : void + public function testGetSetItemExtra(): void { - $style = new CheckboxStyle; + $style = new CheckboxStyle(); self::assertSame('✔', $style->getItemExtra()); @@ -65,18 +65,18 @@ public function testGetSetItemExtra() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void + public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra(): void { - $style = new CheckboxStyle; + $style = new CheckboxStyle(); self::assertFalse($style->getDisplaysExtra()); $style->setItemExtra('[!EXTRA]!'); self::assertTrue($style->getDisplaysExtra()); } - public function testGetSetDisplayExtra() : void + public function testGetSetDisplayExtra(): void { - $style = new CheckboxStyle; + $style = new CheckboxStyle(); self::assertFalse($style->getDisplaysExtra()); diff --git a/test/Style/DefaultStyleTest.php b/test/Style/DefaultStyleTest.php index 7ec6b069..3df1af15 100644 --- a/test/Style/DefaultStyleTest.php +++ b/test/Style/DefaultStyleTest.php @@ -10,31 +10,31 @@ class DefaultStyleTest extends TestCase { - public function testHasChangedFromDefaultsWhenNoStylesChanged() : void + public function testHasChangedFromDefaultsWhenNoStylesChanged(): void { self::assertTrue((new DefaultStyle())->hasChangedFromDefaults()); } - public function testGetMarker() : void + public function testGetMarker(): void { $item = new LineBreakItem('X'); - $style = new DefaultStyle; + $style = new DefaultStyle(); self::assertSame('', $style->getMarker($item, false)); self::assertSame('', $style->getMarker($item, true)); } - public function testGetSetItemExtra() : void + public function testGetSetItemExtra(): void { - $style = new DefaultStyle; + $style = new DefaultStyle(); self::assertSame('', $style->getItemExtra()); } - public function testGetSetDisplayExtra() : void + public function testGetSetDisplayExtra(): void { - $style = new DefaultStyle; + $style = new DefaultStyle(); self::assertFalse($style->getDisplaysExtra()); } diff --git a/test/Style/LocatorTest.php b/test/Style/LocatorTest.php index e8778275..80905cc5 100644 --- a/test/Style/LocatorTest.php +++ b/test/Style/LocatorTest.php @@ -24,7 +24,7 @@ class LocatorTest extends TestCase { - public function testImportIgnoresOtherWhenStylesModified() : void + public function testImportIgnoresOtherWhenStylesModified(): void { $locator = new Locator(); @@ -49,7 +49,7 @@ public function testImportIgnoresOtherWhenStylesModified() : void self::assertSame($radioStyle, $locator->getStyle(RadioStyle::class)); } - public function testImportStylesWhenOneStyleNotModified() : void + public function testImportStylesWhenOneStyleNotModified(): void { $locator = new Locator(); @@ -73,7 +73,7 @@ public function testImportStylesWhenOneStyleNotModified() : void self::assertSame($otherLocator->getStyle(SelectableStyle::class), $locator->getStyle(SelectableStyle::class)); } - public function testImportStylesWhenStyleNotModified() : void + public function testImportStylesWhenStyleNotModified(): void { $locator = new Locator(); @@ -94,18 +94,18 @@ public function testImportStylesWhenStyleNotModified() : void self::assertSame($otherLocator->getStyle(RadioStyle::class), $locator->getStyle(RadioStyle::class)); } - public function testGetStyleForMenuItemThrowsExceptionIfItemNotRegistered() : void + public function testGetStyleForMenuItemThrowsExceptionIfItemNotRegistered(): void { self::expectException(InvalidStyle::class); - $myItem = new class extends LineBreakItem { + $myItem = new class () extends LineBreakItem { }; $locator = new Locator(); $locator->getStyleForMenuItem($myItem); } - public static function itemStyleProvider() : array + public static function itemStyleProvider(): array { $action = function () { }; @@ -122,14 +122,14 @@ public static function itemStyleProvider() : array } #[DataProvider('itemStyleProvider')] - public function testGetStyleForMenuItem(string $expectedStyleClass, MenuItemInterface $menuItem) : void + public function testGetStyleForMenuItem(string $expectedStyleClass, MenuItemInterface $menuItem): void { $locator = new Locator(); self::assertInstanceOf($expectedStyleClass, $locator->getStyleForMenuItem($menuItem)); } - public function testGetStyleThrowsExceptionIfStyleClassNotRegistered() : void + public function testGetStyleThrowsExceptionIfStyleClassNotRegistered(): void { self::expectException(InvalidStyle::class); @@ -137,7 +137,7 @@ public function testGetStyleThrowsExceptionIfStyleClassNotRegistered() : void $locator->getStyle('NonExistingStyleClass'); } - public static function styleProvider() : array + public static function styleProvider(): array { return [ [DefaultStyle::class], @@ -149,14 +149,14 @@ public static function styleProvider() : array } #[DataProvider('styleProvider')] - public function testGetStyle(string $styleClass) : void + public function testGetStyle(string $styleClass): void { $locator = new Locator(); self::assertInstanceOf($styleClass, $locator->getStyle($styleClass)); } - public function testSetStyleThrowsExceptionIfStyleClassNotRegistered() : void + public function testSetStyleThrowsExceptionIfStyleClassNotRegistered(): void { self::expectException(InvalidStyle::class); @@ -164,18 +164,18 @@ public function testSetStyleThrowsExceptionIfStyleClassNotRegistered() : void $locator->setStyle(new DefaultStyle(), 'NonExistingStyleClass'); } - public function testSetStyleThrowsExceptionIfStyleNotInstanceOfStyleClass() : void + public function testSetStyleThrowsExceptionIfStyleNotInstanceOfStyleClass(): void { self::expectException(InvalidStyle::class); - $invalidStyle = new class extends SelectableStyle { + $invalidStyle = new class () extends SelectableStyle { }; $locator = new Locator(); $locator->setStyle($invalidStyle, DefaultStyle::class); } - public function testSetStyle() : void + public function testSetStyle(): void { $locator = new Locator(); @@ -184,29 +184,29 @@ public function testSetStyle() : void self::assertSame($new, $locator->getStyle(DefaultStyle::class)); } - public function testHasStyleForMenuItem() : void + public function testHasStyleForMenuItem(): void { $locator = new Locator(); - $customClass = new class extends LineBreakItem { + $customClass = new class () extends LineBreakItem { }; self::assertTrue($locator->hasStyleForMenuItem(new LineBreakItem())); self::assertFalse($locator->hasStyleForMenuItem($customClass)); } - public function testRegisterItemStyleThrowsExceptionIfItemAlreadyRegistered() : void + public function testRegisterItemStyleThrowsExceptionIfItemAlreadyRegistered(): void { self::expectException(InvalidStyle::class); (new Locator())->registerItemStyle(LineBreakItem::class, new DefaultStyle()); } - public function testRegisterItemStyle() : void + public function testRegisterItemStyle(): void { $locator = new Locator(); - $customClass = new class extends LineBreakItem { + $customClass = new class () extends LineBreakItem { }; self::assertFalse($locator->hasStyleForMenuItem($customClass)); diff --git a/test/Style/RadioStyleTest.php b/test/Style/RadioStyleTest.php index 4630c63b..76cc0e3f 100644 --- a/test/Style/RadioStyleTest.php +++ b/test/Style/RadioStyleTest.php @@ -10,17 +10,17 @@ class RadioStyleTest extends TestCase { - public function testHasChangedFromDefaultsWhenNoStylesChanged() : void + public function testHasChangedFromDefaultsWhenNoStylesChanged(): void { self::assertFalse((new RadioStyle())->hasChangedFromDefaults()); } - public function testGetMarker() : void + public function testGetMarker(): void { $item = new RadioItem('My Radio', 'var_dump'); $item->setChecked(); - $style = new RadioStyle; + $style = new RadioStyle(); self::assertSame('[●] ', $style->getMarker($item, false)); @@ -29,9 +29,9 @@ public function testGetMarker() : void self::assertSame('[○] ', $style->getMarker($item, false)); } - public function testGetSetMarkerOn() : void + public function testGetSetMarkerOn(): void { - $style = new RadioStyle; + $style = new RadioStyle(); self::assertSame('[●] ', $style->getCheckedMarker()); @@ -41,9 +41,9 @@ public function testGetSetMarkerOn() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testGetSetMarkerOff() : void + public function testGetSetMarkerOff(): void { - $style = new RadioStyle; + $style = new RadioStyle(); self::assertSame('[○] ', $style->getUncheckedMarker()); @@ -53,9 +53,9 @@ public function testGetSetMarkerOff() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testGetSetItemExtra() : void + public function testGetSetItemExtra(): void { - $style = new RadioStyle; + $style = new RadioStyle(); self::assertSame('✔', $style->getItemExtra()); @@ -65,18 +65,18 @@ public function testGetSetItemExtra() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void + public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra(): void { - $style = new RadioStyle; + $style = new RadioStyle(); self::assertFalse($style->getDisplaysExtra()); $style->setItemExtra('[!EXTRA]!'); self::assertTrue($style->getDisplaysExtra()); } - public function testGetSetDisplayExtra() : void + public function testGetSetDisplayExtra(): void { - $style = new RadioStyle; + $style = new RadioStyle(); self::assertFalse($style->getDisplaysExtra()); diff --git a/test/Style/SelectableStyleTest.php b/test/Style/SelectableStyleTest.php index d8311b6f..401be730 100644 --- a/test/Style/SelectableStyleTest.php +++ b/test/Style/SelectableStyleTest.php @@ -10,23 +10,23 @@ class SelectableStyleTest extends TestCase { - public function testHasChangedFromDefaultsWhenNoStylesChanged() : void + public function testHasChangedFromDefaultsWhenNoStylesChanged(): void { self::assertFalse((new SelectableStyle())->hasChangedFromDefaults()); } - public function testGetMarker() : void + public function testGetMarker(): void { $item = new SelectableItem('My Item', 'var_dump'); - $style = new SelectableStyle; + $style = new SelectableStyle(); self::assertSame('● ', $style->getMarker($item, true)); self::assertSame('○ ', $style->getMarker($item, false)); } - public function testGetSetMarkerOn() : void + public function testGetSetMarkerOn(): void { - $style = new SelectableStyle; + $style = new SelectableStyle(); self::assertSame('● ', $style->getSelectedMarker()); @@ -36,9 +36,9 @@ public function testGetSetMarkerOn() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testGetSetMarkerOff() : void + public function testGetSetMarkerOff(): void { - $style = new SelectableStyle; + $style = new SelectableStyle(); self::assertSame('○ ', $style->getUnselectedMarker()); @@ -48,9 +48,9 @@ public function testGetSetMarkerOff() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testGetSetItemExtra() : void + public function testGetSetItemExtra(): void { - $style = new SelectableStyle; + $style = new SelectableStyle(); self::assertSame('✔', $style->getItemExtra()); @@ -60,18 +60,18 @@ public function testGetSetItemExtra() : void self::assertTrue($style->hasChangedFromDefaults()); } - public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void + public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra(): void { - $style = new SelectableStyle; + $style = new SelectableStyle(); self::assertFalse($style->getDisplaysExtra()); $style->setItemExtra('[!EXTRA]!'); self::assertTrue($style->getDisplaysExtra()); } - public function testGetSetDisplayExtra() : void + public function testGetSetDisplayExtra(): void { - $style = new SelectableStyle; + $style = new SelectableStyle(); self::assertFalse($style->getDisplaysExtra()); diff --git a/test/Util/ArrayUtilTest.php b/test/Util/ArrayUtilTest.php index 4936f759..66008830 100644 --- a/test/Util/ArrayUtilTest.php +++ b/test/Util/ArrayUtilTest.php @@ -6,6 +6,7 @@ use PhpSchool\CliMenu\Util\ArrayUtil; use PHPUnit\Framework\TestCase; + use function PhpSchool\CliMenu\Util\collect; use function PhpSchool\CliMenu\Util\each; use function PhpSchool\CliMenu\Util\filter; @@ -14,7 +15,7 @@ class ArrayUtilTest extends TestCase { - public function testMapWithStringKeys() : void + public function testMapWithStringKeys(): void { self::assertEquals( ['one' => 1, 'two' => 4, 'three' => 9], @@ -27,7 +28,7 @@ function (string $key, int $num) { ); } - public function testMapWithIntKeys() : void + public function testMapWithIntKeys(): void { self::assertEquals( [1, 4, 9], @@ -40,7 +41,7 @@ function (string $key, int $num) { ); } - public function testEach() : void + public function testEach(): void { $i = 0; $cb = function (int $k, int $v) use (&$i) { @@ -51,14 +52,14 @@ public function testEach() : void self::assertEquals(3, $i); } - public function testMax() : void + public function testMax(): void { self::assertEquals(0, max([])); self::assertEquals(3, max([1, 2, 3])); self::assertEquals(6, max([1, 6, 3])); } - public function testFilter() : void + public function testFilter(): void { $cb = function (int $k, int $v) { return $v > 3; @@ -67,7 +68,7 @@ public function testFilter() : void self::assertEquals([3 => 4, 4 => 5, 5 => 6], filter([1, 2, 3, 4, 5, 6], $cb)); } - public function testCollect() : void + public function testCollect(): void { self::assertEquals([1, 2, 3], collect([1, 2, 3])->all()); } diff --git a/test/Util/CollectionTest.php b/test/Util/CollectionTest.php index 66e69e50..cbf61942 100644 --- a/test/Util/CollectionTest.php +++ b/test/Util/CollectionTest.php @@ -9,7 +9,7 @@ class CollectionTest extends TestCase { - public function testCollection() : void + public function testCollection(): void { $result = (new Collection([1, 2, 3, 4, 5, 6])) ->filter(function ($k, $v) { diff --git a/test/Util/ColourUtilTest.php b/test/Util/ColourUtilTest.php index 9305b5d8..75fe28d6 100644 --- a/test/Util/ColourUtilTest.php +++ b/test/Util/ColourUtilTest.php @@ -1,4 +1,5 @@ createMock(Terminal::class); ColourUtil::validateColour($terminal, 'teal'); } - - public function testValidateColourThrowsExceptionIfFallbackNotValidWhenTerminalDoesNotSupport256Colours() : void + + public function testValidateColourThrowsExceptionIfFallbackNotValidWhenTerminalDoesNotSupport256Colours(): void { self::expectException(\Assert\InvalidArgumentException::class); @@ -69,7 +70,7 @@ public function testValidateColourThrowsExceptionIfFallbackNotValidWhenTerminalD ColourUtil::validateColour($terminal, '255', 'teal'); } - public function testValidateColourWithFallbackWhenTerminalDoesNotSupport256Colours() : void + public function testValidateColourWithFallbackWhenTerminalDoesNotSupport256Colours(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->once()) @@ -79,7 +80,7 @@ public function testValidateColourWithFallbackWhenTerminalDoesNotSupport256Colou self::assertEquals('red', ColourUtil::validateColour($terminal, '255', 'red')); } - public function testValidateColourPicksFallbackFromPreComputedListWhenTerminalDoesNotSupport256Colours() : void + public function testValidateColourPicksFallbackFromPreComputedListWhenTerminalDoesNotSupport256Colours(): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->once()) @@ -90,14 +91,14 @@ public function testValidateColourPicksFallbackFromPreComputedListWhenTerminalDo } #[DataProvider('invalidColourCodeProvider')] - public function testValidateColourThrowsExceptionIfInvalid256ColourCodeUsed(string $colourCode) : void + public function testValidateColourThrowsExceptionIfInvalid256ColourCodeUsed(string $colourCode): void { self::expectException(\Assert\InvalidArgumentException::class); ColourUtil::validateColour($this->createMock(Terminal::class), $colourCode); } - public static function invalidColourCodeProvider() : array + public static function invalidColourCodeProvider(): array { return [ ['-1'], @@ -107,17 +108,17 @@ public static function invalidColourCodeProvider() : array } #[DataProvider('validColourCodeProvider')] - public function testValidateColourWith256ColoursWhenTerminalSupports256Colours(string $colourCode) : void + public function testValidateColourWith256ColoursWhenTerminalSupports256Colours(string $colourCode): void { $terminal = $this->createMock(Terminal::class); $terminal->expects($this->once()) ->method('getColourSupport') ->willReturn(256); - + self::assertEquals($colourCode, ColourUtil::validateColour($terminal, $colourCode)); } - public static function validColourCodeProvider() : array + public static function validColourCodeProvider(): array { return [ ['0'], @@ -127,7 +128,7 @@ public static function validColourCodeProvider() : array ]; } - public function testValidateColourWithValid8ColourName() : void + public function testValidateColourWithValid8ColourName(): void { self::assertEquals('red', ColourUtil::validateColour($this->createMock(Terminal::class), 'red')); } diff --git a/test/Util/StringUtilTest.php b/test/Util/StringUtilTest.php index e52c3605..c9d5562d 100644 --- a/test/Util/StringUtilTest.php +++ b/test/Util/StringUtilTest.php @@ -1,4 +1,5 @@ dummyText, 80); @@ -39,7 +40,7 @@ public function testItWrapsAsExpectedTo80Length() : void self::assertEquals($expected, $result); } - public function testItWrapsAsExpectedTo60Length() : void + public function testItWrapsAsExpectedTo60Length(): void { $result = StringUtil::wordwrap($this->dummyText, 60); @@ -55,10 +56,10 @@ public function testItWrapsAsExpectedTo60Length() : void self::assertEquals($expected, $result); } - public function testItCanUseACustomBreakCharacter() : void + public function testItCanUseACustomBreakCharacter(): void { $result = StringUtil::wordwrap($this->dummyText, 60, 'H'); - + $expected = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sedH" . "do eiusmod tempor incididunt ut labore et dolore magnaH" . "aliqua. Ut enim ad minim veniam, quis nostrud exercitationH" . @@ -71,7 +72,7 @@ public function testItCanUseACustomBreakCharacter() : void self::assertEquals($expected, $result); } - public function testItCanStripAnsiEscapeSequence() : void + public function testItCanStripAnsiEscapeSequence(): void { $result = StringUtil::stripAnsiEscapeSequence("\x1b[7mfoo\x1b[0m"); $this->assertEquals('foo', $result); @@ -83,17 +84,17 @@ public function testItCanStripAnsiEscapeSequence() : void $this->assertEquals('foobarbaz!!!', $result); } - public function testSplitItemBug() : void + public function testSplitItemBug(): void { $test = 'Item three I guess it isn\'t that bad, is it ?'; - + self::assertEquals( "Item three\nI guess it\nisn't that\nbad, is it\n?", StringUtil::wordwrap($test, 11) ); } - public function testLengthIgnoresAnsiEscapeSequences() : void + public function testLengthIgnoresAnsiEscapeSequences(): void { $result = StringUtil::length("\x1b[7mfoo\x1b[0m"); $this->assertEquals(3, $result); @@ -105,7 +106,7 @@ public function testLengthIgnoresAnsiEscapeSequences() : void $this->assertEquals(12, $result); } - public function testLengthIncludingAnsiEscapeSequences() : void + public function testLengthIncludingAnsiEscapeSequences(): void { $result = StringUtil::length("\x1b[7mfoo\x1b[0m", false); $this->assertEquals(11, $result);