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 |
||
12 | trait DrupalParametersTrait |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Test parameters. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $drupalParameters; |
||
21 | |||
22 | /** |
||
23 | * Set parameters provided for Drupal. |
||
24 | * |
||
25 | * @param array $parameters |
||
26 | * The parameters to set. |
||
27 | */ |
||
28 | public function setDrupalParameters(array $parameters) |
||
32 | |||
33 | /** |
||
34 | * Returns a specific Drupal parameter. |
||
35 | * |
||
36 | * @param string $name |
||
37 | * Parameter name. |
||
38 | * |
||
39 | * @return mixed |
||
40 | * The value. |
||
41 | */ |
||
42 | public function getDrupalParameter($name) |
||
46 | |||
47 | /** |
||
48 | * Returns a specific Drupal text value. |
||
49 | * |
||
50 | * @param string $name |
||
51 | * Text value name, such as 'log_out', which corresponds to the default |
||
52 | * 'Log out' link text. |
||
53 | * |
||
54 | * @return string |
||
55 | * The text value. |
||
56 | * |
||
57 | * @throws \Exception |
||
58 | * Thrown when the text is not present in the list of parameters. |
||
59 | */ |
||
60 | View Code Duplication | public function getDrupalText($name) |
|
68 | |||
69 | /** |
||
70 | * Returns a specific CSS selector. |
||
71 | * |
||
72 | * @param string $name |
||
73 | * The name of the CSS selector. |
||
74 | * |
||
75 | * @return string |
||
76 | * The CSS selector. |
||
77 | * |
||
78 | * @throws \Exception |
||
79 | * Thrown when the selector is not present in the list of parameters. |
||
80 | */ |
||
81 | View Code Duplication | public function getDrupalSelector($name) |
|
89 | } |
||
90 |
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.