Conditions | 23 |
Paths | 28 |
Total Lines | 94 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
165 | public static function shouldSendAnalytics(): bool |
||
166 | { |
||
167 | $result = true; |
||
168 | $request = Craft::$app->getRequest(); |
||
169 | |||
170 | $logExclusion = static function (string $setting) |
||
171 | { |
||
172 | if (InstantAnalytics::$settings->logExcludedAnalytics) { |
||
173 | $request = Craft::$app->getRequest(); |
||
174 | $requestIp = $request->getUserIP(); |
||
175 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
176 | 'Analytics excluded for:: {requestIp} due to: `{setting}`', |
||
177 | compact('requestIp', 'setting'), |
||
178 | __METHOD__ |
||
179 | ); |
||
180 | } |
||
181 | }; |
||
182 | |||
183 | if (!InstantAnalytics::$settings->sendAnalyticsData) { |
||
184 | $logExclusion('sendAnalyticsData'); |
||
185 | return false; |
||
186 | } |
||
187 | |||
188 | if (!InstantAnalytics::$settings->sendAnalyticsInDevMode && Craft::$app->getConfig()->getGeneral()->devMode) { |
||
189 | $logExclusion('sendAnalyticsInDevMode'); |
||
190 | return false; |
||
191 | } |
||
192 | |||
193 | if ($request->getIsConsoleRequest()) { |
||
194 | $logExclusion('Craft::$app->getRequest()->getIsConsoleRequest()'); |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | if ($request->getIsCpRequest()) { |
||
199 | $logExclusion('Craft::$app->getRequest()->getIsCpRequest()'); |
||
200 | return false; |
||
201 | } |
||
202 | |||
203 | if ($request->getIsLivePreview()) { |
||
204 | $logExclusion('Craft::$app->getRequest()->getIsLivePreview()'); |
||
205 | return false; |
||
206 | } |
||
207 | |||
208 | // Check the $_SERVER[] super-global exclusions |
||
209 | if (InstantAnalytics::$settings->serverExcludes !== null |
||
210 | && is_array(InstantAnalytics::$settings->serverExcludes)) { |
||
211 | foreach (InstantAnalytics::$settings->serverExcludes as $match => $matchArray) { |
||
212 | if (isset($_SERVER[$match])) { |
||
213 | foreach ($matchArray as $matchItem) { |
||
214 | if (preg_match($matchItem, $_SERVER[$match])) { |
||
215 | $logExclusion('serverExcludes'); |
||
216 | |||
217 | return false; |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | // Filter out bot/spam requests via UserAgent |
||
225 | if (InstantAnalytics::$settings->filterBotUserAgents) { |
||
226 | $crawlerDetect = new CrawlerDetect; |
||
227 | // Check the user agent of the current 'visitor' |
||
228 | if ($crawlerDetect->isCrawler()) { |
||
229 | $logExclusion('filterBotUserAgents'); |
||
230 | |||
231 | return false; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | // Filter by user group |
||
236 | $userService = Craft::$app->getUser(); |
||
237 | /** @var UserElement $user */ |
||
238 | $user = $userService->getIdentity(); |
||
239 | if ($user) { |
||
240 | if (InstantAnalytics::$settings->adminExclude && $user->admin) { |
||
241 | $logExclusion('adminExclude'); |
||
242 | |||
243 | return false; |
||
244 | } |
||
245 | |||
246 | if (InstantAnalytics::$settings->groupExcludes !== null |
||
247 | && is_array(InstantAnalytics::$settings->groupExcludes)) { |
||
248 | foreach (InstantAnalytics::$settings->groupExcludes as $matchItem) { |
||
249 | if ($user->isInGroup($matchItem)) { |
||
250 | $logExclusion('groupExcludes'); |
||
251 | |||
252 | return false; |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | return $result; |
||
259 | } |
||
335 |