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:
Complex classes like Browscap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Browscap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
63 | class Browscap |
||
64 | { |
||
65 | /** |
||
66 | * Parser to use |
||
67 | * |
||
68 | * @var \BrowscapPHP\Parser\ParserInterface |
||
69 | */ |
||
70 | private $parser = null; |
||
71 | |||
72 | /** |
||
73 | * Formatter to use |
||
74 | * |
||
75 | * @var \BrowscapPHP\Formatter\FormatterInterface |
||
76 | */ |
||
77 | private $formatter = null; |
||
78 | |||
79 | /** |
||
80 | * The cache instance |
||
81 | * |
||
82 | * @var \BrowscapPHP\Cache\BrowscapCacheInterface |
||
83 | */ |
||
84 | private $cache = null; |
||
85 | |||
86 | /** |
||
87 | * @var @var \Psr\Log\LoggerInterface |
||
88 | */ |
||
89 | private $logger = null; |
||
90 | |||
91 | /** |
||
92 | * Options for the updater. The array should be overwritten, |
||
93 | * containing all options as keys, set to the default value. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | private $options = array(); |
||
98 | |||
99 | /** |
||
100 | * @var \BrowscapPHP\Helper\IniLoader |
||
101 | */ |
||
102 | private $loader = null; |
||
103 | |||
104 | /** |
||
105 | * Set theformatter instance to use for the getBrowser() result |
||
106 | * |
||
107 | * @param \BrowscapPHP\Formatter\FormatterInterface $formatter |
||
108 | * |
||
109 | * @return \BrowscapPHP\Browscap |
||
110 | */ |
||
111 | 6 | public function setFormatter(Formatter\FormatterInterface $formatter) |
|
117 | |||
118 | /** |
||
119 | * @return \BrowscapPHP\Formatter\FormatterInterface |
||
120 | */ |
||
121 | 4 | public function getFormatter() |
|
129 | |||
130 | /** |
||
131 | * Gets a cache instance |
||
132 | * |
||
133 | * @return \BrowscapPHP\Cache\BrowscapCacheInterface |
||
134 | */ |
||
135 | 17 | public function getCache() |
|
149 | |||
150 | /** |
||
151 | * Sets a cache instance |
||
152 | * |
||
153 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache |
||
154 | * |
||
155 | * @throws \BrowscapPHP\Exception |
||
156 | * @return \BrowscapPHP\Browscap |
||
157 | */ |
||
158 | 16 | public function setCache($cache) |
|
174 | |||
175 | /** |
||
176 | * Sets the parser instance to use |
||
177 | * |
||
178 | * @param \BrowscapPHP\Parser\ParserInterface $parser |
||
179 | * |
||
180 | * @return \BrowscapPHP\Browscap |
||
181 | */ |
||
182 | 4 | public function setParser(ParserInterface $parser) |
|
188 | |||
189 | /** |
||
190 | * returns an instance of the used parser class |
||
191 | * |
||
192 | * @return \BrowscapPHP\Parser\ParserInterface |
||
193 | */ |
||
194 | 6 | public function getParser() |
|
209 | |||
210 | /** |
||
211 | * Sets a logger instance |
||
212 | * |
||
213 | * @param \Psr\Log\LoggerInterface $logger |
||
214 | * |
||
215 | * @return \BrowscapPHP\Browscap |
||
216 | */ |
||
217 | 12 | public function setLogger(LoggerInterface $logger) |
|
223 | |||
224 | /** |
||
225 | * returns a logger instance |
||
226 | * |
||
227 | * @return \Psr\Log\LoggerInterface |
||
228 | */ |
||
229 | 16 | public function getLogger() |
|
237 | |||
238 | /** |
||
239 | * Sets multiple loader options at once |
||
240 | * |
||
241 | * @param array $options |
||
242 | * |
||
243 | * @return \BrowscapPHP\Browscap |
||
244 | */ |
||
245 | 1 | public function setOptions(array $options) |
|
251 | |||
252 | /** |
||
253 | * @return \BrowscapPHP\Helper\IniLoader |
||
254 | */ |
||
255 | 10 | public function getLoader() |
|
256 | { |
||
257 | 10 | if (null === $this->loader) { |
|
258 | 1 | $this->loader = new IniLoader(); |
|
259 | } |
||
260 | |||
261 | 10 | return $this->loader; |
|
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param \BrowscapPHP\Helper\IniLoader $loader |
||
266 | */ |
||
267 | 10 | public function setLoader(IniLoader $loader) |
|
271 | |||
272 | /** |
||
273 | * parses the given user agent to get the information about the browser |
||
274 | * |
||
275 | * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it |
||
276 | * |
||
277 | * @param string $userAgent the user agent string |
||
278 | * |
||
279 | * @throws \BrowscapPHP\Exception |
||
280 | * @return \stdClass the object containing the browsers details. Array if |
||
281 | * $return_array is set to true. |
||
282 | */ |
||
283 | 4 | public function getBrowser($userAgent = null) |
|
302 | |||
303 | /** |
||
304 | * reads and parses an ini file and writes the results into the cache |
||
305 | * |
||
306 | * @param string $iniFile |
||
307 | * |
||
308 | * @throws \BrowscapPHP\Exception |
||
309 | */ |
||
310 | 4 | public function convertFile($iniFile) |
|
328 | |||
329 | /** |
||
330 | * reads and parses an ini string and writes the results into the cache |
||
331 | * |
||
332 | * @param string $iniString |
||
333 | */ |
||
334 | 3 | public function convertString($iniString) |
|
341 | |||
342 | /** |
||
343 | * fetches a remote file and stores it into a local folder |
||
344 | * |
||
345 | * @param string $file The name of the file where to store the remote content |
||
346 | * @param string $remoteFile The code for the remote file to load |
||
347 | * |
||
348 | * @throws \BrowscapPHP\Exception\FetcherException |
||
349 | * @throws \BrowscapPHP\Helper\Exception |
||
350 | */ |
||
351 | 3 | public function fetch($file, $remoteFile = IniLoader::PHP_INI) |
|
392 | |||
393 | /** |
||
394 | * fetches a remote file, parses it and writes the result into the cache |
||
395 | * |
||
396 | * if the local stored information are in the same version as the remote data no actions are |
||
397 | * taken |
||
398 | * |
||
399 | * @param string $remoteFile The code for the remote file to load |
||
400 | * @param string|null $buildFolder |
||
401 | * @param int|null $buildNumber |
||
402 | * |
||
403 | * @throws \BrowscapPHP\Exception\FileNotFoundException |
||
404 | * @throws \BrowscapPHP\Helper\Exception |
||
405 | * @throws \BrowscapPHP\Exception\FetcherException |
||
406 | */ |
||
407 | 2 | public function update($remoteFile = IniLoader::PHP_INI, $buildFolder = null, $buildNumber = null) |
|
408 | { |
||
409 | 2 | $this->getLogger()->debug('started fetching remote file'); |
|
410 | |||
411 | 2 | $converter = new Converter($this->getLogger(), $this->getCache()); |
|
412 | |||
413 | 2 | if (class_exists('\Browscap\Browscap')) { |
|
414 | $resourceFolder = 'vendor/browscap/browscap/resources/'; |
||
415 | |||
416 | if (null === $buildNumber) { |
||
417 | $buildNumber = (int)file_get_contents('vendor/browscap/browscap/BUILD_NUMBER'); |
||
418 | } |
||
419 | |||
420 | if (null === $buildFolder) { |
||
421 | $buildFolder = 'resources'; |
||
422 | } |
||
423 | |||
424 | $buildFolder .= '/browscap-ua-test-'.$buildNumber; |
||
425 | $iniFile = $buildFolder.'/full_php_browscap.ini'; |
||
426 | |||
427 | mkdir($buildFolder, 0777, true); |
||
428 | |||
429 | $writerCollectionFactory = new PhpWriterFactory(); |
||
430 | $writerCollection = $writerCollectionFactory->createCollection($this->getLogger(), $buildFolder); |
||
431 | |||
432 | $buildGenerator = new BuildGenerator($resourceFolder, $buildFolder); |
||
433 | $buildGenerator |
||
434 | ->setLogger($this->getLogger()) |
||
435 | ->setCollectionCreator(new CollectionCreator()) |
||
436 | ->setWriterCollection($writerCollection) |
||
437 | ->run($buildNumber, false) |
||
438 | ; |
||
439 | |||
440 | $converter |
||
441 | ->setVersion($buildNumber) |
||
442 | ->storeVersion() |
||
443 | ->convertFile($iniFile) |
||
444 | ; |
||
445 | |||
446 | $filesystem = new Filesystem(); |
||
447 | $filesystem->remove($buildFolder); |
||
448 | } else { |
||
449 | 2 | if (null === ($cachedVersion = $this->checkUpdate($remoteFile))) { |
|
450 | // no newer version available |
||
451 | return; |
||
452 | } |
||
453 | |||
454 | 2 | $this->getLoader() |
|
455 | 2 | ->setRemoteFilename($remoteFile) |
|
456 | 2 | ->setOptions($this->options) |
|
457 | 2 | ->setLogger($this->getLogger()) |
|
458 | ; |
||
459 | |||
460 | try { |
||
461 | 2 | $content = $this->getLoader()->load(); |
|
462 | 2 | } catch (Helper\Exception $e) { |
|
463 | throw new FetcherException('an error occured while loading remote data', 0, $e); |
||
464 | } |
||
465 | |||
466 | 2 | View Code Duplication | if (false === $content) { |
467 | 1 | $internalLoader = $this->getLoader()->getLoader(); |
|
468 | 1 | $error = error_get_last(); |
|
469 | |||
470 | 1 | throw FetcherException::httpError($internalLoader->getUri(), $error['message']); |
|
471 | } |
||
472 | |||
473 | 1 | $this->getLogger()->debug('finished fetching remote file'); |
|
474 | |||
475 | 1 | $this->storeContent($converter, $content, $cachedVersion); |
|
476 | } |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * checks if an update on a remote location for the local file or the cache |
||
481 | * |
||
482 | * @param string $remoteFile |
||
483 | * |
||
484 | * @return int|null The actual cached version if a newer version is available, null otherwise |
||
485 | * @throws \BrowscapPHP\Helper\Exception |
||
486 | * @throws \BrowscapPHP\Exception\FetcherException |
||
487 | */ |
||
488 | 9 | public function checkUpdate($remoteFile = IniLoader::PHP_INI) |
|
532 | |||
533 | /** |
||
534 | * @param string $content |
||
535 | * |
||
536 | * @return mixed |
||
537 | */ |
||
538 | 6 | private function sanitizeContent($content) |
|
546 | |||
547 | /** |
||
548 | * reads and parses an ini string and writes the results into the cache |
||
549 | * |
||
550 | * @param \BrowscapPHP\Helper\Converter $converter |
||
551 | * @param string $content |
||
552 | * @param int|null $cachedVersion |
||
553 | */ |
||
554 | 4 | private function storeContent(Converter $converter, $content, $cachedVersion) |
|
566 | } |
||
567 |
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.