Complex classes like Moxiecode_Logger 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 Moxiecode_Logger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Moxiecode_Logger { |
||
22 | // Private fields |
||
23 | var $_path; |
||
24 | var $_filename; |
||
25 | var $_maxSize; |
||
26 | var $_maxFiles; |
||
27 | var $_maxSizeBytes; |
||
28 | var $_level; |
||
29 | var $_format; |
||
30 | |||
31 | /** |
||
32 | * Constructs a new logger instance. |
||
33 | */ |
||
34 | function Moxiecode_Logger() { |
||
42 | |||
43 | /** |
||
44 | * Sets the current log level, use the MC_LOGGER constants. |
||
45 | * |
||
46 | * @param int $level Log level instance for example MC_LOGGER_DEBUG. |
||
47 | */ |
||
48 | function setLevel($level) { |
||
79 | |||
80 | /** |
||
81 | * Returns the current log level for example MC_LOGGER_DEBUG. |
||
82 | * |
||
83 | * @return int Current log level for example MC_LOGGER_DEBUG. |
||
84 | */ |
||
85 | function getLevel() { |
||
88 | |||
89 | function setPath($path) { |
||
92 | |||
93 | function getPath() { |
||
96 | |||
97 | function setFileName($file_name) { |
||
100 | |||
101 | function getFileName() { |
||
104 | |||
105 | function setFormat($format) { |
||
108 | |||
109 | function getFormat() { |
||
112 | |||
113 | function setMaxSize($size) { |
||
128 | |||
129 | function getMaxSize() { |
||
132 | |||
133 | function setMaxFiles($max_files) { |
||
136 | |||
137 | function getMaxFiles() { |
||
140 | |||
141 | function debug($msg) { |
||
145 | |||
146 | function info($msg) { |
||
150 | |||
151 | function warn($msg) { |
||
155 | |||
156 | function error($msg) { |
||
160 | |||
161 | function fatal($msg) { |
||
165 | |||
166 | function isDebugEnabled() { |
||
169 | |||
170 | function isInfoEnabled() { |
||
173 | |||
174 | function isWarnEnabled() { |
||
177 | |||
178 | function isErrorEnabled() { |
||
181 | |||
182 | function isFatalEnabled() { |
||
185 | |||
186 | function _logMsg($level, $message) { |
||
257 | |||
258 | /** |
||
259 | * Converts a Unix path to OS specific path. |
||
260 | * |
||
261 | * @param String $path Unix path to convert. |
||
262 | */ |
||
263 | function toOSPath($path) { |
||
266 | } |
||
267 | |||
269 |