Complex classes like Target 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 Target, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class Target extends Component |
||
36 | { |
||
37 | /** |
||
38 | * @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories. |
||
39 | * You can use an asterisk at the end of a category so that the category may be used to |
||
40 | * match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
||
41 | * categories starting with 'yii\db\', such as `yii\db\Connection`. |
||
42 | */ |
||
43 | public $categories = []; |
||
44 | /** |
||
45 | * @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages. |
||
46 | * If this property is not empty, then any category listed here will be excluded from [[categories]]. |
||
47 | * You can use an asterisk at the end of a category so that the category can be used to |
||
48 | * match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
||
49 | * categories starting with 'yii\db\', such as `yii\db\Connection`. |
||
50 | * @see categories |
||
51 | */ |
||
52 | public $except = []; |
||
53 | /** |
||
54 | * @var array the message levels that this target is interested in. |
||
55 | * |
||
56 | * The parameter should be an array of interested level names. See [[LogLevel]] constants for valid level names. |
||
57 | * |
||
58 | * For example: |
||
59 | * |
||
60 | * ```php |
||
61 | * ['error', 'warning'], |
||
62 | * // or |
||
63 | * [LogLevel::ERROR, LogLevel::WARNING] |
||
64 | * ``` |
||
65 | * |
||
66 | * Defaults is empty array, meaning all available levels. |
||
67 | */ |
||
68 | public $levels = []; |
||
69 | /** |
||
70 | * @var array list of the PHP predefined variables that should be logged in a message. |
||
71 | * Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be logged. |
||
72 | * |
||
73 | * Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`. |
||
74 | * |
||
75 | * Since version 2.0.9 additional syntax can be used: |
||
76 | * Each element could be specified as one of the following: |
||
77 | * |
||
78 | * - `var` - `var` will be logged. |
||
79 | * - `var.key` - only `var[key]` key will be logged. |
||
80 | * - `!var.key` - `var[key]` key will be excluded. |
||
81 | * |
||
82 | * @see \yii\helpers\ArrayHelper::filter() |
||
83 | */ |
||
84 | public $logVars = ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']; |
||
85 | /** |
||
86 | * @var callable a PHP callable that returns a string to be prefixed to every exported message. |
||
87 | * |
||
88 | * If not set, [[getMessagePrefix()]] will be used, which prefixes the message with context information |
||
89 | * such as user IP, user ID and session ID. |
||
90 | * |
||
91 | * The signature of the callable should be `function ($message)`. |
||
92 | */ |
||
93 | public $prefix; |
||
94 | /** |
||
95 | * @var int how many messages should be accumulated before they are exported. |
||
96 | * Defaults to 1000. Note that messages will always be exported when the application terminates. |
||
97 | * Set this property to be 0 if you don't want to export messages until the application terminates. |
||
98 | */ |
||
99 | public $exportInterval = 1000; |
||
100 | /** |
||
101 | * @var array the messages that are retrieved from the logger so far by this log target. |
||
102 | * Please refer to [[Logger::messages]] for the details about the message structure. |
||
103 | */ |
||
104 | public $messages = []; |
||
105 | |||
106 | /** |
||
107 | * @var bool |
||
108 | */ |
||
109 | private $_enabled = true; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Exports log [[messages]] to a specific destination. |
||
114 | * Child classes must implement this method. |
||
115 | */ |
||
116 | abstract public function export(); |
||
117 | |||
118 | /** |
||
119 | * Processes the given log messages. |
||
120 | * This method will filter the given messages with [[levels]] and [[categories]]. |
||
121 | * And if requested, it will also export the filtering result to specific medium (e.g. email). |
||
122 | * @param array $messages log messages to be processed. See [[Logger::messages]] for the structure |
||
123 | * of each message. |
||
124 | * @param bool $final whether this method is called at the end of the current application |
||
125 | */ |
||
126 | 334 | public function collect($messages, $final) |
|
143 | |||
144 | /** |
||
145 | * Generates the context information to be logged. |
||
146 | * The default implementation will dump user information, system variables, etc. |
||
147 | * @return string the context information. If an empty string, it means no context information. |
||
148 | */ |
||
149 | 26 | protected function getContextMessage() |
|
159 | |||
160 | /** |
||
161 | * Filters the given messages according to their categories and levels. |
||
162 | * @param array $messages messages to be filtered. |
||
163 | * The message structure follows that in [[Logger::messages]]. |
||
164 | * @param array $levels the message levels to filter by. Empty value means allowing all levels. |
||
165 | * @param array $categories the message categories to filter by. If empty, it means all categories are allowed. |
||
166 | * @param array $except the message categories to exclude. If empty, it means all categories are allowed. |
||
167 | * @return array the filtered messages. |
||
168 | */ |
||
169 | 334 | public static function filterMessages($messages, $levels = [], $categories = [], $except = []) |
|
202 | |||
203 | /** |
||
204 | * Formats a log message for display as a string. |
||
205 | * @param array $message the log message to be formatted. |
||
206 | * The message structure follows that in [[Logger::messages]]. |
||
207 | * @return string the formatted message |
||
208 | */ |
||
209 | 2 | public function formatMessage($message) |
|
226 | |||
227 | /** |
||
228 | * Returns a string to be prefixed to the given message. |
||
229 | * If [[prefix]] is configured it will return the result of the callback. |
||
230 | * The default implementation will return user IP, user ID and session ID as a prefix. |
||
231 | * @param array $message the message being exported. |
||
232 | * The message structure follows that in [[Logger::messages]]. |
||
233 | * @return string the prefix string |
||
234 | */ |
||
235 | 8 | public function getMessagePrefix($message) |
|
262 | |||
263 | /** |
||
264 | * Sets a value indicating whether this log target is enabled. |
||
265 | * @param bool|callable $value a boolean value or a callable to obtain the value from. |
||
266 | * The callable value is available since version 2.0.13. |
||
267 | * |
||
268 | * A callable may be used to determine whether the log target should be enabled in a dynamic way. |
||
269 | * For example, to only enable a log if the current user is logged in you can configure the target |
||
270 | * as follows: |
||
271 | * |
||
272 | * ```php |
||
273 | * 'enabled' => function() { |
||
274 | * return !Yii::$app->user->isGuest; |
||
275 | * } |
||
276 | * ``` |
||
277 | */ |
||
278 | 1 | public function setEnabled($value) |
|
282 | |||
283 | /** |
||
284 | * Check whether the log target is enabled. |
||
285 | * @property Indicates whether this log target is enabled. Defaults to true. |
||
286 | * @return bool A value indicating whether this log target is enabled. |
||
287 | */ |
||
288 | 335 | public function getEnabled() |
|
296 | } |
||
297 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.