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 | 4 | 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) |
|
| 122 | |||
| 123 | 3 | public function getVersion() |
|
| 127 | |||
| 128 | 2 | public function setVersion($version) |
|
| 134 | |||
| 135 | 3 | public function getHost() |
|
| 139 | |||
| 140 | 2 | public function setHost($host) |
|
| 146 | |||
| 147 | 3 | public function getShortMessage() |
|
| 151 | |||
| 152 | 3 | public function setShortMessage($shortMessage) |
|
| 158 | |||
| 159 | 3 | public function getFullMessage() |
|
| 163 | |||
| 164 | 2 | public function setFullMessage($fullMessage) |
|
| 170 | |||
| 171 | 4 | public function getTimestamp() |
|
| 175 | |||
| 176 | 3 | public function setTimestamp($timestamp) |
|
| 186 | |||
| 187 | 1 | public function getLevel() |
|
| 191 | |||
| 192 | 3 | public function getSyslogLevel() |
|
| 196 | |||
| 197 | 18 | public function setLevel($level) |
|
| 203 | |||
| 204 | 3 | public function getFacility() |
|
| 208 | |||
| 209 | 2 | public function setFacility($facility) |
|
| 215 | |||
| 216 | 3 | public function getFile() |
|
| 220 | |||
| 221 | 3 | public function setFile($file) |
|
| 227 | |||
| 228 | 3 | public function getLine() |
|
| 232 | |||
| 233 | 2 | public function setLine($line) |
|
| 239 | |||
| 240 | 2 | public function getAdditional($key) |
|
| 250 | |||
| 251 | 1 | public function hasAdditional($key) |
|
| 255 | |||
| 256 | 5 | public function setAdditional($key, $value) |
|
| 266 | |||
| 267 | 3 | public function getAllAdditionals() |
|
| 271 | |||
| 272 | 2 | public function toArray() |
|
| 305 | } |
||
| 306 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.