Passed
Push — master ( 2bc6a1...487703 )
by Tom
02:53
created

LibYaml::parseFile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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 1
    public static function isAvailable()
12
    {
13 1
        return extension_loaded('yaml') && function_exists('yaml_parse_file') && function_exists('yaml_parse');
14
    }
15
16
    /**
17
     * @param string $path
18
     * @return null|array
19
     */
20 3
    public function parseFile($path)
21
    {
22 3
        $error = ErrorCatcher::create();
23
24 3
        $result = yaml_parse_file($path, 0);
25
26 3
        $result = $error->end() ? null : $result;
27
28 3
        return !is_array($result)
29 2
            ? null
30
            # libyaml parser does aliases, remove any potential ones
31 3
            : json_decode(json_encode($result), true);
32
    }
33
34
    /**
35
     * @param string $buffer
36
     * @return null|array
37
     */
38 1
    public function parseBuffer($buffer)
39
    {
40 1
        $result = yaml_parse($buffer, 0);
41
42 1
        return !is_array($result) ? null : $result;
43
    }
44
}
45