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 Notification 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 Notification, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 11 | class Notification extends DataObject  | 
            ||
| 12 | { | 
            ||
| 13 | private $date;  | 
            ||
| 14 | private $type;  | 
            ||
| 15 | private $text;  | 
            ||
| 16 | |||
| 17 | #region database operations  | 
            ||
| 18 | public function delete()  | 
            ||
| 19 | 	{ | 
            ||
| 20 | 		throw new Exception("You shouldn't be doing this..."); | 
            ||
| 21 | }  | 
            ||
| 22 | |||
| 23 | public function save()  | 
            ||
| 41 | }  | 
            ||
| 42 | }  | 
            ||
| 43 | #endregion  | 
            ||
| 44 | |||
| 45 | #region properties  | 
            ||
| 46 | |||
| 47 | public function getDate()  | 
            ||
| 48 | 	{ | 
            ||
| 49 | return $this->date;  | 
            ||
| 50 | }  | 
            ||
| 51 | |||
| 52 | public function getType()  | 
            ||
| 53 | 	{ | 
            ||
| 54 | return $this->type;  | 
            ||
| 55 | }  | 
            ||
| 56 | |||
| 57 | public function getText()  | 
            ||
| 58 | 	{ | 
            ||
| 59 | return $this->text;  | 
            ||
| 60 | }  | 
            ||
| 61 | |||
| 62 | public function setDate($date)  | 
            ||
