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 |
||
34 | class CsvExportService |
||
|
|||
35 | { |
||
36 | /** |
||
37 | * @var resource |
||
38 | */ |
||
39 | protected $fp; |
||
40 | |||
41 | /** |
||
42 | * @var boolean |
||
43 | */ |
||
44 | protected $closed = false; |
||
45 | |||
46 | /** |
||
47 | * @var \Closure |
||
48 | */ |
||
49 | protected $convertEncodingCallBack; |
||
50 | |||
51 | /** |
||
52 | * @var EntityManagerInterface |
||
53 | */ |
||
54 | protected $entityManager; |
||
55 | |||
56 | /** |
||
57 | * @var QueryBuilder; |
||
58 | */ |
||
59 | protected $qb; |
||
60 | |||
61 | /** |
||
62 | * @var EccubeConfig |
||
63 | */ |
||
64 | protected $eccubeConfig; |
||
65 | |||
66 | /** |
||
67 | * @var CsvType |
||
68 | */ |
||
69 | protected $CsvType; |
||
70 | |||
71 | /** |
||
72 | * @var Csv[] |
||
73 | */ |
||
74 | protected $Csvs; |
||
75 | |||
76 | /** |
||
77 | * @var CsvRepository |
||
78 | */ |
||
79 | protected $csvRepository; |
||
80 | |||
81 | /** |
||
82 | * @var CsvTypeRepository |
||
83 | */ |
||
84 | protected $csvTypeRepository; |
||
85 | |||
86 | /** |
||
87 | * @var OrderRepository |
||
88 | */ |
||
89 | protected $orderRepository; |
||
90 | |||
91 | /** |
||
92 | * @var ShippingRepository |
||
93 | */ |
||
94 | protected $shippingRepository; |
||
95 | |||
96 | /** |
||
97 | * @var CustomerRepository |
||
98 | */ |
||
99 | protected $customerRepository; |
||
100 | |||
101 | /** |
||
102 | * @var ProductRepository |
||
103 | */ |
||
104 | protected $productRepository; |
||
105 | |||
106 | /** |
||
107 | * @var FormFactoryInterface |
||
108 | */ |
||
109 | protected $formFactory; |
||
110 | |||
111 | /** |
||
112 | * CsvExportService constructor. |
||
113 | * |
||
114 | * @param EntityManagerInterface $entityManager |
||
115 | * @param CsvRepository $csvRepository |
||
116 | * @param CsvTypeRepository $csvTypeRepository |
||
117 | * @param OrderRepository $orderRepository |
||
118 | * @param CustomerRepository $customerRepository |
||
119 | * @param EccubeConfig $eccubeConfig |
||
120 | */ |
||
121 | 60 | public function __construct( |
|
142 | |||
143 | /** |
||
144 | * @param $config |
||
145 | */ |
||
146 | public function setConfig($config) |
||
150 | |||
151 | /** |
||
152 | * @param CsvRepository $csvRepository |
||
153 | */ |
||
154 | public function setCsvRepository(CsvRepository $csvRepository) |
||
158 | |||
159 | /** |
||
160 | * @param CsvTypeRepository $csvTypeRepository |
||
161 | */ |
||
162 | public function setCsvTypeRepository(CsvTypeRepository $csvTypeRepository) |
||
166 | |||
167 | /** |
||
168 | * @param OrderRepository $orderRepository |
||
169 | */ |
||
170 | public function setOrderRepository(OrderRepository $orderRepository) |
||
174 | |||
175 | /** |
||
176 | * @param CustomerRepository $customerRepository |
||
177 | */ |
||
178 | public function setCustomerRepository(CustomerRepository $customerRepository) |
||
182 | |||
183 | /** |
||
184 | * @param ProductRepository $productRepository |
||
185 | */ |
||
186 | public function setProductRepository(ProductRepository $productRepository) |
||
190 | |||
191 | /** |
||
192 | * @param EntityManagerInterface $entityManager |
||
193 | */ |
||
194 | public function setEntityManager(EntityManagerInterface $entityManager) |
||
198 | |||
199 | /** |
||
200 | * @return EntityManagerInterface |
||
201 | */ |
||
202 | public function getEntityManager() |
||
206 | |||
207 | /** |
||
208 | * @param QueryBuilder $qb |
||
209 | */ |
||
210 | public function setExportQueryBuilder(QueryBuilder $qb) |
||
214 | |||
215 | /** |
||
216 | * Csv種別からServiceの初期化を行う. |
||
217 | * |
||
218 | * @param $CsvType|integer |
||
219 | */ |
||
220 | 3 | public function initCsvType($CsvType) |
|
237 | |||
238 | /** |
||
239 | * @return Csv[] |
||
240 | */ |
||
241 | public function getCsvs() |
||
245 | |||
246 | /** |
||
247 | * ヘッダ行を出力する. |
||
248 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
||
249 | */ |
||
250 | public function exportHeader() |
||
265 | |||
266 | /** |
||
267 | * クエリビルダにもとづいてデータ行を出力する. |
||
268 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
||
269 | * |
||
270 | * @param \Closure $closure |
||
271 | */ |
||
272 | public function exportData(\Closure $closure) |
||
290 | |||
291 | /** |
||
292 | * CSV出力項目と比較し, 合致するデータを返す. |
||
293 | * |
||
294 | * @param \Eccube\Entity\Csv $Csv |
||
295 | * @param $entity |
||
296 | * |
||
297 | * @return mixed|null|string|void |
||
298 | */ |
||
299 | public function getData(Csv $Csv, $entity) |
||
341 | |||
342 | /** |
||
343 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
||
344 | * |
||
345 | * @return \Closure |
||
346 | */ |
||
347 | public function getConvertEncodhingCallback() |
||
357 | |||
358 | public function fopen() |
||
364 | |||
365 | /** |
||
366 | * @param $row |
||
367 | * @param null $callback |
||
368 | */ |
||
369 | public function fputcsv($row) |
||
377 | |||
378 | public function fclose() |
||
385 | |||
386 | /** |
||
387 | * 受注検索用のクエリビルダを返す. |
||
388 | * |
||
389 | * @param Request $request |
||
390 | * |
||
391 | * @return \Doctrine\ORM\QueryBuilder |
||
392 | */ |
||
393 | View Code Duplication | public function getOrderQueryBuilder(Request $request) |
|
409 | |||
410 | /** |
||
411 | * 会員検索用のクエリビルダを返す. |
||
412 | * |
||
413 | * @param Request $request |
||
414 | * |
||
415 | * @return \Doctrine\ORM\QueryBuilder |
||
416 | */ |
||
417 | View Code Duplication | public function getCustomerQueryBuilder(Request $request) |
|
433 | |||
434 | /** |
||
435 | * 商品検索用のクエリビルダを返す. |
||
436 | * |
||
437 | * @param Request $request |
||
438 | * |
||
439 | * @return \Doctrine\ORM\QueryBuilder |
||
440 | */ |
||
441 | View Code Duplication | public function getProductQueryBuilder(Request $request) |
|
457 | } |
||
458 |