Skip to content

Commit 33a1a94

Browse files
committed
Update phpstan and fix findings
1 parent 9e3d9fb commit 33a1a94

File tree

10 files changed

+26
-22
lines changed

10 files changed

+26
-22
lines changed

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": "^7.1",
1818
"squizlabs/php_codesniffer": "^3.2",
19-
"phpstan/phpstan": "^0.9.2"
19+
"phpstan/phpstan": "^0.11"
2020
},
2121
"require": {
2222
"php" : ">=7.1",

src/CliMenu.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ private function display() : void
244244
$reader = new NonCanonicalReader($this->terminal);
245245
$reader->addControlMappings($this->defaultControlMappings);
246246

247-
while ($this->isOpen() && $char = $reader->readCharacter()) {
247+
while ($this->isOpen()) {
248+
$char = $reader->readCharacter();
248249
if (!$char->isHandledControl()) {
249250
$rawChar = $char->get();
250251
if (isset($this->customControlMappings[$rawChar])) {
@@ -293,7 +294,7 @@ protected function moveSelectionVertically(string $direction) : void
293294
? $this->selectedItem--
294295
: $this->selectedItem++;
295296

296-
if (!array_key_exists($this->selectedItem, $this->items)) {
297+
if ($this->selectedItem !== null && !array_key_exists($this->selectedItem, $this->items)) {
297298
$this->selectedItem = $direction === 'UP'
298299
? end($itemKeys)
299300
: reset($itemKeys);
@@ -371,7 +372,9 @@ protected function executeCurrentItem() : void
371372

372373
if ($item->canSelect()) {
373374
$callable = $item->getSelectAction();
374-
$callable($this);
375+
if ($callable) {
376+
$callable($this);
377+
}
375378
}
376379
}
377380

src/Dialogue/Confirm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function display(string $confirmText = 'OK') : void
3636
$this->emptyRow();
3737

3838
$confirmText = sprintf(' < %s > ', $confirmText);
39-
$leftFill = ($promptWidth / 2) - (mb_strlen($confirmText) / 2);
39+
$leftFill = (int) (($promptWidth / 2) - (mb_strlen($confirmText) / 2));
4040

4141
$this->write(sprintf(
4242
"%s%s%s%s%s%s%s\n",

src/Dialogue/Dialogue.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ protected function assertMenuOpen() : void
6868
protected function calculateCoordinates() : void
6969
{
7070
//y
71-
$textLines = count(explode("\n", $this->text)) + 2;
72-
$this->y = ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($textLines / 2) + 1;
71+
$textLines = count(explode("\n", $this->text)) + 2;
72+
$this->y = (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($textLines / 2) + 1);
7373

7474
//x
75-
$parentStyle = $this->parentMenu->getStyle();
76-
$dialogueHalfLength = (mb_strlen($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2;
77-
$widthHalfLength = ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
78-
$this->x = $widthHalfLength - $dialogueHalfLength;
75+
$parentStyle = $this->parentMenu->getStyle();
76+
$dialogueHalfLength = (int) ((mb_strlen($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2);
77+
$widthHalfLength = (int) ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
78+
$this->x = $widthHalfLength - $dialogueHalfLength;
7979
}
8080

8181
/**

src/Input/InputIO.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ private function calculateYPosition() : int
110110
{
111111
$lines = 5; //1. empty 2. prompt text 3. empty 4. input 5. empty
112112

113-
return ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1;
113+
return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1);
114114
}
115115

116116
private function calculateYPositionWithError() : int
117117
{
118118
$lines = 7; //1. empty 2. prompt text 3. empty 4. input 5. empty 6. error 7. empty
119119

120-
return ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1;
120+
return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1);
121121
}
122122

123123
private function calculateXPosition(Input $input, string $userInput) : int
@@ -134,7 +134,7 @@ private function calculateXPosition(Input $input, string $userInput) : int
134134
$halfWidth = ($width + ($input->getStyle()->getPaddingLeftRight() * 2)) / 2;
135135
$parentHalfWidth = ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
136136

137-
return $parentHalfWidth - $halfWidth;
137+
return (int) ($parentHalfWidth - $halfWidth);
138138
}
139139

140140
private function drawLine(Input $input, string $userInput, string $text) : void
@@ -164,8 +164,8 @@ private function drawCenteredLine(Input $input, string $userInput, string $text)
164164
);
165165

166166
$textLength = mb_strlen(StringUtil::stripAnsiEscapeSequence($text));
167-
$leftFill = ($width / 2) - ($textLength / 2);
168-
$rightFill = ceil($width - $leftFill - $textLength);
167+
$leftFill = (int) (($width / 2) - ($textLength / 2));
168+
$rightFill = (int) ceil($width - $leftFill - $textLength);
169169

170170
$this->drawLine(
171171
$input,

src/Input/Number.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ public function getPlaceholderText() : string
8080
public function ask() : InputResult
8181
{
8282
$this->inputIO->registerControlCallback(InputCharacter::UP, function (string $input) {
83-
return $this->validate($input) ? $input + 1 : $input;
83+
return $this->validate($input) ? (int) $input + 1 : $input;
8484
});
8585

8686
$this->inputIO->registerControlCallback(InputCharacter::DOWN, function (string $input) {
87-
return $this->validate($input) ? $input - 1 : $input;
87+
return $this->validate($input) ? (int) $input - 1 : $input;
8888
});
8989

9090
return $this->inputIO->collect($this);

src/MenuItem/AsciiArtItem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
6767
break;
6868
case self::POSITION_CENTER:
6969
default:
70-
$left = ceil($padding / 2);
70+
$left = (int) ceil($padding / 2);
7171
$row = sprintf('%s%s', str_repeat(' ', $left), $row);
7272
break;
7373
}
@@ -117,7 +117,7 @@ public function setText(string $text) : void
117117
*/
118118
private function calculateArtLength() : void
119119
{
120-
$this->artLength = max(array_map('mb_strlen', explode("\n", $this->text)));
120+
$this->artLength = (int) max(array_map('mb_strlen', explode("\n", $this->text)));
121121
}
122122

123123
/**

src/MenuItem/SplitItem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
117117
: floor($style->getContentWidth() / $numberOfItems);
118118

119119
$length -= $this->gutter;
120+
$length = (int) $length;
120121

121122
$missingLength = $style->getContentWidth() % $numberOfItems;
122123

src/MenuStyle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public function getMargin() : int
485485
public function setMarginAuto() : self
486486
{
487487
$this->marginAuto = true;
488-
$this->margin = floor(($this->terminal->getWidth() - $this->width) / 2);
488+
$this->margin = (int) floor(($this->terminal->getWidth() - $this->width) / 2);
489489

490490
$this->generateBorderRows();
491491
$this->generatePaddingTopBottomRows();

src/Util/StringUtil.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public static function wordwrap(string $string, int $width, string $break = "\n"
4141

4242
public static function stripAnsiEscapeSequence(string $str) : string
4343
{
44-
return preg_replace('/\x1b[^m]*m/', '', $str);
44+
return (string) preg_replace('/\x1b[^m]*m/', '', $str);
4545
}
4646
}

0 commit comments

Comments
 (0)