Complex classes like PhpExcelWrapper 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 PhpExcelWrapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class PhpExcelWrapper |
||
19 | { |
||
20 | private $__phpexcel; |
||
21 | private $__sheet = []; |
||
22 | private $__deleteSheetList = []; |
||
23 | private static $__underlineType = [ |
||
24 | 'double' => PHPExcel_Style_Font::UNDERLINE_DOUBLE, |
||
25 | 'doubleaccounting' => PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING, |
||
26 | 'none' => PHPExcel_Style_Font::UNDERLINE_NONE, |
||
27 | 'single' => PHPExcel_Style_Font::UNDERLINE_SINGLE, |
||
28 | 'singleaccounting' => PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING, |
||
29 | ]; |
||
30 | |||
31 | private static $__borderType = [ |
||
32 | 'none' => PHPExcel_Style_Border::BORDER_NONE, |
||
33 | 'thin' => PHPExcel_Style_Border::BORDER_THIN, |
||
34 | 'medium' => PHPExcel_Style_Border::BORDER_MEDIUM, |
||
35 | 'dashed' => PHPExcel_Style_Border::BORDER_DASHED, |
||
36 | 'dotted' => PHPExcel_Style_Border::BORDER_DOTTED, |
||
37 | 'thick' => PHPExcel_Style_Border::BORDER_THICK, |
||
38 | 'double' => PHPExcel_Style_Border::BORDER_DOUBLE, |
||
39 | 'hair' => PHPExcel_Style_Border::BORDER_HAIR, |
||
40 | 'mediumdashed' => PHPExcel_Style_Border::BORDER_MEDIUMDASHED, |
||
41 | 'dashdot' => PHPExcel_Style_Border::BORDER_DASHDOT, |
||
42 | 'mediumdashdot' => PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT, |
||
43 | 'dashdotdot' => PHPExcel_Style_Border::BORDER_DASHDOTDOT, |
||
44 | 'mediumdashdotdot' => PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT, |
||
45 | 'slantdashdot' => PHPExcel_Style_Border::BORDER_SLANTDASHDOT, |
||
46 | ]; |
||
47 | |||
48 | private static $__alignHolizonalType = [ |
||
49 | 'general' => PHPExcel_Style_Alignment::HORIZONTAL_GENERAL, |
||
50 | 'center' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, |
||
51 | 'left' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT, |
||
52 | 'right' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, |
||
53 | 'justify' => PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY, |
||
54 | 'countinuous' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS, |
||
55 | ]; |
||
56 | |||
57 | private static $__alignVerticalType = [ |
||
58 | 'bottom' => PHPExcel_Style_Alignment::VERTICAL_BOTTOM, |
||
59 | 'center' => PHPExcel_Style_Alignment::VERTICAL_CENTER, |
||
60 | 'justify' => PHPExcel_Style_Alignment::VERTICAL_JUSTIFY, |
||
61 | 'top' => PHPExcel_Style_Alignment::VERTICAL_TOP, |
||
62 | ]; |
||
63 | |||
64 | private static $__fillType = [ |
||
65 | 'linear' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR, |
||
66 | 'path' => PHPExcel_Style_Fill::FILL_GRADIENT_PATH, |
||
67 | 'none' => PHPExcel_Style_Fill::FILL_NONE, |
||
68 | 'darkdown' => PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN, |
||
69 | 'darkgray' => PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY, |
||
70 | 'darkgrid' => PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID, |
||
71 | 'darkhorizontal' => PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL, |
||
72 | 'darktrellis' => PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS, |
||
73 | 'darkup' => PHPExcel_Style_Fill::FILL_PATTERN_DARKUP, |
||
74 | 'darkvertical' => PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL, |
||
75 | 'gray0625' => PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625, |
||
76 | 'gray125' => PHPExcel_Style_Fill::FILL_PATTERN_GRAY125, |
||
77 | 'lightdown' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN, |
||
78 | 'lightgray' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY, |
||
79 | 'lightgrid' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID, |
||
80 | 'lighthorizontal' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL, |
||
81 | 'lighttrellis' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS, |
||
82 | 'lightup' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP, |
||
83 | 'lightvertical' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL, |
||
84 | 'mediumgray' => PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY, |
||
85 | 'solid' => PHPExcel_Style_Fill::FILL_SOLID, |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * __construct |
||
90 | * |
||
91 | * @param string $template テンプレートファイルのパス |
||
92 | * @author hagiwara |
||
93 | */ |
||
94 | public function __construct($template = null, $type = 'Excel2007') |
||
105 | |||
106 | /** |
||
107 | * setVal |
||
108 | * 値のセット |
||
109 | * @param string $value 値 |
||
110 | * @param integer $col 行 |
||
111 | * @param integer $row 列 |
||
112 | * @param integer $sheetNo シート番号 |
||
113 | * @param integer $refCol 参照セル行 |
||
114 | * @param integer $refRow 参照セル列 |
||
115 | * @param integer $refSheet 参照シート |
||
116 | * @author hagiwara |
||
117 | */ |
||
118 | public function setVal($value, $col, $row, $sheetNo = 0, $refCol = null, $refRow = null, $refSheet = 0) |
||
129 | |||
130 | /** |
||
131 | * getVal |
||
132 | * 値の取得 |
||
133 | * @param integer $col 行 |
||
134 | * @param integer $row 列 |
||
135 | * @param integer $sheetNo シート番号 |
||
136 | * @author hagiwara |
||
137 | */ |
||
138 | public function getVal($col, $row, $sheetNo = 0) |
||
144 | |||
145 | /** |
||
146 | * setImage |
||
147 | * 画像のセット |
||
148 | * @param string $img 画像のファイルパス |
||
149 | * @param integer $col 行 |
||
150 | * @param integer $row 列 |
||
151 | * @param integer $sheetNo シート番号 |
||
152 | * @param integer $height 画像の縦幅 |
||
153 | * @param integer $width 画像の横幅 |
||
154 | * @param boolean $proportial 縦横比を維持するか |
||
155 | * @param integer $offsetx セルから何ピクセルずらすか(X軸) |
||
156 | * @param integer $offsety セルから何ピクセルずらすか(Y軸) |
||
157 | * @author hagiwara |
||
158 | */ |
||
159 | public function setImage($img, $col, $row, $sheetNo = 0, $height = null, $width = null, $proportial = false, $offsetx = null, $offsety = null) |
||
183 | |||
184 | /** |
||
185 | * cellMerge |
||
186 | * セルのマージ |
||
187 | * @param integer $col1 行 |
||
188 | * @param integer $row1 列 |
||
189 | * @param integer $col2 行 |
||
190 | * @param integer $row2 列 |
||
191 | * @param integer $sheetNo シート番号 |
||
192 | * @author hagiwara |
||
193 | */ |
||
194 | public function cellMerge($col1, $row1, $col2, $row2, $sheetNo) |
||
201 | |||
202 | |||
203 | /** |
||
204 | * styleCopy |
||
205 | * セルの書式コピー |
||
206 | * @param integer $col 行 |
||
207 | * @param integer $row 列 |
||
208 | * @param integer $sheetNo シート番号 |
||
209 | * @param integer $refCol 参照セル行 |
||
210 | * @param integer $refRow 参照セル列 |
||
211 | * @param integer $refSheet 参照シート |
||
212 | * @author hagiwara |
||
213 | */ |
||
214 | public function styleCopy($col, $row, $sheetNo, $refCol, $refRow, $refSheet) |
||
222 | |||
223 | /** |
||
224 | * setStyle |
||
225 | * 書式のセット(まとめて) |
||
226 | * @param integer $col 行 |
||
227 | * @param integer $row 列 |
||
228 | * @param integer $sheetNo シート番号 |
||
229 | * @param array $style スタイル情報 |
||
230 | * @author hagiwara |
||
231 | */ |
||
232 | public function setStyle($col, $row, $sheetNo, $style) |
||
261 | |||
262 | /** |
||
263 | * setFontName |
||
264 | * フォントのセット |
||
265 | * @param integer $col 行 |
||
266 | * @param integer $row 列 |
||
267 | * @param integer $sheetNo シート番号 |
||
268 | * @param string|null $fontName フォント名 |
||
269 | * @author hagiwara |
||
270 | */ |
||
271 | public function setFontName($col, $row, $sheetNo, $fontName) |
||
278 | |||
279 | /** |
||
280 | * setUnderline |
||
281 | * 下線のセット |
||
282 | * @param integer $col 行 |
||
283 | * @param integer $row 列 |
||
284 | * @param integer $sheetNo シート番号 |
||
285 | * @param string|null $underline 下線の種類 |
||
286 | * @author hagiwara |
||
287 | */ |
||
288 | public function setUnderline($col, $row, $sheetNo, $underline) |
||
295 | |||
296 | |||
297 | /** |
||
298 | * getUnderlineType |
||
299 | * 下線の種類の設定 |
||
300 | * @param string $type |
||
301 | * @author hagiwara |
||
302 | */ |
||
303 | private function getUnderlineType($type) |
||
311 | |||
312 | /** |
||
313 | * setFontBold |
||
314 | * 太字のセット |
||
315 | * @param integer $col 行 |
||
316 | * @param integer $row 列 |
||
317 | * @param integer $sheetNo シート番号 |
||
318 | * @param boolean|null $bold 太字を引くか |
||
319 | * @author hagiwara |
||
320 | */ |
||
321 | public function setFontBold($col, $row, $sheetNo, $bold) |
||
328 | |||
329 | /** |
||
330 | * setItalic |
||
331 | * イタリックのセット |
||
332 | * @param integer $col 行 |
||
333 | * @param integer $row 列 |
||
334 | * @param integer $sheetNo シート番号 |
||
335 | * @param boolean|null $italic イタリックにするか |
||
336 | * @author hagiwara |
||
337 | */ |
||
338 | public function setItalic($col, $row, $sheetNo, $italic) |
||
345 | |||
346 | /** |
||
347 | * setStrikethrough |
||
348 | * 打ち消し線のセット |
||
349 | * @param integer $col 行 |
||
350 | * @param integer $row 列 |
||
351 | * @param integer $sheetNo シート番号 |
||
352 | * @param boolean|null $strikethrough 打ち消し線をつけるか |
||
353 | * @author hagiwara |
||
354 | */ |
||
355 | public function setStrikethrough($col, $row, $sheetNo, $strikethrough) |
||
362 | |||
363 | /** |
||
364 | * setColor |
||
365 | * 文字の色 |
||
366 | * @param integer $col 行 |
||
367 | * @param integer $row 列 |
||
368 | * @param integer $sheetNo シート番号 |
||
369 | * @param string|null $color 色(ARGB) |
||
370 | * @author hagiwara |
||
371 | */ |
||
372 | public function setColor($col, $row, $sheetNo, $color) |
||
379 | |||
380 | /** |
||
381 | * setSize |
||
382 | * 文字サイズ |
||
383 | * @param integer $col 行 |
||
384 | * @param integer $row 列 |
||
385 | * @param integer $sheetNo シート番号 |
||
386 | * @param integer|null $size |
||
387 | * @author hagiwara |
||
388 | */ |
||
389 | public function setSize($col, $row, $sheetNo, $size) |
||
396 | |||
397 | /** |
||
398 | * getFont |
||
399 | * fontデータ取得 |
||
400 | * @param integer $col 行 |
||
401 | * @param integer $row 列 |
||
402 | * @param integer $sheetNo シート番号 |
||
403 | * @author hagiwara |
||
404 | */ |
||
405 | private function getFont($col, $row, $sheetNo) |
||
410 | |||
411 | /** |
||
412 | * setAlignHolizonal |
||
413 | * 水平方向のalign |
||
414 | * @param integer $col 行 |
||
415 | * @param integer $row 列 |
||
416 | * @param integer $sheetNo シート番号 |
||
417 | * @param string|null $type |
||
418 | * typeはgetAlignHolizonalType参照 |
||
419 | * @author hagiwara |
||
420 | */ |
||
421 | public function setAlignHolizonal($col, $row, $sheetNo, $type) |
||
428 | |||
429 | /** |
||
430 | * setAlignVertical |
||
431 | * 垂直方法のalign |
||
432 | * @param integer $col 行 |
||
433 | * @param integer $row 列 |
||
434 | * @param integer $sheetNo シート番号 |
||
435 | * @param string|null $type |
||
436 | * typeはgetAlignVerticalType参照 |
||
437 | * @author hagiwara |
||
438 | */ |
||
439 | public function setAlignVertical($col, $row, $sheetNo, $type) |
||
446 | |||
447 | /** |
||
448 | * getAlignment |
||
449 | * alignmentデータ取得 |
||
450 | * @param integer $col 行 |
||
451 | * @param integer $row 列 |
||
452 | * @param integer $sheetNo シート番号 |
||
453 | * @author hagiwara |
||
454 | */ |
||
455 | private function getAlignment($col, $row, $sheetNo) |
||
460 | |||
461 | /** |
||
462 | * setBorder |
||
463 | * 罫線の設定 |
||
464 | * @param integer $col 行 |
||
465 | * @param integer $row 列 |
||
466 | * @param integer $sheetNo シート番号 |
||
467 | * @param array|null $border |
||
468 | * borderの内部はgetBorderType参照 |
||
469 | * @author hagiwara |
||
470 | */ |
||
471 | public function setBorder($col, $row, $sheetNo, $border) |
||
502 | |||
503 | /** |
||
504 | * setBackgroundColor |
||
505 | * 背景色の設定 |
||
506 | * @param integer $col 行 |
||
507 | * @param integer $row 列 |
||
508 | * @param integer $sheetNo シート番号 |
||
509 | * @param string $color 色 |
||
510 | * @param string $fillType 塗りつぶし方(デフォルトsolid) |
||
511 | * fillTypeの内部はgetFillType参照 |
||
512 | * @author hagiwara |
||
513 | */ |
||
514 | public function setBackgroundColor($col, $row, $sheetNo, $color, $fillType = 'solid') |
||
520 | |||
521 | /** |
||
522 | * getBorderType |
||
523 | * 罫線の種類の設定 |
||
524 | * @param string $type |
||
525 | * @author hagiwara |
||
526 | */ |
||
527 | private function getBorderType($type) |
||
535 | |||
536 | /** |
||
537 | * getAlignHolizonalType |
||
538 | * 水平方向のAlignの設定 |
||
539 | * @param string $type |
||
540 | * @author hagiwara |
||
541 | */ |
||
542 | private function getAlignHolizonalType($type) |
||
550 | |||
551 | /** |
||
552 | * getAlignVerticalType |
||
553 | * 垂直方向のAlignの設定 |
||
554 | * @param string $type |
||
555 | * @author hagiwara |
||
556 | */ |
||
557 | private function getAlignVerticalType($type) |
||
565 | |||
566 | /** |
||
567 | * getFillType |
||
568 | * 塗りつぶしの設定 |
||
569 | * @param string $type |
||
570 | * @author hagiwara |
||
571 | */ |
||
572 | private function getFillType($type) |
||
580 | |||
581 | /** |
||
582 | * createSheet |
||
583 | * シートの作成 |
||
584 | * @param string $name |
||
585 | * @author hagiwara |
||
586 | */ |
||
587 | public function createSheet($name = null) |
||
597 | |||
598 | /** |
||
599 | * deleteSheet |
||
600 | * シートの削除 |
||
601 | * @param integer $sheetNo |
||
602 | * @author hagiwara |
||
603 | */ |
||
604 | public function deleteSheet($sheetNo) |
||
609 | |||
610 | /** |
||
611 | * copySheet |
||
612 | * シートのコピー |
||
613 | * @param integer $sheetNo |
||
614 | * @param integer $position |
||
615 | * @param string $name |
||
616 | * @author hagiwara |
||
617 | */ |
||
618 | public function copySheet($sheetNo, $position = null, $name = null) |
||
629 | |||
630 | /** |
||
631 | * renameSheet |
||
632 | * シート名の変更 |
||
633 | * @param integer $sheetNo |
||
634 | * @param string $name |
||
635 | * @author hagiwara |
||
636 | */ |
||
637 | public function renameSheet($sheetNo, $name) |
||
641 | |||
642 | /** |
||
643 | * write |
||
644 | * xlsxファイルの書き込み |
||
645 | * @param string $file 書き込み先のファイルパス |
||
646 | * @author hagiwara |
||
647 | */ |
||
648 | public function write($file, $type = 'Excel2007') |
||
657 | |||
658 | /** |
||
659 | * getReader |
||
660 | * readerを返す(※直接PHPExcelの関数を実行できるように) |
||
661 | * @author hagiwara |
||
662 | */ |
||
663 | public function getReader() |
||
667 | |||
668 | /** |
||
669 | * getSheet |
||
670 | * シート情報の読み込み |
||
671 | * @param integer $sheetNo シート番号 |
||
672 | * @author hagiwara |
||
673 | * @return null|\PHPExcel_Worksheet |
||
674 | */ |
||
675 | private function getSheet($sheetNo) |
||
682 | |||
683 | /** |
||
684 | * cellInfo |
||
685 | * R1C1参照をA1参照に変換して返す |
||
686 | * @param integer $col 行 |
||
687 | * @param integer $row 列 |
||
688 | * @author hagiwara |
||
689 | */ |
||
690 | private function cellInfo($col, $row) |
||
695 | |||
696 | /** |
||
697 | * cellInfo |
||
698 | * http://qiita.com/Hiraku/items/036080976884fad1e450 |
||
699 | * @param string $str |
||
700 | */ |
||
701 | private function camelize($str) |
||
706 | } |
||
707 |