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 ChromeLogger 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 ChromeLogger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class ChromeLogger extends AbstractLogger implements LoggerInterface |
||
26 | { |
||
27 | const VERSION = "4.1.0"; |
||
28 | |||
29 | const COLUMN_LOG = "log"; |
||
30 | const COLUMN_BACKTRACE = "backtrace"; |
||
31 | const COLUMN_TYPE = "type"; |
||
32 | |||
33 | const CLASS_NAME = "type"; |
||
34 | const TABLES = "tables"; |
||
35 | const HEADER_NAME = "X-ChromeLogger-Data"; |
||
36 | const LOCATION_HEADER_NAME = "X-ServerLog-Location"; |
||
37 | |||
38 | const LOG = "log"; |
||
39 | const WARN = "warn"; |
||
40 | const ERROR = "error"; |
||
41 | const INFO = "info"; |
||
42 | |||
43 | const GROUP = "group"; |
||
44 | const GROUP_END = "groupEnd"; |
||
45 | const GROUP_COLLAPSED = "groupCollapsed"; |
||
46 | const TABLE = "table"; |
||
47 | |||
48 | const DATETIME_FORMAT = "Y-m-d\\TH:i:s\\Z"; // ISO-8601 UTC date/time format |
||
49 | |||
50 | const LIMIT_WARNING = "Beginning of log entries omitted - total header size over Chrome's internal limit!"; |
||
51 | |||
52 | /** |
||
53 | * @var int header size limit (in bytes, defaults to 240KB) |
||
54 | */ |
||
55 | protected $limit = 245760; |
||
56 | |||
57 | /** |
||
58 | * @var LogEntry[] |
||
59 | */ |
||
60 | protected $entries = []; |
||
61 | |||
62 | /** |
||
63 | * @var string|null |
||
64 | */ |
||
65 | private $local_path; |
||
66 | |||
67 | /** |
||
68 | * @var string|null |
||
69 | */ |
||
70 | private $public_path; |
||
71 | |||
72 | /** |
||
73 | * Logs with an arbitrary level. |
||
74 | * |
||
75 | * @param mixed $level |
||
76 | * @param string $message |
||
77 | * @param array $context |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function log($level, $message, array $context = []) |
||
85 | |||
86 | /** |
||
87 | * Allows you to override the internal 240 KB header size limit. |
||
88 | * |
||
89 | * (Chrome has a 250 KB limit for the total size of all headers.) |
||
90 | * |
||
91 | * @see https://cs.chromium.org/chromium/src/net/http/http_stream_parser.h?q=ERR_RESPONSE_HEADERS_TOO_BIG&sq=package:chromium&dr=C&l=159 |
||
92 | * |
||
93 | * @param int $limit header size limit (in bytes) |
||
94 | */ |
||
95 | public function setLimit($limit) |
||
99 | |||
100 | /** |
||
101 | * @return int header size limit (in bytes) |
||
102 | */ |
||
103 | public function getLimit() |
||
107 | |||
108 | /** |
||
109 | * Enables persistence to local log-files served from your web-root. |
||
110 | * |
||
111 | * Bypasses the header-size limitation (imposed by Chrome, NGINX, etc.) by avoiding the |
||
112 | * large `X-ChromeLogger-Data` header and instead storing the log in a flat file. |
||
113 | * |
||
114 | * Requires the [ServerLog](https://github.com/mindplay-dk/server-log) Chrome extension, |
||
115 | * which replaces the ChromeLogger extension - this does NOT work with the regular |
||
116 | * ChromeLogger extension. |
||
117 | * |
||
118 | * @link https://github.com/mindplay-dk/server-log |
||
119 | * |
||
120 | * @param string $local_path absolute local path to a dedicated log-folder in your public web-root, |
||
121 | * e.g. "/var/www/mysite.com/webroot/log" |
||
122 | * @param string $public_path absolute public path, e.g. "/log" |
||
123 | */ |
||
124 | public function usePersistence(string $local_path, string $public_path) |
||
133 | |||
134 | /** |
||
135 | * Adds headers for recorded log-entries in the ChromeLogger format, and clear the internal log-buffer. |
||
136 | * |
||
137 | * (You should call this at the end of the request/response cycle in your PSR-7 project, e.g. |
||
138 | * immediately before emitting the Response.) |
||
139 | * |
||
140 | * @param ResponseInterface $response |
||
141 | * |
||
142 | * @return ResponseInterface |
||
143 | */ |
||
144 | public function writeToResponse(ResponseInterface $response) |
||
158 | |||
159 | /** |
||
160 | * Emit the header for recorded log-entries directly using `header()`, and clear the internal buffer. |
||
161 | * |
||
162 | * (You can use this in a non-PSR-7 project, immediately before you start emitting the response body.) |
||
163 | * |
||
164 | * @throws RuntimeException if you've already started emitting the response body |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | public function emitHeader() |
||
184 | |||
185 | /** |
||
186 | * @return string raw value for the X-ChromeLogger-Data header |
||
187 | */ |
||
188 | protected function getHeaderValue() |
||
218 | |||
219 | /** |
||
220 | * @return string public path to log-file |
||
221 | */ |
||
222 | protected function createLogFile(): string |
||
241 | |||
242 | /** |
||
243 | * @return string pseudo-random log filename |
||
244 | */ |
||
245 | protected function createUniqueFilename(): string |
||
249 | |||
250 | /** |
||
251 | * Garbage-collects log-files older than one minute. |
||
252 | */ |
||
253 | protected function collectGarbage() |
||
263 | |||
264 | /** |
||
265 | * @return int |
||
266 | */ |
||
267 | protected function getTime(): int |
||
271 | |||
272 | /** |
||
273 | * Encodes the ChromeLogger-compatible data-structure in JSON/base64-format |
||
274 | * |
||
275 | * @param array $data header data |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | protected function encodeData(array $data) |
||
290 | |||
291 | /** |
||
292 | * Internally builds the ChromeLogger-compatible data-structure from internal log-entries. |
||
293 | * |
||
294 | * @param LogEntry[] $entries |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | protected function createData(array $entries) |
||
314 | |||
315 | /** |
||
316 | * Encode an individual LogEntry in ChromeLogger-compatible format |
||
317 | * |
||
318 | * @param LogEntry $entry |
||
319 | * |
||
320 | * @return array log-entries in ChromeLogger row-format |
||
321 | */ |
||
322 | protected function createEntryData(LogEntry $entry) |
||
403 | |||
404 | /** |
||
405 | * Internally marshall and sanitize context values, producing a JSON-compatible data-structure. |
||
406 | * |
||
407 | * @param mixed $data any PHP object, array or value |
||
408 | * @param true[] $processed map where SPL object-hash => TRUE (eliminates duplicate objects from data-structures) |
||
409 | * |
||
410 | * @return mixed marshalled and sanitized context |
||
411 | */ |
||
412 | protected function sanitize($data, &$processed = []) |
||
473 | |||
474 | /** |
||
475 | * @param DateTimeInterface $datetime |
||
476 | * |
||
477 | * @return array |
||
478 | */ |
||
479 | protected function extractDateTimeProperties(DateTimeInterface $datetime) |
||
488 | |||
489 | /** |
||
490 | * @param object $object |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | protected function extractObjectProperties($object) |
||
524 | |||
525 | /** |
||
526 | * @param Throwable $exception |
||
527 | * |
||
528 | * @return array |
||
529 | */ |
||
530 | protected function extractExceptionProperties($exception) |
||
542 | } |
||
543 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: