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 |
||
29 | class Log |
||
30 | { |
||
31 | private $sensitiveXmlTags = NULL; |
||
32 | private $logFile = ''; |
||
33 | private $logLevel = ANET_LOG_LEVEL; |
||
34 | |||
35 | /** |
||
36 | * Takes a regex pattern (string) as argument and adds the forward slash delimiter. |
||
37 | * Also adds the u flag to enable Unicode mode regex. |
||
38 | * |
||
39 | * @param string $regexPattern |
||
40 | * |
||
41 | * @return string |
||
42 | 8 | */ |
|
43 | private function addDelimiterFwdSlash($regexPattern) |
||
47 | |||
48 | /** |
||
49 | * Takes an xml as string and masks the sensitive fields. |
||
50 | * |
||
51 | * @param string $rawString The xml as a string. |
||
52 | * |
||
53 | * @return string The xml as a string after masking sensitive fields |
||
54 | 8 | */ |
|
55 | 8 | private function maskSensitiveXmlString($rawString){ |
|
81 | |||
82 | /** |
||
83 | * Takes a string and masks credit card regex matching parts. |
||
84 | * |
||
85 | * @param string $rawString The string. |
||
86 | * |
||
87 | * @return string The string after masking credit card regex matching parts. |
||
88 | 8 | */ |
|
89 | 8 | private function maskCreditCards($rawString){ |
|
104 | |||
105 | /** |
||
106 | * Object data masking related functions START |
||
107 | */ |
||
108 | |||
109 | /** |
||
110 | * private function getPropertiesInclBase($reflClass). |
||
111 | * |
||
112 | * Receives a ReflectionObject, ... |
||
113 | * iteratively fetches the properties of the object (including from the base classes up the hierarchy), ... |
||
114 | * collects them in an array of ReflectionProperty and returns the array. |
||
115 | * |
||
116 | * @param ReflectionObject $reflClass |
||
117 | * |
||
118 | * @return \ReflectionProperty[] |
||
119 | 8 | */ |
|
120 | private function getPropertiesInclBase($reflClass) |
||
134 | |||
135 | /** |
||
136 | * private function checkPropertyAndMask($prop, $obj). |
||
137 | * |
||
138 | * Receives a ReflectionProperty and an object, and returns a masked object if the ReflectionProperty corresponds to a sensitive field, else returns false. |
||
139 | * |
||
140 | * @param ReflectionProperty $prop |
||
141 | * @param object $obj |
||
142 | * |
||
143 | * @return string|bool |
||
144 | 8 | */ |
|
145 | 8 | private function checkPropertyAndMask($prop, $obj){ |
|
168 | |||
169 | /** |
||
170 | * called by getMasked() to mask sensitive fields of an object. |
||
171 | * |
||
172 | * @param object $obj |
||
173 | * |
||
174 | * @return object |
||
175 | 8 | */ |
|
176 | private function maskSensitiveProperties ($obj) |
||
208 | |||
209 | /** |
||
210 | * Object data masking related functions END |
||
211 | */ |
||
212 | |||
213 | /** |
||
214 | * private function getMasked($raw). |
||
215 | * |
||
216 | * called by log() |
||
217 | * |
||
218 | * @param mixed $raw |
||
219 | * |
||
220 | * @return string |
||
221 | 8 | */ |
|
222 | private function getMasked($raw) |
||
249 | 8 | ||
250 | private function log($logLevelPrefix, $logMessage, $flags){ |
||
271 | |||
272 | 8 | public function debug($logMessage, $flags=FILE_APPEND) |
|
278 | 8 | ||
279 | 8 | public function info($logMessage, $flags=FILE_APPEND){ |
|
280 | if(ANET_LOG_INFO >= $this->logLevel) { |
||
281 | 8 | $this->log(ANET_LOG_INFO_PREFIX, $logMessage,$flags); |
|
282 | } |
||
283 | } |
||
284 | |||
285 | public function warn($logMessage, $flags=FILE_APPEND){ |
||
290 | |||
291 | public function error($logMessage, $flags=FILE_APPEND){ |
||
296 | |||
297 | private function logFormat($logLevelPrefix, $format, $objects, $flags){ |
||
309 | |||
310 | public function debugFormat($format, $args=array(), $flags=FILE_APPEND) |
||
316 | |||
317 | public function infoFormat($format, $args=array(), $flags=FILE_APPEND){ |
||
318 | if(ANET_LOG_INFO >= $this->logLevel) { |
||
319 | $this->logFormat(ANET_LOG_INFO_PREFIX, $format, $args , $flags); |
||
320 | } |
||
321 | } |
||
322 | |||
323 | public function warnFormat($format, $args=array(), $flags=FILE_APPEND){ |
||
328 | |||
329 | public function errorFormat($format, $args=array(), $flags=FILE_APPEND){ |
||
330 | if(ANET_LOG_ERROR >= $this->logLevel) { |
||
331 | $this->logFormat(ANET_LOG_ERROR_PREFIX, $format, $args , $flags); |
||
332 | } |
||
333 | 1 | } |
|
334 | 1 | ||
335 | 1 | /** |
|
336 | 1 | * @param string $logLevel |
|
337 | * possible values = ANET_LOG_DEBUG, ANET_LOG_INFO, ANET_LOG_WARN, ANET_LOG_ERROR |
||
338 | */ |
||
339 | public function setLogLevel($logLevel){ |
||
340 | $this->logLevel = $logLevel; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @return string |
||
345 | */ |
||
346 | public function getLogLevel(){ |
||
349 | |||
350 | /** |
||
351 | * @param string $logFile |
||
352 | */ |
||
353 | public function setLogFile($logFile){ |
||
354 | $this->logFile = $logFile; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @return string |
||
359 | */ |
||
360 | public function getLogFile(){ |
||
363 | |||
364 | public function __construct(){ |
||
368 | } |
||
369 | ?> |
||
370 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: