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 |
||
21 | class Redis extends AbstractCacheItemPool |
||
22 | { |
||
23 | /** |
||
24 | * The redis driver. |
||
25 | * |
||
26 | * @var \Redis |
||
27 | * @since 1.0 |
||
28 | */ |
||
29 | protected $driver; |
||
30 | |||
31 | /** |
||
32 | * Constructor. |
||
33 | * |
||
34 | * @param \Redis $redis The Redis driver being used for this pool |
||
35 | * @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
||
36 | * |
||
37 | * @since __DEPLOY_VERSION__ |
||
38 | */ |
||
39 | 19 | public function __construct(\Redis $redis, $options = []) |
|
46 | |||
47 | /** |
||
48 | * This will wipe out the entire cache's keys |
||
49 | * |
||
50 | * @return boolean True if the pool was successfully cleared. False if there was an error. |
||
51 | * |
||
52 | * @since 1.0 |
||
53 | */ |
||
54 | 19 | public function clear() |
|
58 | |||
59 | /** |
||
60 | * Returns a Cache Item representing the specified key. |
||
61 | * |
||
62 | * @param string $key The key for which to return the corresponding Cache Item. |
||
63 | * |
||
64 | * @return CacheItemInterface The corresponding Cache Item. |
||
65 | * |
||
66 | * @since __DEPLOY_VERSION__ |
||
67 | */ |
||
68 | 12 | View Code Duplication | public function getItem($key) |
80 | |||
81 | /** |
||
82 | * Removes the item from the pool. |
||
83 | * |
||
84 | * @param string $key The key to delete. |
||
85 | * |
||
86 | * @return boolean True if the item was successfully removed. False if there was an error. |
||
87 | * |
||
88 | * @since __DEPLOY_VERSION__ |
||
89 | */ |
||
90 | 4 | public function deleteItem($key) |
|
100 | |||
101 | /** |
||
102 | * Persists a cache item immediately. |
||
103 | * |
||
104 | * @param CacheItemInterface $item The cache item to save. |
||
105 | * |
||
106 | * @return boolean True if the item was successfully persisted. False if there was an error. |
||
107 | * |
||
108 | * @since __DEPLOY_VERSION__ |
||
109 | */ |
||
110 | 16 | public function save(CacheItemInterface $item) |
|
111 | { |
||
112 | 16 | if ($item instanceof HasExpirationDateInterface) |
|
113 | 16 | { |
|
114 | 8 | $ttl = $this->convertItemExpiryToSeconds($item); |
|
115 | |||
116 | 8 | if ($ttl > 0) |
|
117 | 8 | { |
|
118 | 7 | return $this->driver->setex($item->getKey(), $ttl, $item->get()); |
|
119 | } |
||
120 | 1 | } |
|
121 | |||
122 | 9 | return $this->driver->set($item->getKey(), $item->get()); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Confirms if the cache contains specified cache item. |
||
127 | * |
||
128 | * @param string $key The key for which to check existence. |
||
129 | * |
||
130 | * @return boolean True if item exists in the cache, false otherwise. |
||
131 | * |
||
132 | * @since 1.0 |
||
133 | */ |
||
134 | 8 | public function hasItem($key) |
|
138 | |||
139 | /** |
||
140 | * Test to see if the CacheItemPoolInterface is available |
||
141 | * |
||
142 | * @return boolean True on success, false otherwise |
||
143 | * |
||
144 | * @since __DEPLOY_VERSION__ |
||
145 | */ |
||
146 | 19 | public static function isSupported() |
|
150 | } |
||
151 |
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.