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 |
||
12 | class Cache |
||
13 | { |
||
14 | /** |
||
15 | * The driver used by a cache. |
||
16 | * @var StoreInterface |
||
17 | */ |
||
18 | protected $driver; |
||
19 | |||
20 | /** |
||
21 | * Key prefix to use. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $prefix = ""; |
||
26 | |||
27 | /** |
||
28 | * Class constructor. |
||
29 | * |
||
30 | * @param StoreInterface $driver |
||
31 | */ |
||
32 | public function __construct(StoreInterface $driver) |
||
36 | |||
37 | /** |
||
38 | * Sets key prefix. |
||
39 | * |
||
40 | * @param string $prefix |
||
41 | */ |
||
42 | public function setPrefix($prefix) |
||
46 | |||
47 | /** |
||
48 | * Returns key prefix. |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getPrefix() |
||
56 | |||
57 | /** |
||
58 | * Stores an item in the cache for a specified period of time only if it is not already stored. |
||
59 | * Cache::add() is similar to Cache::set(), but the operation fails if the key already exists. |
||
60 | * |
||
61 | * @param string $key |
||
62 | * @param mixed $value The value to be stored. |
||
63 | * @param integer $ttl Time to live in seconds. 0 means to store it for a maximum time possible |
||
64 | * |
||
65 | * @return boolean TRUE if the value is set, FALSE on failure |
||
66 | */ |
||
67 | public function add($key, $value, $ttl = 0) |
||
73 | |||
74 | /** |
||
75 | * Stores an item in the data store |
||
76 | * Cache::set() is similar to Cache::add(), but the operation will not fail if the key already exist. |
||
77 | * |
||
78 | * @param string $key The key under which to store the value |
||
79 | * @param mixed $value The value to store |
||
80 | * @param integer $ttl Expiration time in seconds, after which the value is invalidated (deleted) |
||
81 | * |
||
82 | * @return boolean TRUE on success or FALSE on failure |
||
83 | */ |
||
84 | public function set($key, $value, $ttl = 0) |
||
90 | |||
91 | /** |
||
92 | * Fetches a stored variable from the cache |
||
93 | * |
||
94 | * @param string $key The key used to store the value |
||
95 | * |
||
96 | * @return mixed Returns NULL if the key does not exist in the store or the value was expired (see $ttl) |
||
97 | */ |
||
98 | public function get($key, $defaultValue = null) |
||
105 | |||
106 | /** |
||
107 | * Checks if the key exists |
||
108 | * |
||
109 | * @param string $key |
||
110 | * |
||
111 | * @return boolean TRUE if the key exists, otherwise FALSE |
||
112 | */ |
||
113 | public function has($key) |
||
119 | |||
120 | /** |
||
121 | * Removes a stored variable from the cache |
||
122 | * |
||
123 | * @param string $key |
||
124 | */ |
||
125 | public function delete($key) |
||
131 | |||
132 | /** |
||
133 | * Invalidate all items in the cache |
||
134 | */ |
||
135 | public function flush() |
||
139 | |||
140 | /** |
||
141 | * Increment numeric item's value. |
||
142 | * If there is no such key or the stored value is not numeric FALSE is returned |
||
143 | * |
||
144 | * @param string $key |
||
145 | * @param integer $step |
||
146 | * |
||
147 | * @return integer|false Returns the new incremented value or FALSE on failure |
||
148 | */ |
||
149 | View Code Duplication | public function inc($key, $step = 1) |
|
165 | |||
166 | /** |
||
167 | * Decrements numeric item's value. |
||
168 | * If there is no such key or the stored value is not numeric FALSE is returned |
||
169 | * |
||
170 | * @param string $key |
||
171 | * @param integer $step |
||
172 | * |
||
173 | * @return integer|false Returns the new decremented value or FALSE on failure |
||
174 | */ |
||
175 | View Code Duplication | public function dec($key, $step = 1) |
|
191 | } |
||
192 |
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.