| 63 | 	{ | 
            ||
| 64 | $this->date = $date;  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * Summary of setType  | 
            ||
| 69 | * @param int $type  | 
            ||
| 70 | */  | 
            ||
| 71 | public function setType($type)  | 
            ||
| 72 | 	{ | 
            ||
| 73 | $this->type = $type;  | 
            ||
| 74 | }  | 
            ||
| 75 | |||
| 76 | /**  | 
            ||
| 77 | * Summary of setText  | 
            ||
| 78 | * @param string $text  | 
            ||
| 79 | */  | 
            ||
| 80 | public function setText($text)  | 
            ||
| 81 | 	{ | 
            ||
| 82 | $this->text = $text;  | 
            ||
| 83 | }  | 
            ||
| 84 | #endregion  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Send a notification  | 
            ||
| 88 | * @param string $message The text to send  | 
            ||
| 89 | */  | 
            ||
| 90 | protected static function send($message)  | 
            ||
| 91 | 	{ | 
            ||
| 92 | global $ircBotNotificationType, $whichami, $ircBotNotificationsEnabled;  | 
            ||
| 93 | |||
| 94 | 		if (!$ircBotNotificationsEnabled) { | 
            ||
| 95 | return;  | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 | 		$blacklist = array("DCC", "CCTP", "PRIVMSG"); | 
            ||
| 99 | $message = str_replace($blacklist, "(IRC Blacklist)", $message); //Lets stop DCC etc  | 
            ||
| 100 | |||
| 101 | $msg = IrcColourCode::RESET . IrcColourCode::BOLD . "[$whichami]" . IrcColourCode::RESET . ": $message";  | 
            ||
| 102 | |||
| 103 | 		try { | 
            ||
| 104 | 			$database = gGetDb('notifications'); | 
            ||
| 105 | |||
| 106 | $notification = new Notification();  | 
            ||
| 107 | $notification->setDatabase($database);  | 
            ||
| 108 | $notification->setType($ircBotNotificationType);  | 
            ||
| 109 | $notification->setText($msg);  | 
            ||
| 110 | |||
| 111 | $notification->save();  | 
            ||
| 112 | }  | 
            ||
| 113 | 		catch (Exception $ex) { | 
            ||
| 114 | // OK, so we failed to send the notification - that db might be down?  | 
            ||
| 115 | // This is non-critical, so silently fail.  | 
            ||
| 116 | |||
| 117 | // Disable notifications for remainder of request.  | 
            ||
| 118 | $ircBotNotificationsEnabled = false;  | 
            ||
| 119 | }  | 
            ||
| 120 | }  | 
            ||
| 121 | |||
| 122 | #region user management  | 
            ||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * send a new user notification  | 
            ||
| 126 | * @param User $user  | 
            ||
| 127 | */  | 
            ||
| 128 | public static function userNew(User $user)  | 
            ||
| 129 | 	{ | 
            ||
| 130 | 		self::send("New user: {$user->getUsername()}"); | 
            ||
| 131 | }  | 
            ||
| 132 | |||
| 133 | /**  | 
            ||
| 134 | * send an approved notification  | 
            ||
| 135 | * @param User $user  | 
            ||
| 136 | */  | 
            ||
| 137 | public static function userApproved(User $user)  | 
            ||
| 138 | 	{ | 
            ||
| 139 | 		self::send("{$user->getUsername()} approved by " . User::getCurrent()->getUsername()); | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | /**  | 
            ||
| 143 | * send a promoted notification  | 
            ||
| 144 | * @param User $user  | 
            ||
| 145 | */  | 
            ||
| 146 | public static function userPromoted(User $user)  | 
            ||
| 147 | 	{ | 
            ||
| 148 | 		self::send("{$user->getUsername()} promoted to tool admin by " . User::getCurrent()->getUsername()); | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | /**  | 
            ||
| 152 | * send a declined notification  | 
            ||
| 153 | * @param User $user  | 
            ||
| 154 | * @param string $reason the reason the user was declined  | 
            ||
| 155 | */  | 
            ||
| 156 | public static function userDeclined(User $user, $reason)  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | /**  | 
            ||
| 162 | * send a demotion notification  | 
            ||
| 163 | * @param User $user  | 
            ||
| 164 | * @param string $reason the reason the user was demoted  | 
            ||
| 165 | */  | 
            ||
| 166 | public static function userDemoted(User $user, $reason)  | 
            ||
| 167 | 	{ | 
            ||
| 168 | 		self::send("{$user->getUsername()} demoted by " . User::getCurrent()->getUsername() . " ($reason)"); | 
            ||
| 169 | }  | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * send a suspended notification  | 
            ||
| 173 | * @param User $user  | 
            ||
| 174 | * @param string $reason The reason the user has been suspended  | 
            ||
| 175 | */  | 
            ||
| 176 | public static function userSuspended(User $user, $reason)  | 
            ||
| 177 | 	{ | 
            ||
| 178 | 		self::send("{$user->getUsername()} suspended by " . User::getCurrent()->getUsername() . " ($reason)"); | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | /**  | 
            ||
| 182 | * Send a preference change notification  | 
            ||
| 183 | * @param User $user  | 
            ||
| 184 | */  | 
            ||
| 185 | public static function userPrefChange(User $user)  | 
            ||
| 186 | 	{ | 
            ||
| 187 | 		self::send("{$user->getUsername()}'s preferences were changed by " . User::getCurrent()->getUsername()); | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Send a user renamed notification  | 
            ||
| 192 | * @param User $user  | 
            ||
| 193 | * @param mixed $old  | 
            ||
| 194 | */  | 
            ||
| 195 | public static function userRenamed(User $user, $old)  | 
            ||
| 196 | 	{ | 
            ||
| 197 | 		self::send(User::getCurrent()->getUsername() . " renamed $old to {$user->getUsername()}"); | 
            ||
| 198 | }  | 
            ||
| 199 | |||
| 200 | #endregion  | 
            ||
| 201 | |||
| 202 | #region Interface Messages  | 
            ||
| 203 | |||
| 204 | /**  | 
            ||
| 205 | * Summary of interfaceMessageEdited  | 
            ||
| 206 | * @param InterfaceMessage $message  | 
            ||
| 207 | */  | 
            ||
| 208 | public static function interfaceMessageEdited(InterfaceMessage $message)  | 
            ||
| 209 | 	{ | 
            ||
| 210 | 		self::send("Message {$message->getDescription()} ({$message->getId()}) edited by " . User::getCurrent()->getUsername()); | 
            ||
| 211 | }  | 
            ||
| 212 | #endregion  | 
            ||
| 213 | |||
| 214 | #region Welcome Templates  | 
            ||
| 215 | /**  | 
            ||
| 216 | * Summary of welcomeTemplateCreated  | 
            ||
| 217 | * @param WelcomeTemplate $template  | 
            ||
| 218 | */  | 
            ||
| 219 | public static function welcomeTemplateCreated(WelcomeTemplate $template)  | 
            ||
| 220 | 	{ | 
            ||
| 221 | 		self::send("Welcome template {$template->getId()} created by " . User::getCurrent()->getUsername()); | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * Summary of welcomeTemplateDeleted  | 
            ||
| 226 | * @param int $templateid  | 
            ||
| 227 | */  | 
            ||
| 228 | public static function welcomeTemplateDeleted($templateid)  | 
            ||
| 229 | 	{ | 
            ||
| 230 | 		self::send("Welcome template {$templateid} deleted by " . User::getCurrent()->getUsername()); | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | * Summary of welcomeTemplateEdited  | 
            ||
| 235 | * @param WelcomeTemplate $template  | 
            ||
| 236 | */  | 
            ||
| 237 | public static function welcomeTemplateEdited(WelcomeTemplate $template)  | 
            ||
| 238 | 	{ | 
            ||
| 239 | 		self::send("Welcome template {$template->getId()} edited by " . User::getCurrent()->getUsername()); | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | #endregion  | 
            ||
| 243 | |||
| 244 | #region bans  | 
            ||
| 245 | /**  | 
            ||
| 246 | * Summary of banned  | 
            ||
| 247 | * @param Ban $ban  | 
            ||
| 248 | */  | 
            ||
| 249 | public static function banned(Ban $ban)  | 
            ||
| 250 | 	{ | 
            ||
| 251 | 		if ($ban->getDuration() == -1) { | 
            ||
| 252 | $duration = "indefinitely";  | 
            ||
| 253 | }  | 
            ||
| 254 | 		else { | 
            ||
| 255 | 			$duration = "until " . date("F j, Y, g:i a", $ban->getDuration()); | 
            ||
| 256 | }  | 
            ||
| 257 | |||
| 258 | $username = User::getCurrent()->getUsername();  | 
            ||
| 259 | |||
| 260 | 		self::send("{$ban->getTarget()} banned by {$username} for '{$ban->getReason()}' {$duration}"); | 
            ||
| 261 | }  | 
            ||
| 262 | |||
| 263 | /**  | 
            ||
| 264 | * Summary of unbanned  | 
            ||
| 265 | * @param Ban $ban  | 
            ||
| 266 | * @param string $unbanreason  | 
            ||
| 267 | */  | 
            ||
| 268 | public static function unbanned(Ban $ban, $unbanreason)  | 
            ||
| 269 | 	{ | 
            ||
| 270 | 		self::send($ban->getTarget() . " unbanned by " . User::getCurrent()->getUsername() . " (" . $unbanreason . ")"); | 
            ||
| 271 | }  | 
            ||
| 272 | |||
| 273 | #endregion  | 
            ||
| 274 | |||
| 275 | #region request management  | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * Summary of requestReceived  | 
            ||
| 279 | * @param Request $request  | 
            ||
| 280 | */  | 
            ||
| 281 | public static function requestReceived(Request $request)  | 
            ||
| 282 | 	{ | 
            ||
| 283 | global $baseurl;  | 
            ||
| 284 | |||
| 285 | self::send(  | 
            ||
| 286 | IrcColourCode::DARK_GREY . "[["  | 
            ||
| 287 | . IrcColourCode::DARK_GREEN . "acc:"  | 
            ||
| 288 | . IrcColourCode::ORANGE . $request->getId()  | 
            ||
| 289 | . IrcColourCode::DARK_GREY . "]]"  | 
            ||
| 290 | . IrcColourCode::RED . " N "  | 
            ||
| 291 | 			. IrcColourCode::DARK_BLUE . $baseurl . "/acc.php?action=zoom&id={$request->getId()} " | 
            ||
| 292 | . IrcColourCode::DARK_RED . "* "  | 
            ||
| 293 | . IrcColourCode::DARK_GREEN . $request->getName()  | 
            ||
| 294 | . IrcColourCode::DARK_RED . " * "  | 
            ||
| 295 | . IrcColourCode::RESET  | 
            ||
| 296 | );  | 
            ||
| 297 | }  | 
            ||
| 298 | |||
| 299 | /**  | 
            ||
| 300 | * Summary of requestDeferred  | 
            ||
| 301 | * @param Request $request  | 
            ||
| 302 | */  | 
            ||
| 303 | public static function requestDeferred(Request $request)  | 
            ||
| 304 | 	{ | 
            ||
| 305 | global $availableRequestStates;  | 
            ||
| 306 | |||
| 307 | $deferTo = $availableRequestStates[$request->getStatus()]['deferto'];  | 
            ||
| 308 | $username = User::getCurrent()->getUsername();  | 
            ||
| 309 | |||
| 310 | 		self::send("Request {$request->getId()} ({$request->getName()}) deferred to {$deferTo} by {$username}"); | 
            ||
| 311 | }  | 
            ||
| 312 | /**  | 
            ||
| 313 | *  | 
            ||
| 314 | * Summary of requestDeferredWithMail  | 
            ||
| 315 | * @param Request $request  | 
            ||
| 316 | */  | 
            ||
| 317 | public static function requestDeferredWithMail(Request $request)  | 
            ||
| 318 | 	{ | 
            ||
| 319 | global $availableRequestStates;  | 
            ||
| 320 | |||
| 321 | $deferTo = $availableRequestStates[$request->getStatus()]['deferto'];  | 
            ||
| 322 | $username = User::getCurrent()->getUsername();  | 
            ||
| 323 | |||
| 324 | 		self::send("Request {$request->getId()} ({$request->getName()}) deferred to {$deferTo} with an email by {$username}"); | 
            ||
| 325 | }  | 
            ||
| 326 | |||
| 327 | /**  | 
            ||
| 328 | * Summary of requestClosed  | 
            ||
| 329 | * @param Request $request  | 
            ||
| 330 | * @param string $closetype  | 
            ||
| 331 | */  | 
            ||
| 332 | public static function requestClosed(Request $request, $closetype)  | 
            ||
| 337 | }  | 
            ||
| 338 | |||
| 339 | /**  | 
            ||
| 340 | * Summary of sentMail  | 
            ||
| 341 | * @param Request $request  | 
            ||
| 342 | */  | 
            ||
| 343 | public static function sentMail(Request $request)  | 
            ||
| 344 | 	{ | 
            ||
| 345 | self::send(User::getCurrent()->getUsername()  | 
            ||
| 346 | 			. " sent an email related to Request {$request->getId()} ({$request->getName()})"); | 
            ||
| 347 | }  | 
            ||
| 348 | |||
| 349 | #endregion  | 
            ||
| 350 | |||
| 351 | #region reservations  | 
            ||
| 352 | |||
| 353 | /**  | 
            ||
| 354 | * Summary of requestReserved  | 
            ||
| 355 | * @param Request $request  | 
            ||
| 356 | */  | 
            ||
| 357 | public static function requestReserved(Request $request)  | 
            ||
| 358 | 	{ | 
            ||
| 359 | $username = User::getCurrent()->getUsername();  | 
            ||
| 360 | |||
| 361 | 		self::send("Request {$request->getId()} ({$request->getName()}) reserved by {$username}"); | 
            ||
| 362 | }  | 
            ||
| 363 | |||
| 364 | /**  | 
            ||
| 365 | * Summary of requestReserveBroken  | 
            ||
| 366 | * @param Request $request  | 
            ||
| 367 | */  | 
            ||
| 368 | public static function requestReserveBroken(Request $request)  | 
            ||
| 369 | 	{ | 
            ||
| 370 | $username = User::getCurrent()->getUsername();  | 
            ||
| 371 | |||
| 372 | 		self::send("Reservation on request {$request->getId()} ({$request->getName()}) broken by {$username}"); | 
            ||
| 373 | }  | 
            ||
| 374 | |||
| 375 | /**  | 
            ||
| 376 | * Summary of requestUnreserved  | 
            ||
| 377 | * @param Request $request  | 
            ||
| 378 | */  | 
            ||
| 379 | public static function requestUnreserved(Request $request)  | 
            ||
| 380 | 	{ | 
            ||
| 381 | 		self::send("Request {$request->getId()} ({$request->getName()}) is no longer being handled."); | 
            ||
| 382 | }  | 
            ||
| 383 | |||
| 384 | /**  | 
            ||
| 385 | * Summary of requestReservationSent  | 
            ||
| 386 | * @param Request $request  | 
            ||
| 387 | * @param User $target  | 
            ||
| 388 | */  | 
            ||
| 389 | public static function requestReservationSent(Request $request, User $target)  | 
            ||
| 390 | 	{ | 
            ||
| 391 | $username = User::getCurrent()->getUsername();  | 
            ||
| 392 | |||
| 393 | self::send(  | 
            ||
| 394 | 			"Reservation of request {$request->getId()} ({$request->getName()}) sent to {$target->getUsername()} by "  | 
            ||
| 395 | . $username);  | 
            ||
| 396 | }  | 
            ||
| 397 | |||
| 398 | #endregion  | 
            ||
| 399 | |||
| 400 | #region comments  | 
            ||
| 401 | |||
| 402 | /**  | 
            ||
| 403 | * Summary of commentCreated  | 
            ||
| 404 | * @param Comment $comment  | 
            ||
| 405 | */  | 
            ||
| 406 | public static function commentCreated(Comment $comment)  | 
            ||
| 407 | 	{ | 
            ||
| 408 | $req = $comment->getRequestObject();  | 
            ||
| 409 | $username = User::getCurrent()->getUsername();  | 
            ||
| 410 | $visibility = ($comment->getVisibility() == "admin" ? "private " : "");  | 
            ||
| 411 | |||
| 412 | 		self::send("{$username} posted a {$visibility}comment on request {$req->getId()} ({$req->getName()})"); | 
            ||
| 413 | }  | 
            ||
| 414 | |||
| 415 | /**  | 
            ||
| 416 | * Summary of commentEdited  | 
            ||
| 417 | * @param Comment $comment  | 
            ||
| 418 | */  | 
            ||
| 419 | public static function commentEdited(Comment $comment)  | 
            ||
| 420 | 	{ | 
            ||
| 421 | $req = $comment->getRequestObject();  | 
            ||
| 422 | $username = User::getCurrent()->getUsername();  | 
            ||
| 423 | |||
| 424 | 		self::send("Comment {$comment->getId()} on request {$req->getId()} ({$req->getName()}) edited by {$username}"); | 
            ||
| 425 | }  | 
            ||
| 426 | |||
| 427 | #endregion  | 
            ||
| 428 | |||
| 429 | #region email management (close reasons)  | 
            ||
| 430 | |||
| 431 | /**  | 
            ||
| 432 | * Summary of emailCreated  | 
            ||
| 433 | * @param EmailTemplate $template  | 
            ||
| 434 | */  | 
            ||
| 435 | public static function emailCreated(EmailTemplate $template)  | 
            ||
| 436 | 	{ | 
            ||
| 437 | 		self::send("Email {$template->getId()} ({$template->getName()}) created by " . User::getCurrent()->getUsername()); | 
            ||
| 438 | }  | 
            ||
| 439 | |||
| 440 | /**  | 
            ||
| 441 | * Summary of emailEdited  | 
            ||
| 442 | * @param EmailTemplate $template  | 
            ||
| 443 | */  | 
            ||
| 444 | public static function emailEdited(EmailTemplate $template)  | 
            ||
| 447 | }  | 
            ||
| 448 | |||
| 449 | #endregion  | 
            ||
| 450 | }  | 
            ||
| 451 | 
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.