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

Spyc::parseFile()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 13
nc 7
nop 1
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 9
rs 4.909
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
use Ktomk\Pipelines\Lib;
9
use Spyc as MustangostangSpyc;
10
11
class Spyc implements ParserInterface
12
{
13
    /**
14
     * @return bool
15
     */
16 1
    public static function isAvailable()
17
    {
18 1
        return true;
19
    }
20
21
    /**
22
     * @param string $path
23
     * @return null|array
24
     */
25 3
    public function parseFile($path)
26
    {
27 3
        $fsIsStreamUri = Lib::fsIsStreamUri($path);
28 3
        if (!$fsIsStreamUri && (!is_file($path) || !is_readable($path))) {
29 1
            return null;
30
        }
31
32 3
        $error = ErrorCatcher::create();
33
34 3
        if ($fsIsStreamUri) {
35 1
            $path = file_get_contents($path);
36
        }
37
38 3
        $array = MustangostangSpyc::YAMLLoad($path);
39
40 3
        if ($error->end() || !is_array($array) || array() === $array) {
41 1
            return null;
42
        }
43
44 3
        $keys = array_keys($array);
45 3
        if ($keys === range(0, count($array) - 1, 1)) {
46 2
            return null;
47
        }
48
49 1
        return $array;
50
    }
51
52
    /**
53
     * @param string $buffer
54
     * @return null|array
55
     */
56 1
    public function parseBuffer($buffer)
57
    {
58 1
        $error = ErrorCatcher::create();
59
60 1
        $array = MustangostangSpyc::YAMLLoadString($buffer);
61
62 1
        return $error->end() ? null : $array;
63
    }
64
}
65