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

LibYaml::isAvailable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 1
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 3
rs 10
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