Skip to content

Commit 77dd7fc

Browse files
committed
Upgrade phpstan + fix issues
1 parent 6b3e058 commit 77dd7fc

File tree

16 files changed

+264
-249
lines changed

16 files changed

+264
-249
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
2828
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
2929
30-
- name: Install dependencies
30+
- name: Install cdependencies
3131
run: composer update --no-interaction --no-progress
3232

3333
- name: Tests

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"require-dev": {
1717
"phpunit/phpunit": "^11.5",
1818
"squizlabs/php_codesniffer": "^3.2",
19-
"phpstan/phpstan": "^1.2.0"
19+
"phpstan/phpstan": "^2.1"
2020
},
2121
"require": {
2222
"php" : ">=8.2",

phpstan.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
parameters:
2-
checkMissingIterableValueType: false

src/Builder/CliMenuBuilder.php

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,66 +32,38 @@
3232
*/
3333
class CliMenuBuilder
3434
{
35-
/**
36-
* @var CliMenu
37-
*/
38-
private $menu;
35+
private CliMenu $menu;
3936

40-
/**
41-
* @var string
42-
*/
43-
private $goBackButtonText = 'Go Back';
37+
private string $goBackButtonText = 'Go Back';
4438

45-
/**
46-
* @var string
47-
*/
48-
private $exitButtonText = 'Exit';
39+
private string $exitButtonText = 'Exit';
4940

50-
/**
51-
* @var MenuStyle
52-
*/
53-
private $style;
41+
private MenuStyle $style;
5442

55-
/**
56-
* @var Terminal
57-
*/
58-
private $terminal;
43+
private Terminal $terminal;
5944

60-
/**
61-
* @var bool
62-
*/
63-
private $disableDefaultItems = false;
45+
private bool $disableDefaultItems = false;
6446

65-
/**
66-
* @var bool
67-
*/
68-
private $disabled = false;
47+
private bool $disabled = false;
6948

7049
/**
7150
* Whether or not to auto create keyboard shortcuts for items
7251
* when they contain square brackets. Eg: [M]y item
73-
*
74-
* @var bool
7552
*/
76-
private $autoShortcuts = false;
53+
private bool $autoShortcuts = false;
7754

7855
/**
7956
* Regex to auto match for shortcuts defaults to looking
8057
* for a single character encased in square brackets
81-
*
82-
* @var string
8358
*/
84-
private $autoShortcutsRegex = '/\[(.)\]/';
59+
private string $autoShortcutsRegex = '/\[(.)\]/';
8560

8661
/**
87-
* @var array
62+
* @var list<array{class: class-string<MenuItemInterface>, style: ItemStyle}>
8863
*/
89-
private $extraItemStyles = [];
64+
private array $extraItemStyles = [];
9065

91-
/**
92-
* @var bool
93-
*/
94-
private $subMenu = false;
66+
private bool $subMenu = false;
9567

9668
public function __construct(?Terminal $terminal = null)
9769
{
@@ -135,6 +107,9 @@ public function addItem(
135107
return $this;
136108
}
137109

110+
/**
111+
* @param list<array{0: string, 1: callable}> $items
112+
*/
138113
public function addItems(array $items) : self
139114
{
140115
foreach ($items as $item) {
@@ -155,6 +130,9 @@ public function addCheckboxItem(
155130
return $this;
156131
}
157132

133+
/**
134+
* @param list<array{0: string, 1: callable}> $items
135+
*/
158136
public function addCheckboxItems(array $items): self
159137
{
160138
foreach ($items as $item) {
@@ -175,6 +153,9 @@ public function addRadioItem(
175153
return $this;
176154
}
177155

156+
/**
157+
* @param list<array{0: string, 1: callable}> $items
158+
*/
178159
public function addRadioItems(array $items): self
179160
{
180161
foreach ($items as $item) {
@@ -504,6 +485,9 @@ public function getTerminal() : Terminal
504485
return $this->terminal;
505486
}
506487

488+
/**
489+
* @return list<MenuItemInterface>
490+
*/
507491
private function getDefaultItems() : array
508492
{
509493
$actions = [];
@@ -532,6 +516,9 @@ public function displayExtra() : self
532516
return $this;
533517
}
534518

519+
/**
520+
* @param array<MenuItemInterface> $items
521+
*/
535522
private function itemsHaveExtra(array $items) : bool
536523
{
537524
return !empty(array_filter($items, function (MenuItemInterface $item) {
@@ -647,6 +634,9 @@ public function modifyStyle(string $styleClass, callable $itemCallable) : self
647634
return $this;
648635
}
649636

637+
/**
638+
* @param class-string<MenuItemInterface> $itemClass
639+
*/
650640
public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : self
651641
{
652642
$this->menu->getStyleLocator()

src/Builder/SplitItemBuilder.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpSchool\CliMenu\MenuItem\SelectableItem;
1313
use PhpSchool\CliMenu\MenuItem\SplitItem;
1414
use PhpSchool\CliMenu\MenuItem\StaticItem;
15+
use PhpSchool\CliMenu\MenuStyle;
1516
use PhpSchool\CliMenu\Style\ItemStyle;
1617
use function \PhpSchool\CliMenu\Util\each;
1718

@@ -20,36 +21,26 @@
2021
*/
2122
class SplitItemBuilder
2223
{
23-
/**
24-
* @var CliMenu
25-
*/
26-
private $menu;
24+
private CliMenu $menu;
2725

28-
/**
29-
* @var SplitItem
30-
*/
31-
private $splitItem;
26+
private SplitItem $splitItem;
3227

3328
/**
34-
* Whether or not to auto create keyboard shortcuts for items
29+
* Whether to auto create keyboard shortcuts for items
3530
* when they contain square brackets. Eg: [M]y item
36-
*
37-
* @var bool
3831
*/
39-
private $autoShortcuts = false;
32+
private bool $autoShortcuts = false;
4033

4134
/**
4235
* Regex to auto match for shortcuts defaults to looking
4336
* for a single character encased in square brackets
44-
*
45-
* @var string
4637
*/
47-
private $autoShortcutsRegex = '/\[(.)\]/';
38+
private string $autoShortcutsRegex = '/\[(.)\]/';
4839

4940
/**
50-
* @var array
41+
* @var list<array{class: class-string<MenuItemInterface>, style: ItemStyle}>
5142
*/
52-
private $extraItemStyles = [];
43+
private array $extraItemStyles = [];
5344

5445
public function __construct(CliMenu $menu)
5546
{
@@ -155,6 +146,9 @@ public function enableAutoShortcuts(?string $regex = null) : self
155146
return $this;
156147
}
157148

149+
/**
150+
* @param class-string<MenuItemInterface> $itemClass
151+
*/
158152
public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : self
159153
{
160154
$this->extraItemStyles[] = ['class' => $itemClass, 'style' => $itemStyle];

src/CliMenu.php

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,29 @@
3232
*/
3333
class CliMenu
3434
{
35-
/**
36-
* @var Terminal
37-
*/
38-
protected $terminal;
35+
protected Terminal $terminal;
3936

40-
/**
41-
* @var MenuStyle
42-
*/
43-
protected $style;
37+
protected MenuStyle $style;
4438

45-
/**
46-
* @var Locator
47-
*/
48-
private $itemStyleLocator;
39+
private Locator $itemStyleLocator;
4940

50-
/**
51-
* @var ?string
52-
*/
53-
protected $title;
41+
protected string|null $title;
5442

5543
/**
56-
* @var MenuItemInterface[]
44+
* @var array<int, MenuItemInterface>
5745
*/
58-
protected $items = [];
46+
protected array $items = [];
5947

60-
/**
61-
* @var int|null
62-
*/
63-
protected $selectedItem;
48+
protected int|null $selectedItem = null;
6449

65-
/**
66-
* @var bool
67-
*/
68-
protected $open = false;
50+
protected bool $open = false;
6951

70-
/**
71-
* @var CliMenu|null
72-
*/
73-
protected $parent;
52+
protected CliMenu|null $parent;
7453

7554
/**
76-
* @var array
55+
* @var array<string, InputCharacter::*>
7756
*/
78-
protected $defaultControlMappings = [
57+
protected array $defaultControlMappings = [
7958
'^P' => InputCharacter::UP,
8059
'k' => InputCharacter::UP,
8160
'^K' => InputCharacter::DOWN,
@@ -87,15 +66,15 @@ class CliMenu
8766
];
8867

8968
/**
90-
* @var array
69+
* @var array<string, callable>
9170
*/
92-
protected $customControlMappings = [];
71+
protected array $customControlMappings = [];
72+
73+
private Frame $currentFrame;
9374

9475
/**
95-
* @var Frame
76+
* @param list<MenuItemInterface> $items
9677
*/
97-
private $currentFrame;
98-
9978
public function __construct(
10079
?string $title,
10180
array $items,
@@ -185,6 +164,8 @@ public function addItem(MenuItemInterface $item) : void
185164

186165
/**
187166
* Add multiple Items to the menu
167+
*
168+
* @param list<MenuItemInterface> $items
188169
*/
189170
public function addItems(array $items) : void
190171
{
@@ -197,6 +178,8 @@ public function addItems(array $items) : void
197178

198179
/**
199180
* Set Items of the menu
181+
*
182+
* @param list<MenuItemInterface> $items
200183
*/
201184
public function setItems(array $items) : void
202185
{
@@ -231,6 +214,9 @@ public function disableDefaultControlMappings() : void
231214

232215
/**
233216
* Set default control mappings
217+
*
218+
* @param array<string, InputCharacter::*> $defaultControlMappings
219+
*
234220
*/
235221
public function setDefaultControlMappings(array $defaultControlMappings) : void
236222
{
@@ -249,13 +235,18 @@ public function addCustomControlMapping(string $input, callable $callable) : voi
249235
$this->customControlMappings[$input] = $callable;
250236
}
251237

238+
/**
239+
* @return array<string, callable>
240+
*/
252241
public function getCustomControlMappings() : array
253242
{
254243
return $this->customControlMappings;
255244
}
256245

257246
/**
258247
* Shorthand function to add multiple custom control mapping at once
248+
*
249+
* @param array<string, callable> $map
259250
*/
260251
public function addCustomControlMappings(array $map) : void
261252
{
@@ -344,7 +335,7 @@ protected function moveSelectionVertically(string $direction) : void
344335
? $this->selectedItem--
345336
: $this->selectedItem++;
346337

347-
if ($this->selectedItem !== null && !array_key_exists($this->selectedItem, $this->items)) {
338+
if (!array_key_exists($this->selectedItem, $this->items)) {
348339
$this->selectedItem = $direction === 'UP'
349340
? (int) end($itemKeys)
350341
: (int) reset($itemKeys);
@@ -544,6 +535,8 @@ protected function draw() : void
544535

545536
/**
546537
* Draw a menu item
538+
*
539+
* @return list<string>
547540
*/
548541
protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) : array
549542
{
@@ -662,6 +655,9 @@ public function setStyle(MenuStyle $style) : void
662655
$this->style = $style;
663656
}
664657

658+
/**
659+
* @param class-string $styleClass
660+
*/
665661
public function setItemStyle(ItemStyle $style, string $styleClass) : void
666662
{
667663
$this->itemStyleLocator->setStyle($style, $styleClass);

0 commit comments

Comments
 (0)