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 |
||
36 | class CsvExportService |
||
37 | { |
||
38 | /** |
||
39 | * @var resource |
||
40 | */ |
||
41 | protected $fp; |
||
42 | |||
43 | /** |
||
44 | * @var boolean |
||
45 | */ |
||
46 | protected $closed = false; |
||
47 | |||
48 | /** |
||
49 | * @var \Closure |
||
50 | */ |
||
51 | protected $convertEncodingCallBack; |
||
52 | |||
53 | /** |
||
54 | * @var EntityManagerInterface |
||
55 | */ |
||
56 | protected $entityManager; |
||
57 | |||
58 | /** |
||
59 | * @var QueryBuilder; |
||
60 | */ |
||
61 | protected $qb; |
||
62 | |||
63 | /** |
||
64 | * @var EccubeConfig |
||
65 | */ |
||
66 | protected $eccubeConfig; |
||
67 | |||
68 | /** |
||
69 | * @var CsvType |
||
70 | */ |
||
71 | protected $CsvType; |
||
72 | |||
73 | /** |
||
74 | * @var Csv[] |
||
75 | */ |
||
76 | protected $Csvs; |
||
77 | |||
78 | /** |
||
79 | * @var CsvRepository |
||
80 | */ |
||
81 | protected $csvRepository; |
||
82 | |||
83 | /** |
||
84 | * @var CsvTypeRepository |
||
85 | */ |
||
86 | protected $csvTypeRepository; |
||
87 | |||
88 | /** |
||
89 | * @var OrderRepository |
||
90 | */ |
||
91 | protected $orderRepository; |
||
92 | |||
93 | /** |
||
94 | * @var ShippingRepository |
||
95 | */ |
||
96 | protected $shippingRepository; |
||
97 | |||
98 | /** |
||
99 | * @var CustomerRepository |
||
100 | */ |
||
101 | protected $customerRepository; |
||
102 | |||
103 | /** |
||
104 | * @var ProductRepository |
||
105 | */ |
||
106 | protected $productRepository; |
||
107 | |||
108 | /** |
||
109 | * @var FormFactoryInterface |
||
110 | */ |
||
111 | protected $formFactory; |
||
112 | |||
113 | /** @var PaginatorInterface */ |
||
114 | protected $paginator; |
||
115 | |||
116 | /** |
||
117 | * CsvExportService constructor. |
||
118 | * |
||
119 | * @param EntityManagerInterface $entityManager |
||
120 | * @param CsvRepository $csvRepository |
||
121 | * @param CsvTypeRepository $csvTypeRepository |
||
122 | 76 | * @param OrderRepository $orderRepository |
|
123 | * @param ShippingRepository $shippingRepository |
||
124 | * @param CustomerRepository $customerRepository |
||
125 | * @param ProductRepository $productRepository |
||
126 | * @param EccubeConfig $eccubeConfig |
||
127 | * @param FormFactoryInterface $formFactory |
||
128 | * @param PaginatorInterface $paginator |
||
129 | */ |
||
130 | public function __construct( |
||
153 | |||
154 | /** |
||
155 | * @param $config |
||
156 | */ |
||
157 | public function setConfig($config) |
||
161 | |||
162 | /** |
||
163 | * @param CsvRepository $csvRepository |
||
164 | */ |
||
165 | public function setCsvRepository(CsvRepository $csvRepository) |
||
169 | |||
170 | /** |
||
171 | * @param CsvTypeRepository $csvTypeRepository |
||
172 | */ |
||
173 | public function setCsvTypeRepository(CsvTypeRepository $csvTypeRepository) |
||
177 | |||
178 | /** |
||
179 | * @param OrderRepository $orderRepository |
||
180 | */ |
||
181 | public function setOrderRepository(OrderRepository $orderRepository) |
||
185 | |||
186 | /** |
||
187 | * @param CustomerRepository $customerRepository |
||
188 | */ |
||
189 | public function setCustomerRepository(CustomerRepository $customerRepository) |
||
193 | |||
194 | /** |
||
195 | * @param ProductRepository $productRepository |
||
196 | */ |
||
197 | public function setProductRepository(ProductRepository $productRepository) |
||
201 | |||
202 | /** |
||
203 | * @param EntityManagerInterface $entityManager |
||
204 | */ |
||
205 | public function setEntityManager(EntityManagerInterface $entityManager) |
||
209 | |||
210 | /** |
||
211 | 4 | * @return EntityManagerInterface |
|
212 | */ |
||
213 | 4 | public function getEntityManager() |
|
217 | |||
218 | /** |
||
219 | * @param QueryBuilder $qb |
||
220 | */ |
||
221 | 5 | public function setExportQueryBuilder(QueryBuilder $qb) |
|
225 | |||
226 | 5 | /** |
|
227 | * Csv種別からServiceの初期化を行う. |
||
228 | * |
||
229 | * @param $CsvType|integer |
||
230 | 5 | */ |
|
231 | public function initCsvType($CsvType) |
||
248 | |||
249 | /** |
||
250 | * @return Csv[] |
||
251 | 4 | */ |
|
252 | public function getCsvs() |
||
256 | |||
257 | 4 | /** |
|
258 | 4 | * ヘッダ行を出力する. |
|
259 | 4 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
|
260 | */ |
||
261 | public function exportHeader() |
||
276 | |||
277 | /** |
||
278 | * クエリビルダにもとづいてデータ行を出力する. |
||
279 | 4 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
|
280 | * |
||
281 | 4 | * @param \Closure $closure |
|
282 | 4 | */ |
|
283 | 4 | public function exportData(\Closure $closure) |
|
309 | |||
310 | 4 | /** |
|
311 | 2 | * CSV出力項目と比較し, 合致するデータを返す. |
|
312 | * |
||
313 | * @param \Eccube\Entity\Csv $Csv |
||
314 | * @param $entity |
||
315 | 4 | * |
|
316 | * @return string|null |
||
317 | */ |
||
318 | 4 | public function getData(Csv $Csv, $entity) |
|
354 | 5 | ||
355 | /** |
||
356 | 5 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
|
357 | * |
||
358 | * @return \Closure |
||
359 | 5 | */ |
|
360 | public function getConvertEncodingCallback() |
||
370 | |||
371 | 5 | public function fopen() |
|
377 | |||
378 | 5 | /** |
|
379 | * @param $row |
||
380 | 5 | */ |
|
381 | 5 | public function fputcsv($row) |
|
389 | |||
390 | public function fclose() |
||
397 | 1 | ||
398 | 1 | /** |
|
399 | * 受注検索用のクエリビルダを返す. |
||
400 | 1 | * |
|
401 | 1 | * @param Request $request |
|
402 | * |
||
403 | * @return \Doctrine\ORM\QueryBuilder |
||
404 | 1 | */ |
|
405 | 1 | View Code Duplication | public function getOrderQueryBuilder(Request $request) |
421 | 1 | ||
422 | 1 | /** |
|
423 | * 会員検索用のクエリビルダを返す. |
||
424 | 1 | * |
|
425 | 1 | * @param Request $request |
|
426 | * |
||
427 | * @return \Doctrine\ORM\QueryBuilder |
||
428 | 1 | */ |
|
429 | 1 | View Code Duplication | public function getCustomerQueryBuilder(Request $request) |
445 | |||
446 | /** |
||
447 | * 商品検索用のクエリビルダを返す. |
||
448 | * |
||
449 | * @param Request $request |
||
450 | * |
||
451 | * @return \Doctrine\ORM\QueryBuilder |
||
452 | */ |
||
453 | View Code Duplication | public function getProductQueryBuilder(Request $request) |
|
469 | } |
||
470 |