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 | View Code Duplication | class PlaceAutocompleteHelper extends Helper |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * @var BasePlaceAutocompleteHelper |
||
25 | */ |
||
26 | private $placeAutocompleteHelper; |
||
27 | |||
28 | /** |
||
29 | * @param BasePlaceAutocompleteHelper $placeAutocompleteHelper |
||
30 | */ |
||
31 | 36 | public function __construct(BasePlaceAutocompleteHelper $placeAutocompleteHelper) |
|
35 | |||
36 | /** |
||
37 | * @param Autocomplete $autocomplete |
||
38 | * @param string[] $attributes |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | 9 | public function render(Autocomplete $autocomplete, array $attributes = []) |
|
48 | |||
49 | /** |
||
50 | * @param Autocomplete $autocomplete |
||
51 | * @param string[] $attributes |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 9 | public function renderHtml(Autocomplete $autocomplete, array $attributes = []) |
|
61 | |||
62 | /** |
||
63 | * @param Autocomplete $autocomplete |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 9 | public function renderJavascript(Autocomplete $autocomplete) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 9 | public function getName() |
|
79 | } |
||
80 |
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.