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 |
||
32 | class Logger extends AbstractLogger { |
||
33 | use StringTranslationTrait; |
||
34 | |||
35 | // Configuration-related constants. |
||
36 | // The configuration item. |
||
37 | const CONFIG_NAME = 'mongodb_watchdog.settings'; |
||
38 | // The individual configuration keys. |
||
39 | const CONFIG_ITEMS = 'items'; |
||
40 | const CONFIG_REQUESTS = 'requests'; |
||
41 | const CONFIG_LIMIT = 'limit'; |
||
42 | const CONFIG_ITEMS_PER_PAGE = 'items_per_page'; |
||
43 | const CONFIG_REQUEST_TRACKING = 'request_tracking'; |
||
44 | |||
45 | // The logger database alias. |
||
46 | const DB_LOGGER = 'logger'; |
||
47 | |||
48 | // The default channel exposed when using the raw PSR-3 contract. |
||
49 | const DEFAULT_CHANNEL = 'psr-3'; |
||
50 | |||
51 | const MODULE = 'mongodb_watchdog'; |
||
52 | |||
53 | // The service for the specific PSR-3 logger for MongoDB. |
||
54 | const SERVICE_LOGGER = 'mongodb.logger'; |
||
55 | // The service for the Drupal LoggerChannel for this module, logging to all |
||
56 | // active loggers. |
||
57 | const SERVICE_CHANNEL = 'logger.channel.mongodb_watchdog'; |
||
58 | // The service for hook_requirements(). |
||
59 | const SERVICE_REQUIREMENTS = 'mongodb.watchdog_requirements'; |
||
60 | const SERVICE_SANITY_CHECK = 'mongodb.watchdog.sanity_check'; |
||
61 | |||
62 | const TRACKER_COLLECTION = 'watchdog_tracker'; |
||
63 | const TEMPLATE_COLLECTION = 'watchdog'; |
||
64 | const EVENT_COLLECTION_PREFIX = 'watchdog_event_'; |
||
65 | const EVENT_COLLECTIONS_PATTERN = '^watchdog_event_[[:xdigit:]]{32}$'; |
||
66 | |||
67 | const LEGACY_TYPE_MAP = [ |
||
68 | 'typeMap' => [ |
||
69 | 'array' => 'array', |
||
70 | 'document' => 'array', |
||
71 | 'root' => 'array', |
||
72 | ], |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * Map of PSR3 log constants to RFC 5424 log constants. |
||
77 | * |
||
78 | * @var array |
||
79 | * |
||
80 | * @see \Drupal\Core\Logger\LoggerChannel |
||
81 | * @see \Drupal\mongodb_watchdog\Logger::log() |
||
82 | */ |
||
83 | protected $rfc5424levels = [ |
||
84 | LogLevel::EMERGENCY => RfcLogLevel::EMERGENCY, |
||
85 | LogLevel::ALERT => RfcLogLevel::ALERT, |
||
86 | LogLevel::CRITICAL => RfcLogLevel::CRITICAL, |
||
87 | LogLevel::ERROR => RfcLogLevel::ERROR, |
||
88 | LogLevel::WARNING => RfcLogLevel::WARNING, |
||
89 | LogLevel::NOTICE => RfcLogLevel::NOTICE, |
||
90 | LogLevel::INFO => RfcLogLevel::INFO, |
||
91 | LogLevel::DEBUG => RfcLogLevel::DEBUG, |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * The logger storage. |
||
96 | * |
||
97 | * @var \MongoDB\Database |
||
98 | */ |
||
99 | protected $database; |
||
100 | |||
101 | /** |
||
102 | * The limit for the capped event collections. |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | protected $items; |
||
107 | |||
108 | /** |
||
109 | * The minimum logging level. |
||
110 | * |
||
111 | * @var int |
||
112 | * |
||
113 | * @see https://drupal.org/node/1355808 |
||
114 | */ |
||
115 | protected $limit = RfcLogLevel::DEBUG; |
||
116 | |||
117 | /** |
||
118 | * The messenger service. |
||
119 | * |
||
120 | * @var \Drupal\Core\Messenger\MessengerInterface |
||
121 | */ |
||
122 | protected $messenger; |
||
123 | |||
124 | /** |
||
125 | * The message's placeholders parser. |
||
126 | * |
||
127 | * @var \Drupal\Core\Logger\LogMessageParserInterface |
||
128 | */ |
||
129 | protected $parser; |
||
130 | |||
131 | /** |
||
132 | * The "requests" setting. |
||
133 | * |
||
134 | * @var int |
||
135 | */ |
||
136 | protected $requests; |
||
137 | |||
138 | /** |
||
139 | * The request_stack service. |
||
140 | * |
||
141 | * @var \Symfony\Component\HttpFoundation\RequestStack |
||
142 | */ |
||
143 | protected $requestStack; |
||
144 | |||
145 | /** |
||
146 | * A sequence number for log events during a request. |
||
147 | * |
||
148 | * @var int |
||
149 | */ |
||
150 | protected $sequence = 0; |
||
151 | |||
152 | /** |
||
153 | * An array of templates already used in this request. |
||
154 | * |
||
155 | * Used only with request tracking enabled. |
||
156 | * |
||
157 | * @var string[] |
||
158 | */ |
||
159 | protected $templates = []; |
||
160 | |||
161 | /** |
||
162 | * The datetime.time service. |
||
163 | * |
||
164 | * @var \Drupal\Component\Datetime\TimeInterface |
||
165 | */ |
||
166 | protected $time; |
||
167 | |||
168 | /** |
||
169 | * Logger constructor. |
||
170 | * |
||
171 | * @param \MongoDB\Database $database |
||
172 | * The database object. |
||
173 | * @param \Drupal\Core\Logger\LogMessageParserInterface $parser |
||
174 | * The parser to use when extracting message variables. |
||
175 | * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory |
||
176 | * The core config_factory service. |
||
177 | * @param \Symfony\Component\HttpFoundation\RequestStack $stack |
||
178 | * The core request_stack service. |
||
179 | * @param \Drupal\Core\Messenger\MessengerInterface $messenger |
||
180 | * The messenger service. |
||
181 | * @param \Drupal\Component\Datetime\TimeInterface $time |
||
182 | * The datetime.time service. |
||
183 | */ |
||
184 | public function __construct( |
||
208 | |||
209 | /** |
||
210 | * Fill in the log_entry function, file, and line. |
||
211 | * |
||
212 | * @param array $entry |
||
213 | * An event information to be logger. |
||
214 | * @param array $backtrace |
||
215 | * A call stack. |
||
216 | * |
||
217 | * @throws \ReflectionException |
||
218 | */ |
||
219 | protected function enhanceLogEntry(array &$entry, array $backtrace): void { |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | * |
||
270 | * @see https://httpd.apache.org/docs/2.4/en/mod/mod_unique_id.html |
||
271 | */ |
||
272 | public function log($level, $template, array $context = []): void { |
||
389 | |||
390 | /** |
||
391 | * Ensure a collection is capped with the proper size. |
||
392 | * |
||
393 | * @param string $name |
||
394 | * The collection name. |
||
395 | * @param int $size |
||
396 | * The collection size cap. |
||
397 | * |
||
398 | * @return \MongoDB\Collection |
||
399 | * The collection, usable for additional commands like index creation. |
||
400 | * |
||
401 | * @TODO support sharded clusters: convertToCapped does not support them. |
||
402 | * |
||
403 | * @throws \MongoDB\Exception\InvalidArgumentException |
||
404 | * @throws \MongoDB\Exception\UnsupportedException |
||
405 | * @throws \MongoDB\Exception\UnexpectedValueException |
||
406 | * @throws \MongoDB\Driver\Exception\RuntimeException |
||
407 | * |
||
408 | * @see https://docs.mongodb.com/manual/reference/command/convertToCapped |
||
409 | * |
||
410 | * Note that MongoDB 4.2 still misses a proper exists() command, which is the |
||
411 | * reason for the weird try/catch logic. |
||
412 | * |
||
413 | * @see https://jira.mongodb.org/browse/SERVER-1938 |
||
414 | */ |
||
415 | public function ensureCappedCollection(string $name, int $size): Collection { |
||
444 | |||
445 | /** |
||
446 | * Ensure a collection exists in the logger database. |
||
447 | * |
||
448 | * - If it already existed, it will not lose any data. |
||
449 | * - If it gets created, it will be empty. |
||
450 | * |
||
451 | * @param string $name |
||
452 | * The name of the collection. |
||
453 | * |
||
454 | * @return \MongoDB\Collection |
||
455 | * The chosen collection, guaranteed to exist. |
||
456 | * |
||
457 | * @throws \MongoDB\Exception\InvalidArgumentException |
||
458 | * @throws \MongoDB\Exception\UnsupportedException |
||
459 | * @throws \MongoDB\Exception\UnexpectedValueException |
||
460 | * @throws \MongoDB\Driver\Exception\RuntimeException |
||
461 | */ |
||
462 | public function ensureCollection(string $name): Collection { |
||
496 | |||
497 | /** |
||
498 | * Ensure indexes are set on the collections and tracker collection is capped. |
||
499 | * |
||
500 | * First index is on <line, timestamp> instead of <function, line, timestamp>, |
||
501 | * because we write to this collection a lot, and the smaller index on two |
||
502 | * numbers should be much faster to create than one with a string included. |
||
503 | */ |
||
504 | public function ensureSchema(): void { |
||
548 | |||
549 | /** |
||
550 | * Return a collection, given its template id. |
||
551 | * |
||
552 | * @param string $templateId |
||
553 | * The string representation of a template \MongoId. |
||
554 | * |
||
555 | * @return \MongoDB\Collection |
||
556 | * A collection object for the specified template id. |
||
557 | */ |
||
558 | public function eventCollection($templateId): Collection { |
||
568 | |||
569 | /** |
||
570 | * List the event collections. |
||
571 | * |
||
572 | * @return \MongoDB\Model\CollectionInfoIterator |
||
573 | * The collections with a name matching the event pattern. |
||
574 | */ |
||
575 | public function eventCollections() : CollectionInfoIterator { |
||
584 | |||
585 | /** |
||
586 | * Return the number of events for a template. |
||
587 | * |
||
588 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
589 | * A template for which to count events. |
||
590 | * |
||
591 | * @return int |
||
592 | * The number of matching events. |
||
593 | */ |
||
594 | public function eventCount(EventTemplate $template) : int { |
||
597 | |||
598 | /** |
||
599 | * Return the events having occurred during a given request. |
||
600 | * |
||
601 | * @param string $requestId |
||
602 | * The request unique_id. |
||
603 | * @param int $skip |
||
604 | * The number of events to skip in the result. |
||
605 | * @param int $limit |
||
606 | * The maximum number of events to return. |
||
607 | * |
||
608 | * @return \Drupal\mongodb_watchdog\EventTemplate|\Drupal\mongodb_watchdog\Event[] |
||
609 | * An array of [template, event] arrays, ordered by occurrence order. |
||
610 | */ |
||
611 | public function requestEvents($requestId, $skip = 0, $limit = 0): array { |
||
646 | |||
647 | /** |
||
648 | * Count events matching a request unique_id. |
||
649 | * |
||
650 | * XXX This implementation may be very inefficient in case of a request gone |
||
651 | * bad generating non-templated varying messages: #requests is O(#templates). |
||
652 | * |
||
653 | * @param string $requestId |
||
654 | * The unique_id of the request. |
||
655 | * |
||
656 | * @return int |
||
657 | * The number of events matching the unique_id. |
||
658 | */ |
||
659 | public function requestEventsCount($requestId): int { |
||
676 | |||
677 | /** |
||
678 | * Setter for limit. |
||
679 | * |
||
680 | * @param int $limit |
||
681 | * The limit value. |
||
682 | */ |
||
683 | public function setLimit(int $limit): void { |
||
686 | |||
687 | /** |
||
688 | * Return the number of event templates. |
||
689 | * |
||
690 | * @throws \ReflectionException |
||
691 | */ |
||
692 | public function templatesCount(): int { |
||
695 | |||
696 | /** |
||
697 | * Return an array of templates uses during a given request. |
||
698 | * |
||
699 | * @param string $unsafeRequestId |
||
700 | * A request "unique_id". |
||
701 | * |
||
702 | * @return \Drupal\mongodb_watchdog\EventTemplate[] |
||
703 | * An array of EventTemplate instances. |
||
704 | * |
||
705 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
706 | * @see https://github.com/phpmd/phpmd/issues/561 |
||
707 | */ |
||
708 | public function requestTemplates($unsafeRequestId): array { |
||
747 | |||
748 | /** |
||
749 | * Return the request events tracker collection. |
||
750 | * |
||
751 | * @return \MongoDB\Collection |
||
752 | * The collection. |
||
753 | */ |
||
754 | public function trackerCollection(): Collection { |
||
757 | |||
758 | /** |
||
759 | * Return the event templates collection. |
||
760 | * |
||
761 | * @return \MongoDB\Collection |
||
762 | * The collection. |
||
763 | */ |
||
764 | public function templateCollection(): Collection { |
||
767 | |||
768 | /** |
||
769 | * Return templates matching type and level criteria. |
||
770 | * |
||
771 | * @param string[] $types |
||
772 | * An array of EventTemplate types. May be a hash. |
||
773 | * @param string[]|int[] $levels |
||
774 | * An array of severity levels. |
||
775 | * @param int $skip |
||
776 | * The number of templates to skip before the first one displayed. |
||
777 | * @param int $limit |
||
778 | * The maximum number of templates to return. |
||
779 | * |
||
780 | * @return \MongoDB\Driver\Cursor |
||
781 | * A query result for the templates. |
||
782 | */ |
||
783 | public function templates(array $types = [], array $levels = [], $skip = 0, $limit = 0): Cursor { |
||
813 | |||
814 | /** |
||
815 | * Return the template types actually present in storage. |
||
816 | * |
||
817 | * @return string[] |
||
818 | * An array of distinct EventTemplate types. |
||
819 | */ |
||
820 | public function templateTypes(): array { |
||
824 | |||
825 | } |
||
826 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.