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 |
||
| 14 | { |
||
| 15 | /** Paths of XML files relative to the XLSX file root */ |
||
| 16 | const STYLES_XML_FILE_PATH = 'xl/styles.xml'; |
||
| 17 | |||
| 18 | /** Nodes used to find relevant information in the styles XML file */ |
||
| 19 | const XML_NODE_NUM_FMTS = 'numFmts'; |
||
| 20 | const XML_NODE_NUM_FMT = 'numFmt'; |
||
| 21 | const XML_NODE_CELL_XFS = 'cellXfs'; |
||
| 22 | const XML_NODE_XF = 'xf'; |
||
| 23 | |||
| 24 | /** Attributes used to find relevant information in the styles XML file */ |
||
| 25 | const XML_ATTRIBUTE_NUM_FMT_ID = 'numFmtId'; |
||
| 26 | const XML_ATTRIBUTE_FORMAT_CODE = 'formatCode'; |
||
| 27 | const XML_ATTRIBUTE_APPLY_NUMBER_FORMAT = 'applyNumberFormat'; |
||
| 28 | |||
| 29 | /** By convention, default style ID is 0 */ |
||
| 30 | const DEFAULT_STYLE_ID = 0; |
||
| 31 | |||
| 32 | const NUMBER_FORMAT_GENERAL = 'General'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @see https://msdn.microsoft.com/en-us/library/ff529597(v=office.12).aspx |
||
| 36 | * @var array Mapping between built-in numFmtId and the associated format - for dates only |
||
| 37 | */ |
||
| 38 | protected static $builtinNumFmtIdToNumFormatMapping = [ |
||
| 39 | 14 => 'm/d/yyyy', // @NOTE: ECMA spec is 'mm-dd-yy' |
||
| 40 | 15 => 'd-mmm-yy', |
||
| 41 | 16 => 'd-mmm', |
||
| 42 | 17 => 'mmm-yy', |
||
| 43 | 18 => 'h:mm AM/PM', |
||
| 44 | 19 => 'h:mm:ss AM/PM', |
||
| 45 | 20 => 'h:mm', |
||
| 46 | 21 => 'h:mm:ss', |
||
| 47 | 22 => 'm/d/yyyy h:mm', // @NOTE: ECMA spec is 'm/d/yy h:mm', |
||
| 48 | 45 => 'mm:ss', |
||
| 49 | 46 => '[h]:mm:ss', |
||
| 50 | 47 => 'mm:ss.0', // @NOTE: ECMA spec is 'mmss.0', |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** @var string Path of the XLSX file being read */ |
||
| 54 | protected $filePath; |
||
| 55 | |||
| 56 | /** @var EntityFactory Factory to create entities */ |
||
| 57 | protected $entityFactory; |
||
| 58 | |||
| 59 | /** @var array Array containing the IDs of built-in number formats indicating a date */ |
||
| 60 | protected $builtinNumFmtIdIndicatingDates; |
||
| 61 | |||
| 62 | /** @var array Array containing a mapping NUM_FMT_ID => FORMAT_CODE */ |
||
| 63 | protected $customNumberFormats; |
||
| 64 | |||
| 65 | /** @var array Array containing a mapping STYLE_ID => [STYLE_ATTRIBUTES] */ |
||
| 66 | protected $stylesAttributes; |
||
| 67 | |||
| 68 | /** @var array Cache containing a mapping NUM_FMT_ID => IS_DATE_FORMAT. Used to avoid lots of recalculations */ |
||
| 69 | protected $numFmtIdToIsDateFormatCache = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $filePath Path of the XLSX file being read |
||
| 73 | * @param EntityFactory $entityFactory Factory to create entities |
||
| 74 | */ |
||
| 75 | 66 | public function __construct($filePath, $entityFactory) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Returns whether the style with the given ID should consider |
||
| 84 | * numeric values as timestamps and format the cell as a date. |
||
| 85 | * |
||
| 86 | * @param int $styleId Zero-based style ID |
||
| 87 | * @return bool Whether the cell with the given cell should display a date instead of a numeric value |
||
| 88 | */ |
||
| 89 | 43 | public function shouldFormatNumericValueAsDate($styleId) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Reads the styles.xml file and extract the relevant information from the file. |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | 10 | protected function extractRelevantInfo() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Extracts number formats from the "numFmt" nodes. |
||
| 133 | * For simplicity, the styles attributes are kept in memory. This is possible thanks |
||
| 134 | * to the reuse of formats. So 1 million cells should not use 1 million formats. |
||
| 135 | * |
||
| 136 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XML Reader positioned on the "numFmts" node |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | 7 | protected function extractNumberFormats($xmlReader) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Extracts style attributes from the "xf" nodes, inside the "cellXfs" section. |
||
| 155 | * For simplicity, the styles attributes are kept in memory. This is possible thanks |
||
| 156 | * to the reuse of styles. So 1 million cells should not use 1 million styles. |
||
| 157 | * |
||
| 158 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XML Reader positioned on the "cellXfs" node |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | 10 | protected function extractStyleAttributes($xmlReader) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return array The custom number formats |
||
| 184 | */ |
||
| 185 | 5 | protected function getCustomNumberFormats() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @return array The styles attributes |
||
| 196 | */ |
||
| 197 | 10 | protected function getStylesAttributes() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param array $styleAttributes Array containing the style attributes (2 keys: "applyNumberFormat" and "numFmtId") |
||
| 208 | * @return bool Whether the style with the given attributes indicates that the number is a date |
||
| 209 | */ |
||
| 210 | 37 | protected function doesStyleIndicateDate($styleAttributes) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Returns whether the number format ID indicates that the number is a date. |
||
| 229 | * The result is cached to avoid recomputing the same thing over and over, as |
||
| 230 | * "numFmtId" attributes can be shared between multiple styles. |
||
| 231 | * |
||
| 232 | * @param int $numFmtId |
||
| 233 | * @return bool Whether the number format ID indicates that the number is a date |
||
| 234 | */ |
||
| 235 | 35 | protected function doesNumFmtIdIndicateDate($numFmtId) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param int $numFmtId |
||
| 251 | * @return string|null The custom number format or NULL if none defined for the given numFmtId |
||
| 252 | */ |
||
| 253 | 35 | protected function getFormatCodeForNumFmtId($numFmtId) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param int $numFmtId |
||
| 263 | * @return bool Whether the number format ID indicates that the number is a date |
||
| 264 | */ |
||
| 265 | 35 | protected function isNumFmtIdBuiltInDateFormat($numFmtId) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param string|null $formatCode |
||
| 272 | * @return bool Whether the given format code indicates that the number is a date |
||
| 273 | */ |
||
| 274 | 33 | protected function isFormatCodeCustomDateFormat($formatCode) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $formatCode |
||
| 286 | * @return bool Whether the given format code matches a date format pattern |
||
| 287 | */ |
||
| 288 | 27 | protected function isFormatCodeMatchingDateFormatPattern($formatCode) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Returns the format as defined in "styles.xml" of the given style. |
||
| 315 | * NOTE: It is assumed that the style DOES have a number format associated to it. |
||
| 316 | * |
||
| 317 | * @param int $styleId Zero-based style ID |
||
| 318 | * @return string The number format code associated with the given style |
||
| 319 | */ |
||
| 320 | 2 | public function getNumberFormatCode($styleId) |
|
| 335 | } |
||
| 336 |