| Conditions | 11 |
| Paths | 126 |
| Total Lines | 60 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 46 | public function getRows(MenuStyle $style, bool $selected = false) : array |
||
| 47 | { |
||
| 48 | $numberOfItems = count($this->items); |
||
| 49 | |||
| 50 | if (!$selected) { |
||
| 51 | $this->selectedItemIndex = -1; |
||
| 52 | } else { |
||
| 53 | if ($this->selectedItemIndex === -1) { |
||
| 54 | $this->selectedItemIndex = 0; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | $length = $style->getDisplaysExtra() |
||
| 59 | ? floor(($style->getContentWidth() - mb_strlen($style->getItemExtra()) + 2) / $numberOfItems) - $this->margin |
||
| 60 | : floor($style->getContentWidth() / $numberOfItems) - $this->margin; |
||
| 61 | $missingLength = $style->getContentWidth() % $numberOfItems; |
||
| 62 | |||
| 63 | $lines = 0; |
||
| 64 | $cells = []; |
||
| 65 | foreach ($this->items as $index => $item) { |
||
| 66 | $marker = sprintf("%s ", $style->getMarker($index === $this->selectedItemIndex)); |
||
| 67 | $content = StringUtil::wordwrap( |
||
| 68 | sprintf('%s%s', $marker, $item->getText()), |
||
| 69 | $length |
||
| 70 | ); |
||
| 71 | $cell = array_map(function ($row) use ($index, $length, $style) { |
||
| 72 | $row = $row . str_repeat(' ', $length - strlen($row)); |
||
| 73 | if ($index === $this->selectedItemIndex) { |
||
| 74 | $row = $style->getSelectedSetCode() . $row . $style->getSelectedUnsetCode(); |
||
| 75 | } else { |
||
| 76 | $row = $style->getUnselectedSetCode() . $row . $style->getUnselectedUnsetCode(); |
||
| 77 | } |
||
| 78 | $row .= $style->getUnselectedSetCode() . str_repeat(' ', $this->margin); |
||
| 79 | return $row; |
||
| 80 | }, explode("\n", $content)); |
||
| 81 | $lineCount = count($cell); |
||
| 82 | if ($lineCount > $lines) { |
||
| 83 | $lines = $lineCount; |
||
| 84 | } |
||
| 85 | $cells[] = $cell; |
||
| 86 | } |
||
| 87 | |||
| 88 | $rows = []; |
||
| 89 | for ($i = 0; $i < $lines; $i++) { |
||
| 90 | $row = ""; |
||
| 91 | foreach ($cells as $cell) { |
||
| 92 | if (isset($cell[$i])) { |
||
| 93 | $row .= $cell[$i]; |
||
| 94 | } else { |
||
| 95 | $row .= str_repeat(' ', $length); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | if ($missingLength) { |
||
| 99 | $row .= str_repeat(' ', $missingLength); |
||
| 100 | } |
||
| 101 | $rows[] = $row; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $rows; |
||
| 105 | } |
||
| 106 | |||
| 182 |