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 |
||
| 49 | class FilesEvents { |
||
| 50 | |||
| 51 | |||
| 52 | use TArrayTools; |
||
| 53 | |||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $userId; |
||
| 57 | |||
| 58 | /** @var IFullTextSearchManager */ |
||
| 59 | private $fullTextSearchManager; |
||
| 60 | |||
| 61 | /** @var FilesService */ |
||
| 62 | private $filesService; |
||
| 63 | |||
| 64 | /** @var MiscService */ |
||
| 65 | private $miscService; |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * FilesEvents constructor. |
||
| 70 | * |
||
| 71 | * @param string $userId |
||
| 72 | * @param IFullTextSearchManager $fullTextSearchManager |
||
| 73 | * @param FilesService $filesService |
||
| 74 | * @param MiscService $miscService |
||
| 75 | */ |
||
| 76 | public function __construct( |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array $params |
||
| 89 | * |
||
| 90 | * @throws InvalidPathException |
||
| 91 | * @throws NotFoundException |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function onNewFile(array $params) { |
|
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * @param array $params |
||
| 108 | * |
||
| 109 | * @throws InvalidPathException |
||
| 110 | * @throws NotFoundException |
||
| 111 | */ |
||
| 112 | public function onFileUpdate(array $params) { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @param array $params |
||
| 127 | * |
||
| 128 | * @throws NotFoundException |
||
| 129 | * @throws InvalidPathException |
||
| 130 | */ |
||
| 131 | public function onFileRename(array $params) { |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * @param array $params |
||
| 146 | * |
||
| 147 | * @throws InvalidPathException |
||
| 148 | * @throws NotFoundException |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function onFileTrash(array $params) { |
|
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * @param array $params |
||
| 168 | * |
||
| 169 | * @throws InvalidPathException |
||
| 170 | * @throws NotFoundException |
||
| 171 | */ |
||
| 172 | public function onFileRestore(array $params) { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @param array $params |
||
| 187 | */ |
||
| 188 | public function onFileDelete(array $params) { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @param array $params |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function onFileShare(array $params) { |
|
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @param array $params |
||
| 216 | */ |
||
| 217 | View Code Duplication | public function onFileUnshare(array $params) { |
|
| 227 | |||
| 228 | |||
| 229 | public function onNewScannedFile2(array $params) { |
||
| 231 | |||
| 232 | |||
| 233 | public function onNewScannedFile(array $params) { |
||
| 235 | } |
||
| 236 | |||
| 240 |
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.