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 | /** |
||
| 114 | * CsvExportService constructor. |
||
| 115 | * |
||
| 116 | * @param EntityManagerInterface $entityManager |
||
| 117 | * @param CsvRepository $csvRepository |
||
| 118 | * @param CsvTypeRepository $csvTypeRepository |
||
| 119 | * @param OrderRepository $orderRepository |
||
| 120 | * @param CustomerRepository $customerRepository |
||
| 121 | * @param EccubeConfig $eccubeConfig |
||
| 122 | 76 | */ |
|
| 123 | public function __construct( |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param $config |
||
| 147 | */ |
||
| 148 | public function setConfig($config) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param CsvRepository $csvRepository |
||
| 155 | */ |
||
| 156 | public function setCsvRepository(CsvRepository $csvRepository) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param CsvTypeRepository $csvTypeRepository |
||
| 163 | */ |
||
| 164 | public function setCsvTypeRepository(CsvTypeRepository $csvTypeRepository) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param OrderRepository $orderRepository |
||
| 171 | */ |
||
| 172 | public function setOrderRepository(OrderRepository $orderRepository) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param CustomerRepository $customerRepository |
||
| 179 | */ |
||
| 180 | public function setCustomerRepository(CustomerRepository $customerRepository) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param ProductRepository $productRepository |
||
| 187 | */ |
||
| 188 | public function setProductRepository(ProductRepository $productRepository) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param EntityManagerInterface $entityManager |
||
| 195 | */ |
||
| 196 | public function setEntityManager(EntityManagerInterface $entityManager) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return EntityManagerInterface |
||
| 203 | */ |
||
| 204 | public function getEntityManager() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param QueryBuilder $qb |
||
| 211 | 4 | */ |
|
| 212 | public function setExportQueryBuilder(QueryBuilder $qb) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Csv種別からServiceの初期化を行う. |
||
| 219 | * |
||
| 220 | * @param $CsvType|integer |
||
| 221 | 5 | */ |
|
| 222 | public function initCsvType($CsvType) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return Csv[] |
||
| 242 | 4 | */ |
|
| 243 | public function getCsvs() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * ヘッダ行を出力する. |
||
| 250 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
||
| 251 | 4 | */ |
|
| 252 | public function exportHeader() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * クエリビルダにもとづいてデータ行を出力する. |
||
| 270 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
||
| 271 | * |
||
| 272 | * @param \Closure $closure |
||
| 273 | 4 | */ |
|
| 274 | public function exportData(\Closure $closure) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * CSV出力項目と比較し, 合致するデータを返す. |
||
| 295 | * |
||
| 296 | * @param \Eccube\Entity\Csv $Csv |
||
| 297 | * @param $entity |
||
| 298 | * |
||
| 299 | * @return string|null |
||
| 300 | 4 | */ |
|
| 301 | public function getData(Csv $Csv, $entity) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
||
| 346 | * |
||
| 347 | * @return \Closure |
||
| 348 | 5 | */ |
|
| 349 | public function getConvertEncodingCallback() |
||
| 350 | 5 | { |
|
| 351 | $config = $this->eccubeConfig; |
||
| 352 | 5 | ||
| 353 | 5 | return function ($value) use ($config) { |
|
| 354 | 5 | return mb_convert_encoding( |
|
| 355 | (string) $value, $config['eccube_csv_export_encoding'], 'UTF-8' |
||
| 356 | 5 | ); |
|
| 357 | }; |
||
| 358 | } |
||
| 359 | 5 | ||
| 360 | public function fopen() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param $row |
||
| 369 | 5 | */ |
|
| 370 | public function fputcsv($row) |
||
| 378 | 5 | ||
| 379 | public function fclose() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * 受注検索用のクエリビルダを返す. |
||
| 389 | * |
||
| 390 | * @param Request $request |
||
| 391 | * |
||
| 392 | * @return \Doctrine\ORM\QueryBuilder |
||
| 393 | 1 | */ |
|
| 394 | public function getOrderQueryBuilder(Request $request) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * 会員検索用のクエリビルダを返す. |
||
| 413 | * |
||
| 414 | * @param Request $request |
||
| 415 | * |
||
| 416 | * @return \Doctrine\ORM\QueryBuilder |
||
| 417 | 1 | */ |
|
| 418 | public function getCustomerQueryBuilder(Request $request) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * 商品検索用のクエリビルダを返す. |
||
| 437 | * |
||
| 438 | * @param Request $request |
||
| 439 | * |
||
| 440 | * @return \Doctrine\ORM\QueryBuilder |
||
| 441 | */ |
||
| 442 | public function getProductQueryBuilder(Request $request) |
||
| 458 | } |
||
| 459 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.