HandlerMapper   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 52
lcom 0
cbo 0
dl 0
loc 114
ccs 106
cts 106
cp 1
rs 7.44
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F map() 0 111 52

How to fix   Complexity   

Complex Class

Complex classes like HandlerMapper 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 HandlerMapper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WShafer\PSR11MonoLog\Handler;
6
7
use WShafer\PSR11MonoLog\MapperInterface;
8
9
/**
10
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
12
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
13
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
14
 */
15
class HandlerMapper implements MapperInterface
16
{
17 52
    public function map(string $type)
18
    {
19 52
        $type = strtolower($type);
20
21 52
        switch ($type) {
22 52
            case 'stream':
23 1
                return StreamHandlerFactory::class;
24 51
            case 'rotating':
25 1
                return RotatingFileHandlerFactory::class;
26 50
            case 'syslog':
27 1
                return SyslogHandlerFactory::class;
28 49
            case 'errorlog':
29 1
                return ErrorLogHandlerFactory::class;
30 48
            case 'nativemailer':
31 1
                return NativeMailerHandlerFactory::class;
32 47
            case 'swiftmailer':
33 1
                return SwiftMailerHandlerFactory::class;
34 46
            case 'pushover':
35 1
                return PushoverHandlerFactory::class;
36 45
            case 'flowdock':
37 1
                return FlowdockHandlerFactory::class;
38 44
            case 'slackwebhook':
39 1
                return SlackWebhookHandlerFactory::class;
40 43
            case 'slack':
41 1
                return SlackHandlerFactory::class;
42 42
            case 'mandrill':
43 1
                return MandrillHandlerFactory::class;
44 41
            case 'fleephook':
45 1
                return FleepHookHandlerFactory::class;
46 40
            case 'ifttt':
47 1
                return IFTTTHandlerFactory::class;
48 39
            case 'socket':
49 1
                return SocketHandlerFactory::class;
50 38
            case 'amqp':
51 1
                return AmqpHandlerFactory::class;
52 37
            case 'gelf':
53 1
                return GelfHandlerFactory::class;
54 36
            case 'cube':
55 1
                return CubeHandlerFactory::class;
56 35
            case 'zend':
57 1
                return ZendMonitorHandlerFactory::class;
58 34
            case 'newrelic':
59 1
                return NewRelicHandlerFactory::class;
60 33
            case 'loggly':
61 1
                return LogglyHandlerFactory::class;
62 32
            case 'syslogudp':
63 1
                return SyslogUdpHandlerFactory::class;
64 31
            case 'logentries':
65 1
                return LogEntriesHandlerFactory::class;
66 30
            case 'firephp':
67 1
                return FirePHPHandlerFactory::class;
68 29
            case 'chromephp':
69 1
                return ChromePHPHandlerFactory::class;
70 28
            case 'browserconsole':
71 1
                return BrowserConsoleHandlerFactory::class;
72 27
            case 'phpconsole':
73 1
                return PHPConsoleHandlerFactory::class;
74 26
            case 'redis':
75 1
                return RedisHandlerFactory::class;
76 25
            case 'mongo':
77 1
                return MongoDBHandlerFactory::class;
78 24
            case 'couchdb':
79 1
                return CouchDBHandlerFactory::class;
80 23
            case 'doctrinecouchdb':
81 1
                return DoctrineCouchDBHandlerFactory::class;
82 22
            case 'elastica':
83 1
                return ElasticaHandlerFactory::class;
84 21
            case 'dynamodb':
85 1
                return DynamoDbHandlerFactory::class;
86 20
            case 'fingerscrossed':
87 1
                return FingersCrossedHandlerFactory::class;
88 19
            case 'deduplication':
89 1
                return DeduplicationHandlerFactory::class;
90 18
            case 'whatfailuregrouphandler':
91 1
                return WhatFailureGroupHandlerFactory::class;
92 17
            case 'buffer':
93 1
                return BufferHandlerFactory::class;
94 16
            case 'group':
95 1
                return GroupHandlerFactory::class;
96 15
            case 'filter':
97 1
                return FilterHandlerFactory::class;
98 14
            case 'sampling':
99 1
                return SamplingHandlerFactory::class;
100 13
            case 'null':
101 1
                return NullHandlerFactory::class;
102 12
            case 'psr':
103 1
                return PsrHandlerFactory::class;
104 11
            case 'process':
105 1
                return ProcessHandlerFactory::class;
106 10
            case 'sendgrid':
107 1
                return SendGridHandlerFactory::class;
108 9
            case 'telegrambot':
109 1
                return TelegramBotHandlerFactory::class;
110 8
            case 'insightops':
111 1
                return InsightOpsHandlerFactory::class;
112 7
            case 'logmatic':
113 1
                return LogmaticHandlerFactory::class;
114 6
            case 'sqs':
115 1
                return SqsHandlerFactory::class;
116 5
            case 'fallbackgroup':
117 1
                return FallbackGroupHandlerFactory::class;
118 4
            case 'noop':
119 1
                return NoopHandlerFactory::class;
120 3
            case 'overflow':
121 1
                return OverflowHandlerFactory::class;
122 2
            case 'test':
123 1
                return TestHandlerFactory::class;
124
        }
125
126 1
        return null;
127
    }
128
}
129