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 | |||
| 34 | /** |
||
| 35 | * Takes a regex pattern (string) as argument and adds the forward slash delimiter. |
||
| 36 | * Also adds the u flag to enable Unicode mode regex. |
||
| 37 | * |
||
| 38 | * @param string $regexPattern |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | 8 | private function addDelimiterFwdSlash($regexPattern) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Takes an xml as string and masks the sensitive fields. |
||
| 49 | * |
||
| 50 | * @param string $rawString The xml as a string. |
||
| 51 | * |
||
| 52 | * @return string The xml as a string after masking sensitive fields |
||
| 53 | */ |
||
| 54 | 8 | private function maskSensitiveXmlString($rawString){ |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Takes a string and masks credit card regex matching parts. |
||
| 83 | * |
||
| 84 | * @param string $rawString The string. |
||
| 85 | * |
||
| 86 | * @return string The string after masking credit card regex matching parts. |
||
| 87 | */ |
||
| 88 | 8 | private function maskCreditCards($rawString){ |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Object data masking related functions START |
||
| 106 | */ |
||
| 107 | |||
| 108 | /** |
||
| 109 | * private function getPropertiesInclBase($reflClass). |
||
| 110 | * |
||
| 111 | * Receives a ReflectionObject, ... |
||
| 112 | * iteratively fetches the properties of the object (including from the base classes up the hierarchy), ... |
||
| 113 | * collects them in an array of ReflectionProperty and returns the array. |
||
| 114 | * |
||
| 115 | * @param ReflectionObject $reflClass |
||
| 116 | * |
||
| 117 | * @return \ReflectionProperty[] |
||
| 118 | */ |
||
| 119 | 8 | private function getPropertiesInclBase($reflClass) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * private function checkPropertyAndMask($prop, $obj). |
||
| 136 | * |
||
| 137 | * Receives a ReflectionProperty and an object, and returns a masked object if the ReflectionProperty corresponds to a sensitive field, else returns false. |
||
| 138 | * |
||
| 139 | * @param ReflectionProperty $prop |
||
| 140 | * @param object $obj |
||
| 141 | * |
||
| 142 | * @return string|bool |
||
| 143 | */ |
||
| 144 | 8 | private function checkPropertyAndMask($prop, $obj){ |
|
| 167 | |||
| 168 | /** |
||
| 169 | * called by getMasked() to mask sensitive fields of an object. |
||
| 170 | * |
||
| 171 | * @param object $obj |
||
| 172 | * |
||
| 173 | * @return object |
||
| 174 | */ |
||
| 175 | 8 | private function maskSensitiveProperties ($obj) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Object data masking related functions END |
||
| 210 | */ |
||
| 211 | |||
| 212 | /** |
||
| 213 | * private function getMasked($raw). |
||
| 214 | * |
||
| 215 | * called by log() |
||
| 216 | * |
||
| 217 | * @param mixed $raw |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 8 | private function getMasked($raw) |
|
| 248 | |||
| 249 | 8 | private function log($logLevelPrefix, $logMessage, $flags){ |
|
| 270 | 8 | ||
| 271 | public function debug($logMessage, $flags=FILE_APPEND) |
||
| 277 | 8 | ||
| 278 | 8 | public function info($logMessage, $flags=FILE_APPEND){ |
|
| 283 | |||
| 284 | public function warn($logMessage, $flags=FILE_APPEND){ |
||
| 289 | |||
| 290 | public function error($logMessage, $flags=FILE_APPEND){ |
||
| 295 | |||
| 296 | private function logFormat($logLevelPrefix, $format, $objects, $flags){ |
||
| 308 | |||
| 309 | public function debugFormat($format, $args=array(), $flags=FILE_APPEND) |
||
| 315 | |||
| 316 | public function infoFormat($format, $args=array(), $flags=FILE_APPEND){ |
||
| 321 | |||
| 322 | public function warnFormat($format, $args=array(), $flags=FILE_APPEND){ |
||
| 327 | |||
| 328 | public function errorFormat($format, $args=array(), $flags=FILE_APPEND){ |
||
| 333 | 1 | ||
| 334 | 1 | /** |
|
| 335 | 1 | * @param string $logFile |
|
| 336 | 1 | */ |
|
| 337 | public function setLogFile($logFile){ |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function getLogFile(){ |
||
| 347 | |||
| 348 | public function __construct(){ |
||
| 352 | } |
||
| 353 | ?> |
||
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: