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:
1 | <?php |
||
23 | View Code Duplication | class ListFilesOfIdsHandler |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * The file data transformer. |
||
27 | * |
||
28 | * @var FileDataTransformer |
||
29 | */ |
||
30 | private $dataTransformer; |
||
31 | |||
32 | /** |
||
33 | * The file repository. |
||
34 | * |
||
35 | * @var FileRepository |
||
36 | */ |
||
37 | private $repository; |
||
38 | |||
39 | /** |
||
40 | * The file specification factory. |
||
41 | * |
||
42 | * @var FileSpecificationFactory |
||
43 | */ |
||
44 | private $specificationFactory; |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * |
||
49 | * @param FileRepository $aRepository The file repository |
||
50 | * @param FileSpecificationFactory $fileSpecificationFactory The file specification factory |
||
51 | * @param FileDataTransformer $aDataTransformer The file data transformer |
||
52 | */ |
||
53 | public function __construct( |
||
62 | |||
63 | /** |
||
64 | * Handles the given query. |
||
65 | * |
||
66 | * @param ListFilesOfIdsQuery $aQuery The query |
||
67 | * |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function __invoke(ListFilesOfIdsQuery $aQuery) |
||
88 | } |
||
89 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.