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 Manager implements \OCP\Calendar\Resource\IManager { |
|
|
|||
30 | |||
31 | /** @var IServerContainer */ |
||
32 | private $server; |
||
33 | |||
34 | /** @var string[] holds all registered resource backends */ |
||
35 | private $backends = []; |
||
36 | |||
37 | /** @var IBackend[] holds all backends that have been initialized already */ |
||
38 | private $initializedBackends = []; |
||
39 | |||
40 | /** |
||
41 | * Manager constructor. |
||
42 | * |
||
43 | * @param IServerContainer $server |
||
44 | */ |
||
45 | public function __construct(IServerContainer $server) { |
||
48 | |||
49 | /** |
||
50 | * Registers a resource backend |
||
51 | * |
||
52 | * @param string $backendClass |
||
53 | * @return void |
||
54 | * @since 14.0.0 |
||
55 | */ |
||
56 | public function registerBackend(string $backendClass) { |
||
59 | |||
60 | /** |
||
61 | * Unregisters a resource backend |
||
62 | * |
||
63 | * @param string $backendClass |
||
64 | * @return void |
||
65 | * @since 14.0.0 |
||
66 | */ |
||
67 | public function unregisterBackend(string $backendClass) { |
||
70 | |||
71 | /** |
||
72 | * @return IBackend[] |
||
73 | * @throws \OCP\AppFramework\QueryException |
||
74 | * @since 14.0.0 |
||
75 | */ |
||
76 | public function getBackends():array { |
||
87 | |||
88 | /** |
||
89 | * @param string $backendId |
||
90 | * @throws \OCP\AppFramework\QueryException |
||
91 | * @return IBackend|null |
||
92 | */ |
||
93 | public function getBackend($backendId) { |
||
103 | |||
104 | /** |
||
105 | * removes all registered backend instances |
||
106 | * @return void |
||
107 | * @since 14.0.0 |
||
108 | */ |
||
109 | public function clear() { |
||
113 | } |
||
114 |
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.