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 |
||
27 | class GreyLogException extends \Exception { |
||
28 | |||
29 | /** |
||
30 | * The application name that is logging the exception (facility in GrayLog). |
||
31 | */ |
||
32 | private $sApplication = "SampleApplicationName"; |
||
|
|||
33 | |||
34 | /** |
||
35 | * The IP the GrayLog2-Server listens to. |
||
36 | */ |
||
37 | private $sLogServerIp = "127.0.0.1"; |
||
38 | |||
39 | /** |
||
40 | * PSR LogLevel 8 |
||
41 | */ |
||
42 | const EMERGENCY = \Psr\Log\LogLevel::EMERGENCY; |
||
43 | |||
44 | /** |
||
45 | * PSR LogLevel 7 |
||
46 | */ |
||
47 | const ALERT = \Psr\Log\LogLevel::ALERT; |
||
48 | |||
49 | /** |
||
50 | * PSR LogLevel 6 |
||
51 | */ |
||
52 | const CRITICAL = \Psr\Log\LogLevel::CRITICAL; |
||
53 | |||
54 | /** |
||
55 | * PSR LogLevel 5 |
||
56 | */ |
||
57 | const ERROR = \Psr\Log\LogLevel::ERROR; |
||
58 | |||
59 | /** |
||
60 | * PSR LogLevel 4 |
||
61 | */ |
||
62 | const WARNING = \Psr\Log\LogLevel::WARNING; |
||
63 | |||
64 | /** |
||
65 | * PSR LogLevel 3 |
||
66 | */ |
||
67 | const NOTICE = \Psr\Log\LogLevel::NOTICE; |
||
68 | |||
69 | /** |
||
70 | * PSR LogLevel 2 |
||
71 | */ |
||
72 | const INFO = \Psr\Log\LogLevel::INFO; |
||
73 | |||
74 | /** |
||
75 | * PSR LogLevel 1 |
||
76 | */ |
||
77 | const DEBUG = \Psr\Log\LogLevel::DEBUG; |
||
78 | |||
79 | /** |
||
80 | * The publisher object for GreyLog2-Server. |
||
81 | * @var \Gelf\Publisher $publisher Holds the publisher object. |
||
82 | */ |
||
83 | private $publisher; |
||
84 | |||
85 | /** |
||
86 | * The message object to send to GreyLog2-Server. |
||
87 | * @var \Gelf\Message $message Holds the message object. |
||
88 | */ |
||
89 | private $gelfMessage; |
||
90 | |||
91 | /** |
||
92 | * The actual exception message. |
||
93 | * @var String $sExceptionMessage The message for the exception. |
||
94 | */ |
||
95 | public $sExceptionMessage; |
||
96 | |||
97 | /** |
||
98 | * The actual exception code. |
||
99 | * @var Integer $iExceptionCode Holds the exception code. |
||
100 | */ |
||
101 | public $iExceptionCode; |
||
102 | |||
103 | /** |
||
104 | * The actual exception identifier/shortMessage. |
||
105 | * @var String $sExceptionShortMessage Holds the exceptionShortMessage. |
||
106 | */ |
||
107 | public $sExceptionShortMessage; |
||
108 | |||
109 | /** |
||
110 | * The actual exception level. Possible values: |
||
111 | * EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG |
||
112 | * @var String $sExceptionLevel Holds the exceptionLevel. |
||
113 | */ |
||
114 | private $sExceptionLevel; |
||
115 | |||
116 | /** |
||
117 | * The actual class where the exception was thrown. |
||
118 | * @var String $sExceptionClass Holds the exceptionClass. |
||
119 | */ |
||
120 | private $sExceptionClass; |
||
121 | |||
122 | /** |
||
123 | * The actual function/method where the exception was thrown. |
||
124 | * @var String $sExceptionFunction Holds the exceptionFunction. |
||
125 | */ |
||
126 | private $sExceptionFunction; |
||
127 | |||
128 | /** |
||
129 | * The actual exception class the exception came from. |
||
130 | * @var String $sModuleName Holds the moduleName. |
||
131 | */ |
||
132 | private $sModuleName; |
||
133 | |||
134 | /** |
||
135 | * Sends a new exception with the help of an exceptionDetails array and aditional parameters. |
||
136 | * @param Array $_aExceptionDetails The exceptionDetails in form: "[int ExceptionCode, lfException::LOGLEVEL, string ExceptionMessage]". |
||
137 | * @param Mixed $_aAdditionalInformations The array of all other given parameters for setting them in the exceptionMessage. |
||
138 | */ |
||
139 | public function __construct(Array $_aExceptionDetails, ...$_aAdditionalInformations) { |
||
177 | |||
178 | /** |
||
179 | * Checks the exceptionArray for valid contents and sets its values. |
||
180 | * Sets a standard value if details are missing in array. |
||
181 | * Sends a notice exception if there are missing details. |
||
182 | * @param array $_aExceptionDetails The exceptionDetails in form: "[int ExceptionCode, lfException::LOGLEVEL, string ExceptionMessage]". |
||
183 | */ |
||
184 | private function validateExceptionDetails(Array $_aExceptionDetails) { |
||
204 | |||
205 | /** |
||
206 | * Formats the errorMessage and fills the variables with content if there are any. |
||
207 | * @param array $_aAdditionalInformations An array containing all values for variables that has to be replaced in errorMessage. |
||
208 | * @return string The formated errorMessage. |
||
209 | */ |
||
210 | public function formatErrorMessage(array $_aAdditionalInformations) { |
||
219 | |||
220 | /** |
||
221 | * Gets the number of variables that can be replaces by sprintf function in a string. |
||
222 | * @param string $_sReplaceableString The string to search for replacable variables. |
||
223 | * @return int Returns the number of valriables that can be relaced via sprintf. |
||
224 | */ |
||
225 | public function getNumberOfVariables(string $_sReplaceableString) { |
||
231 | |||
232 | /** |
||
233 | * Seaches the key of the given class constant value. |
||
234 | * @param array $_aExceptionDetails The value of an exception constant. |
||
235 | * @param string $_sExceptionClass The class the exception constant has to be searched. |
||
236 | * @return mixed the key for <i>needle</i> if it is found in the array, <b>FALSE</b> otherwise. |
||
237 | * @todo Could be cached with storing all exceptions in opCache |
||
238 | * @todo Inner exception should be added when getExceptionCode fails. But at the moment innerNotice function has circular reference to ExceptinShortMessage |
||
239 | */ |
||
240 | public function getExceptionCode($_aExceptionDetails, $_sExceptionClass) { |
||
248 | |||
249 | /** |
||
250 | * Sends an notice to GreyLog-Server before sending the actual exception. |
||
251 | * @param string $sShortMessage The notice identifier. |
||
252 | * @param string $sMessage The message for the notice. |
||
253 | */ |
||
254 | private function innerNotice(string $sShortMessage, string $sMessage) { |
||
268 | |||
269 | /** |
||
270 | * Sends an execption of type "EMERGENCY" to configured GreyLog2-Server. |
||
271 | */ |
||
272 | View Code Duplication | private function emergency() { |
|
283 | |||
284 | /** |
||
285 | * Sends an execption of type "ALERT" to configured GreyLog2-Server. |
||
286 | */ |
||
287 | View Code Duplication | private function alert() { |
|
298 | |||
299 | /** |
||
300 | * Sends an execption of type "CRITICAL" to configured GreyLog2-Server. |
||
301 | */ |
||
302 | View Code Duplication | private function critical() { |
|
313 | |||
314 | /** |
||
315 | * Sends an execption of type "ERROR" to configured GreyLog2-Server. |
||
316 | */ |
||
317 | View Code Duplication | private function error() { |
|
328 | |||
329 | /** |
||
330 | * Sends an execption of type "WARNING" to configured GreyLog2-Server. |
||
331 | */ |
||
332 | View Code Duplication | private function warning() { |
|
343 | |||
344 | /** |
||
345 | * Sends an execption of type "NOTICE" to configured GreyLog2-Server. |
||
346 | */ |
||
347 | View Code Duplication | private function notice() { |
|
358 | |||
359 | /** |
||
360 | * Sends an execption of type "INFO" to configured GreyLog2-Server. |
||
361 | */ |
||
362 | View Code Duplication | private function info() { |
|
373 | |||
374 | /** |
||
375 | * Sends an execption of type "DEBUG" to configured GreyLog2-Server. |
||
376 | */ |
||
377 | View Code Duplication | private function debug() { |
|
388 | |||
389 | /** |
||
390 | * Sets the ShortMessage and returns message object. |
||
391 | * @return Gelf\Message Sets the ShortMessage and returns message object. |
||
392 | */ |
||
393 | private function setShortMessage() { |
||
396 | |||
397 | /** |
||
398 | * Sets the Message and returns message object. |
||
399 | * @return Gelf\Message Sets the Message and returns message object. |
||
400 | */ |
||
401 | private function setMessage() { |
||
404 | |||
405 | /** |
||
406 | * Sets the Level and returns message object. |
||
407 | * @return Gelf\Message Sets the Level and returns message object. |
||
408 | */ |
||
409 | private function setLevel() { |
||
412 | |||
413 | /** |
||
414 | * Sets the Facility and returns message object. |
||
415 | * @return Gelf\Message Sets the Facility and returns message object. |
||
416 | */ |
||
417 | private function setFacility() { |
||
420 | |||
421 | /** |
||
422 | * Sets additional details from exception object and returns message object. |
||
423 | * @return Gelf\Message Sets the additional details from exception object and returns message object. |
||
424 | */ |
||
425 | private function setAdditionals() { |
||
434 | |||
435 | /** |
||
436 | * Checks if function/method parameters are equal to function declaration and logs warning if not. |
||
437 | * Gets all names of function/method parameters and its values and adds it to message object as additional. |
||
438 | * @return Gelf\Message Sets additional details to message object and returns the message object. |
||
439 | */ |
||
440 | private function addParameters() { |
||
461 | |||
462 | /** |
||
463 | * Publishes the message object to configured GreyLog2-Server instance. |
||
464 | * @return Gelf\publisher The publisher object. |
||
465 | */ |
||
466 | private function publishMessage() { |
||
469 | |||
470 | private function loggerPanic(string $_sMessage) { |
||
473 | |||
474 | } |
||
475 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.