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) |
||
156 | |||
157 | /** |
||
158 | * Emit the header for recorded log-entries directly using `header()`, and clear the internal buffer. |
||
159 | * |
||
160 | * (You can use this in a non-PSR-7 project, immediately before you start emitting the response body.) |
||
161 | * |
||
162 | * @throws RuntimeException if you've already started emitting the response body |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | public function emitHeader() |
||
180 | |||
181 | /** |
||
182 | * @return string raw value for the X-ChromeLogger-Data header |
||
183 | */ |
||
184 | protected function getHeaderValue() |
||
214 | |||
215 | /** |
||
216 | * @return string public path to log-file |
||
217 | */ |
||
218 | protected function createLogFile(): string |
||
237 | |||
238 | /** |
||
239 | * @return string pseudo-random log filename |
||
240 | */ |
||
241 | protected function createUniqueFilename(): string |
||
245 | |||
246 | /** |
||
247 | * Garbage-collects log-files older than one minute. |
||
248 | */ |
||
249 | protected function collectGarbage() |
||
259 | |||
260 | /** |
||
261 | * @return int |
||
262 | */ |
||
263 | protected function getTime(): int |
||
267 | |||
268 | /** |
||
269 | * Encodes the ChromeLogger-compatible data-structure in JSON/base64-format |
||
270 | * |
||
271 | * @param array $data header data |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | protected function encodeData(array $data) |
||
286 | |||
287 | /** |
||
288 | * Internally builds the ChromeLogger-compatible data-structure from internal log-entries. |
||
289 | * |
||
290 | * @param LogEntry[] $entries |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | protected function createData(array $entries) |
||
310 | |||
311 | /** |
||
312 | * Encode an individual LogEntry in ChromeLogger-compatible format |
||
313 | * |
||
314 | * @param LogEntry $entry |
||
315 | * |
||
316 | * @return array log-entries in ChromeLogger row-format |
||
317 | */ |
||
318 | protected function createEntryData(LogEntry $entry) |
||
399 | |||
400 | /** |
||
401 | * Internally marshall and sanitize context values, producing a JSON-compatible data-structure. |
||
402 | * |
||
403 | * @param mixed $data any PHP object, array or value |
||
404 | * @param true[] $processed map where SPL object-hash => TRUE (eliminates duplicate objects from data-structures) |
||
405 | * |
||
406 | * @return mixed marshalled and sanitized context |
||
407 | */ |
||
408 | protected function sanitize($data, &$processed = []) |
||
469 | |||
470 | /** |
||
471 | * @param DateTimeInterface $datetime |
||
472 | * |
||
473 | * @return array |
||
474 | */ |
||
475 | protected function extractDateTimeProperties(DateTimeInterface $datetime) |
||
484 | |||
485 | /** |
||
486 | * @param object $object |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | protected function extractObjectProperties($object) |
||
520 | |||
521 | /** |
||
522 | * @param Throwable $exception |
||
523 | * |
||
524 | * @return array |
||
525 | */ |
||
526 | protected function extractExceptionProperties($exception) |
||
538 | } |
||
539 |
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: