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 |
||
| 15 | class Record extends \App\BaseModel |
||
| 16 | { |
||
| 17 | /** @var string Module name. */ |
||
| 18 | protected $moduleName; |
||
| 19 | |||
| 20 | /** @var \YF\Modules\Base\Model\Module Module model instance. */ |
||
| 21 | protected $moduleModel; |
||
| 22 | |||
| 23 | /** @var array Information about inventory. */ |
||
| 24 | protected $inventoryData = []; |
||
| 25 | |||
| 26 | /** @var array Information about summary inventory. */ |
||
| 27 | protected $inventorySummaryData = []; |
||
| 28 | |||
| 29 | /** @var string Record ID. */ |
||
| 30 | protected $id; |
||
| 31 | |||
| 32 | /** @var string Record name. */ |
||
| 33 | protected $name = ''; |
||
| 34 | |||
| 35 | /** @var array Privileges. */ |
||
| 36 | protected $privileges = []; |
||
| 37 | |||
| 38 | /** @var array Custom data. */ |
||
| 39 | protected $customData = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Static Function to get the instance of a clean Record for the given module name. |
||
| 43 | * |
||
| 44 | * @param string $module |
||
| 45 | * |
||
| 46 | * @return \self |
||
| 47 | */ |
||
| 48 | public static function getInstance(string $module): self |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Static function to get the instance of record. |
||
| 57 | * |
||
| 58 | * @param string $moduleName |
||
| 59 | * @param int $recordId |
||
| 60 | * @param array $headers |
||
| 61 | * |
||
| 62 | * @return \self |
||
| 63 | */ |
||
| 64 | public static function getInstanceById(string $moduleName, int $recordId, array $headers = []): self |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Sets information about inventory. |
||
| 84 | * |
||
| 85 | * @param array $values |
||
| 86 | * @param array $summary |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function setInventoryData(array $values, array $summary = []) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns information about inventory. |
||
| 98 | * |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | public function getInventoryData() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns information about summary inventory. |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function getInventorySummary() |
||
| 115 | |||
| 116 | public function isInventory() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Function to get the id of the record. |
||
| 123 | * |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getId(): int |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Function to set the id of the record. |
||
| 133 | * |
||
| 134 | * @param int $value |
||
| 135 | * |
||
| 136 | * @return self |
||
| 137 | */ |
||
| 138 | public function setId(int $value): self |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Function to get the raw value. |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getRawData(): array |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set raw data. |
||
| 156 | * |
||
| 157 | * @param array $rawData |
||
| 158 | * |
||
| 159 | * @return self |
||
| 160 | */ |
||
| 161 | public function setRawData(array $rawData): self |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Function to get the raw value for a given key. |
||
| 169 | * |
||
| 170 | * @param string $key |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function getRawValue(string $key) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Function to set the raw value for a given key. |
||
| 181 | * |
||
| 182 | * @param string $key |
||
| 183 | * @param mixed $value |
||
| 184 | * |
||
| 185 | * @return self |
||
| 186 | */ |
||
| 187 | public function setRawValue(string $key, $value): self |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Function to get the name of the module to which the record belongs. |
||
| 195 | * |
||
| 196 | * @return string - Record Module Name |
||
| 197 | */ |
||
| 198 | public function getModuleName(): string |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get record module model instance. |
||
| 205 | * |
||
| 206 | * @return \YF\Modules\Base\Model\Module - Record module model instance |
||
| 207 | */ |
||
| 208 | public function getModuleModel(): Module |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set record name. |
||
| 215 | * |
||
| 216 | * @param string $name |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function setName(string $name) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Record name. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getName() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get custom data. |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function getCustomData(): array |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Function to get the raw value for a given key. |
||
| 247 | * |
||
| 248 | * @param string $key |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getDisplayValue(string $key): string |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get list display value. |
||
| 259 | * |
||
| 260 | * @param string $key |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function getListDisplayValue(string $key): string |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Function to set the name of the module to which the record belongs. |
||
| 282 | * |
||
| 283 | * @param string $moduleName |
||
| 284 | * |
||
| 285 | * @return \self |
||
| 286 | */ |
||
| 287 | public function setModuleName(string $moduleName): self |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Function to get the list view actions for the record. |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function getRecordListViewActions(): string |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Function checks if there are permissions to preview record. |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function isViewable() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Function to get the Detail View url for the record. |
||
| 344 | * |
||
| 345 | * @return string - Record Detail View Url |
||
| 346 | */ |
||
| 347 | public function getDetailViewUrl() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Function checks if there are permissions to edit record. |
||
| 354 | * |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | View Code Duplication | public function isEditable(): bool |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Function to get the Edit View url for the record. |
||
| 367 | * |
||
| 368 | * @return string - Record Edit View Url |
||
| 369 | */ |
||
| 370 | public function getEditViewUrl() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Function checks if there are permissions to delete record. |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function isDeletable(): bool |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Function checks permissions to action. |
||
| 390 | * |
||
| 391 | * @param string $actionName |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function isPermitted(string $actionName): bool |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Function to get the delete action url for the record. |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getDeleteUrl() |
||
| 412 | } |
||
| 413 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.