These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PhpSchool\CliMenu\Dialogue; |
||
4 | |||
5 | use PhpSchool\CliMenu\CliMenu; |
||
6 | use PhpSchool\CliMenu\Exception\MenuNotOpenException; |
||
7 | use PhpSchool\CliMenu\MenuStyle; |
||
8 | use PhpSchool\Terminal\Terminal; |
||
9 | |||
10 | /** |
||
11 | * @author Aydin Hassan <[email protected]> |
||
12 | */ |
||
13 | abstract class Dialogue |
||
14 | { |
||
15 | /** |
||
16 | * @var MenuStyle |
||
17 | */ |
||
18 | protected $style; |
||
19 | |||
20 | /** |
||
21 | * @var CliMenu |
||
22 | */ |
||
23 | protected $parentMenu; |
||
24 | |||
25 | /** |
||
26 | * @var Terminal |
||
27 | */ |
||
28 | protected $terminal; |
||
29 | |||
30 | /** |
||
31 | * @var string $text |
||
32 | */ |
||
33 | protected $text; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $x; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $y; |
||
44 | |||
45 | public function __construct(CliMenu $parentMenu, MenuStyle $menuStyle, Terminal $terminal, string $text) |
||
46 | { |
||
47 | $this->style = $menuStyle; |
||
48 | $this->terminal = $terminal; |
||
49 | $this->text = $text; |
||
50 | $this->parentMenu = $parentMenu; |
||
51 | |||
52 | $this->calculateCoordinates(); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @throws MenuNotOpenException |
||
57 | */ |
||
58 | protected function assertMenuOpen() : void |
||
59 | { |
||
60 | if (!$this->parentMenu->isOpen()) { |
||
61 | throw new MenuNotOpenException; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Calculate the co-ordinates to write the messages |
||
67 | */ |
||
68 | protected function calculateCoordinates() : void |
||
69 | { |
||
70 | //y |
||
71 | $textLines = count(explode("\n", $this->text)) + 2; |
||
72 | $this->y = ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($textLines / 2) + 1; |
||
0 ignored issues
–
show
|
|||
73 | |||
74 | //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; |
||
0 ignored issues
–
show
The property
$x was declared of type integer , but $widthHalfLength - $dialogueHalfLength is of type double . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Write an empty row |
||
83 | */ |
||
84 | protected function emptyRow() : void |
||
85 | { |
||
86 | $this->write( |
||
87 | sprintf( |
||
88 | "%s%s%s%s%s\n", |
||
89 | $this->style->getColoursSetCode(), |
||
90 | str_repeat(' ', $this->style->getPaddingLeftRight()), |
||
91 | str_repeat(' ', mb_strlen($this->text)), |
||
92 | str_repeat(' ', $this->style->getPaddingLeftRight()), |
||
93 | $this->style->getColoursResetCode() |
||
94 | ) |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Write some text at a particular column |
||
100 | */ |
||
101 | protected function write(string $text, int $column = null) : void |
||
102 | { |
||
103 | $this->terminal->moveCursorToColumn($column ?: $this->x); |
||
104 | $this->terminal->write($text); |
||
105 | } |
||
106 | |||
107 | public function getStyle() : MenuStyle |
||
108 | { |
||
109 | return $this->style; |
||
110 | } |
||
111 | } |
||
112 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.