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 namespace Modules\Blog\Repositories\Cache; |
||
7 | class CachePostDecorator extends BaseCacheDecorator implements PostRepository |
||
8 | { |
||
9 | public function __construct(PostRepository $post) |
||
10 | { |
||
11 | parent::__construct(); |
||
12 | $this->entityName = 'posts'; |
||
13 | $this->repository = $post; |
||
14 | } |
||
15 | |||
16 | /** |
||
17 | * Return the latest x blog posts |
||
18 | * @param int $amount |
||
19 | * @return Collection |
||
20 | */ |
||
21 | View Code Duplication | public function latest($amount = 5) |
|
31 | |||
32 | /** |
||
33 | * Get the previous post of the given post |
||
34 | * @param object $post |
||
35 | * @return object |
||
36 | */ |
||
37 | View Code Duplication | public function getPreviousOf($post) |
|
49 | |||
50 | /** |
||
51 | * Get the next post of the given post |
||
52 | * @param object $post |
||
53 | * @return object |
||
54 | */ |
||
55 | View Code Duplication | public function getNextOf($post) |
|
67 | } |
||
68 |
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.