Complex classes like PhpSpreadsheetWrapper 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 PhpSpreadsheetWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class PhpSpreadsheetWrapper |
||
| 20 | { |
||
| 21 | private $__phpexcel; |
||
| 22 | private $__sheet = []; |
||
| 23 | private $__deleteSheetList = []; |
||
| 24 | private static $__underlineType = [ |
||
| 25 | 'double' => Font::UNDERLINE_DOUBLE, |
||
| 26 | 'doubleaccounting' => Font::UNDERLINE_DOUBLEACCOUNTING, |
||
| 27 | 'none' => Font::UNDERLINE_NONE, |
||
| 28 | 'single' => Font::UNDERLINE_SINGLE, |
||
| 29 | 'singleaccounting' => Font::UNDERLINE_SINGLEACCOUNTING, |
||
| 30 | ]; |
||
| 31 | |||
| 32 | private static $__borderType = [ |
||
| 33 | 'none' => Border::BORDER_NONE, |
||
| 34 | 'thin' => Border::BORDER_THIN, |
||
| 35 | 'medium' => Border::BORDER_MEDIUM, |
||
| 36 | 'dashed' => Border::BORDER_DASHED, |
||
| 37 | 'dotted' => Border::BORDER_DOTTED, |
||
| 38 | 'thick' => Border::BORDER_THICK, |
||
| 39 | 'double' => Border::BORDER_DOUBLE, |
||
| 40 | 'hair' => Border::BORDER_HAIR, |
||
| 41 | 'mediumdashed' => Border::BORDER_MEDIUMDASHED, |
||
| 42 | 'dashdot' => Border::BORDER_DASHDOT, |
||
| 43 | 'mediumdashdot' => Border::BORDER_MEDIUMDASHDOT, |
||
| 44 | 'dashdotdot' => Border::BORDER_DASHDOTDOT, |
||
| 45 | 'mediumdashdotdot' => Border::BORDER_MEDIUMDASHDOTDOT, |
||
| 46 | 'slantdashdot' => Border::BORDER_SLANTDASHDOT, |
||
| 47 | ]; |
||
| 48 | |||
| 49 | private static $__alignHolizonalType = [ |
||
| 50 | 'general' => Alignment::HORIZONTAL_GENERAL, |
||
| 51 | 'center' => Alignment::HORIZONTAL_CENTER, |
||
| 52 | 'left' => Alignment::HORIZONTAL_LEFT, |
||
| 53 | 'right' => Alignment::HORIZONTAL_RIGHT, |
||
| 54 | 'justify' => Alignment::HORIZONTAL_JUSTIFY, |
||
| 55 | 'countinuous' => Alignment::HORIZONTAL_CENTER_CONTINUOUS, |
||
| 56 | ]; |
||
| 57 | |||
| 58 | private static $__alignVerticalType = [ |
||
| 59 | 'bottom' => Alignment::VERTICAL_BOTTOM, |
||
| 60 | 'center' => Alignment::VERTICAL_CENTER, |
||
| 61 | 'justify' => Alignment::VERTICAL_JUSTIFY, |
||
| 62 | 'top' => Alignment::VERTICAL_TOP, |
||
| 63 | ]; |
||
| 64 | |||
| 65 | private static $__fillType = [ |
||
| 66 | 'linear' => Fill::FILL_GRADIENT_LINEAR, |
||
| 67 | 'path' => Fill::FILL_GRADIENT_PATH, |
||
| 68 | 'none' => Fill::FILL_NONE, |
||
| 69 | 'darkdown' => Fill::FILL_PATTERN_DARKDOWN, |
||
| 70 | 'darkgray' => Fill::FILL_PATTERN_DARKGRAY, |
||
| 71 | 'darkgrid' => Fill::FILL_PATTERN_DARKGRID, |
||
| 72 | 'darkhorizontal' => Fill::FILL_PATTERN_DARKHORIZONTAL, |
||
| 73 | 'darktrellis' => Fill::FILL_PATTERN_DARKTRELLIS, |
||
| 74 | 'darkup' => Fill::FILL_PATTERN_DARKUP, |
||
| 75 | 'darkvertical' => Fill::FILL_PATTERN_DARKVERTICAL, |
||
| 76 | 'gray0625' => Fill::FILL_PATTERN_GRAY0625, |
||
| 77 | 'gray125' => Fill::FILL_PATTERN_GRAY125, |
||
| 78 | 'lightdown' => Fill::FILL_PATTERN_LIGHTDOWN, |
||
| 79 | 'lightgray' => Fill::FILL_PATTERN_LIGHTGRAY, |
||
| 80 | 'lightgrid' => Fill::FILL_PATTERN_LIGHTGRID, |
||
| 81 | 'lighthorizontal' => Fill::FILL_PATTERN_LIGHTHORIZONTAL, |
||
| 82 | 'lighttrellis' => Fill::FILL_PATTERN_LIGHTTRELLIS, |
||
| 83 | 'lightup' => Fill::FILL_PATTERN_LIGHTUP, |
||
| 84 | 'lightvertical' => Fill::FILL_PATTERN_LIGHTVERTICAL, |
||
| 85 | 'mediumgray' => Fill::FILL_PATTERN_MEDIUMGRAY, |
||
| 86 | 'solid' => Fill::FILL_SOLID, |
||
| 87 | ]; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * __construct |
||
| 91 | * |
||
| 92 | * @param string $template テンプレートファイルのパス |
||
| 93 | * @author hagiwara |
||
| 94 | */ |
||
| 95 | public function __construct($template = null, $type = 'Xlsx') |
||
| 106 | |||
| 107 | /** |
||
| 108 | * setVal |
||
| 109 | * 値のセット |
||
| 110 | * @param string $value 値 |
||
| 111 | * @param integer $col 行 |
||
| 112 | * @param integer $row 列 |
||
| 113 | * @param integer $sheetNo シート番号 |
||
| 114 | * @param integer $refCol 参照セル行 |
||
| 115 | * @param integer $refRow 参照セル列 |
||
| 116 | * @param integer $refSheet 参照シート |
||
| 117 | * @param string $dataType 書き込みするデータの方を強制的に指定 |
||
| 118 | * @author hagiwara |
||
| 119 | */ |
||
| 120 | public function setVal($value, $col, $row, $sheetNo = 0, $refCol = null, $refRow = null, $refSheet = 0, $dataType = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * getVal |
||
| 138 | * 値の取得 |
||
| 139 | * @param integer $col 行 |
||
| 140 | * @param integer $row 列 |
||
| 141 | * @param integer $sheetNo シート番号 |
||
| 142 | * @author hagiwara |
||
| 143 | */ |
||
| 144 | public function getVal($col, $row, $sheetNo = 0) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * getPlainVal |
||
| 153 | * 値の取得 |
||
| 154 | * @param integer $col 行 |
||
| 155 | * @param integer $row 列 |
||
| 156 | * @param integer $sheetNo シート番号 |
||
| 157 | * @author hagiwara |
||
| 158 | */ |
||
| 159 | public function getPlainVal($col, $row, $sheetNo = 0) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * setImage |
||
| 172 | * 画像のセット |
||
| 173 | * @param string $img 画像のファイルパス |
||
| 174 | * @param integer $col 行 |
||
| 175 | * @param integer $row 列 |
||
| 176 | * @param integer $sheetNo シート番号 |
||
| 177 | * @param integer $height 画像の縦幅 |
||
| 178 | * @param integer $width 画像の横幅 |
||
| 179 | * @param boolean $proportial 縦横比を維持するか |
||
| 180 | * @param integer $offsetx セルから何ピクセルずらすか(X軸) |
||
| 181 | * @param integer $offsety セルから何ピクセルずらすか(Y軸) |
||
| 182 | * @author hagiwara |
||
| 183 | */ |
||
| 184 | public function setImage($img, $col, $row, $sheetNo = 0, $height = null, $width = null, $proportial = false, $offsetx = null, $offsety = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * cellMerge |
||
| 211 | * セルのマージ |
||
| 212 | * @param integer $col1 行 |
||
| 213 | * @param integer $row1 列 |
||
| 214 | * @param integer $col2 行 |
||
| 215 | * @param integer $row2 列 |
||
| 216 | * @param integer $sheetNo シート番号 |
||
| 217 | * @author hagiwara |
||
| 218 | */ |
||
| 219 | public function cellMerge($col1, $row1, $col2, $row2, $sheetNo) |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * styleCopy |
||
| 230 | * セルの書式コピー |
||
| 231 | * @param integer $col 行 |
||
| 232 | * @param integer $row 列 |
||
| 233 | * @param integer $sheetNo シート番号 |
||
| 234 | * @param integer $refCol 参照セル行 |
||
| 235 | * @param integer $refRow 参照セル列 |
||
| 236 | * @param integer $refSheet 参照シート |
||
| 237 | * @author hagiwara |
||
| 238 | */ |
||
| 239 | public function styleCopy($col, $row, $sheetNo, $refCol, $refRow, $refSheet) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * setStyle |
||
| 250 | * 書式のセット(まとめて) |
||
| 251 | * @param integer $col 行 |
||
| 252 | * @param integer $row 列 |
||
| 253 | * @param integer $sheetNo シート番号 |
||
| 254 | * @param array $style スタイル情報 |
||
| 255 | * @author hagiwara |
||
| 256 | */ |
||
| 257 | public function setStyle($col, $row, $sheetNo, $style) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * setFontName |
||
| 289 | * フォントのセット |
||
| 290 | * @param integer $col 行 |
||
| 291 | * @param integer $row 列 |
||
| 292 | * @param integer $sheetNo シート番号 |
||
| 293 | * @param string|null $fontName フォント名 |
||
| 294 | * @author hagiwara |
||
| 295 | */ |
||
| 296 | public function setFontName($col, $row, $sheetNo, $fontName) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * setUnderline |
||
| 306 | * 下線のセット |
||
| 307 | * @param integer $col 行 |
||
| 308 | * @param integer $row 列 |
||
| 309 | * @param integer $sheetNo シート番号 |
||
| 310 | * @param string|null $underline 下線の種類 |
||
| 311 | * @author hagiwara |
||
| 312 | */ |
||
| 313 | public function setUnderline($col, $row, $sheetNo, $underline) |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * getUnderlineType |
||
| 324 | * 下線の種類の設定 |
||
| 325 | * @param string $type |
||
| 326 | * @author hagiwara |
||
| 327 | */ |
||
| 328 | private function getUnderlineType($type) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * setFontBold |
||
| 339 | * 太字のセット |
||
| 340 | * @param integer $col 行 |
||
| 341 | * @param integer $row 列 |
||
| 342 | * @param integer $sheetNo シート番号 |
||
| 343 | * @param boolean|null $bold 太字を引くか |
||
| 344 | * @author hagiwara |
||
| 345 | */ |
||
| 346 | public function setFontBold($col, $row, $sheetNo, $bold) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * setItalic |
||
| 356 | * イタリックのセット |
||
| 357 | * @param integer $col 行 |
||
| 358 | * @param integer $row 列 |
||
| 359 | * @param integer $sheetNo シート番号 |
||
| 360 | * @param boolean|null $italic イタリックにするか |
||
| 361 | * @author hagiwara |
||
| 362 | */ |
||
| 363 | public function setItalic($col, $row, $sheetNo, $italic) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * setStrikethrough |
||
| 373 | * 打ち消し線のセット |
||
| 374 | * @param integer $col 行 |
||
| 375 | * @param integer $row 列 |
||
| 376 | * @param integer $sheetNo シート番号 |
||
| 377 | * @param boolean|null $strikethrough 打ち消し線をつけるか |
||
| 378 | * @author hagiwara |
||
| 379 | */ |
||
| 380 | public function setStrikethrough($col, $row, $sheetNo, $strikethrough) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * setColor |
||
| 394 | * 文字の色 |
||
| 395 | * @param integer $col 行 |
||
| 396 | * @param integer $row 列 |
||
| 397 | * @param integer $sheetNo シート番号 |
||
| 398 | * @param string|null $color 色(ARGB) |
||
| 399 | * @author hagiwara |
||
| 400 | */ |
||
| 401 | public function setColor($col, $row, $sheetNo, $color) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * setSize |
||
| 411 | * 文字サイズ |
||
| 412 | * @param integer $col 行 |
||
| 413 | * @param integer $row 列 |
||
| 414 | * @param integer $sheetNo シート番号 |
||
| 415 | * @param integer|null $size |
||
| 416 | * @author hagiwara |
||
| 417 | */ |
||
| 418 | public function setSize($col, $row, $sheetNo, $size) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * getFont |
||
| 428 | * fontデータ取得 |
||
| 429 | * @param integer $col 行 |
||
| 430 | * @param integer $row 列 |
||
| 431 | * @param integer $sheetNo シート番号 |
||
| 432 | * @author hagiwara |
||
| 433 | */ |
||
| 434 | private function getFont($col, $row, $sheetNo) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * setAlignHolizonal |
||
| 442 | * 水平方向のalign |
||
| 443 | * @param integer $col 行 |
||
| 444 | * @param integer $row 列 |
||
| 445 | * @param integer $sheetNo シート番号 |
||
| 446 | * @param string|null $type |
||
| 447 | * typeはgetAlignHolizonalType参照 |
||
| 448 | * @author hagiwara |
||
| 449 | */ |
||
| 450 | public function setAlignHolizonal($col, $row, $sheetNo, $type) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * setAlignVertical |
||
| 460 | * 垂直方法のalign |
||
| 461 | * @param integer $col 行 |
||
| 462 | * @param integer $row 列 |
||
| 463 | * @param integer $sheetNo シート番号 |
||
| 464 | * @param string|null $type |
||
| 465 | * typeはgetAlignVerticalType参照 |
||
| 466 | * @author hagiwara |
||
| 467 | */ |
||
| 468 | public function setAlignVertical($col, $row, $sheetNo, $type) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * getAlignment |
||
| 478 | * alignmentデータ取得 |
||
| 479 | * @param integer $col 行 |
||
| 480 | * @param integer $row 列 |
||
| 481 | * @param integer $sheetNo シート番号 |
||
| 482 | * @author hagiwara |
||
| 483 | */ |
||
| 484 | private function getAlignment($col, $row, $sheetNo) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * setBorder |
||
| 492 | * 罫線の設定 |
||
| 493 | * @param integer $col 行 |
||
| 494 | * @param integer $row 列 |
||
| 495 | * @param integer $sheetNo シート番号 |
||
| 496 | * @param array|null $border |
||
| 497 | * borderの内部はgetBorderType参照 |
||
| 498 | * @author hagiwara |
||
| 499 | */ |
||
| 500 | public function setBorder($col, $row, $sheetNo, $border) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * setBackgroundColor |
||
| 534 | * 背景色の設定 |
||
| 535 | * @param integer $col 行 |
||
| 536 | * @param integer $row 列 |
||
| 537 | * @param integer $sheetNo シート番号 |
||
| 538 | * @param string $color 色 |
||
| 539 | * @param string $fillType 塗りつぶし方(デフォルトsolid) |
||
| 540 | * fillTypeの内部はgetFillType参照 |
||
| 541 | * @author hagiwara |
||
| 542 | */ |
||
| 543 | public function setBackgroundColor($col, $row, $sheetNo, $color, $fillType = 'solid') |
||
| 549 | |||
| 550 | /** |
||
| 551 | * getBorderType |
||
| 552 | * 罫線の種類の設定 |
||
| 553 | * @param string $type |
||
| 554 | * @author hagiwara |
||
| 555 | */ |
||
| 556 | private function getBorderType($type) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * getAlignHolizonalType |
||
| 567 | * 水平方向のAlignの設定 |
||
| 568 | * @param string $type |
||
| 569 | * @author hagiwara |
||
| 570 | */ |
||
| 571 | private function getAlignHolizonalType($type) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * getAlignVerticalType |
||
| 582 | * 垂直方向のAlignの設定 |
||
| 583 | * @param string $type |
||
| 584 | * @author hagiwara |
||
| 585 | */ |
||
| 586 | private function getAlignVerticalType($type) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * getFillType |
||
| 597 | * 塗りつぶしの設定 |
||
| 598 | * @param string $type |
||
| 599 | * @author hagiwara |
||
| 600 | */ |
||
| 601 | private function getFillType($type) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * createSheet |
||
| 612 | * シートの作成 |
||
| 613 | * @param string $name |
||
| 614 | * @author hagiwara |
||
| 615 | */ |
||
| 616 | public function createSheet($name = null) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * deleteSheet |
||
| 629 | * シートの削除 |
||
| 630 | * @param integer $sheetNo |
||
| 631 | * @author hagiwara |
||
| 632 | */ |
||
| 633 | public function deleteSheet($sheetNo) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * copySheet |
||
| 641 | * シートのコピー |
||
| 642 | * @param integer $sheetNo |
||
| 643 | * @param integer $position |
||
| 644 | * @param string $name |
||
| 645 | * @author hagiwara |
||
| 646 | */ |
||
| 647 | public function copySheet($sheetNo, $position = null, $name = null) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * renameSheet |
||
| 661 | * シート名の変更 |
||
| 662 | * @param integer $sheetNo |
||
| 663 | * @param string $name |
||
| 664 | * @author hagiwara |
||
| 665 | */ |
||
| 666 | public function renameSheet($sheetNo, $name) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * write |
||
| 673 | * xlsxファイルの書き込み |
||
| 674 | * @param string $file 書き込み先のファイルパス |
||
| 675 | * @author hagiwara |
||
| 676 | */ |
||
| 677 | public function write($file, $type = 'Xlsx') |
||
| 686 | |||
| 687 | /** |
||
| 688 | * getReader |
||
| 689 | * readerを返す(※直接Spreadsheetの関数を実行できるように) |
||
| 690 | * @author hagiwara |
||
| 691 | */ |
||
| 692 | public function getReader() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * getSheet |
||
| 699 | * シート情報の読み込み |
||
| 700 | * @param integer $sheetNo シート番号 |
||
| 701 | * @author hagiwara |
||
| 702 | * @return null|\Spreadsheet_Worksheet |
||
| 703 | */ |
||
| 704 | private function getSheet($sheetNo) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * cellInfo |
||
| 714 | * R1C1参照をA1参照に変換して返す |
||
| 715 | * @param integer $col 行 |
||
| 716 | * @param integer $row 列 |
||
| 717 | * @author hagiwara |
||
| 718 | */ |
||
| 719 | private function cellInfo($col, $row) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * cellInfo |
||
| 727 | * http://qiita.com/Hiraku/items/036080976884fad1e450 |
||
| 728 | * @param string $str |
||
| 729 | */ |
||
| 730 | private function camelize($str) |
||
| 735 | } |
||
| 736 |