Conditions | 13 |
Paths | 96 |
Total Lines | 76 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
66 | public function clean(bool $debug = false): array |
||
67 | { |
||
68 | if (!is_dir($this->statdir)) { |
||
69 | throw new Exception('Statistics module: output dir does not exist [' . $this->statdir . ']'); |
||
70 | } |
||
71 | |||
72 | if (!file_exists($this->inputfile)) { |
||
73 | throw new Exception('Statistics module: input file does not exist [' . $this->inputfile . ']'); |
||
74 | } |
||
75 | |||
76 | $file = fopen($this->inputfile, 'r'); |
||
77 | |||
78 | $logparser = new LogParser( |
||
79 | $this->statconfig->getOptionalValue('datestart', 0), |
||
80 | $this->statconfig->getOptionalValue('datelength', 15), |
||
81 | $this->statconfig->getOptionalValue('offsetspan', 44), |
||
82 | ); |
||
83 | |||
84 | $sessioncounter = []; |
||
85 | |||
86 | $i = 0; |
||
87 | // Parse through log file, line by line |
||
88 | while (!feof($file)) { |
||
89 | $logline = fgets($file, 4096); |
||
90 | |||
91 | // Continue if STAT is not found on line |
||
92 | if (!preg_match('/STAT/', $logline)) { |
||
93 | continue; |
||
94 | } |
||
95 | $i++; |
||
96 | |||
97 | // Parse log, and extract epoch time and rest of content. |
||
98 | $epoch = $logparser->parseEpoch($logline); |
||
99 | $content = $logparser->parseContent($logline); |
||
100 | |||
101 | if (($i % 10000) == 0) { |
||
102 | echo "Read line " . $i . "\n"; |
||
103 | } |
||
104 | |||
105 | $trackid = $content[4]; |
||
106 | |||
107 | if (!isset($sessioncounter[$trackid])) { |
||
108 | $sessioncounter[$trackid] = 0; |
||
109 | } |
||
110 | $sessioncounter[$trackid]++; |
||
111 | |||
112 | if ($debug) { |
||
113 | echo "----------------------------------------\n"; |
||
114 | echo 'Log line: ' . $logline . "\n"; |
||
115 | echo 'Date parse [' . substr($logline, 0, $this->statconfig->getOptionalValue('datelength', 15)) . |
||
116 | '] to [' . date(DATE_RFC822, $epoch) . ']' . "\n"; |
||
117 | $ret = print_r($content, true); |
||
118 | echo htmlentities($ret); |
||
|
|||
119 | if ($i >= 13) { |
||
120 | exit; |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | $histogram = []; |
||
126 | foreach ($sessioncounter as $trackid => $sc) { |
||
127 | if (!isset($histogram[$sc])) { |
||
128 | $histogram[$sc] = 0; |
||
129 | } |
||
130 | $histogram[$sc]++; |
||
131 | } |
||
132 | ksort($histogram); |
||
133 | |||
134 | $todelete = []; |
||
135 | foreach ($sessioncounter as $trackid => $sc) { |
||
136 | if ($sc > 200) { |
||
137 | $todelete[] = $trackid; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return $todelete; |
||
142 | } |
||
205 |