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 |
||
32 | class UpstreamJsonConfiguration implements UpstreamConfigurationInterface |
||
33 | { |
||
34 | /** |
||
35 | * Holds the name of the upstream node |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $name; |
||
40 | |||
41 | /** |
||
42 | * Holds the type of the upstream node |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | public $type; |
||
47 | |||
48 | /** |
||
49 | * Holds the channel of the upstream node |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | public $channel; |
||
54 | |||
55 | /** |
||
56 | * Holds the servers for the upstream node |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | public $servers; |
||
61 | |||
62 | /** |
||
63 | * Constructs config |
||
64 | * |
||
65 | * @param \stdClass $node The JSON node used to build config |
||
66 | */ |
||
67 | public function __construct($node) |
||
80 | |||
81 | /** |
||
82 | * Prepares server nodes configured for upstream |
||
83 | * |
||
84 | * @param \stdClass $node The JSON node for servers to prepare |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | protected function prepareServers($node) |
||
116 | |||
117 | /** |
||
118 | * Returns name |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getName() |
||
126 | |||
127 | /** |
||
128 | * Returns type |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getType() |
||
136 | |||
137 | /** |
||
138 | * Returns servers |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getServers() |
||
146 | } |
||
147 |
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.