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