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::detectFrameType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BultonFr\NMEA;
4
5
use \Exception;
6
7
/**
8
 * Parse a nmea line to determine the frame type of the line and create an
9
 * instance of the frame type parser.
10
 * 
11
 * @package BultonFr\NMEA
12
 * @author Vermeulen Maxime <[email protected]>
13
 */
14
class Parser
15
{
16
    /**
17
     * @const ERR_FRAME_DETECT_FAILED Error code if the frame type can not be
18
     * detected.
19
     */
20
    const ERR_FRAME_DETECT_FAILED = 10101;
21
    
22
    /**
23
     * @const ERR_NO_PARSER_FOR_FRAME_TYPE Error code if there is not parser
24
     * for the readed line
25
     */
26
    const ERR_NO_PARSER_FOR_FRAME_TYPE = 10102;
27
    
28
    /**
29
     * Read a new line, find the frame type, create a new instance of the frame
30
     * type parser and return the parser instanciated after reading and
31
     * parsing the line.
32
     * 
33
     * @param string $line The line to parse
34
     * 
35
     * @return \BultonFr\NMEA\Frame The frame type parser instance
36
     */
37
    public function readLine($line)
38
    {
39
        $frameType = $this->detectFrameType($line);
40
        $frame     = $this->obtainFrameParser($line, $frameType);
41
        
42
        $frame->readFrame();
43
        return $frame;
44
    }
45
    
46
    /**
47
     * Detect the frame type from the line
48
     * 
49
     * @param string $line The line to read
50
     * 
51
     * @return string
52
     * 
53
     * @throws Exception If the line format not corresponding.
54
     */
55
    protected function detectFrameType($line)
56
    {
57 1
        $matches = [];
58 1
        if (!preg_match('/^\$([A-Z]{2})([A-Z]{3}),/', $line, $matches)) {
59 1
            throw new Exception(
60 1
                'The detection of the frame type has failed.',
61 1
                static::ERR_FRAME_DETECT_FAILED
62
            );
63
        }
64
        
65 1
        return $matches[2]; //Always exist, else preg_match failed.
66
    }
67
    
68
    /**
69
     * Instanciate the frame type class parser.
70
     * 
71
     * @param string $line The line to read
72
     * @param string $frameType The frame type
73
     * 
74
     * @return \BultonFr\NMEA\Frame
75
     * 
76
     * @throws Exception If no parse exist for this frame type
77
     */
78
    protected function obtainFrameParser($line, $frameType)
79
    {
80 1
        $frameClassName = '\BultonFr\NMEA\Frames\\'.$frameType;
81
        
82 1
        if (!class_exists($frameClassName)) {
83 1
            throw new Exception(
84 1
                'There is no class defined for frame type '.$frameType.'.',
85 1
                static::ERR_NO_PARSER_FOR_FRAME_TYPE
86
            );
87
        }
88
        
89 1
        return new $frameClassName($line);
90
    }
91
}
92