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 | ||
| 34 | * @property string|null $userIp user IP address. | ||
| 35 | */ | ||
| 36 | class Message extends Object | ||
| 37 | { | ||
| 38 | /** | ||
| 39 | * @var array raw message. | ||
| 40 | */ | ||
| 41 | public $message; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * @var Target message target. | ||
| 45 | * Must be a [[Target]] instance or `null`. | ||
| 46 | */ | ||
| 47 | public $target; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @var bool whether the current request is a console request. | ||
| 51 | */ | ||
| 52 | private $_isConsoleRequest; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Constructor. | ||
| 56 | * @param array $message message. | ||
| 57 | * @param Target|null $target message target. | ||
| 58 | * Must be a [[Target]] instance or `null`. | ||
| 59 | * @param array $config name-value pairs that will be used to initialize the object properties. | ||
| 60 | */ | ||
| 61 | 143 | public function __construct(array $message, $target = null, $config = []) | |
| 67 | |||
| 68 | /** | ||
| 69 | * @inheritDoc | ||
| 70 | */ | ||
| 71 | 143 | public function init() | |
| 78 | |||
| 79 | /** | ||
| 80 | * Returns the message category. | ||
| 81 | * @return string message category. | ||
| 82 | */ | ||
| 83 | 12 | public function getCategory() | |
| 87 | |||
| 88 | /** | ||
| 89 | * Returns the command line. | ||
| 90 | * @return string|null command line, `null` if not available. | ||
| 91 | */ | ||
| 92 | 12 | public function getCommandLine() | |
| 104 | |||
| 105 | /** | ||
| 106 | * Returns whether the current request is a console request. | ||
| 107 | * @return bool whether the current request is a console request. | ||
| 108 | * @throws InvalidConfigException if unable to determine. | ||
| 109 | */ | ||
| 110 | 33 | public function getIsConsoleRequest() | |
| 125 | |||
| 126 | /** | ||
| 127 | * Returns the message level as a string. | ||
| 128 | * @return string message level as a string. | ||
| 129 | */ | ||
| 130 | 12 | public function getLevel() | |
| 134 | |||
| 135 | /** | ||
| 136 | * Returns a string to be prefixed to the message. | ||
| 137 | * @return string messsage prefix string. | ||
| 138 | */ | ||
| 139 | 12 | public function getPrefix() | |
| 147 | |||
| 148 | /** | ||
| 149 | * Returns the session ID. | ||
| 150 | * @return string|null session ID, `null` if not available. | ||
| 151 | */ | ||
| 152 | 12 | public function getSessionId() | |
| 165 | |||
| 166 | /** | ||
| 167 | * Returns the additional stack trace as a string. | ||
| 168 | * @return string|null stack trace, `null` if not available. | ||
| 169 | */ | ||
| 170 | 13 | public function getStackTrace() | |
| 181 | |||
| 182 | /** | ||
| 183 | * Returns the message text. | ||
| 184 | * @return string message text. | ||
| 185 | */ | ||
| 186 | 12 | public function getText() | |
| 198 | |||
| 199 | /** | ||
| 200 | * Returns the message creation timestamp. | ||
| 201 | * @return float message creation timestamp. | ||
| 202 | */ | ||
| 203 | 12 | public function getTimestamp() | |
| 207 | |||
| 208 | /** | ||
| 209 | * Returns the current absolute URL. | ||
| 210 | * @return null|string absolute URL, `null` if not available. | ||
| 211 | */ | ||
| 212 | 12 | public function getUrl() | |
| 220 | |||
| 221 | /** | ||
| 222 | * Returns the user identity ID. | ||
| 223 | * @return int|string|null user identity ID, `null` if not available. | ||
| 224 | */ | ||
| 225 | 12 | public function getUserId() | |
| 239 | |||
| 240 | /** | ||
| 241 | * Returns the user IP address. | ||
| 242 | * @return string|null user IP address, `null` if not available. | ||
| 243 | */ | ||
| 244 | 12 | public function getUserIp() | |
| 245 |     { | ||
| 246 | 12 |         if (\Yii::$app === null || $this->getIsConsoleRequest()) { | |
| 247 | 8 | return null; | |
| 248 | } | ||
| 249 | |||
| 250 | 4 | return \Yii::$app->getRequest()->getUserIP(); | |
| 253 |