Passed
Push — master ( ce13c2...e57cc1 )
by Peter
05:04 queued 02:18
created

YamlService::isLineOpeningAnArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace YamlStandards\Model\Component;
4
5
use Symfony\Component\Yaml\Inline;
6
use Symfony\Component\Yaml\Yaml;
7
8
class YamlService
9
{
10
    /**
11
     * @param string $pathToYamlFile
12
     * @throws \Symfony\Component\Yaml\Exception\ParseException
13
     * @return string[]
14
     */
15
    public static function getYamlData($pathToYamlFile)
16
    {
17
        return (array)Yaml::parse(file_get_contents($pathToYamlFile), Yaml::PARSE_CUSTOM_TAGS);
18
    }
19
20
    /**
21
     * @param string $key
22
     * @return bool
23
     */
24
    public static function hasArrayKeyUnderscoreAsFirstCharacter($key)
25
    {
26
        return strpos($key, '_') === 0;
27
    }
28
29
    /**
30
     * @param string $key
31
     * @return bool
32
     */
33
    public static function hasNotArrayKeyUnderscoreAsFirstCharacter($key)
34
    {
35
        return strpos($key, '_') !== 0;
36
    }
37
38
    /**
39
     * @param string $yamlLine
40
     * @return bool
41
     */
42
    public static function isLineNotBlank($yamlLine)
43
    {
44
        return trim($yamlLine) !== '';
45
    }
46
47
    /**
48
     * @param string $yamlLine
49
     * @return bool
50
     */
51
    public static function isLineComment($yamlLine)
52
    {
53
        return preg_match('/^\s*#/', $yamlLine) === 1;
54
    }
55
56
    /**
57
     * @param string $value
58
     * @return bool
59
     */
60
    public static function isValueReuseVariable($value)
61
    {
62
        return strpos($value, '&') === 0;
63
    }
64
65
    /**
66
     * @param string $value
67
     * @return bool
68
     */
69
    public static function hasLineDashOnStartOfLine($value)
70
    {
71
        return strpos($value, '-') === 0;
72
    }
73
74
    /**
75
     * @param string $trimmedLine
76
     * @return bool
77
     */
78
    public static function hasLineThreeDashesOnStartOfLine($trimmedLine)
79
    {
80
        return strpos($trimmedLine, '---') === 0;
81
    }
82
83
    /**
84
     * @param string $value
85
     * @return bool
86
     */
87
    public static function isCurlyBracketInStartOfString($value)
88
    {
89
        return strpos($value, '{') === 0;
90
    }
91
92
    /**
93
     * line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }"
94
     *
95
     * @param string $trimmedLine
96
     * @return bool
97
     */
98
    public static function isLineStartOfArrayWithKeyAndValue($trimmedLine)
99
    {
100
        return $trimmedLine !== '-' && self::hasLineDashOnStartOfLine($trimmedLine);
101
    }
102
103
    /**
104
     * value starting with key, e.g. 'foo: bar' or '"foo bar": baz'
105
     *
106
     * @param string $value
107
     * @return bool
108
     */
109
    public static function isKeyInStartOfString($value)
110
    {
111
        return (bool)preg_match('~^(' . Inline::REGEX_QUOTED_STRING . '|[^ \'"{\[].*?) *:~u', $value);
112
    }
113
114
    /**
115
     * line possibly opening an array, e.g. 'foo:' or '- foo:'
116
     *
117
     * @param string $trimmedLine
118
     * @return bool
119
     */
120
    public static function isLineOpeningAnArray($trimmedLine)
121
    {
122
        return (bool)preg_match('~^(- +)*(' . Inline::REGEX_QUOTED_STRING . '|[^ \'"{\[].*?) *:$~u', $trimmedLine);
123
    }
124
125
    /**
126
     * @param string $line
127
     * @return int
128
     */
129
    public static function rowIndentsOf($line)
130
    {
131
        return strlen($line) - strlen(ltrim($line));
132
    }
133
134
    /**
135
     * @param string $line
136
     * @return int
137
     */
138
    public static function keyIndentsOf($line)
139
    {
140
        return strlen($line) - strlen(ltrim($line, '- '));
141
    }
142
}
143