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 |
||
7 | class ArticleRepository |
||
8 | { |
||
9 | /** |
||
10 | * Find the latest articles for the sidebar. |
||
11 | * |
||
12 | * @return \Illuminate\Database\Eloquent\Collection |
||
13 | */ |
||
14 | public static function sidebar(): Collection |
||
18 | |||
19 | /** |
||
20 | * Create the new article and save it. |
||
21 | * |
||
22 | * @param array $data The data used to create the article. |
||
23 | * |
||
24 | * @return bool |
||
25 | */ |
||
26 | View Code Duplication | public static function create(array $data): bool |
|
36 | |||
37 | /** |
||
38 | * Update the article data and save it. |
||
39 | * |
||
40 | * @param array $data The data used to update the article. |
||
41 | * |
||
42 | * @return bool |
||
43 | */ |
||
44 | View Code Duplication | public static function update(array $data, Article $article): bool |
|
53 | } |
||
54 |
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.