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 |
||
38 | abstract class Target extends Component |
||
39 | { |
||
40 | /** |
||
41 | * @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories. |
||
42 | * You can use an asterisk at the end of a category so that the category may be used to |
||
43 | * match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
||
44 | * categories starting with 'yii\db\', such as 'yii\db\Connection'. |
||
45 | */ |
||
46 | public $categories = []; |
||
47 | /** |
||
48 | * @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages. |
||
49 | * If this property is not empty, then any category listed here will be excluded from [[categories]]. |
||
50 | * You can use an asterisk at the end of a category so that the category can be used to |
||
51 | * match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
||
52 | * categories starting with 'yii\db\', such as 'yii\db\Connection'. |
||
53 | * @see categories |
||
54 | */ |
||
55 | public $except = []; |
||
56 | /** |
||
57 | * @var array list of the PHP predefined variables that should be logged in a message. |
||
58 | * Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be logged. |
||
59 | * |
||
60 | * Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`. |
||
61 | * |
||
62 | * Since version 2.0.9 additional syntax can be used: |
||
63 | * Each element could be specified as one of the following: |
||
64 | * |
||
65 | * - `var` - `var` will be logged. |
||
66 | * - `var.key` - only `var[key]` key will be logged. |
||
67 | * - `!var.key` - `var[key]` key will be excluded. |
||
68 | * |
||
69 | * @see \yii\helpers\ArrayHelper::filter() |
||
70 | */ |
||
71 | public $logVars = ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']; |
||
72 | /** |
||
73 | * @var callable a PHP callable that returns a string to be prefixed to every exported message. |
||
74 | * |
||
75 | * If not set, [[getMessagePrefix()]] will be used, which prefixes the message with context information |
||
76 | * such as user IP, user ID and session ID. |
||
77 | * |
||
78 | * The signature of the callable should be `function ($message)`. |
||
79 | */ |
||
80 | public $prefix; |
||
81 | /** |
||
82 | * @var int how many messages should be accumulated before they are exported. |
||
83 | * Defaults to 1000. Note that messages will always be exported when the application terminates. |
||
84 | * Set this property to be 0 if you don't want to export messages until the application terminates. |
||
85 | */ |
||
86 | public $exportInterval = 1000; |
||
87 | /** |
||
88 | * @var array the messages that are retrieved from the logger so far by this log target. |
||
89 | * Please refer to [[Logger::messages]] for the details about the message structure. |
||
90 | */ |
||
91 | public $messages = []; |
||
92 | |||
93 | /** |
||
94 | * @var bool whether to log time with microseconds. |
||
95 | * Defaults to false. |
||
96 | * @since 2.0.13 |
||
97 | */ |
||
98 | public $microtime = false; |
||
99 | |||
100 | private $_levels = 0; |
||
101 | private $_enabled = true; |
||
102 | |||
103 | /** |
||
104 | * Exports log [[messages]] to a specific destination. |
||
105 | * Child classes must implement this method. |
||
106 | */ |
||
107 | abstract public function export(); |
||
108 | |||
109 | /** |
||
110 | * Processes the given log messages. |
||
111 | * This method will filter the given messages with [[levels]] and [[categories]]. |
||
112 | * And if requested, it will also export the filtering result to specific medium (e.g. email). |
||
113 | * @param array $messages log messages to be processed. See [[Logger::messages]] for the structure |
||
114 | * of each message. |
||
115 | * @param bool $final whether this method is called at the end of the current application |
||
116 | */ |
||
117 | 337 | public function collect($messages, $final) |
|
134 | |||
135 | /** |
||
136 | * Generates the context information to be logged. |
||
137 | * The default implementation will dump user information, system variables, etc. |
||
138 | * @return string the context information. If an empty string, it means no context information. |
||
139 | */ |
||
140 | 26 | protected function getContextMessage() |
|
150 | |||
151 | /** |
||
152 | * @return int the message levels that this target is interested in. This is a bitmap of |
||
153 | * level values. Defaults to 0, meaning all available levels. |
||
154 | */ |
||
155 | 339 | public function getLevels() |
|
159 | |||
160 | /** |
||
161 | * Sets the message levels that this target is interested in. |
||
162 | * |
||
163 | * The parameter can be either an array of interested level names or an integer representing |
||
164 | * the bitmap of the interested level values. Valid level names include: 'error', |
||
165 | * 'warning', 'info', 'trace' and 'profile'; valid level values include: |
||
166 | * [[Logger::LEVEL_ERROR]], [[Logger::LEVEL_WARNING]], [[Logger::LEVEL_INFO]], |
||
167 | * [[Logger::LEVEL_TRACE]] and [[Logger::LEVEL_PROFILE]]. |
||
168 | * |
||
169 | * For example, |
||
170 | * |
||
171 | * ```php |
||
172 | * ['error', 'warning'] |
||
173 | * // which is equivalent to: |
||
174 | * Logger::LEVEL_ERROR | Logger::LEVEL_WARNING |
||
175 | * ``` |
||
176 | * |
||
177 | * @param array|int $levels message levels that this target is interested in. |
||
178 | * @throws InvalidConfigException if $levels value is not correct. |
||
179 | */ |
||
180 | 24 | public function setLevels($levels) |
|
208 | |||
209 | /** |
||
210 | * Filters the given messages according to their categories and levels. |
||
211 | * @param array $messages messages to be filtered. |
||
212 | * The message structure follows that in [[Logger::messages]]. |
||
213 | * @param int $levels the message levels to filter by. This is a bitmap of |
||
214 | * level values. Value 0 means allowing all levels. |
||
215 | * @param array $categories the message categories to filter by. If empty, it means all categories are allowed. |
||
216 | * @param array $except the message categories to exclude. If empty, it means all categories are allowed. |
||
217 | * @return array the filtered messages. |
||
218 | */ |
||
219 | 337 | public static function filterMessages($messages, $levels = 0, $categories = [], $except = []) |
|
252 | |||
253 | /** |
||
254 | * Formats a log message for display as a string. |
||
255 | * @param array $message the log message to be formatted. |
||
256 | * The message structure follows that in [[Logger::messages]]. |
||
257 | * @return string the formatted message |
||
258 | */ |
||
259 | 3 | public function formatMessage($message) |
|
282 | |||
283 | /** |
||
284 | * Returns a string to be prefixed to the given message. |
||
285 | * If [[prefix]] is configured it will return the result of the callback. |
||
286 | * The default implementation will return user IP, user ID and session ID as a prefix. |
||
287 | * @param array $message the message being exported. |
||
288 | * The message structure follows that in [[Logger::messages]]. |
||
289 | * @return string the prefix string |
||
290 | */ |
||
291 | 9 | public function getMessagePrefix($message) |
|
318 | |||
319 | /** |
||
320 | * Sets a value indicating whether this log target is enabled. |
||
321 | * @param bool|callable $value a boolean value or a callable to obtain the value from. |
||
322 | * The callable value is available since version 2.0.13. |
||
323 | * |
||
324 | * A callable may be used to determine whether the log target should be enabled in a dynamic way. |
||
325 | * For example, to only enable a log if the current user is logged in you can configure the target |
||
326 | * as follows: |
||
327 | * |
||
328 | * ```php |
||
329 | * 'enabled' => function() { |
||
330 | * return !Yii::$app->user->isGuest; |
||
331 | * } |
||
332 | * ``` |
||
333 | */ |
||
334 | 2 | public function setEnabled($value) |
|
338 | |||
339 | /** |
||
340 | * Check whether the log target is enabled. |
||
341 | * @property Indicates whether this log target is enabled. Defaults to true. |
||
342 | * @return bool A value indicating whether this log target is enabled. |
||
343 | */ |
||
344 | 338 | public function getEnabled() |
|
352 | |||
353 | /** |
||
354 | * Returns formatted ('Y-m-d H:i:s') timestamp for message. |
||
355 | * If [[microtime]] is configured to true it will return format 'Y-m-d H:i:s.u' |
||
356 | * @param float $timestamp |
||
357 | * @return string |
||
358 | * @since 2.0.13 |
||
359 | */ |
||
360 | 3 | protected function getTime($timestamp) |
|
366 | } |
||
367 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.