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 | class MysqlBlogRepository implements BlogRepositoryInterface |
||
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 $id |
||
24 | * |
||
25 | * @return array|false |
||
26 | */ |
||
27 | public function getBlogById($id) |
||
43 | |||
44 | View Code Duplication | public function getBlogByPermalink($permalink) |
|
60 | |||
61 | /** |
||
62 | * @param string $title |
||
63 | * |
||
64 | * @return array|false |
||
65 | */ |
||
66 | public function getBlogByTitle($title) |
||
82 | |||
83 | public function getBlogs($limit = null, $offset = 0) |
||
99 | |||
100 | public function getBlogsUpdatedSince(DateTime $datetime) |
||
116 | |||
117 | View Code Duplication | public function insertBlog($permalink, DateTime $datetime, array $metadata) |
|
136 | } |
||
137 |
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.