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:
Complex classes like CsvExportService 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 CsvExportService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class CsvExportService |
||
|
|||
33 | { |
||
34 | /** |
||
35 | * @var |
||
36 | */ |
||
37 | protected $fp; |
||
38 | |||
39 | /** |
||
40 | * @var |
||
41 | */ |
||
42 | protected $closed = false; |
||
43 | |||
44 | /** |
||
45 | * @var \Closure |
||
46 | */ |
||
47 | protected $convertEncodingCallBack; |
||
48 | |||
49 | /** |
||
50 | * @var \Doctrine\ORM\EntityManager |
||
51 | */ |
||
52 | protected $em; |
||
53 | |||
54 | /** |
||
55 | * @var \Doctrine\ORM\QueryBuilder; |
||
56 | */ |
||
57 | protected $qb; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $config; |
||
63 | |||
64 | /** |
||
65 | * @var \Eccube\Entity\Master\CsvType |
||
66 | */ |
||
67 | protected $CsvType; |
||
68 | |||
69 | /** |
||
70 | * @var \Eccube\Entity\Csv[] |
||
71 | */ |
||
72 | protected $Csvs; |
||
73 | |||
74 | /** |
||
75 | * @var \Eccube\Repository\CsvRepository |
||
76 | */ |
||
77 | protected $csvRepository; |
||
78 | |||
79 | /** |
||
80 | * @var \Eccube\Repository\Master\CsvTypeRepository |
||
81 | */ |
||
82 | protected $csvTypeRepository; |
||
83 | |||
84 | /** |
||
85 | * @var \Eccube\Repository\OrderRepository |
||
86 | */ |
||
87 | protected $orderRepository; |
||
88 | |||
89 | /** |
||
90 | * @var \Eccube\Repository\CustomerRepository |
||
91 | */ |
||
92 | protected $customerRepository; |
||
93 | |||
94 | /** |
||
95 | * @var \Eccube\Repository\ProductRepository |
||
96 | */ |
||
97 | protected $productRepository; |
||
98 | |||
99 | /** |
||
100 | * @param $config |
||
101 | */ |
||
102 | 3 | public function setConfig($config) |
|
106 | |||
107 | /** |
||
108 | * @param \Eccube\Repository\CsvRepository $csvRepository |
||
109 | */ |
||
110 | 3 | public function setCsvRepository(\Eccube\Repository\CsvRepository $csvRepository) |
|
111 | { |
||
112 | 3 | $this->csvRepository = $csvRepository; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param \Eccube\Repository\Master\CsvTypeRepository $csvTypeRepository |
||
117 | */ |
||
118 | 3 | public function setCsvTypeRepository(\Eccube\Repository\Master\CsvTypeRepository $csvTypeRepository) |
|
119 | { |
||
120 | 3 | $this->csvTypeRepository = $csvTypeRepository; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param \Eccube\Repository\OrderRepository $orderRepository |
||
125 | */ |
||
126 | 3 | public function setOrderRepository(\Eccube\Repository\OrderRepository $orderRepository) |
|
127 | { |
||
128 | 3 | $this->orderRepository = $orderRepository; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param \Eccube\Repository\CustomerRepository $customerRepository |
||
133 | */ |
||
134 | 3 | public function setCustomerRepository(\Eccube\Repository\CustomerRepository $customerRepository) |
|
135 | { |
||
136 | 3 | $this->customerRepository = $customerRepository; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param \Eccube\Repository\ProductRepository $productRepository |
||
141 | */ |
||
142 | 3 | public function setProductRepository(\Eccube\Repository\ProductRepository $productRepository) |
|
143 | { |
||
144 | 3 | $this->productRepository = $productRepository; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param \Doctrine\ORM\EntityManager $em |
||
149 | */ |
||
150 | 2 | public function setEntityManager(\Doctrine\ORM\EntityManager $em) |
|
154 | |||
155 | /** |
||
156 | * @return \Doctrine\ORM\EntityManager |
||
157 | */ |
||
158 | public function getEntityManager() |
||
162 | |||
163 | /** |
||
164 | * @param \Doctrine\ORM\QueryBuilder $qb |
||
165 | */ |
||
166 | 2 | public function setExportQueryBuilder(\Doctrine\ORM\QueryBuilder $qb) |
|
167 | { |
||
168 | 2 | $this->qb = $qb; |
|
169 | $this->setEntityManager($qb->getEntityManager()); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Csv種別からServiceの初期化を行う. |
||
174 | * |
||
175 | * @param $CsvType|integer |
||
176 | */ |
||
177 | 3 | public function initCsvType($CsvType) |
|
178 | { |
||
179 | 3 | if ($CsvType instanceof \Eccube\Entity\Master\CsvType) { |
|
180 | $this->CsvType = $CsvType; |
||
181 | } else { |
||
182 | $this->CsvType = $this->csvTypeRepository->find($CsvType); |
||
183 | } |
||
184 | |||
185 | $criteria = array( |
||
186 | 'CsvType' => $CsvType, |
||
187 | 3 | 'enable_flg' => Constant::ENABLED |
|
188 | ); |
||
189 | $orderBy = array( |
||
190 | 'rank' => 'ASC' |
||
191 | 3 | ); |
|
192 | $this->Csvs = $this->csvRepository->findBy($criteria, $orderBy); |
||
193 | 3 | } |
|
194 | |||
195 | /** |
||
196 | * @return \Eccube\Entity\Csv[] |
||
197 | */ |
||
198 | public function getCsvs() |
||
202 | |||
203 | /** |
||
204 | * ヘッダ行を出力する. |
||
205 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
||
206 | */ |
||
207 | 2 | public function exportHeader() |
|
222 | |||
223 | /** |
||
224 | * クエリビルダにもとづいてデータ行を出力する. |
||
225 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
||
226 | * |
||
227 | * @param \Closure $closure |
||
228 | */ |
||
229 | 2 | public function exportData(\Closure $closure) |
|
248 | |||
249 | /** |
||
250 | * CSV出力項目と比較し, 合致するデータを返す. |
||
251 | * |
||
252 | * @param \Eccube\Entity\Csv $Csv |
||
253 | * @param $entity |
||
254 | * @return mixed|null|string|void |
||
255 | */ |
||
256 | public function getData(\Eccube\Entity\Csv $Csv, $entity) |
||
299 | |||
300 | /** |
||
301 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
||
302 | * |
||
303 | * @return \Closure |
||
304 | */ |
||
305 | 3 | public function getConvertEncodhingCallback() |
|
315 | |||
316 | /** |
||
317 | * |
||
318 | */ |
||
319 | 2 | public function fopen() |
|
325 | |||
326 | /** |
||
327 | * @param $row |
||
328 | * @param null $callback |
||
329 | */ |
||
330 | 1 | public function fputcsv($row) |
|
338 | |||
339 | /** |
||
340 | * |
||
341 | */ |
||
342 | 3 | public function fclose() |
|
349 | |||
350 | /** |
||
351 | * 受注検索用のクエリビルダを返す. |
||
352 | * |
||
353 | * @param Request $request |
||
354 | * @return \Doctrine\ORM\QueryBuilder |
||
355 | */ |
||
356 | View Code Duplication | public function getOrderQueryBuilder(Request $request) |
|
371 | |||
372 | /** |
||
373 | * 会員検索用のクエリビルダを返す. |
||
374 | * |
||
375 | * @param Request $request |
||
376 | * @return \Doctrine\ORM\QueryBuilder |
||
377 | */ |
||
378 | 1 | public function getCustomerQueryBuilder(Request $request) |
|
379 | { |
||
380 | $session = $request->getSession(); |
||
381 | if ($session->has('eccube.admin.customer.search')) { |
||
382 | $searchData = $session->get('eccube.admin.customer.search'); |
||
383 | } else { |
||
384 | 1 | $searchData = array(); |
|
385 | } |
||
386 | |||
387 | // 会員データのクエリビルダを構築. |
||
388 | 1 | $qb = $this->customerRepository |
|
389 | ->getQueryBuilderBySearchData($searchData); |
||
390 | |||
391 | 1 | return $qb; |
|
392 | 1 | } |
|
393 | |||
394 | /** |
||
395 | * 商品検索用のクエリビルダを返す. |
||
396 | * |
||
397 | * @param Request $request |
||
398 | * @return \Doctrine\ORM\QueryBuilder |
||
399 | */ |
||
400 | View Code Duplication | public function getProductQueryBuilder(Request $request) |
|
415 | } |
||
416 |