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 |
||
22 | class ColumnComposer |
||
23 | { |
||
24 | /** |
||
25 | * @var Provider The Provider for the underlying data. |
||
26 | */ |
||
27 | private $provider; |
||
28 | |||
29 | /** |
||
30 | * @var VersionEngine The version engine that will parse the request parameter. |
||
31 | */ |
||
32 | private $version; |
||
33 | |||
34 | /** |
||
35 | * @var ColumnConfiguration[] An array of the configurations of the columns |
||
36 | */ |
||
37 | private $columnConfiguration = []; |
||
38 | |||
39 | /** |
||
40 | * @var Factory |
||
41 | */ |
||
42 | private $viewFactory; |
||
43 | |||
44 | /** |
||
45 | * @var Repository |
||
46 | */ |
||
47 | private $configRepository; |
||
48 | |||
49 | /** |
||
50 | * Will create a new datatable composer instance with the given provider |
||
51 | * @param Provider $provider the provider that will process the underlying data |
||
52 | * @param VersionEngine $versionEngine The version engine to handle the request data |
||
53 | * @param Factory $viewFactory The factory providing the views |
||
54 | * @param Repository $configRepository The repository providing the user defined options |
||
55 | */ |
||
56 | View Code Duplication | public function __construct( |
|
|
|||
57 | Provider $provider, |
||
58 | VersionEngine $versionEngine, |
||
59 | Factory $viewFactory, |
||
60 | Repository $configRepository |
||
61 | ) { |
||
62 | $this->provider = $provider; |
||
63 | $this->version = $versionEngine; |
||
64 | $this->viewFactory = $viewFactory; |
||
65 | $this->configRepository = $configRepository; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Will return the Provider for the underlying data. |
||
70 | * @return Provider |
||
71 | */ |
||
72 | public function getProvider() |
||
76 | |||
77 | /** |
||
78 | * Will return the internal column configurations that are registered with the current composer. |
||
79 | * |
||
80 | * @return ColumnConfiguration[] |
||
81 | */ |
||
82 | public function getColumnConfiguration() |
||
86 | |||
87 | /** |
||
88 | * Will create a new ColumnConfiguration with all defaults but allows overriding of all properties through the method. |
||
89 | * |
||
90 | * @param string $name The name of the configuration, required for the configuration |
||
91 | * @param string|callable $callable The function to execute, defaults to null which means the default will be set. |
||
92 | * @param Searchable $searchable If the column should be searchable or not |
||
93 | * @param Orderable $orderable If the column should be orderable or not |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function column($name, $callable = null, Searchable $searchable = null, Orderable $orderable = null) |
||
124 | |||
125 | /** |
||
126 | * This method will add the given ColumnConfiguration to the composer. |
||
127 | * |
||
128 | * @param ColumnConfiguration $configuration the configuration to add to the composer |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function add(ColumnConfiguration $configuration) |
||
137 | |||
138 | /** |
||
139 | * @return DatatableService Will return the fully built DatatableService that will contain the ColumnConfiguration |
||
140 | */ |
||
141 | public function build() |
||
151 | |||
152 | /** |
||
153 | * Determine a sane configuration value for a column's callable function |
||
154 | * @param $config |
||
155 | * @param $callable |
||
156 | */ |
||
157 | private function setCallableColumn($config, $callable) |
||
167 | } |
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.