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 |
||
| 45 | class CsvExportService |
||
|
|
|||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var resource |
||
| 49 | */ |
||
| 50 | protected $fp; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var boolean |
||
| 54 | */ |
||
| 55 | protected $closed = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \Closure |
||
| 59 | */ |
||
| 60 | protected $convertEncodingCallBack; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var EntityManagerInterface |
||
| 64 | */ |
||
| 65 | protected $entityManager; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var QueryBuilder; |
||
| 69 | */ |
||
| 70 | protected $qb; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var EccubeConfig |
||
| 74 | */ |
||
| 75 | protected $eccubeConfig; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var CsvType |
||
| 79 | */ |
||
| 80 | protected $CsvType; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var Csv[] |
||
| 84 | */ |
||
| 85 | protected $Csvs; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var CsvRepository |
||
| 89 | */ |
||
| 90 | protected $csvRepository; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var CsvTypeRepository |
||
| 94 | */ |
||
| 95 | protected $csvTypeRepository; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var OrderRepository |
||
| 99 | */ |
||
| 100 | protected $orderRepository; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var ShippingRepository |
||
| 104 | */ |
||
| 105 | protected $shippingRepository; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var CustomerRepository |
||
| 109 | */ |
||
| 110 | protected $customerRepository; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var ProductRepository |
||
| 114 | */ |
||
| 115 | protected $productRepository; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var FormFactoryInterface |
||
| 119 | */ |
||
| 120 | protected $formFactory; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * CsvExportService constructor. |
||
| 124 | * |
||
| 125 | * @param EntityManagerInterface $entityManager |
||
| 126 | * @param CsvRepository $csvRepository |
||
| 127 | * @param CsvTypeRepository $csvTypeRepository |
||
| 128 | * @param OrderRepository $orderRepository |
||
| 129 | * @param CustomerRepository $customerRepository |
||
| 130 | * @param EccubeConfig $eccubeConfig |
||
| 131 | */ |
||
| 132 | 61 | 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 | * @return EntityManagerInterface |
||
| 212 | */ |
||
| 213 | 1 | public function getEntityManager() |
|
| 214 | { |
||
| 215 | 1 | return $this->entityManager; |
|
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param QueryBuilder $qb |
||
| 220 | */ |
||
| 221 | 5 | public function setExportQueryBuilder(QueryBuilder $qb) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Csv種別からServiceの初期化を行う. |
||
| 228 | * |
||
| 229 | * @param $CsvType|integer |
||
| 230 | */ |
||
| 231 | 6 | public function initCsvType($CsvType) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @return Csv[] |
||
| 251 | */ |
||
| 252 | 5 | public function getCsvs() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * ヘッダ行を出力する. |
||
| 259 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
||
| 260 | */ |
||
| 261 | 5 | public function exportHeader() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * クエリビルダにもとづいてデータ行を出力する. |
||
| 279 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
||
| 280 | * |
||
| 281 | * @param \Closure $closure |
||
| 282 | */ |
||
| 283 | 5 | public function exportData(\Closure $closure) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * CSV出力項目と比較し, 合致するデータを返す. |
||
| 304 | * |
||
| 305 | * @param \Eccube\Entity\Csv $Csv |
||
| 306 | * @param $entity |
||
| 307 | * |
||
| 308 | * @return mixed|null|string|void |
||
| 309 | */ |
||
| 310 | 5 | public function getData(Csv $Csv, $entity) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
||
| 355 | * |
||
| 356 | * @return \Closure |
||
| 357 | */ |
||
| 358 | 6 | public function getConvertEncodhingCallback() |
|
| 368 | |||
| 369 | 6 | public function fopen() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param $row |
||
| 378 | * @param null $callback |
||
| 379 | */ |
||
| 380 | 6 | public function fputcsv($row) |
|
| 388 | |||
| 389 | 6 | public function fclose() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * 受注検索用のクエリビルダを返す. |
||
| 399 | * |
||
| 400 | * @param Request $request |
||
| 401 | * |
||
| 402 | * @return \Doctrine\ORM\QueryBuilder |
||
| 403 | */ |
||
| 404 | 1 | View Code Duplication | public function getOrderQueryBuilder(Request $request) |
| 405 | { |
||
| 406 | 1 | $session = $request->getSession(); |
|
| 407 | 1 | if ($session->has('eccube.admin.order.search')) { |
|
| 408 | 1 | $searchData = $session->get('eccube.admin.order.search'); |
|
| 409 | 1 | $this->findDeserializeObjects($searchData); |
|
| 410 | } else { |
||
| 411 | $searchData = []; |
||
| 412 | } |
||
| 413 | |||
| 414 | // 受注データのクエリビルダを構築. |
||
| 415 | 1 | $qb = $this->orderRepository |
|
| 416 | 1 | ->getQueryBuilderBySearchDataForAdmin($searchData); |
|
| 417 | |||
| 418 | 1 | return $qb; |
|
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * 出荷検索用のクエリビルダを返す. |
||
| 423 | * |
||
| 424 | * @param Request $request |
||
| 425 | * |
||
| 426 | * @return \Doctrine\ORM\QueryBuilder |
||
| 427 | */ |
||
| 428 | 1 | public function getShippingQueryBuilder(Request $request) |
|
| 429 | { |
||
| 430 | 1 | $session = $request->getSession(); |
|
| 431 | 1 | $form = $this->formFactory->create(SearchShippingType::class); |
|
| 432 | 1 | $searchData = FormUtil::submitAndGetData($form, $session->get('eccube.admin.shipping.search', [])); |
|
| 433 | |||
| 434 | // 出荷データのクエリビルダを構築. |
||
| 435 | 1 | $qb = $this->shippingRepository |
|
| 436 | 1 | ->getQueryBuilderBySearchDataForAdmin($searchData); |
|
| 437 | |||
| 438 | 1 | return $qb; |
|
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * 会員検索用のクエリビルダを返す. |
||
| 443 | * |
||
| 444 | * @param Request $request |
||
| 445 | * |
||
| 446 | * @return \Doctrine\ORM\QueryBuilder |
||
| 447 | */ |
||
| 448 | 1 | View Code Duplication | public function getCustomerQueryBuilder(Request $request) |
| 464 | |||
| 465 | /** |
||
| 466 | * 商品検索用のクエリビルダを返す. |
||
| 467 | * |
||
| 468 | * @param Request $request |
||
| 469 | * |
||
| 470 | * @return \Doctrine\ORM\QueryBuilder |
||
| 471 | */ |
||
| 472 | View Code Duplication | public function getProductQueryBuilder(Request $request) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * セッションでシリアライズされた Doctrine のオブジェクトを取得し直す. |
||
| 490 | * |
||
| 491 | * XXX self::setExportQueryBuilder() をコールする前に EntityManager を取得したいので、引数で渡している |
||
| 492 | * |
||
| 493 | * @param array $searchData セッションから取得した検索条件の配列 |
||
| 494 | */ |
||
| 495 | 1 | protected function findDeserializeObjects(array &$searchData) |
|
| 512 | } |
||
| 513 |