Completed
Push — master ( 677cdd...a74cf9 )
by Richard
10:49
created

Yaml   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 185
Duplicated Lines 29.19 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 71.93%
Metric Value
wmc 18
lcom 0
cbo 2
dl 54
loc 185
ccs 41
cts 57
cp 0.7193
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 10 10 2
A load() 0 10 2
A save() 11 11 2
A dumpWrapped() 0 11 3
A loadWrapped() 0 13 3
A readWrapped() 11 11 2
A saveWrapped() 11 11 2
A read() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Xoops\Core;
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  Xoops\Core\Yaml
30
 * @package   Yaml
31
 * @author    Richard Griffith <[email protected]>
32
 * @copyright 2013-2014 XOOPS Project (http://xoops.org)
33
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
34
 * @version   Release: 1.0
35
 * @link      http://xoops.org
36
 * @see       http://www.yaml.org/
37
 * @since     1.0
38
 */
39
class Yaml
40
{
41
42
    /**
43
     * Dump an PHP array as a YAML string
44
     *
45
     * @param mixed   $var    Variable which will be dumped
46
     * @param integer $inline Nesting level where you switch to inline YAML
47
     * @param integer $indent Number of spaces to indent for nested nodes
48
     *
49
     * @return string|bool YAML string or false on error
50
     */
51 1 View Code Duplication
    public static function dump($var, $inline = 4, $indent = 4)
52
    {
53
        try {
54 1
            $ret = VendorYaml::dump($var, $inline, $indent);
55 1
        } catch (\Exception $e) {
56
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
57
            $ret = false;
58
        }
59 1
        return $ret;
60
    }
61
62
    /**
63
     * Load a YAML string into a PHP array
64
     *
65
     * @param string $yamlString YAML dump string
66
     *
67
     * @return array|boolean PHP array or false on error
68
     */
69 1
    public static function load($yamlString)
70
    {
71
        try {
72 1
            $ret = VendorYaml::parse($yamlString);
73 1
        } catch (\Exception $e) {
74
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
75
            $ret = false;
76
        }
77 1
        return $ret;
78
    }
79
80
    /**
81
     * Read a file containing YAML into a PHP array
82
     *
83
     * @param string $yamlFile filename of YAML file
84
     *
85
     * @return array|boolean PHP array or false on error
86
     */
87 3 View Code Duplication
    public static function read($yamlFile)
88
    {
89
        try {
90 3
            $yamlString = file_get_contents($yamlFile);
91 3
            $ret = VendorYaml::parse($yamlString);
92 3
        } catch (\Exception $e) {
93
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
94
            $ret = false;
95
        }
96 3
        return $ret;
97 1
    }
98
99
    /**
100
     * Save a PHP array as a YAML file
101
     *
102
     * @param array   $var      variable which will be dumped
103
     * @param string  $yamlFile filename of YAML file
104
     * @param integer $inline   Nesting level where you switch to inline YAML
105
     * @param integer $indent   Number of spaces to indent for nested nodes
106
     *
107
     * @return integer|boolean number of bytes written, or false on error
108
     */
109 2 View Code Duplication
    public static function save($var, $yamlFile, $inline = 4, $indent = 4)
110
    {
111
        try {
112 2
            $yamlString = VendorYaml::dump($var, $inline, $indent);
113 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...
114 2
        } catch (\Exception $e) {
115
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
116
            $ret = false;
117
        }
118 2
        return $ret;
119
    }
120
121
    /**
122
     * Dump an PHP array as a YAML string with a php wrapper
123
     *
124
     * The wrap is a php header that surrounds the yaml with section markers,
125
     * '---' and '...' along with php comment markers. The php wrapper keeps the
126
     * yaml file contents from being revealed by serving the file directly from
127
     * a poorly configured server.
128
     *
129
     * @param mixed   $var    Variable which will be dumped
130
     * @param integer $inline Nesting level where you switch to inline YAML
131
     * @param integer $indent Number of spaces to indent for nested nodes
132
     *
133
     * @return string|boolean YAML string or false on error
134
     */
135 1
    public static function dumpWrapped($var, $inline = 4, $indent = 4)
136
    {
137
        try {
138 1
            $yamlString = VendorYaml::dump($var, $inline, $indent);
139 1
            $ret = empty($yamlString) ? false : "<?php\n/*\n---\n" . $yamlString . "\n...\n*/\n";
140 1
        } catch (\Exception $e) {
141
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
142
            $ret = false;
143
        }
144 1
        return $ret;
145
    }
146
147
    /**
148
     * Load a YAML string with a php wrapper into a PHP array
149
     *
150
     * The wrap is a php header that surrounds the yaml with section markers,
151
     * '---' and '...' along with php comment markers. The php wrapper keeps the
152
     * yaml file contents from being revealed by serving the file directly from
153
     * a poorly configured server.
154
     *
155
     * @param string $yamlString YAML dump string
156
     *
157
     * @return array|boolean PHP array or false on error
158
     */
159 1
    public static function loadWrapped($yamlString)
160
    {
161
        try {
162 1
            $match = array();
163 1
            $matched = preg_match('/(---.*\.\.\.)/s', $yamlString, $match);
164 1
            $unwrapped = $matched ? $match[1] : $yamlString;
165 1
            $ret = VendorYaml::parse($unwrapped);
166 1
        } catch (\Exception $e) {
167
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
168
            $ret = false;
169
        }
170 1
        return $ret;
171
    }
172
173
    /**
174
     * Read a file containing YAML with a php wrapper into a PHP array
175
     *
176
     * The wrap is a php header that surrounds the yaml with section markers,
177
     * '---' and '...' along with php comment markers. The php wrapper keeps the
178
     * yaml file contents from being revealed by serving the file directly from
179
     * a poorly configured server.
180
     *
181
     * @param string $yamlFile filename of YAML file
182
     *
183
     * @return array|boolean PHP array or false on error
184
     */
185 1 View Code Duplication
    public static function readWrapped($yamlFile)
186
    {
187
        try {
188 1
            $yamlString = file_get_contents($yamlFile);
189 1
            $ret = self::loadWrapped($yamlString);
190 1
        } catch (\Exception $e) {
191
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
192
            $ret = false;
193
        }
194 1
        return $ret;
195
    }
196
197
    /**
198
     * Save a PHP array as a YAML file with a php wrapper
199
     *
200
     * The wrap is a php header that surrounds the yaml with section markers,
201
     * '---' and '...' along with php comment markers. The php wrapper keeps the
202
     * yaml file contents from being revealed by serving the file directly from
203
     * a poorly configured server.
204
     *
205
     * @param array   $var      variable which will be dumped
206
     * @param string  $yamlFile filename of YAML file
207
     * @param integer $inline   Nesting level where you switch to inline YAML
208
     * @param integer $indent   Number of spaces to indent for nested nodes
209
     *
210
     * @return integer|boolean number of bytes written, or false on error
211
     */
212 1 View Code Duplication
    public static function saveWrapped($var, $yamlFile, $inline = 4, $indent = 4)
213
    {
214
        try {
215 1
            $yamlString = self::dumpWrapped($var, $inline, $indent);
216 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...
217 1
        } catch (\Exception $e) {
218
            \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
219
            $ret = false;
220
        }
221 1
        return $ret;
222
    }
223
}
224