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 | * setImage |
||
| 132 | * 画像のセット |
||
| 133 | * @param string $img 画像のファイルパス |
||
| 134 | * @param integer $col 行 |
||
| 135 | * @param integer $row 列 |
||
| 136 | * @param integer $sheetNo シート番号 |
||
| 137 | * @param integer $height 画像の縦幅 |
||
| 138 | * @param integer $width 画像の横幅 |
||
| 139 | * @param boolean $proportial 縦横比を維持するか |
||
| 140 | * @param integer $offsetx セルから何ピクセルずらすか(X軸) |
||
| 141 | * @param integer $offsety セルから何ピクセルずらすか(Y軸) |
||
| 142 | * @author hagiwara |
||
| 143 | */ |
||
| 144 | public function setImage($img, $col, $row, $sheetNo = 0, $height = null, $width = null, $proportial = false, $offsetx = null, $offsety = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * cellMerge |
||
| 171 | * セルのマージ |
||
| 172 | * @param integer $col1 行 |
||
| 173 | * @param integer $row1 列 |
||
| 174 | * @param integer $col2 行 |
||
| 175 | * @param integer $row2 列 |
||
| 176 | * @param integer $sheetNo シート番号 |
||
| 177 | * @author hagiwara |
||
| 178 | */ |
||
| 179 | public function cellMerge($col1, $row1, $col2, $row2, $sheetNo) |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * styleCopy |
||
| 190 | * セルの書式コピー |
||
| 191 | * @param integer $col 行 |
||
| 192 | * @param integer $row 列 |
||
| 193 | * @param integer $sheetNo シート番号 |
||
| 194 | * @param integer $refCol 参照セル行 |
||
| 195 | * @param integer $refRow 参照セル列 |
||
| 196 | * @param integer $refSheet 参照シート |
||
| 197 | * @author hagiwara |
||
| 198 | */ |
||
| 199 | public function styleCopy($col, $row, $sheetNo, $refCol, $refRow, $refSheet) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * setStyle |
||
| 210 | * 書式のセット(まとめて) |
||
| 211 | * @param integer $col 行 |
||
| 212 | * @param integer $row 列 |
||
| 213 | * @param integer $sheetNo シート番号 |
||
| 214 | * @param array $style スタイル情報 |
||
| 215 | * @author hagiwara |
||
| 216 | */ |
||
| 217 | public function setStyle($col, $row, $sheetNo, $style) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * setFontName |
||
| 249 | * フォントのセット |
||
| 250 | * @param integer $col 行 |
||
| 251 | * @param integer $row 列 |
||
| 252 | * @param integer $sheetNo シート番号 |
||
| 253 | * @param string|null $fontName フォント名 |
||
| 254 | * @author hagiwara |
||
| 255 | */ |
||
| 256 | public function setFontName($col, $row, $sheetNo, $fontName) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * setUnderline |
||
| 266 | * 下線のセット |
||
| 267 | * @param integer $col 行 |
||
| 268 | * @param integer $row 列 |
||
| 269 | * @param integer $sheetNo シート番号 |
||
| 270 | * @param string|null $underline 下線の種類 |
||
| 271 | * @author hagiwara |
||
| 272 | */ |
||
| 273 | public function setUnderline($col, $row, $sheetNo, $underline) |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * getUnderlineType |
||
| 284 | * 下線の種類の設定 |
||
| 285 | * @param string $type |
||
| 286 | * @author hagiwara |
||
| 287 | */ |
||
| 288 | private function getUnderlineType($type) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * setFontBold |
||
| 299 | * 太字のセット |
||
| 300 | * @param integer $col 行 |
||
| 301 | * @param integer $row 列 |
||
| 302 | * @param integer $sheetNo シート番号 |
||
| 303 | * @param boolean|null $bold 太字を引くか |
||
| 304 | * @author hagiwara |
||
| 305 | */ |
||
| 306 | public function setFontBold($col, $row, $sheetNo, $bold) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * setItalic |
||
| 316 | * イタリックのセット |
||
| 317 | * @param integer $col 行 |
||
| 318 | * @param integer $row 列 |
||
| 319 | * @param integer $sheetNo シート番号 |
||
| 320 | * @param boolean|null $italic イタリックにするか |
||
| 321 | * @author hagiwara |
||
| 322 | */ |
||
| 323 | public function setItalic($col, $row, $sheetNo, $italic) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * setStrikethrough |
||
| 333 | * 打ち消し線のセット |
||
| 334 | * @param integer $col 行 |
||
| 335 | * @param integer $row 列 |
||
| 336 | * @param integer $sheetNo シート番号 |
||
| 337 | * @param boolean|null $strikethrough 打ち消し線をつけるか |
||
| 338 | * @author hagiwara |
||
| 339 | */ |
||
| 340 | public function setStrikethrough($col, $row, $sheetNo, $strikethrough) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * setColor |
||
| 350 | * 文字の色 |
||
| 351 | * @param integer $col 行 |
||
| 352 | * @param integer $row 列 |
||
| 353 | * @param integer $sheetNo シート番号 |
||
| 354 | * @param string|null $color 色(ARGB) |
||
| 355 | * @author hagiwara |
||
| 356 | */ |
||
| 357 | public function setColor($col, $row, $sheetNo, $color) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * setSize |
||
| 367 | * 文字サイズ |
||
| 368 | * @param integer $col 行 |
||
| 369 | * @param integer $row 列 |
||
| 370 | * @param integer $sheetNo シート番号 |
||
| 371 | * @param integer|null $size |
||
| 372 | * @author hagiwara |
||
| 373 | */ |
||
| 374 | public function setSize($col, $row, $sheetNo, $size) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * getFont |
||
| 384 | * fontデータ取得 |
||
| 385 | * @param integer $col 行 |
||
| 386 | * @param integer $row 列 |
||
| 387 | * @param integer $sheetNo シート番号 |
||
| 388 | * @author hagiwara |
||
| 389 | */ |
||
| 390 | private function getFont($col, $row, $sheetNo) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * setAlignHolizonal |
||
| 398 | * 水平方向のalign |
||
| 399 | * @param integer $col 行 |
||
| 400 | * @param integer $row 列 |
||
| 401 | * @param integer $sheetNo シート番号 |
||
| 402 | * @param string|null $type |
||
| 403 | * typeはgetAlignHolizonalType参照 |
||
| 404 | * @author hagiwara |
||
| 405 | */ |
||
| 406 | public function setAlignHolizonal($col, $row, $sheetNo, $type) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * setAlignVertical |
||
| 416 | * 垂直方法のalign |
||
| 417 | * @param integer $col 行 |
||
| 418 | * @param integer $row 列 |
||
| 419 | * @param integer $sheetNo シート番号 |
||
| 420 | * @param string|null $type |
||
| 421 | * typeはgetAlignVerticalType参照 |
||
| 422 | * @author hagiwara |
||
| 423 | */ |
||
| 424 | public function setAlignVertical($col, $row, $sheetNo, $type) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * getAlignment |
||
| 434 | * alignmentデータ取得 |
||
| 435 | * @param integer $col 行 |
||
| 436 | * @param integer $row 列 |
||
| 437 | * @param integer $sheetNo シート番号 |
||
| 438 | * @author hagiwara |
||
| 439 | */ |
||
| 440 | private function getAlignment($col, $row, $sheetNo) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * setBorder |
||
| 448 | * 罫線の設定 |
||
| 449 | * @param integer $col 行 |
||
| 450 | * @param integer $row 列 |
||
| 451 | * @param integer $sheetNo シート番号 |
||
| 452 | * @param array $border |
||
| 453 | * borderの内部はgetBorderType参照 |
||
| 454 | * @author hagiwara |
||
| 455 | */ |
||
| 456 | public function setBorder($col, $row, $sheetNo, $border) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * setBackgroundColor |
||
| 490 | * 背景色の設定 |
||
| 491 | * @param integer $col 行 |
||
| 492 | * @param integer $row 列 |
||
| 493 | * @param integer $sheetNo シート番号 |
||
| 494 | * @param string $color 色 |
||
| 495 | * @param string $fillType 塗りつぶし方(デフォルトsolid) |
||
| 496 | * fillTypeの内部はgetFillType参照 |
||
| 497 | * @author hagiwara |
||
| 498 | */ |
||
| 499 | public function setBackgroundColor($col, $row, $sheetNo, $color, $fillType = 'solid') |
||
| 505 | |||
| 506 | /** |
||
| 507 | * getBorderType |
||
| 508 | * 罫線の種類の設定 |
||
| 509 | * @param string $type |
||
| 510 | * @author hagiwara |
||
| 511 | */ |
||
| 512 | private function getBorderType($type) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * getAlignHolizonalType |
||
| 523 | * 水平方向のAlignの設定 |
||
| 524 | * @param string $type |
||
| 525 | * @author hagiwara |
||
| 526 | */ |
||
| 527 | private function getAlignHolizonalType($type) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * getAlignVerticalType |
||
| 538 | * 垂直方向のAlignの設定 |
||
| 539 | * @param string $type |
||
| 540 | * @author hagiwara |
||
| 541 | */ |
||
| 542 | private function getAlignVerticalType($type) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * getFillType |
||
| 553 | * 塗りつぶしの設定 |
||
| 554 | * @param string $type |
||
| 555 | * @author hagiwara |
||
| 556 | */ |
||
| 557 | private function getFillType($type) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * createSheet |
||
| 568 | * シートの作成 |
||
| 569 | * @param string $name |
||
| 570 | * @author hagiwara |
||
| 571 | */ |
||
| 572 | public function createSheet($name = null) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * deleteSheet |
||
| 585 | * シートの削除 |
||
| 586 | * @param integer $sheetNo |
||
| 587 | * @author hagiwara |
||
| 588 | */ |
||
| 589 | public function deleteSheet($sheetNo) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * copySheet |
||
| 597 | * シートのコピー |
||
| 598 | * @param integer $sheetNo |
||
| 599 | * @param integer $position |
||
| 600 | * @param string $name |
||
| 601 | * @author hagiwara |
||
| 602 | */ |
||
| 603 | public function copySheet($sheetNo, $position = null, $name = null) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * renameSheet |
||
| 617 | * シート名の変更 |
||
| 618 | * @param integer $sheetNo |
||
| 619 | * @param string $name |
||
| 620 | * @author hagiwara |
||
| 621 | */ |
||
| 622 | public function renameSheet($sheetNo, $name) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * write |
||
| 629 | * xlsxファイルの書き込み |
||
| 630 | * @param string $file 書き込み先のファイルパス |
||
| 631 | * @author hagiwara |
||
| 632 | */ |
||
| 633 | public function write($file, $type = 'Excel2007') |
||
| 642 | |||
| 643 | /** |
||
| 644 | * getReader |
||
| 645 | * readerを返す(※直接PHPExcelの関数を実行できるように) |
||
| 646 | * @author hagiwara |
||
| 647 | */ |
||
| 648 | public function getReader() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * getSheet |
||
| 655 | * シート情報の読み込み |
||
| 656 | * @param integer $sheetNo シート番号 |
||
| 657 | * @author hagiwara |
||
| 658 | * @return null|\PHPExcel_Worksheet |
||
| 659 | */ |
||
| 660 | private function getSheet($sheetNo) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * cellInfo |
||
| 670 | * R1C1参照をA1参照に変換して返す |
||
| 671 | * @param integer $col 行 |
||
| 672 | * @param integer $row 列 |
||
| 673 | * @author hagiwara |
||
| 674 | */ |
||
| 675 | private function cellInfo($col, $row) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * cellInfo |
||
| 683 | * http://qiita.com/Hiraku/items/036080976884fad1e450 |
||
| 684 | * @param string $str |
||
| 685 | */ |
||
| 686 | private function camelize($str) |
||
| 691 | } |
||
| 692 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: