1<?php
2/*
3 * This file is part of PHPLOC.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace SebastianBergmann\PHPLOC;
11
12class Publisher
13{
14    private $counts;
15
16    public function __construct(array $counts)
17    {
18        $this->counts = $counts;
19    }
20
21    public function getDirectories()
22    {
23        return $this->getCount('directories') - 1;
24    }
25
26    public function getFiles()
27    {
28        return $this->getValue('files');
29    }
30
31    public function getLines()
32    {
33        return $this->getValue('lines');
34    }
35
36    public function getCommentLines()
37    {
38        return $this->getValue('comment lines');
39    }
40
41    public function getNonCommentLines()
42    {
43        return $this->getLines() - $this->getCommentLines();
44    }
45
46    public function getLogicalLines()
47    {
48        return $this->getValue('logical lines');
49    }
50
51    public function getClassLines()
52    {
53        return $this->getSum('class lines');
54    }
55
56    public function getAverageClassLength()
57    {
58        return $this->getAverage('class lines');
59    }
60
61    public function getMinimumClassLength()
62    {
63        return $this->getMinimum('class lines');
64    }
65
66    public function getMaximumClassLength()
67    {
68        return $this->getMaximum('class lines');
69    }
70
71    public function getAverageMethodLength()
72    {
73        return $this->getAverage('method lines');
74    }
75
76    public function getMinimumMethodLength()
77    {
78        return $this->getMinimum('method lines');
79    }
80
81    public function getMaximumMethodLength()
82    {
83        return $this->getMaximum('method lines');
84    }
85
86    public function getFunctionLines()
87    {
88        return $this->getValue('function lines');
89    }
90
91    public function getAverageFunctionLength()
92    {
93        return $this->divide($this->getFunctionLines(), $this->getFunctions());
94    }
95
96    public function getNotInClassesOrFunctions()
97    {
98        return $this->getLogicalLines() - $this->getClassLines() - $this->getFunctionLines();
99    }
100
101    public function getComplexity()
102    {
103        return $this->getValue('complexity');
104    }
105
106    public function getMethodComplexity()
107    {
108        return $this->getValue('total method complexity');
109    }
110
111    public function getAverageComplexityPerLogicalLine()
112    {
113        return $this->divide($this->getComplexity(), $this->getLogicalLines());
114    }
115
116    public function getAverageComplexityPerClass()
117    {
118        return $this->getAverage('class complexity');
119    }
120
121    public function getMinimumClassComplexity()
122    {
123        return $this->getMinimum('class complexity');
124    }
125
126    public function getMaximumClassComplexity()
127    {
128        return $this->getMaximum('class complexity');
129    }
130
131    public function getAverageComplexityPerMethod()
132    {
133        return $this->getAverage('method complexity');
134    }
135
136    public function getMinimumMethodComplexity()
137    {
138        return $this->getMinimum('method complexity');
139    }
140
141    public function getMaximumMethodComplexity()
142    {
143        return $this->getMaximum('method complexity');
144    }
145
146    public function getGlobalAccesses()
147    {
148        return $this->getGlobalConstantAccesses() + $this->getGlobalVariableAccesses() + $this->getSuperGlobalVariableAccesses();
149    }
150
151    public function getGlobalConstantAccesses()
152    {
153        return \count(\array_intersect($this->getValue('possible constant accesses', []), $this->getValue('constant', [])));
154    }
155
156    public function getGlobalVariableAccesses()
157    {
158        return $this->getValue('global variable accesses');
159    }
160
161    public function getSuperGlobalVariableAccesses()
162    {
163        return $this->getValue('super global variable accesses');
164    }
165
166    public function getAttributeAccesses()
167    {
168        return $this->getNonStaticAttributeAccesses() + $this->getStaticAttributeAccesses();
169    }
170
171    public function getNonStaticAttributeAccesses()
172    {
173        return $this->getValue('non-static attribute accesses');
174    }
175
176    public function getStaticAttributeAccesses()
177    {
178        return $this->getValue('static attribute accesses');
179    }
180
181    public function getMethodCalls()
182    {
183        return $this->getNonStaticMethodCalls() + $this->getStaticMethodCalls();
184    }
185
186    public function getNonStaticMethodCalls()
187    {
188        return $this->getValue('non-static method calls');
189    }
190
191    public function getStaticMethodCalls()
192    {
193        return $this->getValue('static method calls');
194    }
195
196    public function getNamespaces()
197    {
198        return $this->getCount('namespaces');
199    }
200
201    public function getInterfaces()
202    {
203        return $this->getValue('interfaces');
204    }
205
206    public function getTraits()
207    {
208        return $this->getValue('traits');
209    }
210
211    public function getClasses()
212    {
213        return $this->getAbstractClasses() + $this->getConcreteClasses();
214    }
215
216    public function getAbstractClasses()
217    {
218        return $this->getValue('abstract classes');
219    }
220
221    public function getConcreteClasses()
222    {
223        return $this->getValue('concrete classes');
224    }
225
226    public function getMethods()
227    {
228        return $this->getNonStaticMethods() + $this->getStaticMethods();
229    }
230
231    public function getNonStaticMethods()
232    {
233        return $this->getValue('non-static methods');
234    }
235
236    public function getStaticMethods()
237    {
238        return $this->getValue('static methods');
239    }
240
241    public function getPublicMethods()
242    {
243        return $this->getValue('public methods');
244    }
245
246    public function getNonPublicMethods()
247    {
248        return $this->getValue('non-public methods');
249    }
250
251    public function getFunctions()
252    {
253        return $this->getNamedFunctions() + $this->getAnonymousFunctions();
254    }
255
256    public function getNamedFunctions()
257    {
258        return $this->getValue('named functions');
259    }
260
261    public function getAnonymousFunctions()
262    {
263        return $this->getValue('anonymous functions');
264    }
265
266    public function getConstants()
267    {
268        return $this->getGlobalConstants() + $this->getClassConstants();
269    }
270
271    public function getGlobalConstants()
272    {
273        return $this->getValue('global constants');
274    }
275
276    public function getClassConstants()
277    {
278        return $this->getValue('class constants');
279    }
280
281    public function getTestClasses()
282    {
283        return $this->getValue('test classes');
284    }
285
286    public function getTestMethods()
287    {
288        return $this->getValue('test methods');
289    }
290
291    public function toArray()
292    {
293        return [
294            'files'                       => $this->getFiles(),
295            'loc'                         => $this->getLines(),
296            'lloc'                        => $this->getLogicalLines(),
297            'llocClasses'                 => $this->getClassLines(),
298            'llocFunctions'               => $this->getFunctionLines(),
299            'llocGlobal'                  => $this->getNotInClassesOrFunctions(),
300            'cloc'                        => $this->getCommentLines(),
301            'ccn'                         => $this->getComplexity(),
302            'ccnMethods'                  => $this->getMethodComplexity(),
303            'interfaces'                  => $this->getInterfaces(),
304            'traits'                      => $this->getTraits(),
305            'classes'                     => $this->getClasses(),
306            'abstractClasses'             => $this->getAbstractClasses(),
307            'concreteClasses'             => $this->getConcreteClasses(),
308            'functions'                   => $this->getFunctions(),
309            'namedFunctions'              => $this->getNamedFunctions(),
310            'anonymousFunctions'          => $this->getAnonymousFunctions(),
311            'methods'                     => $this->getMethods(),
312            'publicMethods'               => $this->getPublicMethods(),
313            'nonPublicMethods'            => $this->getNonPublicMethods(),
314            'nonStaticMethods'            => $this->getNonStaticMethods(),
315            'staticMethods'               => $this->getStaticMethods(),
316            'constants'                   => $this->getConstants(),
317            'classConstants'              => $this->getClassConstants(),
318            'globalConstants'             => $this->getGlobalConstants(),
319            'testClasses'                 => $this->getTestClasses(),
320            'testMethods'                 => $this->getTestMethods(),
321            'ccnByLloc'                   => $this->getAverageComplexityPerLogicalLine(),
322            'llocByNof'                   => $this->getAverageFunctionLength(),
323            'methodCalls'                 => $this->getMethodCalls(),
324            'staticMethodCalls'           => $this->getStaticMethodCalls(),
325            'instanceMethodCalls'         => $this->getNonStaticMethodCalls(),
326            'attributeAccesses'           => $this->getAttributeAccesses(),
327            'staticAttributeAccesses'     => $this->getStaticAttributeAccesses(),
328            'instanceAttributeAccesses'   => $this->getNonStaticAttributeAccesses(),
329            'globalAccesses'              => $this->getGlobalAccesses(),
330            'globalVariableAccesses'      => $this->getGlobalVariableAccesses(),
331            'superGlobalVariableAccesses' => $this->getSuperGlobalVariableAccesses(),
332            'globalConstantAccesses'      => $this->getGlobalConstantAccesses(),
333            'directories'                 => $this->getDirectories(),
334            'classCcnMin'                 => $this->getMinimumClassComplexity(),
335            'classCcnAvg'                 => $this->getAverageComplexityPerClass(),
336            'classCcnMax'                 => $this->getMaximumClassComplexity(),
337            'classLlocMin'                => $this->getMinimumClassLength(),
338            'classLlocAvg'                => $this->getAverageClassLength(),
339            'classLlocMax'                => $this->getMaximumClassLength(),
340            'methodCcnMin'                => $this->getMinimumMethodComplexity(),
341            'methodCcnAvg'                => $this->getAverageComplexityPerMethod(),
342            'methodCcnMax'                => $this->getMaximumMethodComplexity(),
343            'methodLlocMin'               => $this->getMinimumMethodLength(),
344            'methodLlocAvg'               => $this->getAverageMethodLength(),
345            'methodLlocMax'               => $this->getMaximumMethodLength(),
346            'namespaces'                  => $this->getNamespaces(),
347            'ncloc'                       => $this->getNonCommentLines(),
348        ];
349    }
350
351    private function getAverage($key)
352    {
353        return $this->divide($this->getSum($key), $this->getCount($key));
354    }
355
356    private function getCount($key)
357    {
358        return isset($this->counts[$key]) ? \count($this->counts[$key]) : 0;
359    }
360
361    private function getSum($key)
362    {
363        return isset($this->counts[$key]) ? \array_sum($this->counts[$key]) : 0;
364    }
365
366    private function getMaximum($key)
367    {
368        return isset($this->counts[$key]) ? \max($this->counts[$key]) : 0;
369    }
370
371    private function getMinimum($key)
372    {
373        return isset($this->counts[$key]) ? \min($this->counts[$key]) : 0;
374    }
375
376    private function getValue($key, $default = 0)
377    {
378        return isset($this->counts[$key]) ? $this->counts[$key] : $default;
379    }
380
381    private function divide($x, $y)
382    {
383        return $y != 0 ? $x / $y : 0;
384    }
385}
386