Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
15 | class CsvExportComponent extends Component { |
||
16 | |||
17 | private $_controller; |
||
18 | |||
19 | /** |
||
20 | * コンポーネント初期化 |
||
21 | * |
||
22 | * @access public |
||
23 | */ |
||
24 | public function startup(Event $event) |
||
25 | { |
||
26 | $this->_controller = $event->subject(); |
||
|
|||
27 | } |
||
28 | |||
29 | /* |
||
30 | * export CSVの出力アクション |
||
31 | * |
||
32 | * @param array $list 出力のための配列(二次元配列が基本) |
||
33 | * @param string $file_name 出力ファイル名(デフォルトはexport.csv) |
||
34 | * @param string $delimiter 区切り文字の設定(デフォルトは",") |
||
35 | * @param string $directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする) |
||
36 | * @param string $export_encoding 入力するファイルのエンコード(デフォルトはSJIS-win |
||
37 | * @param string $array_encoding 出力する配列のエンコード(デフォルトはUTF-8 |
||
38 | */ |
||
39 | public function export($list, $file_name = 'export.csv', $delimiter = ",", $directory = TMP,$export_encoding = 'SJIS-win',$array_encoding = 'UTF-8') |
||
40 | { |
||
41 | //layoutを切って autoRenderも外しておく |
||
42 | $this->_controller->viewBuilder()->layout('ajax'); |
||
43 | $this->_controller->autoRender = false; |
||
44 | |||
45 | //headerのセット |
||
46 | $save_directory = $this->make($list, $file_name , $delimiter , $directory ,$export_encoding ,$array_encoding); |
||
47 | // 日本語ファイル名出力のため |
||
48 | setlocale(LC_ALL, 'ja_JP.UTF-8'); |
||
49 | $basename = basename($save_directory); |
||
50 | |||
51 | // ファイル名の変換(IE対応) |
||
52 | View Code Duplication | if (strstr(env('HTTP_USER_AGENT'), 'MSIE') || strstr(env('HTTP_USER_AGENT'), 'Trident') || strstr(env('HTTP_USER_AGENT'), 'Edge')) { |
|
53 | $basename = mb_convert_encoding($basename, "SJIS", "UTF-8"); |
||
54 | } |
||
55 | $this->_controller->response->file($save_directory, ['download' => true, 'name' => $basename]); |
||
56 | } |
||
57 | |||
58 | /* |
||
59 | * make CSVの生成アクション |
||
60 | * |
||
61 | * @param array $list 出力のための配列(二次元配列が基本) |
||
62 | * @param string $file_name 出力ファイル名(デフォルトはexport.csv) |
||
63 | * @param string $delimiter 区切り文字の設定(デフォルトは",") |
||
64 | * @param string $directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする) |
||
65 | * @param string $export_encoding 入力するファイルのエンコード(デフォルトはSJIS-win |
||
66 | * @param string $array_encoding 出力する配列のエンコード(デフォルトはUTF-8 |
||
67 | */ |
||
68 | public function make($list, $file_name = 'export.csv', $delimiter = ",", $directory = TMP,$export_encoding = 'SJIS-win',$array_encoding = 'UTF-8') |
||
108 | |||
109 | /* |
||
110 | * _parseCsv |
||
111 | * csv(など)の形式に変更 |
||
112 | * |
||
113 | * @param string $v 変換する値 |
||
114 | * @param string $delimiter 区切り文字 |
||
115 | */ |
||
116 | private function _parseCsv($v, $delimiter) |
||
125 | |||
126 | } |
||
127 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.