Passed
Pull Request — master (#260)
by Fabien
01:58
created

CyclomaticComplexityAssessor   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 28
c 0
b 0
f 0
dl 0
loc 146
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A countTheTernaryOperators() 0 3 1
A getFileContents() 0 3 1
A countTheLogicalAnds() 0 3 1
A countTheLogicalOrs() 0 3 1
A countTheForLoops() 0 3 1
A countTheIfStatements() 0 3 1
A countTheCaseStatements() 0 3 1
A countTheElseIfStatements() 0 3 1
A countTheWhileLoops() 0 3 1
A hasAtLeastOneMethod() 0 5 2
A howManyPatternMatches() 0 3 1
A assess() 0 19 2
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Assessor;
4
5
use function preg_match;
6
use function preg_match_all;
7
use function file_get_contents;
8
9
class CyclomaticComplexityAssessor
10
{
11
    /**
12
     * The total cyclomatic complexity score.
13
     * @var integer.
0 ignored issues
show
Documentation Bug introduced by
The doc comment integer. at position 0 could not be parsed: Unknown type name 'integer.' at position 0 in integer..
Loading history...
14
     */
15
    protected $score;
16
17
    /**
18
     * Asses the files cyclomatic complexity.
19
     * @param  string $filePath Path and file name.
20
     * @return integer
21
     */
22
    public function assess(string $filePath): int
23
    {
24
        $this->score = 0;
25
26
        $contents = $this->getFileContents($filePath);
27
        $this->hasAtLeastOneMethod($contents);
28
        $this->countTheIfStatements($contents);
29
        $this->countTheElseIfStatements($contents);
30
        $this->countTheWhileLoops($contents);
31
        $this->countTheForLoops($contents);
32
        $this->countTheCaseStatements($contents);
33
        $this->countTheTernaryOperators($contents);
34
        $this->countTheLogicalAnds($contents);
35
        $this->countTheLogicalOrs($contents);
36
37
        if ($this->score === 0) {
38
            $this->score = 1;
39
        }
40
        return $this->score;
41
    }
42
43
    /**
44
     * Does the class have at least one method?
45
     * @param  string $contents File contents.
46
     * @return void
47
     */
48
    protected function hasAtLeastOneMethod(string $contents): void
49
    {
50
        preg_match("/[ ]function[ ]/", $contents, $matches);
51
        if (isset($matches[0])) {
52
            $this->score++;
53
        }
54
    }
55
56
    /**
57
     * Count how many if statements there are.
58
     * @param  string $contents File contents.
59
     * @return void
60
     */
61
    protected function countTheIfStatements(string $contents): void
62
    {
63
        $this->score += $this->howManyPatternMatches("/[ ]if[ ]{0,}\(/", $contents);
64
    }
65
66
    /**
67
     * Count how many else if statements there are.
68
     * @param  string $contents File contents.
69
     * @return void
70
     */
71
    protected function countTheElseIfStatements(string $contents): void
72
    {
73
        $this->score += $this->howManyPatternMatches("/else[ ]{0,}if[ ]{0,}\(/", $contents);
74
    }
75
76
    /**
77
     * Count how many while loops there are.
78
     * @param  string $contents File contents.
79
     * @return void
80
     */
81
    protected function countTheWhileLoops(string $contents): void
82
    {
83
        $this->score += $this->howManyPatternMatches("/while[ ]{0,}\(/", $contents);
84
    }
85
86
    /**
87
     * Count how many for loops there are.
88
     * @param  string $contents File contents.
89
     * @return void
90
     */
91
    protected function countTheForLoops(string $contents): void
92
    {
93
        $this->score += $this->howManyPatternMatches("/[ ]for(each){0,1}[ ]{0,}\(/", $contents);
94
    }
95
96
    /**
97
     * Count how many case statements there are.
98
     * @param  string $contents File contents.
99
     * @return void
100
     */
101
    protected function countTheCaseStatements(string $contents): void
102
    {
103
        $this->score += $this->howManyPatternMatches("/[ ]case[ ]{1}(.*)\:/", $contents);
104
    }
105
106
    /**
107
     * Count how many ternary operators there are.
108
     * @param  string $contents File contents.
109
     * @return void
110
     */
111
    protected function countTheTernaryOperators(string $contents): void
112
    {
113
        $this->score += $this->howManyPatternMatches("/[ ]\?.*:.*;/", $contents);
114
    }
115
116
    /**
117
     * Count how many '&&' there are.
118
     * @param  string $contents File contents.
119
     * @return void
120
     */
121
    protected function countTheLogicalAnds(string $contents): void
122
    {
123
        $this->score += $this->howManyPatternMatches("/[ ]&&[ ]/", $contents);
124
    }
125
126
    /**
127
     * Count how many '||' there are.
128
     * @param  string $contents File contents.
129
     * @return void
130
     */
131
    protected function countTheLogicalOrs(string $contents): void
132
    {
133
        $this->score += $this->howManyPatternMatches("/[ ]\|\|[ ]/", $contents);
134
    }
135
136
    /**
137
     * For the given $pattern on $string, how many matches are returned?
138
     * @param  string $pattern Regex pattern.
139
     * @param  string $string  Any string.
140
     * @return integer
141
     */
142
    protected function howManyPatternMatches(string $pattern, string $string): int
143
    {
144
        return preg_match_all($pattern, $string);
145
    }
146
147
    /**
148
     * Return the contents of the provided file at $filePath.
149
     * @param  string $filePath Path and filename.
150
     * @return string
151
     */
152
    protected function getFileContents(string $filePath): string
153
    {
154
        return file_get_contents($filePath);
155
    }
156
}
157