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 |
||
29 | View Code Duplication | class ByFilter implements IFilter { |
|
|
|||
30 | |||
31 | /** @var IL10N */ |
||
32 | protected $l; |
||
33 | |||
34 | /** @var IURLGenerator */ |
||
35 | protected $url; |
||
36 | |||
37 | /** |
||
38 | * @param IL10N $l |
||
39 | * @param IURLGenerator $url |
||
40 | */ |
||
41 | public function __construct(IL10N $l, IURLGenerator $url) { |
||
45 | |||
46 | /** |
||
47 | * @return string Lowercase a-z only identifier |
||
48 | * @since 9.2.0 |
||
49 | */ |
||
50 | public function getIdentifier() { |
||
53 | |||
54 | /** |
||
55 | * @return string A translated string |
||
56 | * @since 9.2.0 |
||
57 | */ |
||
58 | public function getName() { |
||
61 | |||
62 | /** |
||
63 | * @return int |
||
64 | * @since 9.2.0 |
||
65 | */ |
||
66 | public function getPriority() { |
||
69 | |||
70 | /** |
||
71 | * @return string Full URL to an icon, empty string when none is given |
||
72 | * @since 9.2.0 |
||
73 | */ |
||
74 | public function getIcon() { |
||
77 | |||
78 | /** |
||
79 | * @param string[] $types |
||
80 | * @return string[] An array of allowed apps from which activities should be displayed |
||
81 | * @since 9.2.0 |
||
82 | */ |
||
83 | public function filterTypes(array $types) { |
||
86 | |||
87 | /** |
||
88 | * @return string[] An array of allowed apps from which activities should be displayed |
||
89 | * @since 9.2.0 |
||
90 | */ |
||
91 | public function allowedApps() { |
||
94 | } |
||
95 |
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.