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 | private $_levels = 0; |
||
94 | private $_enabled = true; |
||
95 | |||
96 | /** |
||
97 | * Exports log [[messages]] to a specific destination. |
||
98 | * Child classes must implement this method. |
||
99 | */ |
||
100 | abstract public function export(); |
||
101 | |||
102 | /** |
||
103 | * Processes the given log messages. |
||
104 | * This method will filter the given messages with [[levels]] and [[categories]]. |
||
105 | * And if requested, it will also export the filtering result to specific medium (e.g. email). |
||
106 | * @param array $messages log messages to be processed. See [[Logger::messages]] for the structure |
||
107 | * of each message. |
||
108 | * @param bool $final whether this method is called at the end of the current application |
||
109 | */ |
||
110 | 335 | public function collect($messages, $final) |
|
111 | { |
||
112 | 335 | $this->messages = array_merge($this->messages, static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except)); |
|
113 | 335 | $count = count($this->messages); |
|
114 | 335 | if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) { |
|
115 | 25 | if (($context = $this->getContextMessage()) !== '') { |
|
116 | 6 | $this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME]; |
|
117 | } |
||
118 | // set exportInterval to 0 to avoid triggering export again while exporting |
||
119 | 25 | $oldExportInterval = $this->exportInterval; |
|
120 | 25 | $this->exportInterval = 0; |
|
121 | 25 | $this->export(); |
|
122 | 25 | $this->exportInterval = $oldExportInterval; |
|
123 | |||
124 | 25 | $this->messages = []; |
|
125 | } |
||
126 | 335 | } |
|
127 | |||
128 | /** |
||
129 | * Generates the context information to be logged. |
||
130 | * The default implementation will dump user information, system variables, etc. |
||
131 | * @return string the context information. If an empty string, it means no context information. |
||
132 | */ |
||
133 | 26 | protected function getContextMessage() |
|
134 | { |
||
135 | 26 | $context = ArrayHelper::filter($GLOBALS, $this->logVars); |
|
136 | 26 | $result = []; |
|
137 | 26 | foreach ($context as $key => $value) { |
|
138 | 7 | $result[] = "\${$key} = " . VarDumper::dumpAsString($value); |
|
139 | } |
||
140 | |||
141 | 26 | return implode("\n\n", $result); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return int the message levels that this target is interested in. This is a bitmap of |
||
146 | * level values. Defaults to 0, meaning all available levels. |
||
147 | */ |
||
148 | 337 | public function getLevels() |
|
152 | |||
153 | /** |
||
154 | * Sets the message levels that this target is interested in. |
||
155 | * |
||
156 | * The parameter can be either an array of interested level names or an integer representing |
||
157 | * the bitmap of the interested level values. Valid level names include: 'error', |
||
158 | * 'warning', 'info', 'trace' and 'profile'; valid level values include: |
||
159 | * [[Logger::LEVEL_ERROR]], [[Logger::LEVEL_WARNING]], [[Logger::LEVEL_INFO]], |
||
160 | * [[Logger::LEVEL_TRACE]] and [[Logger::LEVEL_PROFILE]]. |
||
161 | * |
||
162 | * For example, |
||
163 | * |
||
164 | * ```php |
||
165 | * ['error', 'warning'] |
||
166 | * // which is equivalent to: |
||
167 | * Logger::LEVEL_ERROR | Logger::LEVEL_WARNING |
||
168 | * ``` |
||
169 | * |
||
170 | * @param array|int $levels message levels that this target is interested in. |
||
171 | * @throws InvalidConfigException if $levels value is not correct. |
||
172 | */ |
||
173 | 24 | public function setLevels($levels) |
|
201 | |||
202 | /** |
||
203 | * Filters the given messages according to their categories and levels. |
||
204 | * @param array $messages messages to be filtered. |
||
205 | * The message structure follows that in [[Logger::messages]]. |
||
206 | * @param int $levels the message levels to filter by. This is a bitmap of |
||
207 | * level values. Value 0 means allowing all levels. |
||
208 | * @param array $categories the message categories to filter by. If empty, it means all categories are allowed. |
||
209 | * @param array $except the message categories to exclude. If empty, it means all categories are allowed. |
||
210 | * @return array the filtered messages. |
||
211 | */ |
||
212 | 335 | public static function filterMessages($messages, $levels = 0, $categories = [], $except = []) |
|
245 | |||
246 | /** |
||
247 | * Formats a log message for display as a string. |
||
248 | * @param array $message the log message to be formatted. |
||
249 | * The message structure follows that in [[Logger::messages]]. |
||
250 | * @return string the formatted message |
||
251 | */ |
||
252 | 2 | public function formatMessage($message) |
|
275 | |||
276 | /** |
||
277 | * Returns a string to be prefixed to the given message. |
||
278 | * If [[prefix]] is configured it will return the result of the callback. |
||
279 | * The default implementation will return user IP, user ID and session ID as a prefix. |
||
280 | * @param array $message the message being exported. |
||
281 | * The message structure follows that in [[Logger::messages]]. |
||
282 | * @return string the prefix string |
||
283 | */ |
||
284 | 8 | public function getMessagePrefix($message) |
|
311 | |||
312 | /** |
||
313 | * Sets a value indicating whether this log target is enabled. |
||
314 | * @param bool|callable $value a boolean value or a callable to obtain the value from. |
||
315 | * The callable value is available since version 2.0.13. |
||
316 | * |
||
317 | * A callable may be used to determine whether the log target should be enabled in a dynamic way. |
||
318 | * For example, to only enable a log if the current user is logged in you can configure the target |
||
319 | * as follows: |
||
320 | * |
||
321 | * ```php |
||
322 | * 'enabled' => function() { |
||
323 | * return !Yii::$app->user->isGuest; |
||
324 | * } |
||
325 | * ``` |
||
326 | */ |
||
327 | 1 | public function setEnabled($value) |
|
331 | |||
332 | /** |
||
333 | * Check whether the log target is enabled. |
||
334 | * @property Indicates whether this log target is enabled. Defaults to true. |
||
335 | * @return bool A value indicating whether this log target is enabled. |
||
336 | */ |
||
337 | 336 | public function getEnabled() |
|
345 | } |
||
346 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.