Complex classes like Logger 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 Logger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Logger extends AbstractLogger { |
||
| 23 | const CONFIG_NAME = 'mongodb_watchdog.settings'; |
||
| 24 | |||
| 25 | const TRACKER_COLLECTION = 'watchdog_tracker'; |
||
| 26 | const TEMPLATE_COLLECTION = 'watchdog'; |
||
| 27 | const EVENT_COLLECTION_PREFIX = 'watchdog_event_'; |
||
| 28 | const EVENT_COLLECTIONS_PATTERN = '^watchdog_event_[[:xdigit:]]{32}$'; |
||
| 29 | |||
| 30 | const LEGACY_TYPE_MAP = [ |
||
| 31 | 'typeMap' => [ |
||
| 32 | 'array' => 'array', |
||
| 33 | 'document' => 'array', |
||
| 34 | 'root' => 'array', |
||
| 35 | ], |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The logger storage. |
||
| 40 | * |
||
| 41 | * @var \MongoDB\Database |
||
| 42 | */ |
||
| 43 | protected $database; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The limit for the capped event collections. |
||
| 47 | * |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | protected $items; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The minimum logging level. |
||
| 54 | * |
||
| 55 | * @var int |
||
| 56 | * |
||
| 57 | * @see https://drupal.org/node/1355808 |
||
| 58 | */ |
||
| 59 | protected $limit = RfcLogLevel::DEBUG; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The message's placeholders parser. |
||
| 63 | * |
||
| 64 | * @var \Drupal\Core\Logger\LogMessageParserInterface |
||
| 65 | */ |
||
| 66 | protected $parser; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The "requests" setting. |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $requests; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * An array of templates already used in this request. |
||
| 77 | * |
||
| 78 | * Used only with request tracking enabled. |
||
| 79 | * |
||
| 80 | * @var string[] |
||
| 81 | */ |
||
| 82 | protected $templates = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * A sequence number for log events during a request. |
||
| 86 | * |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | protected $sequence = 0; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Logger constructor. |
||
| 93 | * |
||
| 94 | * @param \MongoDB\Database $database |
||
| 95 | * The database object. |
||
| 96 | * @param \Drupal\Core\Logger\LogMessageParserInterface $parser |
||
| 97 | * The parser to use when extracting message variables. |
||
| 98 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
||
| 99 | * The core config_factory service. |
||
| 100 | * @param \Symfony\Component\HttpFoundation\RequestStack $stack |
||
| 101 | * The core request_stack service. |
||
| 102 | */ |
||
| 103 | public function __construct(Database $database, LogMessageParserInterface $parser, ConfigFactoryInterface $config_factory, RequestStack $stack) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Fill in the log_entry function, file, and line. |
||
| 117 | * |
||
| 118 | * @param array $log_entry |
||
| 119 | * An event information to be logger. |
||
| 120 | * @param array $backtrace |
||
| 121 | * A call stack. |
||
| 122 | */ |
||
| 123 | protected function enhanceLogEntry(array &$log_entry, array $backtrace) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | * |
||
| 174 | * @see https://drupal.org/node/1355808 |
||
| 175 | */ |
||
| 176 | public function log($level, $template, array $context = []) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Ensure a collection is capped with the proper size. |
||
| 284 | * |
||
| 285 | * @param string $name |
||
| 286 | * The collection name. |
||
| 287 | * @param int $size |
||
| 288 | * The collection size cap. |
||
| 289 | * |
||
| 290 | * @return \MongoDB\Collection |
||
| 291 | * The collection, usable for additional commands like index creation. |
||
| 292 | * |
||
| 293 | * @TODO support sharded clusters: convertToCapped does not support them. |
||
| 294 | * |
||
| 295 | * @see https://docs.mongodb.com/manual/reference/command/convertToCapped |
||
| 296 | * |
||
| 297 | * Note that MongoDB 3.2 still misses a proper exists() command, which is the |
||
| 298 | * reason for the weird try/catch logic. |
||
| 299 | * |
||
| 300 | * @see https://jira.mongodb.org/browse/SERVER-1938 |
||
| 301 | */ |
||
| 302 | public function ensureCappedCollection($name, $size) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Ensure indexes are set on the collections and tracker collection is capped. |
||
| 360 | * |
||
| 361 | * First index is on <line, timestamp> instead of <function, line, timestamp>, |
||
| 362 | * because we write to this collection a lot, and the smaller index on two |
||
| 363 | * numbers should be much faster to create than one with a string included. |
||
| 364 | */ |
||
| 365 | public function ensureSchema() { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Return a collection, given its template id. |
||
| 418 | * |
||
| 419 | * @param string $template_id |
||
| 420 | * The string representation of a template \MongoId. |
||
| 421 | * |
||
| 422 | * @return \MongoDB\Collection |
||
| 423 | * A collection object for the specified template id. |
||
| 424 | */ |
||
| 425 | public function eventCollection($template_id) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * List the event collections. |
||
| 438 | * |
||
| 439 | * @return \MongoDB\Collection[] |
||
| 440 | * The collections with a name matching the event pattern. |
||
| 441 | */ |
||
| 442 | public function eventCollections() { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Return the number of events for a template. |
||
| 455 | * |
||
| 456 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 457 | * A template for which to count events. |
||
| 458 | * |
||
| 459 | * @return int |
||
| 460 | * The number of matching events. |
||
| 461 | */ |
||
| 462 | public function eventCount(EventTemplate $template) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Return the events having occurred during a given request. |
||
| 468 | * |
||
| 469 | * @param string $requestId |
||
| 470 | * The request unique_id. |
||
| 471 | * @param int $skip |
||
| 472 | * The number of events to skip in the result. |
||
| 473 | * @param int $limit |
||
| 474 | * The maximum number of events to return. |
||
| 475 | * |
||
| 476 | * @return array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> |
||
| 477 | * An array of [template, event] arrays, ordered by occurrence order. |
||
| 478 | */ |
||
| 479 | public function requestEvents($requestId, $skip = 0, $limit = 0) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Count events matching a request unique_id. |
||
| 519 | * |
||
| 520 | * XXX This implementation may be very inefficient in case of a request gone |
||
| 521 | * bad generating non-templated varying messages: #requests is O(#templates). |
||
| 522 | * |
||
| 523 | * @param string $requestId |
||
| 524 | * The unique_id of the request. |
||
| 525 | * |
||
| 526 | * @return int |
||
| 527 | * The number of events matching the unique_id. |
||
| 528 | */ |
||
| 529 | public function requestEventsCount($requestId) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Return the number of event templates. |
||
| 549 | */ |
||
| 550 | public function templatesCount() { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Return an array of templates uses during a given request. |
||
| 556 | * |
||
| 557 | * @param string $unsafe_request_id |
||
| 558 | * A request "unique_id". |
||
| 559 | * |
||
| 560 | * @return \Drupal\mongodb_watchdog\EventTemplate[] |
||
| 561 | * An array of EventTemplate instances. |
||
| 562 | */ |
||
| 563 | public function requestTemplates($unsafe_request_id) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Return the request events tracker collection. |
||
| 604 | * |
||
| 605 | * @return \MongoDB\Collection |
||
| 606 | * The collection. |
||
| 607 | */ |
||
| 608 | public function trackerCollection() { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Return the event templates collection. |
||
| 614 | * |
||
| 615 | * @return \MongoDB\Collection |
||
| 616 | * The collection. |
||
| 617 | */ |
||
| 618 | public function templateCollection() { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Return templates matching type and level criteria. |
||
| 624 | * |
||
| 625 | * @param string[] $types |
||
| 626 | * An array of EventTemplate types. May be a hash. |
||
| 627 | * @param string[]|int[] $levels |
||
| 628 | * An array of severity levels. |
||
| 629 | * @param int $skip |
||
| 630 | * The number of templates to skip before the first one displayed. |
||
| 631 | * @param int $limit |
||
| 632 | * The maximum number of templates to return. |
||
| 633 | * |
||
| 634 | * @return \MongoDB\Driver\Cursor |
||
| 635 | * A query result for the templates. |
||
| 636 | */ |
||
| 637 | public function templates(array $types = [], array $levels = [], $skip = 0, $limit = 0) { |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Return the template types actually present in storage. |
||
| 670 | * |
||
| 671 | * @return string[] |
||
| 672 | * An array of distinct EventTemplate types. |
||
| 673 | */ |
||
| 674 | public function templateTypes() { |
||
| 678 | |||
| 679 | } |
||
| 680 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.