Completed
Pull Request — master (#510)
by Richard
06:52
created

Yaml::loadWrapped()   B

Complexity

Conditions 6
Paths 52

Size

Total Lines 26
Code Lines 19

Duplication

Lines 12
Ratio 46.15 %

Code Coverage

Tests 15
CRAP Score 6.1666

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
c 1
b 0
f 0
nc 52
nop 1
dl 12
loc 26
ccs 15
cts 18
cp 0.8333
crap 6.1666
rs 8.439
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf;
13
14
use Symfony\Component\Yaml\Yaml as VendorYaml;
15
16
/**
17
 * Yaml dump and parse methods
18
 *
19
 * YAML is a serialization format most useful when human readability
20
 * is a consideration. It can be useful for configuration files, as
21
 * well as import and export functions.
22
 *
23
 * This file is a front end for a separate YAML package present in the
24
 * vendor directory. The intent is to provide a consistent interface
25
 * no mater what underlying library is actually used.
26
 *
27
 * At present, this class expects the symfony/yaml package.
28
 *
29
 * @category  Xmf\Yaml
30
 * @package   Xmf
31
 * @author    Richard Griffith <[email protected]>
32
 * @copyright 2013-2016 XOOPS Project (http://xoops.org)
33
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
34
 * @link      http://xoops.org
35
 * @see       http://www.yaml.org/
36
 */
37
class Yaml
38
{
39
40
    /**
41
     * Dump an PHP array as a YAML string
42
     *
43
     * @param mixed   $var    Variable which will be dumped
44
     * @param integer $inline Nesting level where you switch to inline YAML
45
     * @param integer $indent Number of spaces to indent for nested nodes
46
     *
47
     * @return string|bool YAML string or false on error
48
     */
49 1
    public static function dump($var, $inline = 4, $indent = 4)
50
    {
51
        try {
52 1
            $ret = VendorYaml::dump($var, $inline, $indent);
53
        } catch (\Exception $e) {
54
            static::logError($e);
55
            $ret = false;
56
        }
57 1
        return $ret;
58
    }
59
60
    /**
61
     * Load a YAML string into a PHP array
62
     *
63
     * @param string $yamlString YAML dump string
64
     *
65
     * @return array|boolean PHP array or false on error
66
     */
67 1
    public static function load($yamlString)
68
    {
69
        try {
70 1
            $ret = VendorYaml::parse($yamlString);
71
        } catch (\Exception $e) {
72
            static::logError($e);
73
            $ret = false;
74
        }
75 1
        return $ret;
76
    }
77
78
    /**
79
     * Read a file containing YAML into a PHP array
80
     *
81
     * @param string $yamlFile filename of YAML file
82
     *
83
     * @return array|boolean PHP array or false on error
84
     */
85 3 View Code Duplication
    public static function read($yamlFile)
86
    {
87
        try {
88 3
            $yamlString = file_get_contents($yamlFile);
89 3
            $ret = VendorYaml::parse($yamlString);
90
        } catch (\Exception $e) {
91
            static::logError($e);
92
            $ret = false;
93
        }
94 3
        return $ret;
95
    }
96
97
    /**
98
     * Save a PHP array as a YAML file
99
     *
100
     * @param array   $var      variable which will be dumped
101
     * @param string  $yamlFile filename of YAML file
102
     * @param integer $inline   Nesting level where you switch to inline YAML
103
     * @param integer $indent   Number of spaces to indent for nested nodes
104
     *
105
     * @return integer|boolean number of bytes written, or false on error
106
     */
107 2 View Code Duplication
    public static function save($var, $yamlFile, $inline = 4, $indent = 4)
108
    {
109
        try {
110 2
            $yamlString = VendorYaml::dump($var, $inline, $indent);
111 2
            $ret = file_put_contents($yamlFile, $yamlString);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as file_put_contents($yamlFile, $yamlString) (which targets file_put_contents()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
112
        } catch (\Exception $e) {
113
            static::logError($e);
114
            $ret = false;
115
        }
116 2
        return $ret;
117
    }
118
119
    /**
120
     * Dump an PHP array as a YAML string with a php wrapper
121
     *
122
     * The wrap is a php header that surrounds the yaml with section markers,
123
     * '---' and '...' along with php comment markers. The php wrapper keeps the
124
     * yaml file contents from being revealed by serving the file directly from
125
     * a poorly configured server.
126
     *
127
     * @param mixed   $var    Variable which will be dumped
128
     * @param integer $inline Nesting level where you switch to inline YAML
129
     * @param integer $indent Number of spaces to indent for nested nodes
130
     *
131
     * @return string|boolean YAML string or false on error
132
     */
133 2
    public static function dumpWrapped($var, $inline = 4, $indent = 4)
134
    {
135
        try {
136 2
            $yamlString = VendorYaml::dump($var, $inline, $indent);
137 2
            $ret = empty($yamlString) ? false : "<?php\n/*\n---\n" . $yamlString . "\n...\n*/\n";
138
        } catch (\Exception $e) {
139
            static::logError($e);
140
            $ret = false;
141
        }
142 2
        return $ret;
143
    }
144
145
    /**
146
     * Load a YAML string with a php wrapper into a PHP array
147
     *
148
     * The wrap is a php header that surrounds the yaml with section markers,
149
     * '---' and '...' along with php comment markers. The php wrapper keeps the
150
     * yaml file contents from being revealed by serving the file directly from
151
     * a poorly configured server.
152
     *
153
     * @param string $yamlString YAML dump string
154
     *
155
     * @return array|boolean PHP array or false on error
156
     */
157 3
    public static function loadWrapped($yamlString)
158
    {
159
        try {
160 3
            $lines = preg_split('/\R/', $yamlString);
161 3
            $count = count($lines);
162 3 View Code Duplication
            for ($index = $count; --$index > 0;) {
163 3
                if ('...' === $lines[$index]) {
164 2
                    array_splice($lines, $index);
165 2
                    break;
166
                }
167
            }
168 3
            $count = count($lines);
169 3 View Code Duplication
            for ($index = 0; ++$index < $count;) {
170 3
                if ('---' === $lines[$index]) {
171 2
                    array_splice($lines, 0, $index);
172 2
                    break;
173
                }
174
            }
175 3
            $unwrapped = implode("\n", $lines);
176 3
            $ret = VendorYaml::parse($unwrapped);
177
        } catch (\Exception $e) {
178
            static::logError($e);
179
            $ret = false;
180
        }
181 3
        return $ret;
182
    }
183
184
    /**
185
     * Read a file containing YAML with a php wrapper into a PHP array
186
     *
187
     * The wrap is a php header that surrounds the yaml with section markers,
188
     * '---' and '...' along with php comment markers. The php wrapper keeps the
189
     * yaml file contents from being revealed by serving the file directly from
190
     * a poorly configured server.
191
     *
192
     * @param string $yamlFile filename of YAML file
193
     *
194
     * @return array|boolean PHP array or false on error
195
     */
196 1 View Code Duplication
    public static function readWrapped($yamlFile)
197
    {
198
        try {
199 1
            $yamlString = file_get_contents($yamlFile);
200 1
            $ret = static::loadWrapped($yamlString);
201
        } catch (\Exception $e) {
202
            static::logError($e);
203
            $ret = false;
204
        }
205 1
        return $ret;
206
    }
207
208
    /**
209
     * Save a PHP array as a YAML file with a php wrapper
210
     *
211
     * The wrap is a php header that surrounds the yaml with section markers,
212
     * '---' and '...' along with php comment markers. The php wrapper keeps the
213
     * yaml file contents from being revealed by serving the file directly from
214
     * a poorly configured server.
215
     *
216
     * @param array   $var      variable which will be dumped
217
     * @param string  $yamlFile filename of YAML file
218
     * @param integer $inline   Nesting level where you switch to inline YAML
219
     * @param integer $indent   Number of spaces to indent for nested nodes
220
     *
221
     * @return integer|boolean number of bytes written, or false on error
222
     */
223 1 View Code Duplication
    public static function saveWrapped($var, $yamlFile, $inline = 4, $indent = 4)
224
    {
225
        try {
226 1
            $yamlString = static::dumpWrapped($var, $inline, $indent);
227 1
            $ret = file_put_contents($yamlFile, $yamlString);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as file_put_contents($yamlFile, $yamlString) (which targets file_put_contents()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
228
        } catch (\Exception $e) {
229
            static::logError($e);
230
            $ret = false;
231
        }
232 1
        return $ret;
233
    }
234
235
    /**
236
     * @param \Exception $e throwable to log
237
     */
238
    protected static function logError($e)
239
    {
240
        if (class_exists('Xoops')) {
241
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
242
        } else {
243
            trigger_error($e->getMessage(), E_USER_ERROR);
244
        }
245
    }
246
}
247