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 |
||
23 | class MemcacheIndexer extends AbstractIndexer |
||
24 | { |
||
25 | |||
26 | const DIRTINESS_THRESHOLD = 100; |
||
27 | |||
28 | /** |
||
29 | * Holds an instane of a serializer. |
||
30 | * @var Serializer\Stringset |
||
31 | */ |
||
32 | protected $serializer; |
||
33 | |||
34 | /** |
||
35 | * Holds the index store engine. |
||
36 | * @var \Apix\Cache\Memcache |
||
37 | */ |
||
38 | protected $engine; |
||
39 | |||
40 | /** |
||
41 | * Constructor. |
||
42 | * |
||
43 | * @param string $name |
||
44 | * @param \Apix\Cache\Memcache $engine |
||
45 | */ |
||
46 | public function __construct($name, Memcache $engine) |
||
53 | |||
54 | /** |
||
55 | * Gets the adapter. |
||
56 | * |
||
57 | * @return \Memcache |
||
58 | */ |
||
59 | public function getAdapter() |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | View Code Duplication | public function add($elements) |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function remove($elements) |
||
89 | |||
90 | /** |
||
91 | * Returns the indexed items. |
||
92 | * |
||
93 | * @return boolean Returns True on success or Null on failure. |
||
94 | */ |
||
95 | public function load() |
||
109 | |||
110 | /** |
||
111 | * Purge atomically the index. |
||
112 | * |
||
113 | * @param double|null $cas_token |
||
114 | * @return float $cas_token The Memcache CAS token. |
||
115 | */ |
||
116 | protected function purge($cas_token) |
||
122 | |||
123 | } |
||
124 |
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.