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 |
||
42 | class UserStoragesService extends StoragesService implements IUserStoragesService { |
||
43 | use UserTrait; |
||
44 | |||
45 | /** |
||
46 | * Create a user storages service |
||
47 | * |
||
48 | * @param IStoragesBackendService $backendService |
||
49 | * @param DBConfigService $dbConfig |
||
50 | * @param IUserSession $userSession user session |
||
51 | * @param IUserMountCache $userMountCache |
||
52 | */ |
||
53 | public function __construct( |
||
54 | IStoragesBackendService $backendService, |
||
55 | DBConfigService $dbConfig, |
||
56 | IUserSession $userSession, |
||
57 | IUserMountCache $userMountCache |
||
58 | ) { |
||
59 | $this->userSession = $userSession; |
||
60 | $this->userMountCache = $userMountCache; |
||
61 | parent::__construct($backendService, $dbConfig, $userMountCache); |
||
62 | } |
||
63 | |||
64 | protected function readDBConfig() { |
||
67 | |||
68 | /** |
||
69 | * Triggers $signal for all applicable users of the given |
||
70 | * storage |
||
71 | * |
||
72 | * @param IStorageConfig $storage storage data |
||
73 | * @param string $signal signal to trigger |
||
74 | */ |
||
75 | protected function triggerHooks(IStorageConfig $storage, $signal) { |
||
86 | |||
87 | /** |
||
88 | * Triggers signal_create_mount or signal_delete_mount to |
||
89 | * accommodate for additions/deletions in applicableUsers |
||
90 | * and applicableGroups fields. |
||
91 | * |
||
92 | * @param IStorageConfig $oldStorage old storage data |
||
93 | * @param IStorageConfig $newStorage new storage data |
||
94 | */ |
||
95 | protected function triggerChangeHooks(IStorageConfig $oldStorage, IStorageConfig $newStorage) { |
||
102 | |||
103 | protected function getType() { |
||
106 | |||
107 | /** |
||
108 | * Add new storage to the configuration |
||
109 | * |
||
110 | * @param IStorageConfig $newStorage storage attributes |
||
111 | * |
||
112 | * @return IStorageConfig storage config, with added id |
||
113 | */ |
||
114 | public function addStorage(IStorageConfig $newStorage) { |
||
119 | |||
120 | /** |
||
121 | * Update storage to the configuration |
||
122 | * |
||
123 | * @param IStorageConfig $updatedStorage storage attributes |
||
124 | * |
||
125 | * @return IStorageConfig storage config |
||
126 | * @throws NotFoundException if the given storage does not exist in the config |
||
127 | */ |
||
128 | public function updateStorage(IStorageConfig $updatedStorage) { |
||
132 | |||
133 | /** |
||
134 | * Get the visibility type for this controller, used in validation |
||
135 | * |
||
136 | * @return string IStoragesBackendService::VISIBILITY_* constants |
||
137 | */ |
||
138 | public function getVisibilityType() { |
||
141 | |||
142 | protected function isApplicable(IStorageConfig $config) { |
||
145 | |||
146 | /** |
||
147 | * Deletes the storages mounted to a user |
||
148 | * @param IUser $user |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function deleteAllMountsForUser(IUser $user) { |
||
179 | } |
||
180 |
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.