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 |
||
48 | class IniLoader |
||
49 | { |
||
50 | const PHP_INI_LITE = 'Lite_PHP_BrowscapINI'; |
||
51 | const PHP_INI_FULL = 'Full_PHP_BrowscapINI'; |
||
52 | const PHP_INI = 'PHP_BrowscapINI'; |
||
53 | |||
54 | /** |
||
55 | * Options for update capabilities |
||
56 | * |
||
57 | * $remoteTimeUrl: The location to use to check out if a new version of the |
||
58 | * browscap.ini file is available. |
||
59 | * $remoteIniUrl: The location from which download the ini file. |
||
60 | * The placeholder for the file should be represented by a %s. |
||
61 | * $timeout: The timeout for the requests. |
||
62 | */ |
||
63 | private $remoteIniUrl = 'http://browscap.org/stream?q=%q'; |
||
64 | private $remoteTimeUrl = 'http://browscap.org/version'; |
||
65 | private $remoteVersionUrl = 'http://browscap.org/version-number'; |
||
66 | private $timeout = 5; |
||
67 | |||
68 | /** |
||
69 | * The path of the local version of the browscap.ini file from which to |
||
70 | * update (to be set only if used). |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $localFile = null; |
||
75 | |||
76 | /** |
||
77 | * an logger instance |
||
78 | * |
||
79 | * @var \Psr\Log\LoggerInterface |
||
80 | */ |
||
81 | private $logger = null; |
||
82 | |||
83 | /** |
||
84 | * an file loader instance |
||
85 | * |
||
86 | * @var \FileLoader\Loader |
||
87 | */ |
||
88 | private $loader = null; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | private $remoteFilename = self::PHP_INI; |
||
94 | |||
95 | /** |
||
96 | * Options for the updater. The array should be overwritten, |
||
97 | * containing all options as keys, set to the default value. |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | private $options = array(); |
||
102 | |||
103 | /** |
||
104 | * sets the logger |
||
105 | * |
||
106 | * @param \Psr\Log\LoggerInterface $logger |
||
107 | * |
||
108 | * @return \BrowscapPHP\Helper\IniLoader |
||
109 | */ |
||
110 | 6 | public function setLogger(LoggerInterface $logger) |
|
111 | { |
||
112 | 6 | $this->logger = $logger; |
|
113 | |||
114 | 6 | return $this; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * returns the logger |
||
119 | * |
||
120 | * @return \Psr\Log\LoggerInterface |
||
121 | */ |
||
122 | public function getLogger() |
||
123 | { |
||
124 | return $this->logger; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * sets the loader |
||
129 | * |
||
130 | * @param \FileLoader\Loader $loader |
||
131 | * |
||
132 | * @return \BrowscapPHP\Helper\IniLoader |
||
133 | */ |
||
134 | 6 | public function setLoader(Loader $loader) |
|
135 | { |
||
136 | 6 | $this->loader = $loader; |
|
137 | |||
138 | 6 | return $this; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * creates the ini loader |
||
143 | * |
||
144 | * @return \FileLoader\Loader |
||
145 | */ |
||
146 | 11 | public function getLoader() |
|
147 | { |
||
148 | 11 | if (null === $this->loader) { |
|
149 | 5 | $this->loader = new Loader(); |
|
150 | } |
||
151 | |||
152 | 11 | if (null !== $this->localFile) { |
|
153 | 3 | $this->loader->setLocalFile($this->localFile); |
|
154 | } |
||
155 | |||
156 | 11 | $this->loader->setOptions($this->options); |
|
157 | |||
158 | 11 | return $this->loader; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * sets the name of the local file |
||
163 | * |
||
164 | * @param string $filename the file name |
||
165 | * |
||
166 | * @throws \BrowscapPHP\Helper\Exception |
||
167 | * @return \BrowscapPHP\Helper\IniLoader |
||
168 | */ |
||
169 | 6 | public function setLocalFile($filename = null) |
|
170 | { |
||
171 | 6 | if (empty($filename)) { |
|
172 | 2 | throw new Exception( |
|
173 | 2 | 'the filename can not be empty', |
|
174 | Exception::LOCAL_FILE_MISSING |
||
175 | 2 | ); |
|
176 | } |
||
177 | |||
178 | 4 | $this->localFile = $filename; |
|
179 | |||
180 | 4 | return $this; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * sets the name of the local ini file |
||
185 | * |
||
186 | * @param string $name the file name |
||
187 | * |
||
188 | * @throws \BrowscapPHP\Helper\Exception |
||
189 | * @return \BrowscapPHP\Helper\IniLoader |
||
190 | */ |
||
191 | 3 | public function setRemoteFilename($name = null) |
|
192 | { |
||
193 | 3 | if (empty($name)) { |
|
194 | 1 | throw new Exception( |
|
195 | 1 | 'the filename can not be empty', |
|
196 | Exception::INI_FILE_MISSING |
||
197 | 1 | ); |
|
198 | } |
||
199 | |||
200 | 2 | $this->remoteFilename = $name; |
|
201 | |||
202 | 2 | return $this; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * returns the of the remote location for updating the ini file |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | 7 | public function getRemoteIniUrl() |
|
211 | { |
||
212 | 7 | return str_replace('%q', $this->remoteFilename, $this->remoteIniUrl); |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * returns the of the remote location for checking the version of the ini file |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | 3 | public function getRemoteTimeUrl() |
|
224 | |||
225 | /** |
||
226 | * returns the of the remote location for checking the version of the ini file |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 3 | public function getRemoteVersionUrl() |
|
234 | |||
235 | /** |
||
236 | * returns the timeout |
||
237 | * |
||
238 | * @return integer |
||
239 | */ |
||
240 | 11 | public function getTimeout() |
|
241 | { |
||
242 | 11 | return $this->timeout; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * Sets multiple loader options at once |
||
247 | * |
||
248 | * @param array $options |
||
249 | * |
||
250 | * @return IniLoader |
||
251 | */ |
||
252 | 1 | public function setOptions(array $options) |
|
253 | { |
||
254 | 1 | $this->options = $options; |
|
255 | |||
256 | 1 | return $this; |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * loads the ini file from a remote or local location and returns the content of the file |
||
261 | * |
||
262 | * @throws \BrowscapPHP\Helper\Exception |
||
263 | * @return string the content of the loaded ini file |
||
264 | */ |
||
265 | 5 | View Code Duplication | public function load() |
280 | |||
281 | /** |
||
282 | * Gets the remote file update timestamp |
||
283 | * |
||
284 | * @throws \BrowscapPHP\Helper\Exception |
||
285 | * @return integer the remote modification timestamp |
||
286 | */ |
||
287 | 2 | public function getMTime() |
|
311 | |||
312 | /** |
||
313 | * Gets the remote file update timestamp |
||
314 | * |
||
315 | * @throws \BrowscapPHP\Helper\Exception |
||
316 | * @return integer the remote modification timestamp |
||
317 | */ |
||
318 | 3 | View Code Duplication | public function getRemoteVersion() |
333 | } |
||
334 |
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.