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 | * @author hagiwara |
||
| 118 | */ |
||
| 119 | public function setVal($value, $col, $row, $sheetNo = 0, $refCol = null, $refRow = null, $refSheet = 0) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * getVal |
||
| 133 | * 値の取得 |
||
| 134 | * @param integer $col 行 |
||
| 135 | * @param integer $row 列 |
||
| 136 | * @param integer $sheetNo シート番号 |
||
| 137 | * @author hagiwara |
||
| 138 | */ |
||
| 139 | public function getVal($col, $row, $sheetNo = 0) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * getPlainVal |
||
| 148 | * 値の取得 |
||
| 149 | * @param integer $col 行 |
||
| 150 | * @param integer $row 列 |
||
| 151 | * @param integer $sheetNo シート番号 |
||
| 152 | * @author hagiwara |
||
| 153 | */ |
||
| 154 | public function getPlainVal($col, $row, $sheetNo = 0) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * setImage |
||
| 167 | * 画像のセット |
||
| 168 | * @param string $img 画像のファイルパス |
||
| 169 | * @param integer $col 行 |
||
| 170 | * @param integer $row 列 |
||
| 171 | * @param integer $sheetNo シート番号 |
||
| 172 | * @param integer $height 画像の縦幅 |
||
| 173 | * @param integer $width 画像の横幅 |
||
| 174 | * @param boolean $proportial 縦横比を維持するか |
||
| 175 | * @param integer $offsetx セルから何ピクセルずらすか(X軸) |
||
| 176 | * @param integer $offsety セルから何ピクセルずらすか(Y軸) |
||
| 177 | * @author hagiwara |
||
| 178 | */ |
||
| 179 | public function setImage($img, $col, $row, $sheetNo = 0, $height = null, $width = null, $proportial = false, $offsetx = null, $offsety = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * cellMerge |
||
| 206 | * セルのマージ |
||
| 207 | * @param integer $col1 行 |
||
| 208 | * @param integer $row1 列 |
||
| 209 | * @param integer $col2 行 |
||
| 210 | * @param integer $row2 列 |
||
| 211 | * @param integer $sheetNo シート番号 |
||
| 212 | * @author hagiwara |
||
| 213 | */ |
||
| 214 | public function cellMerge($col1, $row1, $col2, $row2, $sheetNo) |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * styleCopy |
||
| 225 | * セルの書式コピー |
||
| 226 | * @param integer $col 行 |
||
| 227 | * @param integer $row 列 |
||
| 228 | * @param integer $sheetNo シート番号 |
||
| 229 | * @param integer $refCol 参照セル行 |
||
| 230 | * @param integer $refRow 参照セル列 |
||
| 231 | * @param integer $refSheet 参照シート |
||
| 232 | * @author hagiwara |
||
| 233 | */ |
||
| 234 | public function styleCopy($col, $row, $sheetNo, $refCol, $refRow, $refSheet) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * setStyle |
||
| 245 | * 書式のセット(まとめて) |
||
| 246 | * @param integer $col 行 |
||
| 247 | * @param integer $row 列 |
||
| 248 | * @param integer $sheetNo シート番号 |
||
| 249 | * @param array $style スタイル情報 |
||
| 250 | * @author hagiwara |
||
| 251 | */ |
||
| 252 | public function setStyle($col, $row, $sheetNo, $style) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * setFontName |
||
| 284 | * フォントのセット |
||
| 285 | * @param integer $col 行 |
||
| 286 | * @param integer $row 列 |
||
| 287 | * @param integer $sheetNo シート番号 |
||
| 288 | * @param string|null $fontName フォント名 |
||
| 289 | * @author hagiwara |
||
| 290 | */ |
||
| 291 | public function setFontName($col, $row, $sheetNo, $fontName) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * setUnderline |
||
| 301 | * 下線のセット |
||
| 302 | * @param integer $col 行 |
||
| 303 | * @param integer $row 列 |
||
| 304 | * @param integer $sheetNo シート番号 |
||
| 305 | * @param string|null $underline 下線の種類 |
||
| 306 | * @author hagiwara |
||
| 307 | */ |
||
| 308 | public function setUnderline($col, $row, $sheetNo, $underline) |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * getUnderlineType |
||
| 319 | * 下線の種類の設定 |
||
| 320 | * @param string $type |
||
| 321 | * @author hagiwara |
||
| 322 | */ |
||
| 323 | private function getUnderlineType($type) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * setFontBold |
||
| 334 | * 太字のセット |
||
| 335 | * @param integer $col 行 |
||
| 336 | * @param integer $row 列 |
||
| 337 | * @param integer $sheetNo シート番号 |
||
| 338 | * @param boolean|null $bold 太字を引くか |
||
| 339 | * @author hagiwara |
||
| 340 | */ |
||
| 341 | public function setFontBold($col, $row, $sheetNo, $bold) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * setItalic |
||
| 351 | * イタリックのセット |
||
| 352 | * @param integer $col 行 |
||
| 353 | * @param integer $row 列 |
||
| 354 | * @param integer $sheetNo シート番号 |
||
| 355 | * @param boolean|null $italic イタリックにするか |
||
| 356 | * @author hagiwara |
||
| 357 | */ |
||
| 358 | public function setItalic($col, $row, $sheetNo, $italic) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * setStrikethrough |
||
| 368 | * 打ち消し線のセット |
||
| 369 | * @param integer $col 行 |
||
| 370 | * @param integer $row 列 |
||
| 371 | * @param integer $sheetNo シート番号 |
||
| 372 | * @param boolean|null $strikethrough 打ち消し線をつけるか |
||
| 373 | * @author hagiwara |
||
| 374 | */ |
||
| 375 | public function setStrikethrough($col, $row, $sheetNo, $strikethrough) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * setColor |
||
| 389 | * 文字の色 |
||
| 390 | * @param integer $col 行 |
||
| 391 | * @param integer $row 列 |
||
| 392 | * @param integer $sheetNo シート番号 |
||
| 393 | * @param string|null $color 色(ARGB) |
||
| 394 | * @author hagiwara |
||
| 395 | */ |
||
| 396 | public function setColor($col, $row, $sheetNo, $color) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * setSize |
||
| 406 | * 文字サイズ |
||
| 407 | * @param integer $col 行 |
||
| 408 | * @param integer $row 列 |
||
| 409 | * @param integer $sheetNo シート番号 |
||
| 410 | * @param integer|null $size |
||
| 411 | * @author hagiwara |
||
| 412 | */ |
||
| 413 | public function setSize($col, $row, $sheetNo, $size) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * getFont |
||
| 423 | * fontデータ取得 |
||
| 424 | * @param integer $col 行 |
||
| 425 | * @param integer $row 列 |
||
| 426 | * @param integer $sheetNo シート番号 |
||
| 427 | * @author hagiwara |
||
| 428 | */ |
||
| 429 | private function getFont($col, $row, $sheetNo) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * setAlignHolizonal |
||
| 437 | * 水平方向のalign |
||
| 438 | * @param integer $col 行 |
||
| 439 | * @param integer $row 列 |
||
| 440 | * @param integer $sheetNo シート番号 |
||
| 441 | * @param string|null $type |
||
| 442 | * typeはgetAlignHolizonalType参照 |
||
| 443 | * @author hagiwara |
||
| 444 | */ |
||
| 445 | public function setAlignHolizonal($col, $row, $sheetNo, $type) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * setAlignVertical |
||
| 455 | * 垂直方法のalign |
||
| 456 | * @param integer $col 行 |
||
| 457 | * @param integer $row 列 |
||
| 458 | * @param integer $sheetNo シート番号 |
||
| 459 | * @param string|null $type |
||
| 460 | * typeはgetAlignVerticalType参照 |
||
| 461 | * @author hagiwara |
||
| 462 | */ |
||
| 463 | public function setAlignVertical($col, $row, $sheetNo, $type) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * getAlignment |
||
| 473 | * alignmentデータ取得 |
||
| 474 | * @param integer $col 行 |
||
| 475 | * @param integer $row 列 |
||
| 476 | * @param integer $sheetNo シート番号 |
||
| 477 | * @author hagiwara |
||
| 478 | */ |
||
| 479 | private function getAlignment($col, $row, $sheetNo) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * setBorder |
||
| 487 | * 罫線の設定 |
||
| 488 | * @param integer $col 行 |
||
| 489 | * @param integer $row 列 |
||
| 490 | * @param integer $sheetNo シート番号 |
||
| 491 | * @param array|null $border |
||
| 492 | * borderの内部はgetBorderType参照 |
||
| 493 | * @author hagiwara |
||
| 494 | */ |
||
| 495 | public function setBorder($col, $row, $sheetNo, $border) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * setBackgroundColor |
||
| 529 | * 背景色の設定 |
||
| 530 | * @param integer $col 行 |
||
| 531 | * @param integer $row 列 |
||
| 532 | * @param integer $sheetNo シート番号 |
||
| 533 | * @param string $color 色 |
||
| 534 | * @param string $fillType 塗りつぶし方(デフォルトsolid) |
||
| 535 | * fillTypeの内部はgetFillType参照 |
||
| 536 | * @author hagiwara |
||
| 537 | */ |
||
| 538 | public function setBackgroundColor($col, $row, $sheetNo, $color, $fillType = 'solid') |
||
| 544 | |||
| 545 | /** |
||
| 546 | * getBorderType |
||
| 547 | * 罫線の種類の設定 |
||
| 548 | * @param string $type |
||
| 549 | * @author hagiwara |
||
| 550 | */ |
||
| 551 | private function getBorderType($type) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * getAlignHolizonalType |
||
| 562 | * 水平方向のAlignの設定 |
||
| 563 | * @param string $type |
||
| 564 | * @author hagiwara |
||
| 565 | */ |
||
| 566 | private function getAlignHolizonalType($type) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * getAlignVerticalType |
||
| 577 | * 垂直方向のAlignの設定 |
||
| 578 | * @param string $type |
||
| 579 | * @author hagiwara |
||
| 580 | */ |
||
| 581 | private function getAlignVerticalType($type) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * getFillType |
||
| 592 | * 塗りつぶしの設定 |
||
| 593 | * @param string $type |
||
| 594 | * @author hagiwara |
||
| 595 | */ |
||
| 596 | private function getFillType($type) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * createSheet |
||
| 607 | * シートの作成 |
||
| 608 | * @param string $name |
||
| 609 | * @author hagiwara |
||
| 610 | */ |
||
| 611 | public function createSheet($name = null) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * deleteSheet |
||
| 624 | * シートの削除 |
||
| 625 | * @param integer $sheetNo |
||
| 626 | * @author hagiwara |
||
| 627 | */ |
||
| 628 | public function deleteSheet($sheetNo) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * copySheet |
||
| 636 | * シートのコピー |
||
| 637 | * @param integer $sheetNo |
||
| 638 | * @param integer $position |
||
| 639 | * @param string $name |
||
| 640 | * @author hagiwara |
||
| 641 | */ |
||
| 642 | public function copySheet($sheetNo, $position = null, $name = null) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * renameSheet |
||
| 656 | * シート名の変更 |
||
| 657 | * @param integer $sheetNo |
||
| 658 | * @param string $name |
||
| 659 | * @author hagiwara |
||
| 660 | */ |
||
| 661 | public function renameSheet($sheetNo, $name) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * write |
||
| 668 | * xlsxファイルの書き込み |
||
| 669 | * @param string $file 書き込み先のファイルパス |
||
| 670 | * @author hagiwara |
||
| 671 | */ |
||
| 672 | public function write($file, $type = 'Xlsx') |
||
| 681 | |||
| 682 | /** |
||
| 683 | * getReader |
||
| 684 | * readerを返す(※直接Spreadsheetの関数を実行できるように) |
||
| 685 | * @author hagiwara |
||
| 686 | */ |
||
| 687 | public function getReader() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * getSheet |
||
| 694 | * シート情報の読み込み |
||
| 695 | * @param integer $sheetNo シート番号 |
||
| 696 | * @author hagiwara |
||
| 697 | * @return null|\Spreadsheet_Worksheet |
||
| 698 | */ |
||
| 699 | private function getSheet($sheetNo) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * cellInfo |
||
| 709 | * R1C1参照をA1参照に変換して返す |
||
| 710 | * @param integer $col 行 |
||
| 711 | * @param integer $row 列 |
||
| 712 | * @author hagiwara |
||
| 713 | */ |
||
| 714 | private function cellInfo($col, $row) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * cellInfo |
||
| 722 | * http://qiita.com/Hiraku/items/036080976884fad1e450 |
||
| 723 | * @param string $str |
||
| 724 | */ |
||
| 725 | private function camelize($str) |
||
| 730 | } |
||
| 731 |