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 |
||
| 16 | class PhpExcelWrapper |
||
| 17 | { |
||
| 18 | private $__phpexcel; |
||
| 19 | private $__sheet = []; |
||
| 20 | private $__deleteSheetList = []; |
||
| 21 | private static $__borderType = [ |
||
| 22 | 'none' => PHPExcel_Style_Border::BORDER_NONE, |
||
| 23 | 'thin' => PHPExcel_Style_Border::BORDER_THIN, |
||
| 24 | 'medium' => PHPExcel_Style_Border::BORDER_MEDIUM, |
||
| 25 | 'dashed' => PHPExcel_Style_Border::BORDER_DASHED, |
||
| 26 | 'dotted' => PHPExcel_Style_Border::BORDER_DOTTED, |
||
| 27 | 'thick' => PHPExcel_Style_Border::BORDER_THICK, |
||
| 28 | 'double' => PHPExcel_Style_Border::BORDER_DOUBLE, |
||
| 29 | 'hair' => PHPExcel_Style_Border::BORDER_HAIR, |
||
| 30 | 'mediumdashed' => PHPExcel_Style_Border::BORDER_MEDIUMDASHED, |
||
| 31 | 'dashdot' => PHPExcel_Style_Border::BORDER_DASHDOT, |
||
| 32 | 'mediumdashdot' => PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT, |
||
| 33 | 'dashdotdot' => PHPExcel_Style_Border::BORDER_DASHDOTDOT, |
||
| 34 | 'mediumdashdotdot' => PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT, |
||
| 35 | 'slantdashdot' => PHPExcel_Style_Border::BORDER_SLANTDASHDOT, |
||
| 36 | ]; |
||
| 37 | |||
| 38 | private static $__alignHolizonalType = [ |
||
| 39 | 'general' => PHPExcel_Style_Alignment::HORIZONTAL_GENERAL, |
||
| 40 | 'center' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, |
||
| 41 | 'left' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT, |
||
| 42 | 'right' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, |
||
| 43 | 'justify' => PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY, |
||
| 44 | 'countinuous' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS, |
||
| 45 | ]; |
||
| 46 | |||
| 47 | private static $__alignVerticalType = [ |
||
| 48 | 'bottom' => PHPExcel_Style_Alignment::VERTICAL_BOTTOM, |
||
| 49 | 'center' => PHPExcel_Style_Alignment::VERTICAL_CENTER, |
||
| 50 | 'justify' => PHPExcel_Style_Alignment::VERTICAL_JUSTIFY, |
||
| 51 | 'top' => PHPExcel_Style_Alignment::VERTICAL_TOP, |
||
| 52 | ]; |
||
| 53 | |||
| 54 | private static $__fillType = [ |
||
| 55 | 'linear' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR, |
||
| 56 | 'path' => PHPExcel_Style_Fill::FILL_GRADIENT_PATH, |
||
| 57 | 'none' => PHPExcel_Style_Fill::FILL_NONE, |
||
| 58 | 'darkdown' => PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN, |
||
| 59 | 'darkgray' => PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY, |
||
| 60 | 'darkgrid' => PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID, |
||
| 61 | 'darkhorizontal' => PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL, |
||
| 62 | 'darktrellis' => PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS, |
||
| 63 | 'darkup' => PHPExcel_Style_Fill::FILL_PATTERN_DARKUP, |
||
| 64 | 'darkvertical' => PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL, |
||
| 65 | 'gray0625' => PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625, |
||
| 66 | 'gray125' => PHPExcel_Style_Fill::FILL_PATTERN_GRAY125, |
||
| 67 | 'lightdown' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN, |
||
| 68 | 'lightgray' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY, |
||
| 69 | 'lightgrid' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID, |
||
| 70 | 'lighthorizontal' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL, |
||
| 71 | 'lighttrellis' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS, |
||
| 72 | 'lightup' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP, |
||
| 73 | 'lightvertical' => PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL, |
||
| 74 | 'mediumgray' => PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY, |
||
| 75 | 'solid' => PHPExcel_Style_Fill::FILL_SOLID, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * __construct |
||
| 80 | * |
||
| 81 | * @param string $template テンプレートファイルのパス |
||
| 82 | * @author hagiwara |
||
| 83 | */ |
||
| 84 | public function __construct($template = null, $type = 'Excel2007') |
||
| 95 | |||
| 96 | /** |
||
| 97 | * setVal |
||
| 98 | * 値のセット |
||
| 99 | * @param string $value 値 |
||
| 100 | * @param integer $col 行 |
||
| 101 | * @param integer $row 列 |
||
| 102 | * @param integer $sheetNo シート番号 |
||
| 103 | * @param integer $refCol 参照セル行 |
||
| 104 | * @param integer $refRow 参照セル列 |
||
| 105 | * @param integer $refSheet 参照シート |
||
| 106 | * @author hagiwara |
||
| 107 | */ |
||
| 108 | public function setVal($value, $col, $row, $sheetNo = 0, $refCol = null, $refRow = null, $refSheet = 0) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * setImage |
||
| 122 | * 画像のセット |
||
| 123 | * @param string $img 画像のファイルパス |
||
| 124 | * @param integer $col 行 |
||
| 125 | * @param integer $row 列 |
||
| 126 | * @param integer $sheetNo シート番号 |
||
| 127 | * @param integer $height 画像の縦幅 |
||
| 128 | * @param integer $width 画像の横幅 |
||
| 129 | * @param boolean $proportial 縦横比を維持するか |
||
| 130 | * @param integer $offsetx セルから何ピクセルずらすか(X軸) |
||
| 131 | * @param integer $offsety セルから何ピクセルずらすか(Y軸) |
||
| 132 | * @author hagiwara |
||
| 133 | */ |
||
| 134 | public function setImage($img, $col, $row, $sheetNo = 0, $height = null, $width = null, $proportial = false, $offsetx = null, $offsety = null) |
||
| 135 | { |
||
| 136 | $cellInfo = $this->cellInfo($col, $row); |
||
| 137 | |||
| 138 | $objDrawing = new PHPExcel_Worksheet_Drawing();//画像用のオプジェクト作成 |
||
| 139 | $objDrawing->setPath($img);//貼り付ける画像のパスを指定 |
||
| 140 | $objDrawing->setCoordinates($cellInfo);//位置 |
||
| 141 | if (!is_null($height)) { |
||
| 142 | $objDrawing->setHeight($height);//画像の高さを指定 |
||
| 143 | } |
||
| 144 | if (!is_null($width)) { |
||
| 145 | $objDrawing->setWidth($width);//画像の高さを指定 |
||
| 146 | } |
||
| 147 | if (!is_null($proportial)) { |
||
| 148 | $objDrawing->setResizeProportional($proportial);//縦横比の変更なし |
||
| 149 | } |
||
| 150 | if (!is_null($offsetx)) { |
||
| 151 | $objDrawing->setOffsetX($offsetx);//指定した位置からどれだけ横方向にずらすか。 |
||
| 152 | } |
||
| 153 | if (!is_null($offsety)) { |
||
| 154 | $objDrawing->setOffsetY($offsety);//指定した位置からどれだけ縦方向にずらすか。 |
||
| 155 | } |
||
| 156 | $objDrawing->setWorksheet($this->getSheet($sheetNo)); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * cellMerge |
||
| 161 | * セルのマージ |
||
| 162 | * @param integer $col1 行 |
||
| 163 | * @param integer $row1 列 |
||
| 164 | * @param integer $col2 行 |
||
| 165 | * @param integer $row2 列 |
||
| 166 | * @param integer $sheetNo シート番号 |
||
| 167 | * @author hagiwara |
||
| 168 | */ |
||
| 169 | public function cellMerge($col1, $row1, $col2, $row2, $sheetNo) |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * styleCopy |
||
| 180 | * セルの書式コピー |
||
| 181 | * @param integer $col 行 |
||
| 182 | * @param integer $row 列 |
||
| 183 | * @param integer $sheetNo シート番号 |
||
| 184 | * @param integer $refCol 参照セル行 |
||
| 185 | * @param integer $refRow 参照セル列 |
||
| 186 | * @param integer $refSheet 参照シート |
||
| 187 | * @author hagiwara |
||
| 188 | */ |
||
| 189 | public function styleCopy($col, $row, $sheetNo, $refCol, $refRow, $refSheet) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * setStyle |
||
| 200 | * 書式のセット(まとめて) |
||
| 201 | * @param integer $col 行 |
||
| 202 | * @param integer $row 列 |
||
| 203 | * @param integer $sheetNo シート番号 |
||
| 204 | * @param array $style スタイル情報 |
||
| 205 | * @author hagiwara |
||
| 206 | */ |
||
| 207 | public function setStyle($col, $row, $sheetNo, $style) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * setFontName |
||
| 237 | * フォントのセット |
||
| 238 | * @param integer $col 行 |
||
| 239 | * @param integer $row 列 |
||
| 240 | * @param integer $sheetNo シート番号 |
||
| 241 | * @param string|null $fontName フォント名 |
||
| 242 | * @author hagiwara |
||
| 243 | */ |
||
| 244 | public function setFontName($col, $row, $sheetNo, $fontName) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * setUnderline |
||
| 254 | * 下線のセット |
||
| 255 | * @param integer $col 行 |
||
| 256 | * @param integer $row 列 |
||
| 257 | * @param integer $sheetNo シート番号 |
||
| 258 | * @param boolean|null $underline 下線を引くか |
||
| 259 | * @author hagiwara |
||
| 260 | */ |
||
| 261 | public function setUnderline($col, $row, $sheetNo, $underline) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * setFontBold |
||
| 271 | * 太字のセット |
||
| 272 | * @param integer $col 行 |
||
| 273 | * @param integer $row 列 |
||
| 274 | * @param integer $sheetNo シート番号 |
||
| 275 | * @param boolean|null $bold 太字を引くか |
||
| 276 | * @author hagiwara |
||
| 277 | */ |
||
| 278 | public function setFontBold($col, $row, $sheetNo, $bold) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * setItalic |
||
| 288 | * イタリックのセット |
||
| 289 | * @param integer $col 行 |
||
| 290 | * @param integer $row 列 |
||
| 291 | * @param integer $sheetNo シート番号 |
||
| 292 | * @param boolean|null $italic イタリックにするか |
||
| 293 | * @author hagiwara |
||
| 294 | */ |
||
| 295 | public function setItalic($col, $row, $sheetNo, $italic) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * setStrikethrough |
||
| 305 | * 打ち消し線のセット |
||
| 306 | * @param integer $col 行 |
||
| 307 | * @param integer $row 列 |
||
| 308 | * @param integer $sheetNo シート番号 |
||
| 309 | * @param boolean|null $strikethrough 打ち消し線をつけるか |
||
| 310 | * @author hagiwara |
||
| 311 | */ |
||
| 312 | public function setStrikethrough($col, $row, $sheetNo, $strikethrough) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * setColor |
||
| 322 | * 文字の色 |
||
| 323 | * @param integer $col 行 |
||
| 324 | * @param integer $row 列 |
||
| 325 | * @param integer $sheetNo シート番号 |
||
| 326 | * @param string|null $color 色(ARGB) |
||
| 327 | * @author hagiwara |
||
| 328 | */ |
||
| 329 | public function setColor($col, $row, $sheetNo, $color) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * setSize |
||
| 339 | * 文字サイズ |
||
| 340 | * @param integer $col 行 |
||
| 341 | * @param integer $row 列 |
||
| 342 | * @param integer $sheetNo シート番号 |
||
| 343 | * @param integer|null $size |
||
| 344 | * @author hagiwara |
||
| 345 | */ |
||
| 346 | public function setSize($col, $row, $sheetNo, $size) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * getFont |
||
| 356 | * fontデータ取得 |
||
| 357 | * @param integer $col 行 |
||
| 358 | * @param integer $row 列 |
||
| 359 | * @param integer $sheetNo シート番号 |
||
| 360 | * @author hagiwara |
||
| 361 | */ |
||
| 362 | private function getFont($col, $row, $sheetNo) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * setAlignHolizonal |
||
| 370 | * 水平方向のalign |
||
| 371 | * @param integer $col 行 |
||
| 372 | * @param integer $row 列 |
||
| 373 | * @param integer $sheetNo シート番号 |
||
| 374 | * @param string|null $type |
||
| 375 | * typeはgetAlignHolizonalType参照 |
||
| 376 | * @author hagiwara |
||
| 377 | */ |
||
| 378 | public function setAlignHolizonal($col, $row, $sheetNo, $type) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * setAlignVertical |
||
| 388 | * 垂直方法のalign |
||
| 389 | * @param integer $col 行 |
||
| 390 | * @param integer $row 列 |
||
| 391 | * @param integer $sheetNo シート番号 |
||
| 392 | * @param string|null $type |
||
| 393 | * typeはgetAlignVerticalType参照 |
||
| 394 | * @author hagiwara |
||
| 395 | */ |
||
| 396 | public function setAlignVertical($col, $row, $sheetNo, $type) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * getAlignment |
||
| 406 | * alignmentデータ取得 |
||
| 407 | * @param integer $col 行 |
||
| 408 | * @param integer $row 列 |
||
| 409 | * @param integer $sheetNo シート番号 |
||
| 410 | * @author hagiwara |
||
| 411 | */ |
||
| 412 | private function getAlignment($col, $row, $sheetNo) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * setBorder |
||
| 420 | * 罫線の設定 |
||
| 421 | * @param integer $col 行 |
||
| 422 | * @param integer $row 列 |
||
| 423 | * @param integer $sheetNo シート番号 |
||
| 424 | * @param array $border |
||
| 425 | * borderの内部はgetBorderType参照 |
||
| 426 | * @author hagiwara |
||
| 427 | */ |
||
| 428 | public function setBorder($col, $row, $sheetNo, $border) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * setBackgroundColor |
||
| 459 | * 背景色の設定 |
||
| 460 | * @param integer $col 行 |
||
| 461 | * @param integer $row 列 |
||
| 462 | * @param integer $sheetNo シート番号 |
||
| 463 | * @param string $color 色 |
||
| 464 | * @param string $fillType 塗りつぶし方(デフォルトsolid) |
||
| 465 | * fillTypeの内部はgetFillType参照 |
||
| 466 | * @author hagiwara |
||
| 467 | */ |
||
| 468 | public function setBackgroundColor($col, $row, $sheetNo, $color, $fillType = 'solid') |
||
| 474 | |||
| 475 | /** |
||
| 476 | * getBorderType |
||
| 477 | * 罫線の種類の設定 |
||
| 478 | * @param string $type |
||
| 479 | * @author hagiwara |
||
| 480 | */ |
||
| 481 | private function getBorderType($type) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * getAlignHolizonalType |
||
| 492 | * 水平方向のAlignの設定 |
||
| 493 | * @param string $type |
||
| 494 | * @author hagiwara |
||
| 495 | */ |
||
| 496 | private function getAlignHolizonalType($type) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * getAlignVerticalType |
||
| 507 | * 垂直方向のAlignの設定 |
||
| 508 | * @param string $type |
||
| 509 | * @author hagiwara |
||
| 510 | */ |
||
| 511 | private function getAlignVerticalType($type) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * getFillType |
||
| 522 | * 塗りつぶしの設定 |
||
| 523 | * @param string $type |
||
| 524 | * @author hagiwara |
||
| 525 | */ |
||
| 526 | private function getFillType($type) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * createSheet |
||
| 537 | * シートの作成 |
||
| 538 | * @param string $name |
||
| 539 | * @author hagiwara |
||
| 540 | */ |
||
| 541 | public function createSheet($name = null) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * deleteSheet |
||
| 554 | * シートの削除 |
||
| 555 | * @param integer $sheetNo |
||
| 556 | * @author hagiwara |
||
| 557 | */ |
||
| 558 | public function deleteSheet($sheetNo) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * copySheet |
||
| 566 | * シートのコピー |
||
| 567 | * @param integer $sheetNo |
||
| 568 | * @param integer $position |
||
| 569 | * @param string $name |
||
| 570 | * @author hagiwara |
||
| 571 | */ |
||
| 572 | public function copySheet($sheetNo, $position = null, $name = null) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * renameSheet |
||
| 586 | * シート名の変更 |
||
| 587 | * @param integer $sheetNo |
||
| 588 | * @param string $name |
||
| 589 | * @author hagiwara |
||
| 590 | */ |
||
| 591 | public function renameSheet($sheetNo, $name) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * write |
||
| 598 | * xlsxファイルの書き込み |
||
| 599 | * @param string $file 書き込み先のファイルパス |
||
| 600 | * @author hagiwara |
||
| 601 | */ |
||
| 602 | public function write($file, $type = 'Excel2007') |
||
| 611 | |||
| 612 | /** |
||
| 613 | * getReader |
||
| 614 | * readerを返す(※直接PHPExcelの関数を実行できるように) |
||
| 615 | * @author hagiwara |
||
| 616 | */ |
||
| 617 | public function getReader() |
||
| 621 | |||
| 622 | /** |
||
| 623 | * getSheet |
||
| 624 | * シート情報の読み込み |
||
| 625 | * @param integer $sheetNo シート番号 |
||
| 626 | * @author hagiwara |
||
| 627 | * @return null|\PHPExcel_Worksheet |
||
| 628 | */ |
||
| 629 | private function getSheet($sheetNo) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * cellInfo |
||
| 639 | * R1C1参照をA1参照に変換して返す |
||
| 640 | * @param integer $col 行 |
||
| 641 | * @param integer $row 列 |
||
| 642 | * @author hagiwara |
||
| 643 | */ |
||
| 644 | private function cellInfo($col, $row) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * cellInfo |
||
| 652 | * http://qiita.com/Hiraku/items/036080976884fad1e450 |
||
| 653 | * @param string $str |
||
| 654 | */ |
||
| 655 | private function camelize($str) |
||
| 660 | } |
||
| 661 |