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 |
||
14 | class Info |
||
15 | { |
||
16 | /** |
||
17 | * Response. |
||
18 | * |
||
19 | * @var \Elastica\Response Response object |
||
20 | */ |
||
21 | protected $_response; |
||
22 | |||
23 | /** |
||
24 | * Stats data. |
||
25 | * |
||
26 | * @var array stats data |
||
27 | */ |
||
28 | protected $_data = []; |
||
29 | |||
30 | /** |
||
31 | * Node. |
||
32 | * |
||
33 | * @var \Elastica\Node Node object |
||
34 | */ |
||
35 | protected $_node; |
||
36 | |||
37 | /** |
||
38 | * Query parameters. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $_params = []; |
||
43 | |||
44 | /** |
||
45 | * Unique node id. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $_id; |
||
50 | |||
51 | /** |
||
52 | * Create new info object for node. |
||
53 | * |
||
54 | * @param \Elastica\Node $node Node object |
||
55 | * @param array $params List of params to return. Can be: settings, os, process, jvm, thread_pool, network, transport, http |
||
56 | */ |
||
57 | public function __construct(BaseNode $node, array $params = []) |
||
62 | |||
63 | /** |
||
64 | * Returns the entry in the data array based on the params. |
||
65 | * Several params possible. |
||
66 | * |
||
67 | * Example 1: get('os', 'mem', 'total') returns total memory of the system the |
||
68 | * node is running on |
||
69 | * Example 2: get('os', 'mem') returns an array with all mem infos |
||
70 | * |
||
71 | * @return mixed Data array entry or null if not found |
||
72 | */ |
||
73 | View Code Duplication | public function get() |
|
87 | |||
88 | /** |
||
89 | * Return port of the node. |
||
90 | * |
||
91 | * @return string Returns Node port |
||
92 | */ |
||
93 | View Code Duplication | public function getPort() |
|
102 | |||
103 | /** |
||
104 | * Return IP of the node. |
||
105 | * |
||
106 | * @return string Returns Node ip address |
||
107 | */ |
||
108 | View Code Duplication | public function getIp() |
|
117 | |||
118 | /** |
||
119 | * Return data regarding plugins installed on this node. |
||
120 | * |
||
121 | * @return array plugin data |
||
122 | * |
||
123 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html |
||
124 | */ |
||
125 | public function getPlugins() |
||
135 | |||
136 | /** |
||
137 | * Check if the given plugin is installed on this node. |
||
138 | * |
||
139 | * @param string $name plugin name |
||
140 | * |
||
141 | * @return bool true if the plugin is installed, false otherwise |
||
142 | */ |
||
143 | public function hasPlugin($name) |
||
153 | |||
154 | /** |
||
155 | * Return all info data. |
||
156 | * |
||
157 | * @return array Data array |
||
158 | */ |
||
159 | public function getData() |
||
163 | |||
164 | /** |
||
165 | * Return node object. |
||
166 | * |
||
167 | * @return \Elastica\Node Node object |
||
168 | */ |
||
169 | public function getNode() |
||
173 | |||
174 | /** |
||
175 | * @return string Unique node id |
||
176 | */ |
||
177 | public function getId() |
||
181 | |||
182 | /** |
||
183 | * @return string Node name |
||
184 | */ |
||
185 | public function getName() |
||
189 | |||
190 | /** |
||
191 | * Returns response object. |
||
192 | * |
||
193 | * @return \Elastica\Response Response object |
||
194 | */ |
||
195 | public function getResponse() |
||
199 | |||
200 | /** |
||
201 | * Reloads all nodes information. Has to be called if informations changed. |
||
202 | * |
||
203 | * @param array $params Params to return (default none). Possible options: settings, os, process, jvm, thread_pool, network, transport, http, plugin |
||
204 | * |
||
205 | * @return \Elastica\Response Response object |
||
206 | */ |
||
207 | public function refresh(array $params = []) |
||
225 | } |
||
226 |
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.