Passed
Push — test ( 66cd7f...018ff3 )
by Tom
04:25
created

LibYaml   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 9
c 2
b 0
f 1
dl 0
loc 35
ccs 11
cts 11
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 3 3
A parseFile() 0 3 1
A parseBuffer() 0 10 3
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Yaml;
6
7
use Ktomk\Pipelines\ErrorCatcher;
8
9
class LibYaml implements ParserInterface
10
{
11
    /**
12
     * @return bool
13
     */
14 1
    public static function isAvailable()
15
    {
16 1
        return extension_loaded('yaml') && function_exists('yaml_parse_file') && function_exists('yaml_parse');
17
    }
18
19
    /**
20
     * @param string $path
21
     *
22
     * @return null|array
23
     */
24 3
    public function parseFile($path)
25
    {
26 3
        return $this->parseBuffer(@file_get_contents($path));
27
    }
28
29
    /**
30
     * @param string $buffer
31
     *
32
     * @return null|array
33
     */
34 4
    public function parseBuffer($buffer)
35
    {
36 4
        $error = ErrorCatcher::create();
37 4
        $result = yaml_parse($buffer, 0);
38 4
        $result = $error->end() ? null : $result;
39
40 4
        return !is_array($result)
41 2
            ? null
42
            # ext-yaml parser does aliases, remove any potential ones
43 4
            : json_decode(json_encode($result), true);
44
    }
45
}
46