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 LERN |
||
| 16 | {
|
||
| 17 | /** |
||
| 18 | * @var Exception |
||
| 19 | */ |
||
| 20 | private $exception; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Notifier |
||
| 24 | */ |
||
| 25 | private $notifier; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Recorder |
||
| 29 | */ |
||
| 30 | private $recorder; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param Notifier|null $notifier Notifier instance |
||
| 34 | * @param Recorder|null $recorder Recorder instance |
||
| 35 | */ |
||
| 36 | 36 | public function __construct(Notifier $notifier = null, Recorder $recorder = null) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Will execute record and notify methods |
||
| 44 | * @param Exception $e The exception to use |
||
| 45 | * @return ExceptionModel the recorded Eloquent Model |
||
| 46 | */ |
||
| 47 | 6 | public function handle(Exception $e) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Stores the exception in the database |
||
| 56 | * @param Exception $e The exception to use |
||
| 57 | * @return \Tylercd100\LERN\Models\ExceptionModel|false The recorded Exception as an Eloquent Model |
||
| 58 | */ |
||
| 59 | 6 | public function record(Exception $e) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Will send the exception to all monolog handlers |
||
| 67 | * @param Exception $e The exception to use |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | 6 | public function notify(Exception $e) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Pushes on another Monolog Handler |
||
| 78 | * @param HandlerInterface $handler The handler instance to add on |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | 3 | public function pushHandler(HandlerInterface $handler) {
|
|
| 82 | 3 | $this->notifier->pushHandler($handler); |
|
| 83 | 3 | return $this; |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get Notifier |
||
| 88 | * @return \Tylercd100\LERN\Components\Notifier |
||
| 89 | */ |
||
| 90 | 3 | public function getNotifier() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Set Notifier |
||
| 97 | * @param \Tylercd100\LERN\Components\Notifier $notifier A Notifier instance to use |
||
| 98 | * @return \Tylercd100\LERN\LERN |
||
| 99 | */ |
||
| 100 | 3 | public function setNotifier(Notifier $notifier) |
|
| 101 | {
|
||
| 102 | 3 | $this->notifier = $notifier; |
|
| 103 | 3 | return $this; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get Recorder |
||
| 108 | * @return \Tylercd100\LERN\Components\Recorder |
||
| 109 | */ |
||
| 110 | 3 | public function getRecorder() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Set Recorder |
||
| 117 | * @param \Tylercd100\LERN\Components\Recorder $recorder A Recorder instance to use |
||
| 118 | * @return \Tylercd100\LERN\LERN |
||
| 119 | */ |
||
| 120 | 3 | public function setRecorder(Recorder $recorder) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get the log level |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | 3 | public function getLogLevel() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Set the log level |
||
| 137 | * @param string $level The log level |
||
| 138 | * @return \Tylercd100\LERN\LERN |
||
| 139 | */ |
||
| 140 | 6 | public function setLogLevel($level) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Set a string or a closure to be called that will generate the message body for the notification |
||
| 148 | * @param function|string $cb This closure function will be passed an Exception and must return a string |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | 3 | public function setMessage($cb) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Set a string or a closure to be called that will generate the subject line for the notification |
||
| 159 | * @param function|string $cb This closure function will be passed an Exception and must return a string |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | 3 | public function setSubject($cb) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Constructs a Notifier |
||
| 170 | * |
||
| 171 | * @param Notifier $notifier |
||
| 172 | * @return Notifier |
||
| 173 | */ |
||
| 174 | 36 | View Code Duplication | protected function buildNotifier(Notifier $notifier = null) |
| 187 | |||
| 188 | /** |
||
| 189 | * Constructs a Recorder |
||
| 190 | * |
||
| 191 | * @param Recorder $recorder |
||
| 192 | * @return Recorder |
||
| 193 | */ |
||
| 194 | 36 | View Code Duplication | protected function buildRecorder(Recorder $recorder = null) |
| 207 | } |
||
| 208 |