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 |
||
10 | */ |
||
11 | final class PredisCache implements Cache |
||
12 | { |
||
13 | /** |
||
14 | * Predis client for storing cache. |
||
15 | * |
||
16 | * @var \Predis\Client |
||
17 | */ |
||
18 | private $client; |
||
19 | |||
20 | /** |
||
21 | * Construct a cache instance. |
||
22 | * |
||
23 | * @param \Predis\Client $client The predis client to send data to. |
||
24 | */ |
||
25 | public function __construct(\Predis\Client $client) |
||
29 | |||
30 | /** |
||
31 | * @see Cache::set() |
||
32 | */ |
||
33 | public function set(Request $request, Response $response, $expires = null) |
||
61 | |||
62 | /** |
||
63 | * @see Cache::get() |
||
64 | */ |
||
65 | public function get(Request $request) |
||
75 | |||
76 | /** |
||
77 | * Helper method to get a unique key for an API request. |
||
78 | * |
||
79 | * This generator does not use the request headers so there is a chance for conflicts |
||
80 | * |
||
81 | * @param Request $request The request from which to generate an unique key |
||
82 | * |
||
83 | * @return string the unique identifier |
||
84 | */ |
||
85 | private static function getKey(Request $request) |
||
90 |
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.