|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Inspector\Analysis\Result; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Kabir Baidhya |
|
10
|
|
|
*/ |
|
11
|
|
|
class AnalyzedFile implements AnalyzedFileInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
const EXTRA_VISIBLE_LINES = 3; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $filename; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $issues; |
|
25
|
|
|
|
|
26
|
|
|
protected $code; |
|
27
|
|
|
|
|
28
|
|
|
protected $linesOfCode; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($filename, array $issues) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->filename = $filename; |
|
33
|
|
|
$this->issues = $issues; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns the analyzed file name |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getFilename() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->filename; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Gets path relative to a base path |
|
48
|
|
|
* |
|
49
|
|
|
* @param $basePath |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getRelativeFilename($basePath) |
|
53
|
|
|
{ |
|
54
|
|
|
$basePath = realpath($basePath); |
|
55
|
|
|
|
|
56
|
|
|
if ($basePath === $this->filename) { |
|
57
|
|
|
return basename($this->filename); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return ltrim(str_replace($basePath, '', $this->filename), '\\/'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Calculates the Quality index of an analyzed file. |
|
65
|
|
|
* |
|
66
|
|
|
* @return int |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getQuality() |
|
69
|
|
|
{ |
|
70
|
|
|
$issueCount = count($this->issues); |
|
71
|
|
|
|
|
72
|
|
|
if ($issueCount === 0) { |
|
73
|
|
|
return 4; |
|
74
|
|
|
} elseif ($issueCount <= 2) { |
|
75
|
|
|
return 3; |
|
76
|
|
|
} elseif ($issueCount <= 5) { |
|
77
|
|
|
return 2; |
|
78
|
|
|
} else { |
|
79
|
|
|
return 1; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the quality rating text for to be displayed in the feedback. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getQualityRating() |
|
89
|
|
|
{ |
|
90
|
|
|
$ratings = [ |
|
91
|
|
|
4 => 'A', |
|
92
|
|
|
3 => 'B', |
|
93
|
|
|
2 => 'C', |
|
94
|
|
|
1 => 'D' |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
return $ratings[$this->getQuality()]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return Issue[] |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getIssues() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->issues; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getIssueCount() |
|
112
|
|
|
{ |
|
113
|
|
|
return count($this->issues); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function isOkay() |
|
120
|
|
|
{ |
|
121
|
|
|
return ($this->getIssueCount() === 0); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getIssueCodeStartLine(Issue $issue, $includeExtra = true) |
|
125
|
|
|
{ |
|
126
|
|
|
$startLine = $issue->getStartLine(); |
|
127
|
|
|
|
|
128
|
|
|
if ($includeExtra) { |
|
129
|
|
|
$startLine = $startLine - self::EXTRA_VISIBLE_LINES; |
|
130
|
|
|
if ($startLine < 1) { |
|
131
|
|
|
$startLine = 1; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $startLine; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get code part contained by the $startLine & the $endLine numbers |
|
140
|
|
|
* Also includes some extra lines($extraLines) before $startLine and after $endLine |
|
141
|
|
|
* |
|
142
|
|
|
* @param $startLine |
|
143
|
|
|
* @param $endLine |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getCodePart($startLine, $endLine) |
|
147
|
|
|
{ |
|
148
|
|
|
$extraLines = self::EXTRA_VISIBLE_LINES; |
|
149
|
|
|
if ($endLine < $startLine) { |
|
150
|
|
|
throw new InvalidArgumentException('End line should be come after the start line'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (!$this->linesOfCode) { |
|
154
|
|
|
$this->linesOfCode = explode("\n", $this->getCode()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$offset = ($startLine - 1 - $extraLines); |
|
158
|
|
|
|
|
159
|
|
|
if ($offset < 0) { |
|
160
|
|
|
$offset = 0; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$noOfLines = ($endLine - $startLine) + 2 * $extraLines; |
|
164
|
|
|
$slicedLines = array_slice($this->linesOfCode, $offset, $noOfLines); |
|
165
|
|
|
|
|
166
|
|
|
return implode("\n", $slicedLines); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the code for an issue. |
|
171
|
|
|
* |
|
172
|
|
|
* @param Issue $issue |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getCodeForIssue(Issue $issue) |
|
176
|
|
|
{ |
|
177
|
|
|
$startLine = $issue->getStartLine(); |
|
178
|
|
|
$endLine = $issue->getEndLine(); |
|
179
|
|
|
|
|
180
|
|
|
return $this->getCodePart($startLine, $endLine); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getCode() |
|
187
|
|
|
{ |
|
188
|
|
|
if (!$this->code) { |
|
189
|
|
|
$this->code = file_get_contents($this->getFilename()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// Remove the php delimiter as it causes trouble for code rendering |
|
193
|
|
|
return str_ireplace('<?php', '', $this->code); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|