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 MongoCache implements Cache |
||
12 | { |
||
13 | /** |
||
14 | * Mongo collection for storing cache |
||
15 | * |
||
16 | * @var \MongoDB\Collection |
||
17 | */ |
||
18 | private $collection; |
||
19 | |||
20 | /** |
||
21 | * Construct a new instance of MongoCache |
||
22 | * |
||
23 | * @param string $url mongo url |
||
24 | * @param string $db name of mongo database |
||
25 | * @param string $collection name of mongo collection |
||
26 | */ |
||
27 | public function __construct($url, $db, $collection) |
||
43 | |||
44 | /** |
||
45 | * @see Cache::set() |
||
46 | */ |
||
47 | public function set(Request $request, Response $response, $expires = null) |
||
74 | |||
75 | /** |
||
76 | * @see Cache::get() |
||
77 | */ |
||
78 | public function get(Request $request) |
||
87 | |||
88 | /** |
||
89 | * Ensures proper indexes are created on the mongo cache collection |
||
90 | * |
||
91 | * @return void |
||
92 | */ |
||
93 | public function ensureIndexes() |
||
97 | |||
98 | /** |
||
112 |
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.