KeyParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canParse() 0 4 2
A parse() 0 16 2
A canParseKey() 0 4 2
A checkForBadlyFormedKey() 0 6 2
1
<?php
2
3
namespace PhpHocon\Token\Parser;
4
5
use PhpHocon\Token\Key;
6
use PhpHocon\Token\Tokens;
7
use Symfony\Component\Yaml\Exception\ParseException;
8
9
class KeyParser implements Parser
10
{
11
    /**
12
     * @var array
13
     */
14
    private $breakCharacters = [
15
        Tokens::EQUALS,
16
        Tokens::COLON,
17
        Tokens::SPACE
18
    ];
19
20
    /**
21
     * @param ParserState $state
22
     * @return boolean
23
     */
24
    public function canParse(ParserState $state)
25
    {
26
        return $state->isParsingKey() && !in_array($state->getHeadCharacter(), $this->breakCharacters);
27
    }
28
29
    /**
30
     * @param ParserState $state
31
     * @return ParseResult
32
     */
33
    public function parse(ParserState $state)
34
    {
35
        $characters = $state->getCharacters();
36
        $keyName = '';
37
38
        while ($this->canParseKey($characters)) {
39
            $keyName .= array_shift($characters);
40
        }
41
42
        $this->checkForBadlyFormedKey($keyName);
43
44
        return new ParseResult(
45
            new ParserState($characters, $state->getBraceCount(), false, true),
46
            new Key($keyName)
47
        );
48
    }
49
50
    /**
51
     * @param string[] $characters
52
     * @return bool
53
     */
54
    private function canParseKey(array $characters)
55
    {
56
        return !empty($characters) && !in_array($characters[0], $this->breakCharacters);
57
    }
58
59
    private function checkForBadlyFormedKey($keyName)
60
    {
61
        if ($keyName[strlen($keyName) - 1] === '.') {
62
            throw new ParseException('Malformed key: ' . $keyName);
63
        }
64
    }
65
}
66