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 namespace Comodojo\Cache\Providers; |
||
30 | View Code Duplication | class Memcached extends AbstractEnhancedProvider { |
|
|
|||
31 | |||
32 | 52 | public function __construct( |
|
33 | $server = '127.0.0.1', |
||
34 | $port = 11211, |
||
35 | $weight = 0, |
||
36 | $persistent_id = null, |
||
37 | LoggerInterface $logger = null |
||
38 | ) { |
||
39 | |||
40 | 52 | if ( empty($server) ) { |
|
41 | throw new InvalidCacheArgumentException("Invalid or unspecified memcached server"); |
||
42 | } |
||
43 | |||
44 | 52 | if ( $persistent_id !== null && DataValidation::validateString($persistent_id) === false ) { |
|
45 | throw new InvalidCacheArgumentException("Invalid persistent id"); |
||
46 | } |
||
47 | |||
48 | 52 | $port = DataFilter::filterPort($port, 11211); |
|
49 | 52 | $weight = DataFilter::filterInteger($weight); |
|
50 | |||
51 | try { |
||
52 | |||
53 | 52 | $this->driver = new MemcachedDriver([ |
|
54 | 52 | 'persistent_id' => $persistent_id, |
|
55 | 52 | 'server' => $server, |
|
56 | 52 | 'port' => $port, |
|
57 | 52 | 'weight' => $weight |
|
58 | ]); |
||
59 | |||
60 | 52 | parent::__construct($logger); |
|
61 | |||
62 | } catch (Exception $e) { |
||
63 | |||
64 | throw new CacheException($e->getMessage()); |
||
65 | |||
66 | } |
||
67 | |||
68 | 52 | } |
|
69 | |||
70 | public function getInstance() { |
||
73 | |||
74 | 2 | public function getStats() { |
|
95 | |||
96 | } |
||
97 |
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.