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 | 63 | 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) |
|
75 | { |
||
76 | 5 | $origLevel = $level; |
|
77 | |||
78 | 5 | if (is_numeric($level)) { |
|
79 | 4 | $level = intval($level); |
|
80 | 4 | if (isset(self::$psrLevels[$level])) { |
|
81 | 4 | return self::$psrLevels[$level]; |
|
82 | } |
||
83 | 2 | } elseif (is_string($level)) { |
|
84 | 2 | $level = strtolower($level); |
|
85 | 2 | if (in_array($level, self::$psrLevels)) { |
|
86 | 1 | return $level; |
|
87 | } |
||
88 | } |
||
89 | |||
90 | 2 | throw new RuntimeException( |
|
91 | 2 | sprintf("Cannot convert log-level '%s' to psr-style", $origLevel) |
|
92 | ); |
||
93 | } |
||
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 | 21 | final public static function logLevelToSyslog($level) |
|
103 | { |
||
104 | 21 | $origLevel = $level; |
|
105 | |||
106 | 21 | if (is_numeric($level)) { |
|
107 | 14 | $level = intval($level); |
|
108 | 14 | if ($level < 8 && $level > -1) { |
|
109 | 14 | return $level; |
|
110 | } |
||
111 | 17 | } elseif (is_string($level)) { |
|
112 | 17 | $level = strtolower($level); |
|
113 | 17 | $syslogLevel = array_search($level, self::$psrLevels); |
|
114 | 17 | if (false !== $syslogLevel) { |
|
115 | 16 | return $syslogLevel; |
|
116 | } |
||
117 | } |
||
118 | |||
119 | 2 | throw new RuntimeException( |
|
120 | 2 | sprintf("Cannot convert log-level '%s' to syslog-style", $origLevel) |
|
121 | ); |
||
122 | } |
||
123 | |||
124 | 13 | public function getVersion() |
|
128 | |||
129 | 3 | public function setVersion($version) |
|
130 | { |
||
131 | 3 | $this->version = $version; |
|
132 | |||
133 | 3 | return $this; |
|
134 | } |
||
135 | |||
136 | 13 | public function getHost() |
|
140 | |||
141 | 2 | public function setHost($host) |
|
147 | |||
148 | 17 | public function getShortMessage() |
|
152 | |||
153 | 17 | public function setShortMessage($shortMessage) |
|
159 | |||
160 | 14 | public function getFullMessage() |
|
164 | |||
165 | 3 | public function setFullMessage($fullMessage) |
|
171 | |||
172 | 14 | public function getTimestamp() |
|
176 | |||
177 | 3 | public function setTimestamp($timestamp) |
|
178 | { |
||
179 | 3 | if ($timestamp instanceof \DateTime || $timestamp instanceof \DateTimeInterface) { |
|
180 | 2 | $timestamp = $timestamp->format("U.u"); |
|
181 | } |
||
182 | |||
183 | 3 | $this->timestamp = (float) $timestamp; |
|
184 | |||
185 | 3 | return $this; |
|
186 | } |
||
187 | |||
188 | 2 | public function getLevel() |
|
192 | |||
193 | 13 | public function getSyslogLevel() |
|
197 | |||
198 | 18 | public function setLevel($level) |
|
204 | |||
205 | 14 | public function getFacility() |
|
209 | |||
210 | 16 | public function setFacility($facility) |
|
216 | |||
217 | 14 | public function getFile() |
|
221 | |||
222 | 4 | public function setFile($file) |
|
228 | |||
229 | 14 | public function getLine() |
|
233 | |||
234 | 4 | public function setLine($line) |
|
240 | |||
241 | 2 | public function getAdditional($key) |
|
242 | { |
||
243 | 2 | if (!isset($this->additionals[$key])) { |
|
244 | 1 | throw new RuntimeException( |
|
245 | 1 | sprintf("Additional key '%s' is not defined", $key) |
|
246 | ); |
||
247 | } |
||
248 | |||
249 | 1 | return $this->additionals[$key]; |
|
250 | } |
||
251 | |||
252 | 1 | public function hasAdditional($key) |
|
256 | |||
257 | 17 | public function setAdditional($key, $value) |
|
267 | |||
268 | 14 | public function getAllAdditionals() |
|
272 | |||
273 | 12 | public function toArray() |
|
274 | { |
||
275 | $message = array( |
||
276 | 12 | 'version' => $this->getVersion(), |
|
277 | 12 | 'host' => $this->getHost(), |
|
308 | } |
||
309 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.