|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Honeybadger; |
|
4
|
|
|
|
|
5
|
|
|
use SplFileObject; |
|
6
|
|
|
|
|
7
|
|
|
class FileSource |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $filename; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $lineNumber; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $radius; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $filename |
|
26
|
|
|
* @param int $lineNumber |
|
27
|
|
|
* @param int $radius |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(string $filename, int $lineNumber, int $radius = 4) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->filename = $filename; |
|
32
|
|
|
$this->lineNumber = $lineNumber < 0 ? 0 : $lineNumber; |
|
33
|
|
|
$this->radius = $radius; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getSource() : array |
|
40
|
|
|
{ |
|
41
|
|
|
if (! $this->canReadFile()) { |
|
42
|
|
|
return []; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return array_slice( |
|
46
|
|
|
$this->fileLines($this->readFile()), |
|
47
|
|
|
$this->startingLineNumber(), |
|
48
|
|
|
($this->radius * 2) + 1, |
|
49
|
|
|
$preserveKeys = true |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $line |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
private function trimLine(string $line) : string |
|
58
|
|
|
{ |
|
59
|
|
|
$trimmed = trim($line, "\n\r\0\x0B"); |
|
60
|
|
|
|
|
61
|
|
|
return preg_replace(['/\s*$/D', '/\t/'], ['', ' '], $trimmed); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
private function canReadFile() : bool |
|
68
|
|
|
{ |
|
69
|
|
|
return is_file($this->filename) && is_readable($this->filename); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return \SplFileObject |
|
74
|
|
|
*/ |
|
75
|
|
|
private function readFile() : SplFileObject |
|
76
|
|
|
{ |
|
77
|
|
|
return new SplFileObject($this->filename, 'r'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param \SplFileObject $file |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
private function fileLines(SplFileObject $file) : array |
|
85
|
|
|
{ |
|
86
|
|
|
$lines = []; |
|
87
|
|
|
while (! $file->eof()) { |
|
88
|
|
|
$lines[] = $this->trimLine($file->fgets()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Set the array to base 1 so it actually reflects the line number of code |
|
92
|
|
|
array_unshift($lines, null); |
|
93
|
|
|
unset($lines[0]); |
|
94
|
|
|
|
|
95
|
|
|
return $lines; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
|
|
private function startingLineNumber() : int |
|
102
|
|
|
{ |
|
103
|
|
|
$start = $this->lineNumber - ($this->radius + 1); |
|
104
|
|
|
|
|
105
|
|
|
return $start >= 0 ? $start : 0; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|