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 | View Code Duplication | trait HasBlocks |
|
1 ignored issue
–
show
|
|||
8 | { |
||
9 | /** |
||
10 | * Get a model from a relationship by model name. |
||
11 | * |
||
12 | * @param string $relationship Name of relationship. |
||
13 | * @param string $name Name of model to get. |
||
14 | * |
||
15 | * @return \Illuminate\Database\Eloquent\Model|null |
||
16 | */ |
||
17 | abstract protected function getFromRelationshipByName($relationship, $name); |
||
18 | |||
19 | /** |
||
20 | * A project has many text blocks. |
||
21 | * |
||
22 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
23 | */ |
||
24 | public function blocks() |
||
28 | |||
29 | /** |
||
30 | * Return true if project has blocks. |
||
31 | * |
||
32 | * @return bool |
||
33 | */ |
||
34 | public function hasBlocks() |
||
38 | |||
39 | /** |
||
40 | * Get a text block by name, if exists. |
||
41 | * |
||
42 | * @param string $name Name of text block to get. |
||
43 | * |
||
44 | * @return Larafolio\Models\TextBlock|null |
||
45 | */ |
||
46 | public function block($name) |
||
50 | |||
51 | /** |
||
52 | * Get block text by block name, if block exists. |
||
53 | * |
||
54 | * @param string $name Name of text block to get. |
||
55 | * @param bool $formatted If true, return formmated text. |
||
56 | * |
||
57 | * @return string|null |
||
58 | */ |
||
59 | public function blockText($name, $formatted = true) |
||
71 | } |
||
72 |
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.