| Total Complexity | 59 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ElementBox often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ElementBox, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ElementBox extends Box |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var Element |
||
| 25 | */ |
||
| 26 | protected $element; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get element. |
||
| 30 | * |
||
| 31 | * @return Element |
||
| 32 | */ |
||
| 33 | public function getElement() |
||
| 34 | { |
||
| 35 | return $this->element; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Set element. |
||
| 40 | * |
||
| 41 | * @param Element $element |
||
| 42 | * |
||
| 43 | * @return $this |
||
| 44 | */ |
||
| 45 | public function setElement(Element $element) |
||
| 46 | { |
||
| 47 | $this->element = $element; |
||
| 48 | $element->setBox($this); |
||
| 49 | |||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get boxes by tag name. |
||
| 55 | * |
||
| 56 | * @param string $tagName |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function getBoxesByTagName(string $tagName) |
||
| 61 | { |
||
| 62 | $boxes = []; |
||
| 63 | $allChildren = []; |
||
| 64 | $this->getAllChildren($allChildren); |
||
| 65 | foreach ($allChildren as $child) { |
||
| 66 | if ($child instanceof self && $child->getElement() && $child->getElement()->getDOMElement()) { |
||
| 67 | if (isset($child->getElement()->getDOMElement()->tagName)) { |
||
| 68 | $elementTagName = $child->getElement()->getDOMElement()->tagName; |
||
| 69 | if ($elementTagName && strtolower($elementTagName) === strtolower($tagName)) { |
||
| 70 | $boxes[] = $child; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | return $boxes; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Fix tables - iterate through cells and insert missing one. |
||
| 81 | * |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | public function fixTables() |
||
| 85 | { |
||
| 86 | $tables = $this->getBoxesByType('TableWrapperBox'); |
||
| 87 | foreach ($tables as $tableWrapperBox) { |
||
| 88 | $tableBox = $tableWrapperBox->getFirstChild(); |
||
| 89 | $rowGroups = $tableBox->getChildren(); |
||
| 90 | foreach ($rowGroups as $rowGroup) { |
||
| 91 | // wrap rows with row groups |
||
| 92 | if ($rowGroup instanceof TableRowBox) { |
||
| 93 | if (!isset($wrapRowGroup)) { |
||
| 94 | $wrapRowGroup = $tableBox->removeChild($tableBox->createRowGroupBox()); |
||
| 95 | $tableBox->insertBefore($wrapRowGroup, $rowGroup); |
||
| 96 | } |
||
| 97 | $wrapRowGroup->appendChild($tableBox->removeChild($rowGroup)); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | unset($wrapRowGroup); |
||
| 101 | $rowGroups = $tableBox->getChildren(); |
||
| 102 | if (empty($rowGroups)) { |
||
| 103 | $rowGroup = $tableBox->createRowGroupBox(); |
||
| 104 | $row = $rowGroup->createRowBox(); |
||
| 105 | $column = $row->createColumnBox(); |
||
| 106 | $column->createCellBox(); |
||
| 107 | } else { |
||
| 108 | $columnsCount = 0; |
||
| 109 | foreach ($rowGroups as $rowGroup) { |
||
| 110 | if (!$rowGroup->hasChildren()) { |
||
| 111 | $row = $rowGroup->createRowBox(); |
||
| 112 | $column = $row->createColumnBox(); |
||
| 113 | $column->createCellBox(); |
||
| 114 | } |
||
| 115 | foreach ($rowGroup->getChildren() as $rowIndex => $row) { |
||
| 116 | $columns = $row->getChildren(); |
||
| 117 | $columnsCount = max($columnsCount, \count($columns)); |
||
| 118 | foreach ($columns as $columnIndex => $column) { |
||
| 119 | if ($column->getRowSpan() > 1) { |
||
| 120 | $rowSpans = $column->getRowSpan(); |
||
| 121 | for ($i = 1; $i < $rowSpans; ++$i) { |
||
| 122 | $nextRow = $rowGroup->getChildren()[$rowIndex + $i]; |
||
| 123 | $rowChildren = $nextRow->getChildren(); |
||
| 124 | $insertColumn = $nextRow->removeChild($nextRow->createColumnBox()); |
||
| 125 | if (isset($rowChildren[$columnIndex])) { |
||
| 126 | $before = $rowChildren[$columnIndex]; |
||
| 127 | $nextRow->insertBefore($insertColumn, $before); |
||
| 128 | } else { |
||
| 129 | $nextRow->appendChild($insertColumn); |
||
| 130 | } |
||
| 131 | $insertColumn->setStyle(clone $column->getStyle()); |
||
| 132 | $insertColumn->getStyle()->setBox($insertColumn); |
||
| 133 | $insertCell = $insertColumn->createCellBox(); |
||
| 134 | $insertCell->setStyle(clone $column->getFirstChild()->getStyle()); |
||
| 135 | $insertCell->getStyle()->setBox($insertCell); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | foreach ($rowGroup->getChildren() as $row) { |
||
| 141 | $columns = $row->getChildren(); |
||
| 142 | $missing = $columnsCount - \count($columns); |
||
| 143 | for ($i = 0; $i < $missing; ++$i) { |
||
| 144 | $column = $row->createColumnBox(); |
||
| 145 | $column->createCellBox(); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | // fix row spans |
||
| 149 | $rowSpans = []; |
||
| 150 | $rowSpansUp = []; |
||
| 151 | foreach ($rowGroup->getChildren() as $row) { |
||
| 152 | foreach ($row->getChildren() as $columnIndex => $column) { |
||
| 153 | if ($column->getRowSpan() > 1) { |
||
| 154 | $rowSpans[$columnIndex] = $column->getRowSpan(); |
||
| 155 | $rowSpansUp[$columnIndex] = 0; |
||
| 156 | $column->setRowSpanUp(0); |
||
| 157 | $row->setRowSpanUp(max($row->getRowSpanUp(), 0)); |
||
| 158 | $row->setRowSpan(max($row->getRowSpan(), $column->getRowSpan())); |
||
| 159 | } else { |
||
| 160 | if (isset($rowSpans[$columnIndex]) && $rowSpans[$columnIndex] > 1) { |
||
| 161 | if ($rowSpansUp[$columnIndex] < $rowSpans[$columnIndex]) { |
||
| 162 | ++$rowSpansUp[$columnIndex]; |
||
| 163 | $column->setRowSpanUp($rowSpansUp[$columnIndex]); |
||
| 164 | $row->setRowSpanUp(max($row->getRowSpanUp(), $rowSpansUp[$columnIndex])); |
||
| 165 | $row->setRowSpan(max($row->getRowSpan(), $column->getRowSpan())); |
||
| 166 | } else { |
||
| 167 | $rowSpansUp[$columnIndex] = 0; |
||
| 168 | $rowSpans[$columnIndex] = 1; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Span all rows. |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function spanAllRows() |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Build tree. |
||
| 198 | * |
||
| 199 | * @param $parentBlock |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function buildTree($parentBlock = null) |
||
| 293 | } |
||
| 294 | } |
||
| 295 |