Complex classes like StyleManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 StyleManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager |
||
| 14 | { |
||
| 15 | /** @var StyleRegistry */ |
||
| 16 | protected $styleRegistry; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * For empty cells, we can specify a style or not. If no style are specified, |
||
| 20 | * then the software default will be applied. But sometimes, it may be useful |
||
| 21 | * to override this default style, for instance if the cell should have a |
||
| 22 | * background color different than the default one or some borders |
||
| 23 | * (fonts property don't really matter here). |
||
| 24 | * |
||
| 25 | * @param int $styleId |
||
| 26 | * @return bool Whether the cell should define a custom style |
||
| 27 | */ |
||
| 28 | 11 | public function shouldApplyStyleOnEmptyCell($styleId) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Returns the content of the "styles.xml" file, given a list of styles. |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | 39 | public function getStylesXMLFileContent() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the content of the "<numFmts>" section. |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 39 | protected function getFormatsSectionContent() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Returns the content of the "<fonts>" section. |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 39 | protected function getFontsSectionContent() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the content of the "<fills>" section. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 39 | protected function getFillsSectionContent() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the content of the "<borders>" section. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 39 | protected function getBordersSectionContent() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Returns the content of the "<cellStyleXfs>" section. |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 39 | protected function getCellStyleXfsSectionContent() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the content of the "<cellXfs>" section. |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 39 | protected function getCellXfsSectionContent() |
|
| 233 | { |
||
| 234 | 39 | $registeredStyles = $this->styleRegistry->getRegisteredStyles(); |
|
| 235 | |||
| 236 | 39 | $content = '<cellXfs count="' . \count($registeredStyles) . '">'; |
|
| 237 | |||
| 238 | 39 | foreach ($registeredStyles as $style) { |
|
| 239 | 39 | $styleId = $style->getId(); |
|
| 240 | 39 | $fillId = $this->getFillIdForStyleId($styleId); |
|
| 241 | 39 | $borderId = $this->getBorderIdForStyleId($styleId); |
|
| 242 | 39 | $numFmtId = $this->getFormatIdForStyleId($styleId); |
|
| 243 | |||
| 244 | 39 | $content .= '<xf numFmtId="' . $numFmtId . '" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"'; |
|
| 245 | |||
| 246 | 39 | if ($style->shouldApplyFont()) { |
|
| 247 | 39 | $content .= ' applyFont="1"'; |
|
| 248 | } |
||
| 249 | |||
| 250 | 39 | $content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0); |
|
| 251 | |||
| 252 | 39 | if ($style->shouldApplyCellAlignment() || $style->shouldWrapText()) { |
|
| 253 | 3 | $content .= ' applyAlignment="1">'; |
|
| 254 | 3 | $content .= '<alignment'; |
|
| 255 | 3 | if ($style->shouldApplyCellAlignment()) { |
|
| 256 | 1 | $content .= \sprintf(' horizontal="%s"', $style->getCellAlignment()); |
|
| 257 | } |
||
| 258 | 3 | if ($style->shouldWrapText()) { |
|
| 259 | 2 | $content .= ' wrapText="1"'; |
|
| 260 | } |
||
| 261 | 3 | $content .= '/>'; |
|
| 262 | 3 | $content .= '</xf>'; |
|
| 263 | } else { |
||
| 264 | 39 | $content .= '/>'; |
|
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | 39 | $content .= '</cellXfs>'; |
|
| 269 | |||
| 270 | 39 | return $content; |
|
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns the fill ID associated to the given style ID. |
||
| 275 | * For the default style, we don't a fill. |
||
| 276 | * |
||
| 277 | * @param int $styleId |
||
| 278 | * @return int |
||
| 279 | */ |
||
| 280 | 39 | private function getFillIdForStyleId($styleId) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Returns the fill ID associated to the given style ID. |
||
| 291 | * For the default style, we don't a border. |
||
| 292 | * |
||
| 293 | * @param int $styleId |
||
| 294 | * @return int |
||
| 295 | */ |
||
| 296 | 39 | private function getBorderIdForStyleId($styleId) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the format ID associated to the given style ID. |
||
| 307 | * For the default style use general format. |
||
| 308 | * |
||
| 309 | * @param int $styleId |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | 39 | private function getFormatIdForStyleId($styleId) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the content of the "<cellStyles>" section. |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 39 | protected function getCellStylesSectionContent() |
|
| 334 | } |
||
| 335 |