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 |
||
19 | class AttributeDecorator implements AttributeDecoratorInterface |
||
20 | { |
||
21 | /** |
||
22 | * Server. |
||
23 | * |
||
24 | * @var Server |
||
25 | */ |
||
26 | protected $server; |
||
27 | |||
28 | /** |
||
29 | * Role decorator. |
||
30 | * |
||
31 | * @var RoleAttribueDecorator |
||
32 | */ |
||
33 | protected $role_decorator; |
||
34 | |||
35 | /** |
||
36 | * Custom attributes. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $custom = []; |
||
41 | |||
42 | /** |
||
43 | * Init. |
||
44 | * |
||
45 | * @param Server $server |
||
46 | * @param RoleAttribueDecorator $role_decorator |
||
47 | */ |
||
48 | public function __construct(Server $server, RoleAttribueDecorator $role_decorator) |
||
53 | |||
54 | /** |
||
55 | * Decorate attributes. |
||
56 | * |
||
57 | * @param array $message |
||
58 | * @param array $attributes |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | View Code Duplication | public function decorate(array $message, array $attributes = []): array |
|
73 | |||
74 | /** |
||
75 | * Add decorator. |
||
76 | * |
||
77 | * @param string $attribute |
||
78 | * @param Closure $decorator |
||
79 | * |
||
80 | * @return AttributeDecorator |
||
81 | */ |
||
82 | public function addDecorator(string $attribute, Closure $decorator): self |
||
88 | |||
89 | /** |
||
90 | * Get Attributes. |
||
91 | * |
||
92 | * @param array $message |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | protected function getAttributes(array $message): array |
||
114 | |||
115 | /** |
||
116 | * Execute closures. |
||
117 | * |
||
118 | * @param array $message |
||
119 | * @param array $attributes |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | View Code Duplication | protected function translateAttributes(array $message, array $attributes): array |
|
141 | } |
||
142 |
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.