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 ExcelDispatchJobService 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 ExcelDispatchJobService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ExcelDispatchJobService extends RenderableComponent |
||
| 35 | { |
||
| 36 | const NAME = 'excel_export'; |
||
| 37 | const INPUT_PARAM = 'xlsx'; |
||
| 38 | const DEFAULT_ROWS_LIMIT = 50000; |
||
| 39 | |||
| 40 | protected $template = '*.components.excel_export'; |
||
| 41 | protected $name = ExcelDispatchJobService::NAME; |
||
| 42 | protected $render_section = RenderableRegistry::SECTION_END; |
||
| 43 | protected $rows_limit = self::DEFAULT_ROWS_LIMIT; |
||
| 44 | protected $extension = 'xlsx'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $output; |
||
| 50 | |||
| 51 | protected $data; |
||
| 52 | |||
| 53 | protected $userId; |
||
| 54 | |||
| 55 | protected $className; |
||
| 56 | |||
| 57 | protected $config; |
||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $fileName; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $sheetName; |
||
| 70 | |||
| 71 | protected $ignored_columns = []; |
||
| 72 | |||
| 73 | protected $is_hidden_columns_exported = false; |
||
| 74 | |||
| 75 | protected $on_file_create; |
||
| 76 | |||
| 77 | protected $on_sheet_create; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return mixed |
||
| 81 | */ |
||
| 82 | |||
| 83 | |||
| 84 | public function getConfig() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param mixed $config |
||
| 91 | */ |
||
| 92 | public function setConfig($config) |
||
| 97 | /** |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | public function getClassName() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param mixed $config |
||
|
|
|||
| 107 | */ |
||
| 108 | public function setClassName($className) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public function getUserId() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param mixed $config |
||
| 124 | */ |
||
| 125 | public function setUserId($userId) |
||
| 130 | |||
| 131 | public function __construct($userId, $showId, $sheetName, $extension, $ignored_columns, $is_hidden_columns_exported, $rows_limit) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param Grid $grid |
||
| 162 | * @return null|void |
||
| 163 | */ |
||
| 164 | public function initialize(Grid $grid) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Sets name of exported file. |
||
| 171 | * |
||
| 172 | * @param string $name |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function setFileName($name) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns name of exported file. |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getFileName() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $name |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function setSheetName($name) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getSheetName() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $name |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setExtension($name) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function getExtension() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return int |
||
| 229 | */ |
||
| 230 | public function getRowsLimit() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param int $limit |
||
| 237 | * |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | public function setRowsLimit($limit = 50000) |
||
| 245 | |||
| 246 | View Code Duplication | protected function resetPagination(DataProvider $provider) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param FieldConfig $column |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | protected function isColumnExported(FieldConfig $column) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @internal |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function getData() |
||
| 297 | |||
| 298 | protected function renderExcel() |
||
| 306 | |||
| 307 | protected function escapeString($str) |
||
| 311 | |||
| 312 | protected function getHeaderRow() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return string[] |
||
| 325 | */ |
||
| 326 | public function getIgnoredColumns() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string[] $ignoredColumns |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function setIgnoredColumns(array $ignoredColumns) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return boolean |
||
| 343 | */ |
||
| 344 | public function isHiddenColumnsExported() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param bool $isHiddenColumnsExported |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function setHiddenColumnsExported($isHiddenColumnsExported) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | public function getOnFileCreate() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param callable $onFileCreate |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | public function setOnFileCreate($onFileCreate) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return callable |
||
| 386 | */ |
||
| 387 | public function getOnSheetCreate() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param callable $onSheetCreate |
||
| 410 | * |
||
| 411 | * @return $this |
||
| 412 | */ |
||
| 413 | public function setOnSheetCreate($onSheetCreate) |
||
| 418 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.