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 |
||
35 | class Message extends BaseObject |
||
36 | { |
||
37 | /** |
||
38 | * @var array raw message. |
||
39 | */ |
||
40 | public $message; |
||
41 | |||
42 | /** |
||
43 | * @var Target message target. |
||
44 | * Must be a [[Target]] instance or `null`. |
||
45 | */ |
||
46 | public $target; |
||
47 | |||
48 | /** |
||
49 | * @var bool whether the current request is a console request. |
||
50 | */ |
||
51 | private $_isConsoleRequest; |
||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * @param array $message message. |
||
56 | * @param Target|null $target message target. |
||
57 | * Must be a [[Target]] instance or `null`. |
||
58 | * @param array $config name-value pairs that will be used to initialize the object properties. |
||
59 | */ |
||
60 | 143 | public function __construct(array $message, $target = null, $config = []) |
|
66 | |||
67 | /** |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | 143 | public function init() |
|
77 | |||
78 | /** |
||
79 | * Returns the message category. |
||
80 | * @return string message category. |
||
81 | */ |
||
82 | 12 | public function getCategory() |
|
86 | |||
87 | /** |
||
88 | * Returns the command line. |
||
89 | * @return string|null command line, `null` if not available. |
||
90 | */ |
||
91 | 12 | public function getCommandLine() |
|
103 | |||
104 | /** |
||
105 | * Returns whether the current request is a console request. |
||
106 | * @return bool whether the current request is a console request. |
||
107 | * @throws InvalidConfigException if unable to determine. |
||
108 | */ |
||
109 | 33 | public function getIsConsoleRequest() |
|
124 | |||
125 | /** |
||
126 | * Returns the message level as a string. |
||
127 | * @return string message level as a string. |
||
128 | */ |
||
129 | 12 | public function getLevel() |
|
133 | |||
134 | /** |
||
135 | * Returns a string to be prefixed to the message. |
||
136 | * @return string messsage prefix string. |
||
137 | */ |
||
138 | 12 | public function getPrefix() |
|
146 | |||
147 | /** |
||
148 | * Returns the session ID. |
||
149 | * @return string|null session ID, `null` if not available. |
||
150 | */ |
||
151 | 12 | public function getSessionId() |
|
164 | |||
165 | /** |
||
166 | * Returns the additional stack trace as a string. |
||
167 | * @return string|null stack trace, `null` if not available. |
||
168 | */ |
||
169 | 13 | public function getStackTrace() |
|
180 | |||
181 | /** |
||
182 | * Returns the message text. |
||
183 | * @return string message text. |
||
184 | */ |
||
185 | 12 | public function getText() |
|
197 | |||
198 | /** |
||
199 | * Returns the message creation timestamp. |
||
200 | * @return float message creation timestamp. |
||
201 | */ |
||
202 | 12 | public function getTimestamp() |
|
206 | |||
207 | /** |
||
208 | * Returns the current absolute URL. |
||
209 | * @return null|string absolute URL, `null` if not available. |
||
210 | */ |
||
211 | 12 | public function getUrl() |
|
219 | |||
220 | /** |
||
221 | * Returns the user identity ID. |
||
222 | * @return int|string|null user identity ID, `null` if not available. |
||
223 | */ |
||
224 | 12 | public function getUserId() |
|
238 | |||
239 | /** |
||
240 | * Returns the user IP address. |
||
241 | * @return string|null user IP address, `null` if not available. |
||
242 | */ |
||
243 | 12 | public function getUserIp() |
|
251 | } |
||
252 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.