Complex classes like LibXLWriter 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 LibXLWriter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class LibXLWriter extends AbstractSendableWriter |
||
28 | { |
||
29 | /** |
||
30 | * Cache for currency formats. |
||
31 | * |
||
32 | * @var ArrayObject |
||
33 | */ |
||
34 | protected $currency_formats; |
||
35 | |||
36 | /** |
||
37 | * Cache for unit formats. |
||
38 | * |
||
39 | * @var ArrayObject |
||
40 | */ |
||
41 | protected $unit_formats; |
||
42 | |||
43 | /** |
||
44 | * @var SimpleHeaders |
||
45 | */ |
||
46 | protected $headers; |
||
47 | protected $column_width_multiplier = 1.7; |
||
48 | protected $column_max_width = 75; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected static $default_license; |
||
54 | |||
55 | /** |
||
56 | * @var ExcelBook |
||
57 | */ |
||
58 | protected $excelBook; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $file_format = LibXL::FILE_FORMAT_XLSX; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $currencyMap = [ |
||
69 | 'EUR' => '€', |
||
70 | 'GBP' => '£', |
||
71 | 'CNY' => 'CN¥', |
||
72 | 'USD' => '$', |
||
73 | 'CAD' => 'CA$' |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $typeMap = [ |
||
80 | ColumnType::TYPE_BIT => 'number', |
||
81 | ColumnType::TYPE_BLOB => 'text', |
||
82 | ColumnType::TYPE_BOOLEAN => 'number', |
||
83 | ColumnType::TYPE_DATE => 'date', |
||
84 | ColumnType::TYPE_DATETIME => 'datetime', |
||
85 | ColumnType::TYPE_DECIMAL => 'number', |
||
86 | ColumnType::TYPE_INTEGER => 'number', |
||
87 | ColumnType::TYPE_STRING => 'text', |
||
88 | ColumnType::TYPE_TIME => 'text', |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * @param StoreInterface|null $store |
||
93 | * @param array|\Traversable|null $options |
||
94 | */ |
||
95 | public function __construct(StoreInterface $store = null, $options = null) |
||
101 | |||
102 | /** |
||
103 | * Set file format (xls, xlsx), default is xlsx. |
||
104 | * |
||
105 | * @param string $file_format |
||
106 | * |
||
107 | * @throws Exception\InvalidArgumentException |
||
108 | */ |
||
109 | public function setFormat($file_format): self |
||
118 | |||
119 | /** |
||
120 | * @param ExcelBook $book |
||
121 | * @param string $currency |
||
122 | * @param int $decimals |
||
123 | * |
||
124 | * @return ExcelFormat |
||
125 | */ |
||
126 | protected function getCurrencyFormat(ExcelBook $book, $currency, $decimals) |
||
155 | |||
156 | /** |
||
157 | * @param ExcelBook $book |
||
158 | * @param string $unit |
||
159 | * @param int $decimals |
||
160 | * |
||
161 | * @return ExcelFormat |
||
162 | */ |
||
163 | protected function getUnitFormat(ExcelBook $book, $unit, $decimals) |
||
185 | |||
186 | /** |
||
187 | * @throws ExtensionNotLoadedException |
||
188 | * @throws Exception\InvalidArgumentException |
||
189 | * |
||
190 | * @param string $file_format |
||
191 | * @param string $locale default to en_US.UTF-8 |
||
192 | * |
||
193 | * @return ExcelBook |
||
194 | */ |
||
195 | public function getExcelBook($file_format = null, $locale = 'en_US.UTF-8') |
||
217 | |||
218 | /** |
||
219 | * @param Options $options |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getData(Options $options = null) |
||
263 | |||
264 | /** |
||
265 | * @return ArrayObject |
||
266 | */ |
||
267 | protected function getMetadataSpecs(ExcelBook $book) |
||
268 | { |
||
269 | $hide_thousands_separator = true; |
||
270 | |||
271 | $specs = new ArrayObject(); |
||
272 | $cm = $this->store->getColumnModel(); |
||
273 | $metadata = $cm->getMetadata(); |
||
274 | |||
275 | $columns = $cm->getColumns(); |
||
276 | foreach ($columns as $name => $column) { |
||
277 | $decimals = null; |
||
278 | $format = null; |
||
279 | $custom_column = null; |
||
280 | $formatter = $column->getFormatter(); |
||
281 | |||
282 | if ($formatter instanceof \Soluble\FlexStore\Formatter\FormatterNumberInterface) { |
||
283 | $type = 'number'; |
||
284 | $decimals = $formatter->getDecimals(); |
||
285 | |||
286 | if ($formatter instanceof \Soluble\FlexStore\Formatter\CurrencyFormatter) { |
||
287 | $currency = $formatter->getCurrencyCode(); |
||
288 | if ($currency instanceof \Soluble\FlexStore\Formatter\RowColumn) { |
||
289 | // TODO better handling of callbacks |
||
290 | $format = function (ExcelBook $book, $currency, $decimals) { |
||
291 | return $this->getCurrencyFormat($book, $currency, $decimals); |
||
292 | }; |
||
293 | $custom_column = $currency->getColumnName(); |
||
294 | } else { |
||
295 | $format = $this->getCurrencyFormat($book, $currency, $decimals); |
||
296 | } |
||
297 | } elseif ($formatter instanceof \Soluble\FlexStore\Formatter\UnitFormatter) { |
||
298 | $unit = $formatter->getUnit(); |
||
299 | if ($unit instanceof \Soluble\FlexStore\Formatter\RowColumn) { |
||
300 | // TODO better handling of callbacks |
||
301 | $format = function (ExcelBook $book, $unit, $decimals) { |
||
302 | return $this->getUnitFormat($book, $unit, $decimals); |
||
303 | }; |
||
304 | $custom_column = $unit->getColumnName(); |
||
305 | } else { |
||
306 | $format = $this->getUnitFormat($book, $unit, $decimals); |
||
307 | } |
||
308 | } |
||
309 | } else { |
||
310 | $model_type = $column->getType()->getName(); |
||
311 | //$spec['meta_type'] = $model_type; |
||
312 | if ($model_type == ColumnType::TYPE_INTEGER) { |
||
313 | $decimals = 0; |
||
314 | } |
||
315 | if (array_key_exists($model_type, $this->typeMap)) { |
||
316 | $type = $this->typeMap[$model_type]; |
||
317 | } else { |
||
318 | $type = 'text'; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | // We now have the type |
||
323 | if ($type === 'number' && $decimals === null && $metadata !== null && $metadata->offsetExists($name)) { |
||
324 | // try to guess from metadata |
||
325 | $decimals = $metadata->offsetGet($name)->getNumericPrecision(); |
||
326 | if (!$decimals) { |
||
327 | $decimals = 0; |
||
328 | } |
||
329 | } |
||
330 | |||
331 | // Let's make the format |
||
332 | |||
333 | if ($format === null) { |
||
334 | switch ($type) { |
||
335 | case 'date': |
||
336 | $mask = 'd/mm/yyyy'; |
||
337 | $cfid = $book->addCustomFormat($mask); |
||
338 | $format = $book->addFormat(); |
||
339 | $format->numberFormat($cfid); |
||
340 | break; |
||
341 | case 'datetime': |
||
342 | $mask = 'd/mm/yyyy h:mm'; |
||
343 | $cfid = $book->addCustomFormat($mask); |
||
344 | $format = $book->addFormat(); |
||
345 | $format->numberFormat($cfid); |
||
346 | break; |
||
347 | case 'number': |
||
348 | if ($hide_thousands_separator) { |
||
349 | $formatString = '0'; |
||
350 | } else { |
||
351 | $formatString = '#,##0'; |
||
352 | } |
||
353 | if ($decimals > 0) { |
||
354 | $zeros = str_repeat('0', $decimals); |
||
355 | $formatString = $formatString . '.' . $zeros; |
||
356 | } |
||
357 | $cfid = $book->addCustomFormat($formatString); |
||
358 | $format = $book->addFormat(); |
||
359 | $format->numberFormat($cfid); |
||
360 | |||
361 | break; |
||
362 | default: |
||
363 | $format = null; |
||
364 | } |
||
365 | } |
||
366 | |||
367 | if ($format === null) { |
||
368 | $format = $this->getDefaultTextFormat($book); |
||
369 | } else { |
||
370 | /* Not yet supported, waiting php_excel 1.1 |
||
371 | $format->horizontalAlign($this->getFormatStyle('horizontalAlign')); |
||
372 | $format->verticalAlign($this->getFormatStyle('verticalAlign')); |
||
373 | |||
374 | */ |
||
375 | } |
||
376 | |||
377 | // Save the spec |
||
378 | $spec = new ArrayObject(); |
||
379 | $spec['name'] = $name; |
||
380 | $spec['header'] = $column->getHeader(); |
||
381 | $spec['type'] = $type; |
||
382 | $spec['decimals'] = $decimals; |
||
383 | $spec['format'] = $format; |
||
384 | $spec['custom_column'] = $custom_column; |
||
385 | $specs->offsetSet($name, $spec); |
||
386 | } |
||
387 | |||
388 | //var_dump((array) $specs); |
||
389 | return $specs; |
||
390 | } |
||
391 | |||
392 | protected function getDefaultTextFormat(ExcelBook $book) |
||
402 | |||
403 | protected function getHeaderFormat(ExcelBook $book) |
||
424 | |||
425 | /** |
||
426 | * @param ExcelBook $book |
||
427 | * @param Options $options |
||
428 | * |
||
429 | * @return ExcelBook |
||
430 | */ |
||
431 | protected function generateExcel(ExcelBook $book, Options $options = null) |
||
512 | |||
513 | /** |
||
514 | * @param string $license_name |
||
515 | * @param string $license_key |
||
516 | */ |
||
517 | public static function setDefaultLicense($license_name, $license_key) |
||
521 | |||
522 | public function getFormatStyle($style) |
||
528 | |||
529 | public function getFormatStyles() |
||
538 | |||
539 | /** |
||
540 | * Return default headers for sending store data via http. |
||
541 | * |
||
542 | * @return SimpleHeaders |
||
543 | */ |
||
544 | public function getHttpHeaders() |
||
554 | } |
||
555 |