Complex classes like SentryLogger 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 SentryLogger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class SentryLogger |
||
| 25 | { |
||
| 26 | use Configurable; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var SentryAdaptor |
||
| 30 | */ |
||
| 31 | public $client = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Stipulates what gets shown in the Sentry UI, should some metric not be |
||
| 35 | * available for any reason. |
||
| 36 | * |
||
| 37 | * @const string |
||
| 38 | */ |
||
| 39 | const SLW_NOOP = 'Unavailable'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A static constructor as per {@link Zend_Log_FactoryInterface}. |
||
| 43 | * |
||
| 44 | * @param array $config An array of optional additional configuration for |
||
| 45 | * passing custom information to Sentry. See the README |
||
| 46 | * for more detail. |
||
| 47 | * @return SentryLogger |
||
| 48 | */ |
||
| 49 | public static function factory(array $config = []): SentryLogger |
||
| 50 | { |
||
| 51 | $env = $config['env'] ?? []; |
||
| 52 | $user = $config['user'] ?? []; |
||
| 53 | $tags = $config['tags'] ?? []; |
||
| 54 | $extra = $config['extra'] ?? []; |
||
| 55 | // Set the minimum reporting level |
||
| 56 | $level = $config['level'] ?? self::config()->get('log_level'); |
||
| 57 | $logger = Injector::inst()->create(static::class); |
||
| 58 | |||
| 59 | // Set default environment |
||
| 60 | $env = $env ?: $logger->defaultEnv(); |
||
| 61 | // Set any available user data |
||
| 62 | $user = $user ?: $logger->defaultUser(); |
||
| 63 | // Set any available tags available in SS config |
||
| 64 | $tags = array_merge($logger->defaultTags(), $tags); |
||
| 65 | // Set any available additional (extra) data |
||
| 66 | $extra = array_merge($logger->defaultExtra(), $extra); |
||
| 67 | |||
| 68 | $logger->adaptor->setContext('env', $env); |
||
| 69 | $logger->adaptor->setContext('tags', $tags); |
||
| 70 | $logger->adaptor->setContext('extra', $extra); |
||
| 71 | $logger->adaptor->setContext('user', $user); |
||
| 72 | $logger->adaptor->setContext('level', $level); |
||
| 73 | |||
| 74 | return $logger; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return SentryAdaptor |
||
| 79 | */ |
||
| 80 | public function getAdaptor(): SentryAdaptor |
||
| 81 | { |
||
| 82 | return $this->adaptor; |
||
|
|
|||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns a default environment when one isn't passed to the factory() |
||
| 87 | * method. |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function defaultEnv(): string |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns a default set of additional "tags" we wish to send to Sentry. |
||
| 98 | * By default, Sentry reports on several mertrics, and we're already sending |
||
| 99 | * {@link Member} data. But there are additional data that would be useful |
||
| 100 | * for debugging via the Sentry UI. |
||
| 101 | * |
||
| 102 | * These data can augment that which is sent to Sentry at setup |
||
| 103 | * time in _config.php. See the README for more detail. |
||
| 104 | * |
||
| 105 | * N.b. Tags can be used to group messages within the Sentry UI itself, so there |
||
| 106 | * should only be "static" data being sent, not something that can drastically |
||
| 107 | * or minutely change, such as memory usage for example. |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function defaultTags(): array |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns a default set of extra data to show upon selecting a message for |
||
| 123 | * analysis in the Sentry UI. This can augment the data sent to Sentry at setup |
||
| 124 | * time in _config.php as well as at runtime when calling SS_Log itself. |
||
| 125 | * See the README for more detail. |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function defaultExtra(): array |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Return the version of $pkg taken from composer.lock. |
||
| 140 | * |
||
| 141 | * @param string $pkg e.g. "silverstripe/framework" |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getPackageInfo(string $pkg): string |
||
| 162 | |||
| 163 | /** |
||
| 164 | * What sort of request is this? (A harder question to answer than you might |
||
| 165 | * think: http://stackoverflow.com/questions/6275363/what-is-the-correct-terminology-for-a-non-ajax-request) |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getRequestType(): string |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Return peak memory usage. |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getPeakMemory(): string |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Basic User-Agent check and return. |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getUserAgent(): string |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Basic request method check and return. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getReqMethod(): string |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function getSAPI(): string |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the client IP address which originated this request. |
||
| 231 | * Lifted and modified from SilverStripe 3's SS_HTTPRequest. |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getIP(): string |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns a default set of additional data specific to the user's part in |
||
| 273 | * the request. |
||
| 274 | * |
||
| 275 | * @param mixed Member|null $member |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function defaultUser(Member $member = null): array |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Generate a cleaned-up backtrace of the event that got us here. |
||
| 293 | * |
||
| 294 | * @param array $record |
||
| 295 | * @return array |
||
| 296 | * @todo Unused in sentry-sdk 2.0?? |
||
| 297 | */ |
||
| 298 | public static function backtrace(array $record): array |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the information about the current release, if possible |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | protected function getGitInfo() |
||
| 341 | { |
||
| 342 | // Initialise empty array to return |
||
| 343 | $return = []; |
||
| 344 | // If the file operations error out, we need to catch it |
||
| 345 | try { |
||
| 346 | $return = []; |
||
| 347 | $return = $this->getGitCommitMessage($return); |
||
| 348 | } catch (\Exception $exception) { |
||
| 349 | // Default message. As it's an extra, it's not overly crowding the interface |
||
| 350 | $return['Message'] = 'No git repo found or inaccessible'; |
||
| 351 | } |
||
| 352 | |||
| 353 | return $return; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the HEAD tag or the release |
||
| 358 | * @param SentryAdaptor $client |
||
| 359 | */ |
||
| 360 | public static function getGitHead($client) |
||
| 361 | { |
||
| 362 | $head = file_exists(Director::baseFolder() . '/.git/HEAD'); |
||
| 363 | if ($head) { |
||
| 364 | $head = explode(':', file_get_contents(Director::baseFolder() . '/.git/HEAD')); |
||
| 365 | $ref = file_get_contents(Director::baseFolder() . '/.git/' . trim($head[1])); |
||
| 366 | $ref = self::getGitTag($ref); |
||
| 367 | $client->getOptions()->setRelease(trim($ref)); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Check if we are on a tagged release and if so, use that to set the client release |
||
| 373 | * @param $ref |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | protected static function getGitTag($ref) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param array $return |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | protected function getGitCommitMessage(array $return): array |
||
| 409 | |||
| 410 | } |
||
| 411 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: