Conditions | 23 |
Paths | 28 |
Total Lines | 93 |
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 | if (InstantAnalytics::$settings->logExcludedAnalytics) { |
||
172 | $request = Craft::$app->getRequest(); |
||
173 | $requestIp = $request->getUserIP(); |
||
174 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
175 | 'Analytics excluded for:: {requestIp} due to: `{setting}`', |
||
176 | compact('requestIp', 'setting'), |
||
177 | __METHOD__ |
||
178 | ); |
||
179 | } |
||
180 | }; |
||
181 | |||
182 | if (!InstantAnalytics::$settings->sendAnalyticsData) { |
||
183 | $logExclusion('sendAnalyticsData'); |
||
184 | return false; |
||
185 | } |
||
186 | |||
187 | if (!InstantAnalytics::$settings->sendAnalyticsInDevMode && Craft::$app->getConfig()->getGeneral()->devMode) { |
||
188 | $logExclusion('sendAnalyticsInDevMode'); |
||
189 | return false; |
||
190 | } |
||
191 | |||
192 | if ($request->getIsConsoleRequest()) { |
||
193 | $logExclusion('Craft::$app->getRequest()->getIsConsoleRequest()'); |
||
194 | return false; |
||
195 | } |
||
196 | |||
197 | if ($request->getIsCpRequest()) { |
||
198 | $logExclusion('Craft::$app->getRequest()->getIsCpRequest()'); |
||
199 | return false; |
||
200 | } |
||
201 | |||
202 | if ($request->getIsLivePreview()) { |
||
203 | $logExclusion('Craft::$app->getRequest()->getIsLivePreview()'); |
||
204 | return false; |
||
205 | } |
||
206 | |||
207 | // Check the $_SERVER[] super-global exclusions |
||
208 | if (InstantAnalytics::$settings->serverExcludes !== null |
||
209 | && !empty(InstantAnalytics::$settings->serverExcludes)) { |
||
210 | foreach (InstantAnalytics::$settings->serverExcludes as $match => $matchArray) { |
||
211 | if (isset($_SERVER[$match])) { |
||
212 | foreach ($matchArray as $matchItem) { |
||
213 | if (preg_match($matchItem, $_SERVER[$match])) { |
||
214 | $logExclusion('serverExcludes'); |
||
215 | |||
216 | return false; |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | |||
223 | // Filter out bot/spam requests via UserAgent |
||
224 | if (InstantAnalytics::$settings->filterBotUserAgents) { |
||
225 | $crawlerDetect = new CrawlerDetect(); |
||
226 | // Check the user agent of the current 'visitor' |
||
227 | if ($crawlerDetect->isCrawler()) { |
||
228 | $logExclusion('filterBotUserAgents'); |
||
229 | |||
230 | return false; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | // Filter by user group |
||
235 | $userService = Craft::$app->getUser(); |
||
236 | /** @var ?UserElement $user */ |
||
237 | $user = $userService->getIdentity(); |
||
238 | if ($user) { |
||
239 | if (InstantAnalytics::$settings->adminExclude && $user->admin) { |
||
240 | $logExclusion('adminExclude'); |
||
241 | |||
242 | return false; |
||
243 | } |
||
244 | |||
245 | if (InstantAnalytics::$settings->groupExcludes !== null |
||
246 | && !empty(InstantAnalytics::$settings->groupExcludes)) { |
||
247 | foreach (InstantAnalytics::$settings->groupExcludes as $matchItem) { |
||
248 | if ($user->isInGroup($matchItem)) { |
||
249 | $logExclusion('groupExcludes'); |
||
250 | |||
251 | return false; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | return $result; |
||
258 | } |
||
354 |