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 | class Message extends Object |
||
35 | { |
||
36 | /** |
||
37 | * @var array the message. |
||
38 | */ |
||
39 | public $message; |
||
40 | |||
41 | /** |
||
42 | * @var Target the message target, a [[Target]] instance. May be `null`. |
||
43 | */ |
||
44 | public $target; |
||
45 | |||
46 | /** |
||
47 | * @var bool whether the current request is a console request. |
||
48 | */ |
||
49 | private $_isConsoleRequest; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * @param array $message the message. |
||
54 | * @param Target|null $target the message target, a [[Target]] instance. |
||
55 | * @param array $config name-value pairs that will be used to initialize the object properties. |
||
56 | */ |
||
57 | 143 | public function __construct(array $message, $target = null, $config = []) |
|
63 | |||
64 | /** |
||
65 | * @inheritDoc |
||
66 | */ |
||
67 | 143 | public function init() |
|
74 | |||
75 | /** |
||
76 | * Returns the message category. |
||
77 | * @return string the message category. |
||
78 | */ |
||
79 | 12 | public function getCategory() |
|
83 | |||
84 | /** |
||
85 | * Returns the command line. |
||
86 | * @return string|null the command line, `null` if not available. |
||
87 | */ |
||
88 | 12 | public function getCommandLine() |
|
101 | |||
102 | /** |
||
103 | * Returns whether the current request is a console request. |
||
104 | * @return bool whether the current request is a console request. |
||
105 | * @throws InvalidConfigException if unable to determine. |
||
106 | */ |
||
107 | 33 | public function getIsConsoleRequest() |
|
122 | |||
123 | /** |
||
124 | * Returns the text display of the message level. |
||
125 | * @return string the text display of the message level. |
||
126 | */ |
||
127 | 12 | public function getLevel() |
|
131 | |||
132 | /** |
||
133 | * Returns the string to be prefixed to the message. |
||
134 | * @return string the messsage prefix string. |
||
135 | */ |
||
136 | 12 | public function getPrefix() |
|
144 | |||
145 | /** |
||
146 | * Returns the session ID. |
||
147 | * @return string|null the session ID, `null` if not available. |
||
148 | */ |
||
149 | 12 | public function getSessionId() |
|
162 | |||
163 | /** |
||
164 | * Returns the additional stack trace as a string. |
||
165 | * @return string|null the stack trace, `null` if not available. |
||
166 | */ |
||
167 | 13 | public function getStackTrace() |
|
178 | |||
179 | /** |
||
180 | * Returns the message text. |
||
181 | * @return string the message text. |
||
182 | */ |
||
183 | 12 | public function getText() |
|
195 | |||
196 | /** |
||
197 | * Returns the message creation timestamp. |
||
198 | * @return float the message creation timestamp. |
||
199 | */ |
||
200 | 12 | public function getTimestamp() |
|
204 | |||
205 | /** |
||
206 | * Returns the current absolute URL. |
||
207 | * @return null|string the current absolute URL, `null` if not available. |
||
208 | */ |
||
209 | 12 | public function getUrl() |
|
217 | |||
218 | /** |
||
219 | * Returns the user identity ID. |
||
220 | * @return int|string|null the user identity ID, `null` if not available. |
||
221 | */ |
||
222 | 12 | public function getUserId() |
|
236 | |||
237 | /** |
||
238 | * Returns the user IP address. |
||
239 | * @return string|null the user IP address, `null` if not available. |
||
240 | */ |
||
241 | 12 | public function getUserIp() |
|
249 | } |
||
250 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: