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:
Complex classes like LogHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LogHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class LogHelper |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Summary of getRequestLogs |
||
| 28 | * |
||
| 29 | * @param int $requestId ID of the request to get logs for |
||
| 30 | * @param PdoDatabase $db Database to use |
||
| 31 | * |
||
| 32 | * @return array|bool |
||
|
|
|||
| 33 | */ |
||
| 34 | public static function getRequestLogs($requestId, PdoDatabase $db) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Summary of getRequestLogsWithComments |
||
| 61 | * |
||
| 62 | * @param int $requestId |
||
| 63 | * @param PdoDatabase $db |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public static function getRequestLogsWithComments($requestId, PdoDatabase $db) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Summary of getLogDescription |
||
| 115 | * |
||
| 116 | * @param Log $entry |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public static function getLogDescription(Log $entry) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param PdoDatabase $database |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public static function getLogActions(PdoDatabase $database) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Summary of getLogs |
||
| 245 | * |
||
| 246 | * @param PdoDatabase $database |
||
| 247 | * @param string|null $userFilter |
||
| 248 | * @param string|null $actionFilter |
||
| 249 | * @param string|null $objectTypeFilter |
||
| 250 | * @param integer|null $objectFilter |
||
| 251 | * @param integer $limit |
||
| 252 | * @param integer $offset |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public static function getLogs( |
||
| 257 | PdoDatabase $database, |
||
| 258 | $userFilter, |
||
| 259 | $actionFilter, |
||
| 260 | $objectTypeFilter = null, |
||
| 261 | $objectFilter = null, |
||
| 262 | $limit = 100, |
||
| 263 | $offset = 0 |
||
| 264 | ) { |
||
| 265 | $whereClause = <<<TXT |
||
| 266 | (:userFilter = 0 OR user = :userid) |
||
| 267 | AND (:actionFilter = 0 OR action = :action) |
||
| 268 | AND (:objectFilter = 0 OR objectid = :object) |
||
| 269 | AND (:objectTypeFilter = 0 OR objecttype = :objectType) |
||
| 270 | TXT; |
||
| 271 | $searchSqlStatement = "SELECT * FROM log WHERE $whereClause ORDER BY timestamp DESC LIMIT :limit OFFSET :offset;"; |
||
| 272 | $countSqlStatement = "SELECT COUNT(1) FROM log WHERE $whereClause;"; |
||
| 273 | |||
| 274 | $searchStatement = $database->prepare($searchSqlStatement); |
||
| 275 | $countStatement = $database->prepare($countSqlStatement); |
||
| 276 | |||
| 277 | $searchStatement->bindValue(":limit", $limit, PDO::PARAM_INT); |
||
| 278 | $searchStatement->bindValue(":offset", $offset, PDO::PARAM_INT); |
||
| 279 | |||
| 280 | if ($userFilter === null) { |
||
| 281 | $searchStatement->bindValue(":userFilter", 0, PDO::PARAM_INT); |
||
| 282 | $countStatement->bindValue(":userFilter", 0, PDO::PARAM_INT); |
||
| 283 | $searchStatement->bindValue(":userid", 0, PDO::PARAM_INT); |
||
| 284 | $countStatement->bindValue(":userid", 0, PDO::PARAM_INT); |
||
| 285 | } |
||
| 286 | else { |
||
| 287 | $searchStatement->bindValue(":userFilter", 1, PDO::PARAM_INT); |
||
| 288 | $countStatement->bindValue(":userFilter", 1, PDO::PARAM_INT); |
||
| 289 | $searchStatement->bindValue(":userid", User::getByUsername($userFilter, $database)->getId(), |
||
| 290 | PDO::PARAM_INT); |
||
| 291 | $countStatement->bindValue(":userid", User::getByUsername($userFilter, $database)->getId(), PDO::PARAM_INT); |
||
| 292 | } |
||
| 293 | |||
| 294 | View Code Duplication | if ($actionFilter === null) { |
|
| 295 | $searchStatement->bindValue(":actionFilter", 0, PDO::PARAM_INT); |
||
| 296 | $countStatement->bindValue(":actionFilter", 0, PDO::PARAM_INT); |
||
| 297 | $searchStatement->bindValue(":action", "", PDO::PARAM_STR); |
||
| 298 | $countStatement->bindValue(":action", "", PDO::PARAM_STR); |
||
| 299 | } |
||
| 300 | else { |
||
| 301 | $searchStatement->bindValue(":actionFilter", 1, PDO::PARAM_INT); |
||
| 302 | $countStatement->bindValue(":actionFilter", 1, PDO::PARAM_INT); |
||
| 303 | $searchStatement->bindValue(":action", $actionFilter, PDO::PARAM_STR); |
||
| 304 | $countStatement->bindValue(":action", $actionFilter, PDO::PARAM_STR); |
||
| 305 | } |
||
| 306 | |||
| 307 | View Code Duplication | if ($objectTypeFilter === null) { |
|
| 308 | $searchStatement->bindValue(":objectTypeFilter", 0, PDO::PARAM_INT); |
||
| 309 | $countStatement->bindValue(":objectTypeFilter", 0, PDO::PARAM_INT); |
||
| 310 | $searchStatement->bindValue(":objectType", "", PDO::PARAM_STR); |
||
| 311 | $countStatement->bindValue(":objectType", "", PDO::PARAM_STR); |
||
| 312 | } |
||
| 313 | else { |
||
| 314 | $searchStatement->bindValue(":objectTypeFilter", 1, PDO::PARAM_INT); |
||
| 315 | $countStatement->bindValue(":objectTypeFilter", 1, PDO::PARAM_INT); |
||
| 316 | $searchStatement->bindValue(":objectType", $objectTypeFilter, PDO::PARAM_STR); |
||
| 317 | $countStatement->bindValue(":objectType", $objectTypeFilter, PDO::PARAM_STR); |
||
| 318 | } |
||
| 319 | |||
| 320 | View Code Duplication | if ($objectFilter === null) { |
|
| 321 | $searchStatement->bindValue(":objectFilter", 0, PDO::PARAM_INT); |
||
| 322 | $countStatement->bindValue(":objectFilter", 0, PDO::PARAM_INT); |
||
| 323 | $searchStatement->bindValue(":object", "", PDO::PARAM_STR); |
||
| 324 | $countStatement->bindValue(":object", "", PDO::PARAM_STR); |
||
| 325 | } |
||
| 326 | else { |
||
| 327 | $searchStatement->bindValue(":objectFilter", 1, PDO::PARAM_INT); |
||
| 328 | $countStatement->bindValue(":objectFilter", 1, PDO::PARAM_INT); |
||
| 329 | $searchStatement->bindValue(":object", $objectFilter, PDO::PARAM_INT); |
||
| 330 | $countStatement->bindValue(":object", $objectFilter, PDO::PARAM_INT); |
||
| 331 | } |
||
| 332 | |||
| 333 | if (!$countStatement->execute()) { |
||
| 334 | return array(false, false); |
||
| 335 | } |
||
| 336 | |||
| 337 | $count = $countStatement->fetchColumn(0); |
||
| 338 | $countStatement->closeCursor(); |
||
| 339 | |||
| 340 | if ($searchStatement->execute()) { |
||
| 341 | $data = $searchStatement->fetchAll(PDO::FETCH_CLASS, Log::class); |
||
| 342 | |||
| 343 | /** @var Log $entry */ |
||
| 344 | foreach ($data as $entry) { |
||
| 345 | $entry->setDatabase($database); |
||
| 346 | } |
||
| 347 | |||
| 348 | return array($data, $count); |
||
| 349 | } |
||
| 350 | |||
| 351 | return array(false, false); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * This returns a HTML |
||
| 356 | * |
||
| 357 | * @param string $objectId |
||
| 358 | * @param string $objectType |
||
| 359 | * @param PdoDatabase $database |
||
| 360 | * @param SiteConfiguration $configuration |
||
| 361 | * |
||
| 362 | * @return null|string |
||
| 363 | * @category Security-Critical |
||
| 364 | */ |
||
| 365 | private static function getObjectDescription($objectId, $objectType, PdoDatabase $database, SiteConfiguration $configuration) |
||
| 366 | { |
||
| 367 | if ($objectType == '') { |
||
| 368 | return null; |
||
| 369 | } |
||
| 370 | |||
| 371 | $baseurl = $configuration->getBaseUrl(); |
||
| 372 | |||
| 373 | switch ($objectType) { |
||
| 374 | case 'Ban': |
||
| 375 | /** @var Ban $ban */ |
||
| 376 | $ban = Ban::getById($objectId, $database); |
||
| 377 | |||
| 378 | return 'Ban #' . $objectId . " (" . htmlentities($ban->getTarget()) . ")</a>"; |
||
| 379 | View Code Duplication | case 'EmailTemplate': |
|
| 380 | /** @var EmailTemplate $emailTemplate */ |
||
| 381 | $emailTemplate = EmailTemplate::getById($objectId, $database); |
||
| 382 | $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
||
| 383 | |||
| 384 | return <<<HTML |
||
| 385 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
||
| 386 | HTML; |
||
| 387 | case 'SiteNotice': |
||
| 388 | return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
||
| 389 | View Code Duplication | case 'Request': |
|
| 390 | /** @var Request $request */ |
||
| 391 | $request = Request::getById($objectId, $database); |
||
| 392 | $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
||
| 393 | |||
| 394 | return <<<HTML |
||
| 395 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
||
| 396 | HTML; |
||
| 397 | View Code Duplication | case 'User': |
|
| 398 | /** @var User $user */ |
||
| 399 | $user = User::getById($objectId, $database); |
||
| 400 | $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
||
| 401 | |||
| 402 | return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
||
| 403 | View Code Duplication | case 'WelcomeTemplate': |
|
| 404 | /** @var WelcomeTemplate $welcomeTemplate */ |
||
| 405 | $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
||
| 406 | $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
||
| 407 | |||
| 408 | return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?id={$objectId}\">{$userCode}</a>"; |
||
| 409 | default: |
||
| 410 | return '[' . $objectType . " " . $objectId . ']'; |
||
| 411 | break; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param Log[] $logs |
||
| 417 | * @param PdoDatabase $database |
||
| 418 | * @param SiteConfiguration $configuration |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | * @throws Exception |
||
| 422 | */ |
||
| 423 | public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
||
| 476 | } |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.