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 |
||
12 | class StaticPageView extends BasePageView implements TemplateReadyDocument |
||
13 | { |
||
14 | /** @var JailedDocument */ |
||
15 | private $jailInstance; |
||
16 | |||
17 | /** @var JailedDocument[] */ |
||
18 | private $jailedChildPageViews; |
||
19 | |||
20 | /** @var StaticPageView[] */ |
||
21 | private $childPageViews = []; |
||
22 | |||
23 | /** |
||
24 | * StaticPageView constructor. |
||
25 | */ |
||
26 | 28 | public function __construct(File $file) |
|
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | 28 | public function createJail() |
|
53 | |||
54 | /** |
||
55 | * Get child PageViews. |
||
56 | * |
||
57 | * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of |
||
58 | * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent. |
||
59 | * |
||
60 | * @return StaticPageView[] |
||
61 | */ |
||
62 | 3 | public function &getChildren() |
|
66 | |||
67 | /** |
||
68 | * Get the child PageViews. |
||
69 | * |
||
70 | * @return JailedDocument[] |
||
71 | */ |
||
72 | 3 | public function getJailedChildren() |
|
86 | |||
87 | /** |
||
88 | * Get the PageView's body written in a templating language. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 10 | public function getContent() |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | View Code Duplication | public function jsonSerialize() |
|
108 | } |
||
109 |
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.