GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#57)
by TJ
02:43
created

FileSource   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A trimLine() 0 5 1
A __construct() 0 5 2
A readFile() 0 3 1
A startingLineNumber() 0 5 2
A fileLines() 0 12 2
A canReadFile() 0 3 2
A getSource() 0 11 2
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