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 |
||
21 | class DatatableService |
||
22 | { |
||
23 | /** @var Provider */ |
||
24 | private $provider; |
||
25 | |||
26 | /** @var ColumnConfiguration[] */ |
||
27 | private $columnConfigurations; |
||
28 | |||
29 | /** @var VersionEngine */ |
||
30 | private $versionEngine; |
||
31 | |||
32 | /** @var Factory */ |
||
33 | private $viewFactory; |
||
34 | |||
35 | /** @var Repository */ |
||
36 | private $configRepository; |
||
37 | |||
38 | /** |
||
39 | * DatatableService constructor. |
||
40 | * @param Provider $provider The provider that will prepare the data |
||
41 | * @param ColumnConfiguration[] $columnConfigurations |
||
42 | * @param VersionEngine $versionEngine |
||
43 | * @param Factory $viewFactory The factory to render the views |
||
44 | * @param Repository $configRepository The repository to get the config values from |
||
45 | */ |
||
46 | View Code Duplication | public function __construct( |
|
59 | |||
60 | /** |
||
61 | * @param Version $version The version that should be used to generate the view and the responses |
||
62 | */ |
||
63 | public function setVersion(Version $version) |
||
67 | |||
68 | /** |
||
69 | * @return bool True if any version should handle the current request |
||
70 | */ |
||
71 | public function shouldHandle() |
||
75 | |||
76 | /** |
||
77 | * Prepare a request for processing (without doing the actual datatable). |
||
78 | * |
||
79 | * @return Version |
||
80 | */ |
||
81 | public function prepareRequest() |
||
92 | |||
93 | /** |
||
94 | * Will handle the current request and returns the correct response |
||
95 | * @return JsonResponse the response that should be returned to the client. |
||
96 | */ |
||
97 | public function handleRequest() |
||
106 | |||
107 | /** |
||
108 | * @param string $tableView the view to use or null if the standard view should be used for the table and the script |
||
109 | * @param string $scriptView the view to use or null if the standard view should be used for the table and the script |
||
110 | * @return DatatableView |
||
111 | */ |
||
112 | View Code Duplication | public function view($tableView = null, $scriptView = null) |
|
124 | } |
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.