Complex classes like WorksheetManager 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 WorksheetManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class WorksheetManager implements WorksheetManagerInterface |
||
| 29 | { |
||
| 30 | use ManagesCellSize; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Maximum number of characters a cell can contain |
||
| 34 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] |
||
| 35 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010] |
||
| 36 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016] |
||
| 37 | */ |
||
| 38 | const MAX_CHARACTERS_PER_CELL = 32767; |
||
| 39 | |||
| 40 | const SHEET_XML_FILE_HEADER = <<<'EOD' |
||
| 41 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
| 42 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
| 43 | EOD; |
||
| 44 | |||
| 45 | /** @var bool Whether inline or shared strings should be used */ |
||
| 46 | protected $shouldUseInlineStrings; |
||
| 47 | |||
| 48 | /** @var RowManager Manages rows */ |
||
| 49 | private $rowManager; |
||
| 50 | |||
| 51 | /** @var StyleManager Manages styles */ |
||
| 52 | private $styleManager; |
||
| 53 | |||
| 54 | /** @var StyleMerger Helper to merge styles together */ |
||
| 55 | private $styleMerger; |
||
| 56 | |||
| 57 | /** @var SharedStringsManager Helper to write shared strings */ |
||
| 58 | private $sharedStringsManager; |
||
| 59 | |||
| 60 | /** @var XLSXEscaper Strings escaper */ |
||
| 61 | private $stringsEscaper; |
||
| 62 | |||
| 63 | /** @var StringHelper String helper */ |
||
| 64 | private $stringHelper; |
||
| 65 | |||
| 66 | /** @var InternalEntityFactory Factory to create entities */ |
||
| 67 | private $entityFactory; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * WorksheetManager constructor. |
||
| 71 | * |
||
| 72 | * @param OptionsManagerInterface $optionsManager |
||
| 73 | * @param RowManager $rowManager |
||
| 74 | * @param StyleManager $styleManager |
||
| 75 | * @param StyleMerger $styleMerger |
||
| 76 | * @param SharedStringsManager $sharedStringsManager |
||
| 77 | * @param XLSXEscaper $stringsEscaper |
||
| 78 | * @param StringHelper $stringHelper |
||
| 79 | * @param InternalEntityFactory $entityFactory |
||
| 80 | */ |
||
| 81 | 49 | public function __construct( |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @return SharedStringsManager |
||
| 106 | */ |
||
| 107 | 45 | public function getSharedStringsManager() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 49 | public function startSheet(Worksheet $worksheet) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Writes the sheet data header |
||
| 127 | * |
||
| 128 | * @param Worksheet $worksheet The worksheet to add the row to |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | 45 | private function ensureSheetDataStated(Worksheet $worksheet) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
| 144 | * |
||
| 145 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
| 146 | * @throws IOException If the sheet data file cannot be opened for writing |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | 49 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 42 | public function addRow(Worksheet $worksheet, Row $row) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Adds non empty row to the worksheet. |
||
| 170 | * |
||
| 171 | * @param Worksheet $worksheet The worksheet to add the row to |
||
| 172 | * @param Row $row The row to be written |
||
| 173 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
| 174 | * @throws IOException If the data cannot be written |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | 42 | private function addNonEmptyRow(Worksheet $worksheet, Row $row) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Applies styles to the given style, merging the cell's style with its row's style |
||
| 207 | * |
||
| 208 | * @param Cell $cell |
||
| 209 | * @param Style $rowStyle |
||
| 210 | * |
||
| 211 | * @throws InvalidArgumentException If the given value cannot be processed |
||
| 212 | * @return RegisteredStyle |
||
| 213 | */ |
||
| 214 | 42 | private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Builds and returns xml for a single cell. |
||
| 248 | * |
||
| 249 | * @param int $rowIndexOneBased |
||
| 250 | * @param int $columnIndexZeroBased |
||
| 251 | * @param Cell $cell |
||
| 252 | * @param int $styleId |
||
| 253 | * |
||
| 254 | * @throws InvalidArgumentException If the given value cannot be processed |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 42 | private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell, $styleId) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the XML fragment for a cell containing a non empty string |
||
| 289 | * |
||
| 290 | * @param string $cellValue The cell value |
||
| 291 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
| 292 | * @return string The XML fragment representing the cell |
||
| 293 | */ |
||
| 294 | 38 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Construct column width references xml to inject into worksheet xml file |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 45 | public function getXMLFragmentForColumnWidths() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Constructs default row height and width xml to inject into worksheet xml file |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | 45 | public function getXMLFragmentForDefaultCellSizing() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritdoc} |
||
| 349 | */ |
||
| 350 | 45 | public function close(Worksheet $worksheet) |
|
| 362 | } |
||
| 363 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..