YamlParser   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 44
ccs 12
cts 21
cp 0.5714
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetUnset() 0 3 1
A offsetExists() 0 3 1
A offsetGet() 0 3 2
A yamlToArray() 0 7 2
A parse() 0 5 3
A offsetSet() 0 6 2
1
<?php
2
3
namespace Amock\Parser;
4
5
use ArrayAccess;
6
use Symfony\Component\Yaml\Yaml;
7
8
class YamlParser implements Parser, ArrayAccess
9
{
10
    private $parsed = [];
11
12 10
    public function parse(string $rawYaml): void
13
    {
14 10
        foreach ($this->yamlToArray($rawYaml) as $className => $yamlArray) {
15 10
            foreach ($yamlArray as $id => $mockArray) {
16 10
                $this->parsed[$id] = [$className => $mockArray];
17
            }
18
        }
19 10
    }
20
21 10
    protected function yamlToArray(string $rawYaml): array
22
    {
23 10
        if (function_exists('yaml_parse')) {
24
            return yaml_parse($rawYaml);
25
        }
26
27 10
        return Yaml::parse($rawYaml);
28
    }
29
30
    public function offsetSet($offset, $value)
31
    {
32
        if (is_null($offset)) {
33
            $this->parsed[] = $value;
34
        } else {
35
            $this->parsed[$offset] = $value;
36
        }
37
    }
38
39 1
    public function offsetExists($offset)
40
    {
41 1
        return isset($this->parsed[$offset]);
42
    }
43
44
    public function offsetUnset($offset)
45
    {
46
        unset($this->parsed[$offset]);
47
    }
48
49 10
    public function offsetGet($offset)
50
    {
51 10
        return isset($this->parsed[$offset]) ? $this->parsed[$offset] : null;
52
    }
53
}
54