Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Message implements MessageInterface |
||
24 | { |
||
25 | |||
26 | protected $host; |
||
27 | protected $shortMessage; |
||
28 | protected $fullMessage; |
||
29 | protected $timestamp; |
||
30 | protected $level; |
||
31 | protected $facility; |
||
32 | protected $file; |
||
33 | protected $line; |
||
34 | protected $additionals = array(); |
||
35 | protected $version; |
||
36 | |||
37 | /** |
||
38 | * A list of the PSR LogLevel constants which is also a mapping of |
||
39 | * syslog code to psr-value |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private static $psrLevels = array( |
||
44 | LogLevel::EMERGENCY, // 0 |
||
45 | LogLevel::ALERT, // 1 |
||
46 | LogLevel::CRITICAL, // 2 |
||
47 | LogLevel::ERROR, // 3 |
||
48 | LogLevel::WARNING, // 4 |
||
49 | LogLevel::NOTICE, // 5 |
||
50 | LogLevel::INFO, // 6 |
||
51 | LogLevel::DEBUG // 7 |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Creates a new message |
||
56 | * |
||
57 | * Populates timestamp and host with sane default values |
||
58 | */ |
||
59 | 62 | public function __construct() |
|
66 | |||
67 | /** |
||
68 | * Trys to convert a given log-level (psr or syslog) to |
||
69 | * the psr representation |
||
70 | * |
||
71 | * @param mixed $level |
||
72 | * @return string |
||
73 | */ |
||
74 | 5 | final public static function logLevelToPsr($level) |
|
94 | |||
95 | /** |
||
96 | * Trys to convert a given log-level (psr or syslog) to |
||
97 | * the syslog representation |
||
98 | * |
||
99 | * @param mxied |
||
100 | * @return integer |
||
101 | */ |
||
102 | 20 | final public static function logLevelToSyslog($level) |
|
123 | |||
124 | 12 | public function getVersion() |
|
128 | |||
129 | 3 | public function setVersion($version) |
|
130 | { |
||
131 | 3 | $this->version = $version; |
|
132 | |||
133 | 3 | return $this; |
|
134 | } |
||
135 | |||
136 | 12 | public function getHost() |
|
140 | |||
141 | 2 | public function setHost($host) |
|
147 | |||
148 | 16 | public function getShortMessage() |
|
152 | |||
153 | 17 | public function setShortMessage($shortMessage) |
|
159 | |||
160 | 13 | public function getFullMessage() |
|
164 | |||
165 | 3 | public function setFullMessage($fullMessage) |
|
171 | |||
172 | 13 | public function getTimestamp() |
|
176 | |||
177 | 3 | public function setTimestamp($timestamp) |
|
187 | |||
188 | 2 | public function getLevel() |
|
192 | |||
193 | 12 | public function getSyslogLevel() |
|
197 | |||
198 | 18 | public function setLevel($level) |
|
204 | |||
205 | 13 | public function getFacility() |
|
209 | |||
210 | 16 | public function setFacility($facility) |
|
216 | |||
217 | 13 | public function getFile() |
|
221 | |||
222 | 4 | public function setFile($file) |
|
228 | |||
229 | 13 | public function getLine() |
|
233 | |||
234 | 4 | public function setLine($line) |
|
240 | |||
241 | 2 | public function getAdditional($key) |
|
251 | |||
252 | 1 | public function hasAdditional($key) |
|
256 | |||
257 | 16 | public function setAdditional($key, $value) |
|
267 | |||
268 | 13 | public function getAllAdditionals() |
|
272 | |||
273 | 11 | public function toArray() |
|
306 | |||
307 | /** |
||
308 | * @return string |
||
309 | */ |
||
310 | public function __toString() |
||
314 | } |
||
315 |