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 Log 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 Log, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class Log implements ILogger { |
||
54 | |||
55 | /** @var string */ |
||
56 | private $logger; |
||
57 | |||
58 | /** @var SystemConfig */ |
||
59 | private $config; |
||
60 | |||
61 | /** @var EventDispatcherInterface */ |
||
62 | private $eventDispatcher; |
||
63 | |||
64 | /** @var boolean|null cache the result of the log condition check for the request */ |
||
65 | private $logConditionSatisfied = null; |
||
66 | |||
67 | /** @var Normalizer */ |
||
68 | private $normalizer; |
||
69 | |||
70 | /** |
||
71 | * Flag whether we are within the event block |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $inEvent = false; |
||
76 | |||
77 | protected $methodsWithSensitiveParameters = [ |
||
78 | // Session/User |
||
79 | 'login', |
||
80 | 'checkPassword', |
||
81 | 'updatePrivateKeyPassword', |
||
82 | 'validateUserPass', |
||
83 | 'loginWithPassword', |
||
84 | |||
85 | // TokenProvider |
||
86 | 'getToken', |
||
87 | 'isTokenPassword', |
||
88 | 'getPassword', |
||
89 | 'decryptPassword', |
||
90 | 'logClientIn', |
||
91 | 'generateToken', |
||
92 | 'validateToken', |
||
93 | |||
94 | // TwoFactorAuth |
||
95 | 'solveChallenge', |
||
96 | 'verifyChallenge', |
||
97 | |||
98 | //ICrypto |
||
99 | 'calculateHMAC', |
||
100 | 'encrypt', |
||
101 | 'decrypt', |
||
102 | |||
103 | //LoginController |
||
104 | 'tryLogin' |
||
105 | ]; |
||
106 | |||
107 | /** |
||
108 | * @param string $logger The logger that should be used |
||
109 | * @param SystemConfig $config the system config object |
||
110 | * @param null $normalizer |
||
111 | * @param EventDispatcherInterface $eventDispatcher event dispatcher |
||
112 | */ |
||
113 | public function __construct( |
||
145 | |||
146 | /** |
||
147 | * System is unusable. |
||
148 | * |
||
149 | * @param string $message |
||
150 | * @param array $context |
||
151 | * @return void |
||
152 | */ |
||
153 | public function emergency($message, array $context = []) { |
||
156 | |||
157 | /** |
||
158 | * Action must be taken immediately. |
||
159 | * |
||
160 | * Example: Entire website down, database unavailable, etc. This should |
||
161 | * trigger the SMS alerts and wake you up. |
||
162 | * |
||
163 | * @param string $message |
||
164 | * @param array $context |
||
165 | * @return void |
||
166 | */ |
||
167 | public function alert($message, array $context = []) { |
||
170 | |||
171 | /** |
||
172 | * Critical conditions. |
||
173 | * |
||
174 | * Example: Application component unavailable, unexpected exception. |
||
175 | * |
||
176 | * @param string $message |
||
177 | * @param array $context |
||
178 | * @return void |
||
179 | */ |
||
180 | public function critical($message, array $context = []) { |
||
183 | |||
184 | /** |
||
185 | * Runtime errors that do not require immediate action but should typically |
||
186 | * be logged and monitored. |
||
187 | * |
||
188 | * @param string $message |
||
189 | * @param array $context |
||
190 | * @return void |
||
191 | */ |
||
192 | public function error($message, array $context = []) { |
||
195 | |||
196 | /** |
||
197 | * Exceptional occurrences that are not errors. |
||
198 | * |
||
199 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
200 | * that are not necessarily wrong. |
||
201 | * |
||
202 | * @param string $message |
||
203 | * @param array $context |
||
204 | * @return void |
||
205 | */ |
||
206 | public function warning($message, array $context = []) { |
||
209 | |||
210 | /** |
||
211 | * Normal but significant events. |
||
212 | * |
||
213 | * @param string $message |
||
214 | * @param array $context |
||
215 | * @return void |
||
216 | */ |
||
217 | public function notice($message, array $context = []) { |
||
220 | |||
221 | /** |
||
222 | * Interesting events. |
||
223 | * |
||
224 | * Example: User logs in, SQL logs. |
||
225 | * |
||
226 | * @param string $message |
||
227 | * @param array $context |
||
228 | * @return void |
||
229 | */ |
||
230 | public function info($message, array $context = []) { |
||
233 | |||
234 | /** |
||
235 | * Detailed debug information. |
||
236 | * |
||
237 | * @param string $message |
||
238 | * @param array $context |
||
239 | * @return void |
||
240 | */ |
||
241 | public function debug($message, array $context = []) { |
||
244 | |||
245 | /** |
||
246 | * Logs with an arbitrary level. |
||
247 | * |
||
248 | * @param mixed $level |
||
249 | * @param string $message |
||
250 | * @param array $context |
||
251 | * @return void |
||
252 | */ |
||
253 | public function log($level, $message, array $context = []) { |
||
388 | |||
389 | /** |
||
390 | * interpolate $message as defined in PSR-3 |
||
391 | * @param string $message |
||
392 | * @param array $context |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function interpolate($message, array $context) { |
||
404 | |||
405 | /** |
||
406 | * Logs an exception very detailed |
||
407 | * |
||
408 | * @param \Exception | \Throwable $exception |
||
409 | * @param array $context |
||
410 | * @return void |
||
411 | * @since 8.2.0 |
||
412 | */ |
||
413 | public function logException($exception, array $context = []) { |
||
441 | } |
||
442 |
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.