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