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 | protected $limit; |
||
58 | |||
59 | /** |
||
60 | * The message's placeholders parser. |
||
61 | * |
||
62 | * @var \Drupal\Core\Logger\LogMessageParserInterface |
||
63 | */ |
||
64 | protected $parser; |
||
65 | |||
66 | /** |
||
67 | * The "requests" setting. |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $requests; |
||
72 | |||
73 | /** |
||
74 | * An array of templates already used in this request. |
||
75 | * |
||
76 | * Used only with request tracking enabled. |
||
77 | * |
||
78 | * @var string[] |
||
79 | */ |
||
80 | protected $templates = []; |
||
81 | |||
82 | /** |
||
83 | * A sequence number for log events during a request. |
||
84 | * |
||
85 | * @var int |
||
86 | */ |
||
87 | protected $sequence = 0; |
||
88 | |||
89 | /** |
||
90 | * Logger constructor. |
||
91 | * |
||
92 | * @param \MongoDB\Database $database |
||
93 | * The database object. |
||
94 | * @param \Drupal\Core\Logger\LogMessageParserInterface $parser |
||
95 | * The parser to use when extracting message variables. |
||
96 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
||
97 | * The core config_factory service. |
||
98 | * @param \Symfony\Component\HttpFoundation\RequestStack $stack |
||
99 | * The core request_stack service. |
||
100 | */ |
||
101 | public function __construct(Database $database, LogMessageParserInterface $parser, ConfigFactoryInterface $config_factory, RequestStack $stack) { |
||
112 | |||
113 | /** |
||
114 | * Fill in the log_entry function, file, and line. |
||
115 | * |
||
116 | * @param array $log_entry |
||
117 | * An event information to be logger. |
||
118 | * @param array $backtrace |
||
119 | * A call stack. |
||
120 | */ |
||
121 | protected function enhanceLogEntry(array &$log_entry, array $backtrace) { |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function log($level, $template, array $context = []) { |
||
277 | |||
278 | /** |
||
279 | * Ensure a collection is capped with the proper size. |
||
280 | * |
||
281 | * @param string $name |
||
282 | * The collection name. |
||
283 | * @param int $size |
||
284 | * The collection size cap. |
||
285 | * |
||
286 | * @return \MongoDB\Collection |
||
287 | * The collection, usable for additional commands like index creation. |
||
288 | * |
||
289 | * @TODO support sharded clusters: convertToCapped does not support them. |
||
290 | * |
||
291 | * @see https://docs.mongodb.com/manual/reference/command/convertToCapped |
||
292 | * |
||
293 | * Note that MongoDB 3.2 still misses a propert exists() command, which is the |
||
294 | * reason for the weird try/catch logic. |
||
295 | * |
||
296 | * @see https://jira.mongodb.org/browse/SERVER-1938 |
||
297 | */ |
||
298 | public function ensureCappedCollection($name, $size) { |
||
327 | |||
328 | /** |
||
329 | * Ensure indexes are set on the collections and tracker collection is capped. |
||
330 | * |
||
331 | * First index is on <line, timestamp> instead of <function, line, timestamp>, |
||
332 | * because we write to this collection a lot, and the smaller index on two |
||
333 | * numbers should be much faster to create than one with a string included. |
||
334 | */ |
||
335 | public function ensureSchema() { |
||
380 | |||
381 | /** |
||
382 | * Return a collection, given its template id. |
||
383 | * |
||
384 | * @param string $template_id |
||
385 | * The string representation of a template \MongoId. |
||
386 | * |
||
387 | * @return \MongoDB\Collection |
||
388 | * A collection object for the specified template id. |
||
389 | */ |
||
390 | public function eventCollection($template_id) { |
||
400 | |||
401 | /** |
||
402 | * List the event collections. |
||
403 | * |
||
404 | * @return \MongoDB\Collection[] |
||
405 | * The collections with a name matching the event pattern. |
||
406 | */ |
||
407 | public function eventCollections() { |
||
417 | |||
418 | /** |
||
419 | * Return the number of events for a template. |
||
420 | * |
||
421 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
422 | * A template for which to count events. |
||
423 | * |
||
424 | * @return int |
||
425 | * The number of matching events. |
||
426 | */ |
||
427 | /** |
||
428 | * @return int |
||
429 | */ |
||
430 | public function eventCount(EventTemplate $template) { |
||
433 | |||
434 | /** |
||
435 | * Return the events having occurred during a given request. |
||
436 | * |
||
437 | * @param string $unsafe_request_id |
||
438 | * The raw request_id. |
||
439 | * |
||
440 | * @return array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> |
||
441 | * An array of [template, event] arrays, ordered by occurrence order. |
||
442 | */ |
||
443 | public function requestEvents($unsafe_request_id) { |
||
475 | |||
476 | /** |
||
477 | * Return the number of event templates. |
||
478 | */ |
||
479 | public function templatesCount() { |
||
482 | |||
483 | /** |
||
484 | * Return an array of templates uses during a given request. |
||
485 | * |
||
486 | * @param string $unsafe_request_id |
||
487 | * A request "unique_id". |
||
488 | * |
||
489 | * @return array |
||
490 | * An array of EventTemplate instances. |
||
491 | */ |
||
492 | public function requestTemplates($unsafe_request_id) { |
||
530 | |||
531 | /** |
||
532 | * Return the request events tracker collection. |
||
533 | * |
||
534 | * @return \MongoDB\Collection |
||
535 | * The collection. |
||
536 | */ |
||
537 | public function trackerCollection() { |
||
540 | |||
541 | /** |
||
542 | * Return the event templates collection. |
||
543 | * |
||
544 | * @return \MongoDB\Collection |
||
545 | * The collection. |
||
546 | */ |
||
547 | public function templateCollection() { |
||
550 | |||
551 | /** |
||
552 | * Return templates matching type and level criteria. |
||
553 | * |
||
554 | * @param string[] $types |
||
555 | * An array of EventTemplate types. May be a hash. |
||
556 | * @param string[]|int[] $levels |
||
557 | * An array of severity levels. |
||
558 | * @param int $skip |
||
559 | * The number of templates to skip before the first one displayed. |
||
560 | * @param int $limit |
||
561 | * The maximum number of templates to return. |
||
562 | * |
||
563 | * @return \MongoDB\Driver\Cursor |
||
564 | * A query result for the templates. |
||
565 | */ |
||
566 | public function templates(array $types = [], array $levels = [], $skip = 0, $limit = 0) { |
||
596 | |||
597 | /** |
||
598 | * Return the template types actually present in storage. |
||
599 | * |
||
600 | * @return string[] |
||
601 | * An array of distinct EventTemplate types. |
||
602 | */ |
||
603 | public function templateTypes() { |
||
607 | |||
608 | } |
||
609 |
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.