1
|
|
|
<?php |
2
|
|
|
namespace phphound\output\html; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Format PHP source code highlighting its quality issues. |
6
|
|
|
*/ |
7
|
|
|
class FileHighlighter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Analyzed file. |
11
|
|
|
* @var string file path; |
12
|
|
|
*/ |
13
|
|
|
protected $filePath; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Lines of code with quality issues. |
17
|
|
|
* @var array lines with respective issues. |
18
|
|
|
*/ |
19
|
|
|
protected $linesWithIssues; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set dependencies. |
23
|
|
|
* @param string $filePath analyzed file path. |
24
|
|
|
* @param array $linesWithIssues lines with respective issues. |
25
|
|
|
*/ |
26
|
|
|
public function __construct($filePath, array $linesWithIssues) |
27
|
|
|
{ |
28
|
|
|
$this->filePath = $filePath; |
29
|
|
|
$this->linesWithIssues = $linesWithIssues; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Highlight PHP file showing issues and line numbers. |
34
|
|
|
* @return string HTML. |
35
|
|
|
*/ |
36
|
|
|
public function getHtml() |
37
|
|
|
{ |
38
|
|
|
$html = "<code><span style=\"color: #000000\">"; |
39
|
|
|
$paddingLength = $this->getLineNumberPaddingLength(); |
40
|
|
|
|
41
|
|
|
foreach ($this->getFormattedPHPFileLines() as $i => $line) { |
42
|
|
|
$lineNumber = $i + 1; |
43
|
|
|
$paddedLineNumber = str_pad($lineNumber, $paddingLength, '0', STR_PAD_LEFT); |
44
|
|
|
$hasIssues = isset($this->linesWithIssues[$lineNumber]); |
45
|
|
|
$lineCssClass = $hasIssues ? 'has-issues' : 'no-issues'; |
46
|
|
|
$lineId = 'line' . $lineNumber; |
47
|
|
|
|
48
|
|
|
$html .= '<div class="' . $lineCssClass . '" id="' . $lineId . '">'; |
49
|
|
|
$html .= '<span class="line-number">' . $paddedLineNumber . '</span>'; |
50
|
|
|
$html .= $this->getIssuesTooltip($lineNumber); |
51
|
|
|
$html .= $line . '</div>'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$html .= "</span></code>"; |
55
|
|
|
|
56
|
|
|
return $html; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Ammount of characters of the number of the last line of code. |
61
|
|
|
* @return integer padding length. |
62
|
|
|
*/ |
63
|
|
|
protected function getLineNumberPaddingLength() |
64
|
|
|
{ |
65
|
|
|
$lines = $this->getFormattedPHPFileLines(); |
66
|
|
|
$lineCount = count($lines); |
67
|
|
|
return strlen($lineCount); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Split all formatted PHP lines of code into an array. |
72
|
|
|
* @return string[] HTML splitted into an array. |
73
|
|
|
*/ |
74
|
|
|
protected function getFormattedPHPFileLines() |
75
|
|
|
{ |
76
|
|
|
$code = substr(highlight_file($this->filePath, true), 36, -15); |
77
|
|
|
return explode('<br />', $code); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create MaterialDesign tooltip showing issues for a given line of code. |
82
|
|
|
* @param integer $lineNumber line number. |
83
|
|
|
* @return string HTML. |
84
|
|
|
*/ |
85
|
|
|
protected function getIssuesTooltip($lineNumber) |
86
|
|
|
{ |
87
|
|
|
if (!isset($this->linesWithIssues[$lineNumber])) { |
88
|
|
|
return ''; |
89
|
|
|
} |
90
|
|
|
$html = '<div class="mdl-tooltip mdl-tooltip--large" for="line' . $lineNumber . '">'; |
91
|
|
|
$html .= '<ul>'; |
92
|
|
|
|
93
|
|
|
foreach ($this->linesWithIssues[$lineNumber] as $issue) { |
94
|
|
|
$html .= '<li>' . trim($issue['message']) . '</ul>'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$html .= '</ul></div>'; |
98
|
|
|
|
99
|
|
|
return $html; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|