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 |
||
| 33 | class CsvExportService |
||
|
|
|||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var |
||
| 37 | */ |
||
| 38 | protected $fp; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var |
||
| 42 | */ |
||
| 43 | protected $closed = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \Closure |
||
| 47 | */ |
||
| 48 | protected $convertEncodingCallBack; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \Doctrine\ORM\EntityManager |
||
| 52 | */ |
||
| 53 | protected $em; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \Doctrine\ORM\QueryBuilder; |
||
| 57 | */ |
||
| 58 | protected $qb; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $config; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \Eccube\Entity\Master\CsvType |
||
| 67 | */ |
||
| 68 | protected $CsvType; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var \Eccube\Entity\Csv[] |
||
| 72 | */ |
||
| 73 | protected $Csvs; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var \Eccube\Repository\CsvRepository |
||
| 77 | */ |
||
| 78 | protected $csvRepository; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var \Eccube\Repository\Master\CsvTypeRepository |
||
| 82 | */ |
||
| 83 | protected $csvTypeRepository; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \Eccube\Repository\OrderRepository |
||
| 87 | */ |
||
| 88 | protected $orderRepository; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var \Eccube\Repository\CustomerRepository |
||
| 92 | */ |
||
| 93 | protected $customerRepository; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var \Eccube\Repository\ProductRepository |
||
| 97 | */ |
||
| 98 | protected $productRepository; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param $config |
||
| 102 | */ |
||
| 103 | 6 | public function setConfig($config) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @param \Eccube\Repository\CsvRepository $csvRepository |
||
| 110 | */ |
||
| 111 | 6 | public function setCsvRepository(\Eccube\Repository\CsvRepository $csvRepository) |
|
| 112 | { |
||
| 113 | 6 | $this->csvRepository = $csvRepository; |
|
| 114 | 6 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param \Eccube\Repository\Master\CsvTypeRepository $csvTypeRepository |
||
| 118 | */ |
||
| 119 | 6 | public function setCsvTypeRepository(\Eccube\Repository\Master\CsvTypeRepository $csvTypeRepository) |
|
| 120 | { |
||
| 121 | 6 | $this->csvTypeRepository = $csvTypeRepository; |
|
| 122 | 6 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param \Eccube\Repository\OrderRepository $orderRepository |
||
| 126 | */ |
||
| 127 | 6 | public function setOrderRepository(\Eccube\Repository\OrderRepository $orderRepository) |
|
| 128 | { |
||
| 129 | 6 | $this->orderRepository = $orderRepository; |
|
| 130 | 6 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param \Eccube\Repository\CustomerRepository $customerRepository |
||
| 134 | */ |
||
| 135 | 6 | public function setCustomerRepository(\Eccube\Repository\CustomerRepository $customerRepository) |
|
| 136 | { |
||
| 137 | 6 | $this->customerRepository = $customerRepository; |
|
| 138 | 6 | } |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param \Eccube\Repository\ProductRepository $productRepository |
||
| 142 | */ |
||
| 143 | 6 | public function setProductRepository(\Eccube\Repository\ProductRepository $productRepository) |
|
| 144 | { |
||
| 145 | 6 | $this->productRepository = $productRepository; |
|
| 146 | 6 | } |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @param \Doctrine\ORM\EntityManager $em |
||
| 150 | */ |
||
| 151 | 6 | public function setEntityManager(\Doctrine\ORM\EntityManager $em) |
|
| 152 | { |
||
| 153 | 6 | $this->em = $em; |
|
| 154 | 6 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return \Doctrine\ORM\EntityManager |
||
| 158 | */ |
||
| 159 | 2 | public function getEntityManager() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param \Doctrine\ORM\QueryBuilder $qb |
||
| 166 | */ |
||
| 167 | 5 | public function setExportQueryBuilder(\Doctrine\ORM\QueryBuilder $qb) |
|
| 168 | { |
||
| 169 | 5 | $this->qb = $qb; |
|
| 170 | 5 | } |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Csv種別からServiceの初期化を行う. |
||
| 174 | * |
||
| 175 | * @param $CsvType|integer |
||
| 176 | */ |
||
| 177 | 6 | public function initCsvType($CsvType) |
|
| 178 | { |
||
| 179 | 6 | if ($CsvType instanceof \Eccube\Entity\Master\CsvType) { |
|
| 180 | $this->CsvType = $CsvType; |
||
| 181 | } else { |
||
| 182 | 6 | $this->CsvType = $this->csvTypeRepository->find($CsvType); |
|
| 183 | } |
||
| 184 | |||
| 185 | $criteria = array( |
||
| 186 | 6 | 'CsvType' => $CsvType, |
|
| 187 | 'enable_flg' => Constant::ENABLED |
||
| 188 | 6 | ); |
|
| 189 | $orderBy = array( |
||
| 190 | 'rank' => 'ASC' |
||
| 191 | 6 | ); |
|
| 192 | 6 | $this->Csvs = $this->csvRepository->findBy($criteria, $orderBy); |
|
| 193 | 6 | } |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @return \Eccube\Entity\Csv[] |
||
| 197 | */ |
||
| 198 | 5 | public function getCsvs() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * ヘッダ行を出力する. |
||
| 205 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
||
| 206 | */ |
||
| 207 | 5 | public function exportHeader() |
|
| 208 | { |
||
| 209 | 5 | if (is_null($this->CsvType) || is_null($this->Csvs)) { |
|
| 210 | throw new \LogicException('init csv type incomplete.'); |
||
| 222 | |||
| 223 | /** |
||
| 224 | * クエリビルダにもとづいてデータ行を出力する. |
||
| 225 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
||
| 226 | * |
||
| 227 | * @param \Closure $closure |
||
| 228 | */ |
||
| 229 | 5 | 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 | 5 | public function getData(\Eccube\Entity\Csv $Csv, $entity) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
||
| 305 | * |
||
| 306 | * @return \Closure |
||
| 307 | */ |
||
| 308 | 6 | public function getConvertEncodhingCallback() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * |
||
| 321 | */ |
||
| 322 | 6 | public function fopen() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param $row |
||
| 331 | * @param null $callback |
||
| 332 | */ |
||
| 333 | 6 | public function fputcsv($row) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * |
||
| 344 | */ |
||
| 345 | 6 | public function fclose() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * 受注検索用のクエリビルダを返す. |
||
| 355 | * |
||
| 356 | * @param Request $request |
||
| 357 | * @return \Doctrine\ORM\QueryBuilder |
||
| 358 | */ |
||
| 359 | 2 | View Code Duplication | public function getOrderQueryBuilder(Request $request) |
| 375 | |||
| 376 | /** |
||
| 377 | * 会員検索用のクエリビルダを返す. |
||
| 378 | * |
||
| 379 | * @param Request $request |
||
| 380 | * @return \Doctrine\ORM\QueryBuilder |
||
| 381 | */ |
||
| 382 | 1 | View Code Duplication | public function getCustomerQueryBuilder(Request $request) |
| 398 | |||
| 399 | /** |
||
| 400 | * 商品検索用のクエリビルダを返す. |
||
| 401 | * |
||
| 402 | * @param Request $request |
||
| 403 | * @return \Doctrine\ORM\QueryBuilder |
||
| 404 | */ |
||
| 405 | View Code Duplication | public function getProductQueryBuilder(Request $request) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * セッションでシリアライズされた Doctrine のオブジェクトを取得し直す. |
||
| 424 | * |
||
| 425 | * XXX self::setExportQueryBuilder() をコールする前に EntityManager を取得したいので、引数で渡している |
||
| 426 | * |
||
| 427 | * @param array $searchData セッションから取得した検索条件の配列 |
||
| 428 | * @param EntityManager $em |
||
| 429 | */ |
||
| 430 | 2 | protected function findDeserializeObjects(array &$searchData) |
|
| 447 | } |
||
| 448 |