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 |
||
16 | class Config implements ConfigInterface |
||
17 | { |
||
18 | /** |
||
19 | * Array of parameters (with some default values) |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $_parameters = [ |
||
24 | 'legacy' => Client::LEGACY, |
||
25 | 'ssl' => Client::SSL, |
||
26 | 'timeout' => Client::TIMEOUT, |
||
27 | 'attempts' => Client::ATTEMPTS, |
||
28 | 'delay' => Client::ATTEMPTS_DELAY |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Config constructor. |
||
33 | * |
||
34 | * @param array $parameters List of parameters which can be set on object creation stage |
||
35 | * @throws ConfigException |
||
36 | * @since 0.6 |
||
37 | */ |
||
38 | 21 | public function __construct(array $parameters = []) |
|
44 | |||
45 | /** |
||
46 | * Set parameter into array |
||
47 | * |
||
48 | * @param string $name |
||
49 | * @param mixed $value |
||
50 | * @return \RouterOS\Config |
||
51 | * @throws ConfigException |
||
52 | */ |
||
53 | 15 | public function set(string $name, $value): Config |
|
70 | |||
71 | /** |
||
72 | * Return port number (get from defaults if port is not set by user) |
||
73 | * |
||
74 | * @param string $parameter |
||
75 | * @return bool|int |
||
76 | */ |
||
77 | 10 | private function getPort(string $parameter) |
|
78 | { |
||
79 | // If client need port number and port is not set |
||
80 | 10 | if ($parameter === 'port' && !isset($this->_parameters['port']) && null !== $this->_parameters['port']) { |
|
81 | // then use default with or without ssl encryption |
||
82 | return (isset($this->_parameters['ssl']) && $this->_parameters['ssl']) |
||
83 | ? Client::PORT_SSL |
||
84 | : Client::PORT; |
||
85 | } |
||
86 | 10 | return null; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Remove parameter from array by name |
||
91 | * |
||
92 | * @param string $name |
||
93 | * @return \RouterOS\Config |
||
94 | * @throws \RouterOS\Exceptions\ConfigException |
||
95 | */ |
||
96 | 2 | View Code Duplication | public function delete(string $name): Config |
108 | |||
109 | /** |
||
110 | * Return parameter of current config by name |
||
111 | * |
||
112 | * @param string $name |
||
113 | * @return mixed |
||
114 | * @throws \RouterOS\Exceptions\ConfigException |
||
115 | */ |
||
116 | 11 | View Code Duplication | public function get(string $name) |
125 | |||
126 | /** |
||
127 | * Return array with all parameters of configuration |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | 15 | public function getParameters(): array |
|
135 | } |
||
136 |
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.