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 |
||
8 | View Code Duplication | class MysqlGithubRepository implements GithubRepositoryInterface |
|
|
|||
9 | { |
||
10 | |||
11 | /** @var ConnectionLocator */ |
||
12 | protected $connections; |
||
13 | |||
14 | /** |
||
15 | * @param ConnectonLocator $connections |
||
16 | */ |
||
17 | public function __construct(ConnectionLocator $connections) |
||
21 | |||
22 | /** |
||
23 | * @param integer $limit |
||
24 | * @param integer $offset |
||
25 | * |
||
26 | * @return array|false |
||
27 | */ |
||
28 | public function getEvents($limit = null, $offset = 0) |
||
29 | { |
||
30 | $query = " |
||
31 | SELECT `id`, `event_id`, `datetime` |
||
32 | FROM `jpemeric_stream`.`github` |
||
33 | ORDER BY `datetime` DESC"; |
||
34 | if (!is_null($limit)) { |
||
35 | $query .= " |
||
36 | LIMIT {$offset}, {$limit}"; |
||
37 | } |
||
38 | |||
39 | return $this |
||
40 | ->connections |
||
41 | ->getRead() |
||
42 | ->fetchAll($query); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param integer $eventId |
||
47 | * |
||
48 | * @return array|false |
||
49 | */ |
||
50 | public function getEventByEventId($eventId) |
||
67 | |||
68 | public function getGithubsUpdatedSince(DateTime $datetime) |
||
82 | |||
83 | /** |
||
84 | * @param integer $eventId |
||
85 | * @param string $eventType |
||
86 | * @param DateTime $datetime |
||
87 | * @param array $metadata |
||
88 | * |
||
89 | * @return |
||
90 | */ |
||
91 | public function insertEvent($eventId, $eventType, DateTime $datetime, array $metadata) |
||
111 | } |
||
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.