Passed
Push — master ( 9f85fb...02c0e5 )
by Stefan
01:31 queued 13s
created

ClassifiedClass::getNumberOfPublicMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Wnx\LaravelStats\ValueObjects;
4
5
use Wnx\LaravelStats\ReflectionClass;
6
use SebastianBergmann\PHPLOC\Analyser;
7
use Wnx\LaravelStats\Contracts\Classifier;
8
9
class ClassifiedClass
10
{
11
    /**
12
     * @var \Wnx\LaravelStats\ReflectionClass
13
     */
14
    public $reflectionClass;
15
16
    /**
17
     * Classifier Instance related to the Reflection Class.
18
     *
19
     * @var \Wnx\LaravelStats\Contracts\Classifier
20
     */
21
    public $classifier;
22
23
    /**
24
     * @var int
25
     */
26
    private $numberOfMethods;
27
28
    /**
29
     * @var int
30
     */
31
    private $numberOfPublicMethods;
32
33
    /**
34
     * @var int
35
     */
36
    private $numberOfNonPublicMethods;
37
38
    /**
39
     * @var int
40
     */
41
    private $linesOfCode;
42
43
    /**
44
     * @var int
45
     */
46
    private $logicalLinesOfCode;
47
48
    /**
49
     * @var float
50
     */
51
    private $logicalLinesOfCodePerMethod;
52
53
    public function __construct(ReflectionClass $reflectionClass, Classifier $classifier)
54
    {
55
        $this->reflectionClass = $reflectionClass;
56
        $this->classifier = $classifier;
57
    }
58
59
    /**
60
     * Return the total number of Methods declared in all declared classes.
61
     *
62
     * @return int
63
     */
64
    public function getNumberOfMethods(): int
65
    {
66
        if ($this->numberOfMethods === null) {
67
            $this->numberOfMethods = $this->reflectionClass->getDefinedMethods()->count();
68
        }
69
70
        return $this->numberOfMethods;
71
    }
72
73
    public function getNumberOfPublicMethods(): int
74
    {
75
        if ($this->numberOfPublicMethods === null) {
76
            $this->numberOfPublicMethods = $this->reflectionClass->getDefinedMethods()
77
                ->filter(function (\ReflectionMethod $method) {
78
                    return $method->isPublic();
79
                })->count();
80
        }
81
82
        return $this->numberOfPublicMethods;
83
    }
84
85
    public function getNumberOfNonPublicMethods(): int
86
    {
87
        if ($this->numberOfNonPublicMethods === null) {
88
            $this->numberOfNonPublicMethods = $this->reflectionClass->getDefinedMethods()
89
                ->filter(function (\ReflectionMethod $method) {
90
                    return ! $method->isPublic();
91
                })->count();
92
        }
93
94
        return $this->numberOfNonPublicMethods;
95
    }
96
97
    /**
98
     * Return the total number of lines.
99
     *
100
     * @return int
101
     */
102
    public function getLines(): int
103
    {
104
        if ($this->linesOfCode === null) {
105
            $this->linesOfCode = app(Analyser::class)
106
                ->countFiles([$this->reflectionClass->getFileName()], false)['loc'];
107
        }
108
109
        return $this->linesOfCode;
110
    }
111
112
    /**
113
     * Return the total number of lines of code.
114
     *
115
     * @return float
116
     */
117
    public function getLogicalLinesOfCode(): float
118
    {
119
        if ($this->logicalLinesOfCode === null) {
120
            $this->logicalLinesOfCode = app(Analyser::class)
121
                ->countFiles([$this->reflectionClass->getFileName()], false)['lloc'];
122
        }
123
124
        return $this->logicalLinesOfCode;
125
    }
126
127
    /**
128
     * Return the average number of lines of code per method.
129
     *
130
     * @return float
131
     */
132
    public function getLogicalLinesOfCodePerMethod(): float
133
    {
134
        if ($this->logicalLinesOfCodePerMethod === null) {
135
            if ($this->getNumberOfMethods() === 0) {
136
                $this->logicalLinesOfCodePerMethod = $this->logicalLinesOfCodePerMethod = 0;
137
            } else {
138
                $this->logicalLinesOfCodePerMethod = round($this->getLogicalLinesOfCode() / $this->getNumberOfMethods(), 2);
139
            }
140
        }
141
142
        return $this->logicalLinesOfCodePerMethod;
143
    }
144
}
145