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 |
||
13 | View Code Duplication | class RelationshipNotFoundException extends DocumentException |
|
|
|||
14 | { |
||
15 | /** |
||
16 | * Name of not found relationship |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $name; |
||
21 | |||
22 | /** |
||
23 | * Container inside of which a relationship has not been found |
||
24 | * |
||
25 | * @var RelationshipsAwareInterface |
||
26 | */ |
||
27 | protected $container; |
||
28 | |||
29 | /** |
||
30 | * RelationshipNotFoundException constructor. |
||
31 | * |
||
32 | * @param RelationshipsAwareInterface $container |
||
33 | * @param string $name |
||
34 | * @param \Exception | null $previous |
||
35 | */ |
||
36 | 2 | public function __construct(RelationshipsAwareInterface $container, string $name, \Exception $previous = null) |
|
49 | |||
50 | /** |
||
51 | * Get name of not found relationship |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 1 | public function getName(): string |
|
59 | |||
60 | /** |
||
61 | * Get container inside of which a relationship has not been found |
||
62 | * |
||
63 | * @return RelationshipsAwareInterface |
||
64 | */ |
||
65 | 1 | public function getContainer(): RelationshipsAwareInterface |
|
69 | } |
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.