LogLine   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Test Coverage

Coverage 61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 43
eloc 67
c 1
b 0
f 0
dl 0
loc 215
ccs 61
cts 100
cp 0.61
rs 8.96

35 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeys() 0 3 1
A getUrl() 0 3 1
A getDomain() 0 3 1
A getStatus() 0 3 1
A getReferer() 0 3 1
A getHit() 0 3 1
A setHost() 0 3 1
A getResponseBytes() 0 3 1
A __construct() 0 3 1
A getMethod() 0 3 1
A setRequest() 0 7 1
A setHeaderUserAgent() 0 3 1
A setResponseBytes() 0 4 2
A getLogname() 0 3 1
A getTime() 0 3 1
A setHeaderReferer() 0 4 3
A setUrl() 0 3 1
A setDomain() 0 3 1
A getUser() 0 3 1
A setUseragent() 0 3 1
A setLogname() 0 4 2
A incrementHit() 0 3 1
A ipAdressNumber() 0 8 1
A setHttpVersion() 0 3 1
A getUseragent() 0 3 1
A isInternalReferer() 0 7 3
A setUser() 0 4 2
A getIp() 0 3 1
A setIp() 0 3 1
A setStatus() 0 3 1
A setReferer() 0 3 2
A setMethod() 0 3 1
A getHttpVersion() 0 3 1
A setTime() 0 7 1
A get() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like LogLine 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.

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 LogLine, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PiedWeb\LogsAnalyzer;
4
5
class LogLine
6
{
7
    protected $ip;
8
    protected $logname;
9
    protected $user;
10
    protected $date;
11
    protected $time;
12
    protected $domain;
13
    protected $method;
14
    protected $url;
15
    protected $httpVersion;
16
    protected $status;
17
    protected $responseBytes;
18
    protected $referer;
19
    protected $useragent;
20
    protected $hit;
21
22 3
    public function __construct(?string $domain = null)
23
    {
24 3
        $this->domain = $domain;
25 3
    }
26
27
    public function getKeys()
28
    {
29
        return array_keys(get_object_vars($this));
30
    }
31
32
    public function get()
33
    {
34
        return array_values(get_object_vars($this));
35
    }
36
37
    public function getIp()
38
    {
39
        return $this->ip;
40
    }
41
42 3
    public function setHost($ip)
43
    {
44 3
        $this->setIp($ip);
45 3
    }
46
47 3
    public function setIp($ip)
48
    {
49 3
        $this->ip = $ip;
50 3
    }
51
52
    public function getLogname()
53
    {
54
        return $this->logname;
55
    }
56
57 3
    public function setLogname($logname)
58
    {
59 3
        if ('-' != $logname) {
60
            $this->logname = $logname;
61
        }
62 3
    }
63
64
    public function getUser()
65
    {
66
        return $this->user;
67
    }
68
69 3
    public function setUser($user)
70
    {
71 3
        if ('-' != $user) {
72
            $this->user = $user;
73
        }
74 3
    }
75
76
    public function getTime()
77
    {
78
        return $this->time;
79
    }
80
81 3
    public function setTime($time)
82
    {
83 3
        list($date, $time) = explode(':', $time, 2);
84 3
        $this->time = $time;
85
86 3
        $date = \DateTime::createFromFormat('j/M/Y', $date);
87 3
        $this->date = $date->format('Ymd');
88 3
    }
89
90
    public function getDomain()
91
    {
92
        return $this->domain;
93
    }
94
95
    public function setDomain($domain)
96
    {
97
        $this->domain = $domain;
98
    }
99
100 3
    public function setMethod($method)
101
    {
102 3
        $this->method = $method;
103 3
    }
104
105
    public function getMethod()
106
    {
107
        return $this->method;
108
    }
109
110 3
    public function setUrl($url)
111
    {
112 3
        $this->url = $url;
113 3
    }
114
115
    public function getUrl()
116
    {
117
        return $this->url;
118
    }
119
120 3
    public function setRequest($request)
121
    {
122 3
        $request = str_replace(['http://'.$this->domain.'/', 'https://'.$this->domain.'/'], '/', $request);
123 3
        list($method, $url, $httpVersion) = explode(' ', $request);
124 3
        $this->setMethod($method);
125 3
        $this->setUrl($url);
126 3
        $this->setHttpVersion($httpVersion);
127 3
    }
128
129
    public function getHttpVersion()
130
    {
131
        return $this->httpVersion;
132
    }
133
134 3
    public function setHttpVersion($httpVersion)
135
    {
136 3
        $this->httpVersion = $httpVersion;
137 3
    }
138
139
    public function getStatus()
140
    {
141
        return $this->status;
142
    }
143
144 3
    public function setStatus($status)
145
    {
146 3
        $this->status = $status;
147 3
    }
148
149
    public function getResponseBytes()
150
    {
151
        return $this->responseBytes;
152
    }
153
154 3
    public function setResponseBytes($responseBytes)
155
    {
156 3
        if ('-' != $responseBytes) {
157 3
            $this->responseBytes = $responseBytes;
158
        }
159
    }
160
161
    public function getReferer()
162
    {
163
        return $this->referer;
164 3
    }
165
166 3
    protected function isInternalReferer($referer)
167
    {
168
        if ($this->domain === null) {
169 3
            return false;
170
        }
171 3
172 3
        return 0 === strpos($referer, 'http://'.$this->domain) || 0 === strpos($referer, 'https://'.$this->domain);
173
    }
174 3
175
    public function setHeaderReferer($referer)
176 3
    {
177
        if (! empty($referer) && '-' != $referer) {
178 3
            $this->setReferer($referer);
179
        }
180 3
    }
181 3
182
    public function setReferer($referer)
183 3
    {
184
        $this->referer = $this->isInternalReferer($referer) ? null : $referer;
185 3
    }
186
187
    public function getUseragent()
188 3
    {
189
        return $this->useragent;
190 3
    }
191 3
192
    public function setHeaderUserAgent($useragent)
193 3
    {
194
        $this->setUserAgent($useragent);
195 3
    }
196 3
197
    public function setUseragent($useragent)
198
    {
199
        $this->useragent = $useragent;
200
    }
201
202
    public function getHit()
203
    {
204
        return $this->hit ?? 1;
205
    }
206
207
    public function incrementHit()
208
    {
209
        $this->hit++;
210
    }
211
212
    public function ipAdressNumber($dotted)
213
    {
214
        $dotted = preg_split('/[.]+/', $dotted);
215
216
        return (float) (intval($dotted[0]) * 16777216)
217
            + (intval($dotted[1]) * 65536)
218
            + (intval($dotted[2]) * 256)
219
            + (intval($dotted[3]));
220
    }
221
}
222