| Total Complexity | 72 |
| Total Lines | 274 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GlyphBlock 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 GlyphBlock, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | abstract class GlyphBlock extends Glyph |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var mixed |
||
| 12 | */ |
||
| 13 | private int $deltaWidth; |
||
| 14 | private int $deltaHeight; |
||
| 15 | |||
| 16 | public function __construct(protected FrameConfigInterface $globalConfig) |
||
| 17 | { |
||
| 18 | parent::__construct($globalConfig); |
||
| 19 | $this->config = array_merge( |
||
| 20 | parent::getConfig(), |
||
| 21 | [ |
||
| 22 | 'paddingTop' => 0, |
||
| 23 | 'paddingLeft' => 0, |
||
| 24 | 'paddingRight' => 0, |
||
| 25 | 'paddingBottom' => 0, |
||
| 26 | 'borderTop' => 0, |
||
| 27 | 'borderLeft' => 0, |
||
| 28 | 'borderRight' => 0, |
||
| 29 | 'borderBottom' => 0, |
||
| 30 | ] |
||
| 31 | ); |
||
| 32 | $this->calculateDeltaWidth(); |
||
| 33 | $this->calculateDeltaHeight(); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function getContentWidth(): int |
||
| 37 | { |
||
| 38 | return $this->width - $this->deltaWidth; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function getDeltaWidth(): int |
||
| 42 | { |
||
| 43 | return $this->deltaWidth; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function getDeltaHeight(): int |
||
| 47 | { |
||
| 48 | return $this->deltaHeight; |
||
| 49 | } |
||
| 50 | |||
| 51 | |||
| 52 | public function getContentHeight(): int |
||
| 53 | { |
||
| 54 | return $this->height - $this->deltaHeight; |
||
| 55 | } |
||
| 56 | |||
| 57 | private function calculateDeltaWidth(): void |
||
| 58 | { |
||
| 59 | $this->deltaWidth = $this->__get('paddingLeft') + |
||
| 60 | $this->__get('borderLeft') + |
||
| 61 | $this->__get('paddingRight') + |
||
| 62 | $this->__get('borderRight'); |
||
| 63 | } |
||
| 64 | |||
| 65 | private function calculateDeltaHeight(): void |
||
| 66 | { |
||
| 67 | $this->deltaHeight = $this->__get('paddingTop') + |
||
| 68 | $this->__get('borderTop') + |
||
| 69 | $this->__get('paddingBottom') + |
||
| 70 | $this->__get('borderBottom'); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function setPadding(int $paddingLeft, int $paddingRight, int $paddingTop, int $paddingBottom): GlyphBlock |
||
| 74 | { |
||
| 75 | $this->__set('paddingTop', $paddingTop); |
||
| 76 | $this->__set('paddingLeft', $paddingLeft); |
||
| 77 | $this->__set('paddingRight', $paddingRight); |
||
| 78 | $this->__set('paddingBottom', $paddingBottom); |
||
| 79 | $this->calculateDeltaWidth(); |
||
| 80 | $this->calculateDeltaHeight(); |
||
| 81 | return $this; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function setBorder(int $borderLeft, int $borderRight, int $borderTop, int $borderBottom): GlyphBlock |
||
| 85 | { |
||
| 86 | $this->__set('borderTop', $borderTop); |
||
| 87 | $this->__set('borderLeft', $borderLeft); |
||
| 88 | $this->__set('borderRight', $borderRight); |
||
| 89 | $this->__set('borderBottom', $borderBottom); |
||
| 90 | $this->calculateDeltaWidth(); |
||
| 91 | $this->calculateDeltaHeight(); |
||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function appendWidth(int $value): void |
||
| 96 | { |
||
| 97 | $this->offsetX = $this->__get('paddingLeft') + $this->__get('borderLeft'); |
||
| 98 | $this->contentWidth = $value; |
||
| 99 | $this->width = $value + $this->deltaWidth; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function appendHeight(int $value): void |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getWidthByContent(int $endOfPreviousSibling = 0): int |
||
| 124 | } |
||
| 125 | |||
| 126 | public function getHeightByContent(int $endOfPreviousSibling = 0): int |
||
| 144 | } |
||
| 145 | |||
| 146 | public function render(): Glyph |
||
| 157 | } |
||
| 158 | |||
| 159 | protected function renderBorder(): void |
||
| 160 | { |
||
| 161 | $this->renderBorderTop(); |
||
| 162 | $this->renderBorderBottom(); |
||
| 163 | $this->renderBorderLeft(); |
||
| 164 | $this->renderBorderRight(); |
||
| 165 | } |
||
| 166 | |||
| 167 | private function renderBorderTop(): void |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | private function renderBorderBottom() |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | private function renderBorderLeft() |
||
| 187 | { |
||
| 188 | $height = $this->getHeight(); |
||
| 189 | if ($this->__get('borderLeft')) { |
||
| 190 | for ($i = 0; $i < $this->getWidth(); $i++) { |
||
| 191 | if ($i == 0 && $this->renderMap[$i][0]->is($this->gc('lineHorizontal'))) { |
||
| 192 | $this->renderMap[$i][0] = $this->gc('cornerLeftTop'); |
||
| 193 | } elseif ($i == $height - 1 && $this->renderMap[$i][0]->is($this->gc('lineHorizontal'))) { |
||
| 194 | $this->renderMap[$i][0] = $this->gc('cornerLeftBottom'); |
||
| 195 | } else { |
||
| 196 | $this->renderMap[$i][0] = $this->gc('lineVertical'); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | private function renderBorderRight(): void |
||
| 203 | { |
||
| 204 | $width = $this->getWidth(); |
||
| 205 | $height = $this->getHeight(); |
||
| 206 | if ($this->__get('borderRight')) { |
||
| 207 | for ($i = 0; $i < $height; $i++) { |
||
| 208 | if ($i == 0 && $this->renderMap[$i][$width - 1]->is($this->gc('lineHorizontal'))) { |
||
| 209 | $this->renderMap[$i][$width - 1] = $this->gc('cornerRightTop'); |
||
| 210 | } elseif ( |
||
| 211 | $i == $height - 1 && |
||
| 212 | $this->renderMap[$i][$width - 1]->is($this->gc('lineHorizontal')) |
||
| 213 | ) { |
||
| 214 | $this->renderMap[$i][$width - 1] = $this->gc('cornerRightBottom'); |
||
| 215 | } else { |
||
| 216 | $this->renderMap[$i][$width - 1] = $this->gc('lineVertical'); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | protected function renderBySprites(): void |
||
| 223 | { |
||
| 224 | $this->cachedRenderMap = $this->renderMap; |
||
| 225 | foreach ($this->renderMap as $y => $line) { |
||
| 226 | foreach ($line as $x => $symbol) { |
||
| 227 | $codes = $this->getSprite($x, $y); |
||
| 228 | $newSymbol = $this->pattern($codes); |
||
| 229 | if ($newSymbol) { |
||
| 230 | $this->renderMap[$y][$x] = $newSymbol; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | private function getSprite(int $x, int $y): string |
||
| 237 | { |
||
| 238 | $codes = [ |
||
| 239 | isset($this->cachedRenderMap[$y - 1][$x]) ? $this->cachedRenderMap[$y - 1][$x]->getAlias() : '0', |
||
| 240 | isset($this->cachedRenderMap[$y][$x - 1]) ? $this->cachedRenderMap[$y][$x - 1]->getAlias() : '0', |
||
| 241 | isset($this->cachedRenderMap[$y][$x]) ? $this->cachedRenderMap[$y][$x]->getAlias() : '0', |
||
| 242 | isset($this->cachedRenderMap[$y][$x + 1]) ? $this->cachedRenderMap[$y][$x + 1]->getAlias() : '0', |
||
| 243 | isset($this->cachedRenderMap[$y + 1][$x]) ? $this->cachedRenderMap[$y + 1][$x]->getAlias() : '0', |
||
| 244 | ]; |
||
| 245 | return implode("", $codes); |
||
| 246 | } |
||
| 247 | |||
| 248 | private function pattern(string $codes): object|bool |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | } |
||
| 287 |