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.

Parser::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ChangelogParser\Parser;
4
5
class Parser {
6
    /** @var array **/
7
    protected $releases;
8
    /** @var string **/
9
    protected $currentRelease;
10
    /** @var string **/
11
    protected $currentReleasePart;
12
    
13
    /**
14
     * @param string $filepath
15
     * @return array
16
     */
17 4
    public function parse($filepath) {
18 4
        foreach($this->parseFile($filepath) as $line) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
19 4
            $this->parseLine($line);
20 4
        }
21 4
        return $this->releases;
22
    }
23
    
24
    /**
25
     * @param string $filepath
26
     * @throws \RuntimeException
27
     */
28 4
    private function parseFile($filepath) {
29 4
        if(($file = fopen($filepath, 'r')) === false) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
30
            throw new \RuntimeException("The file $filepath does not exist");
31
        }
32
        
33 4
        while($line = fgets($file)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
34 4
            yield $line;
35 4
        }
36 4
        fclose($file);
37 4
    }
38
    
39
    /**
40
     * @param string $line
41
     */
42 4
    private function parseLine($line) {
43 4
        switch($line{0}) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
44 4
            case '#':
45 4
                $this->parseTitle($line);
46 4
                break;
47 4
            case '-':
48 4
                $this->parseItem($line);
49 4
                break;
50 4
        }
51 4
    }
52
    
53
    /**
54
     * @param string $line
55
     */
56 4
    private function parseTitle($line) {
57 4
        for($i = 0; $i < 3; ++$i) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOR keyword; 0 found
Loading history...
58 4
            if($line{$i} !== '#') {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
59 4
                break;
60
            }
61 4
        }
62
        switch($i) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
63 4
            case 2:
64 4
                $parts = explode('-', $line);
65 4
                if(count($parts) === 1) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
66 4
                    $this->currentRelease = $this->formatReleaseVersion($line);
67 4
                    break;
68
                }
69 4
                $this->currentRelease = $this->formatReleaseVersion($parts[0]);
70 4
                unset($parts[0]);
71 4
                $this->releases[$this->currentRelease]['date'] = trim(implode('-', $parts));
72
                
73 4
                break;
74 4
            case 3:
75 4
                $this->currentReleasePart = strtolower(trim(substr($line, 3)));
76 4
                break;
77
        }
78 4
    }
79
    
80
    /**
81
     * @param string $releaseVersion
82
     * @return string
83
     */
84 4
    private function formatReleaseVersion($releaseVersion) {
85 4
        return strtolower(str_replace(['[', ']'], '', trim(substr($releaseVersion, 2))));
86
    }
87
    
88
    /**
89
     * @param string $line
90
     */
91 4
    private function parseItem($line) {
92 4
        $this->releases[$this->currentRelease]['items'][$this->currentReleasePart][] = trim(substr($line,1));
93
    }
94
}