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