Completed
Push — master ( 56f22b...5c0ad8 )
by Davide
01:07
created

Yaml   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 67
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseFile() 0 15 2
A parseString() 0 15 2
A parse() 0 4 1
A getSupportedExtensions() 0 4 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Exception;
6
use Symfony\Component\Yaml\Yaml as YamlParser;
7
use Noodlehaus\Exception\ParseException;
8
9
/**
10
 * YAML parser
11
 *
12
 * @package    Config
13
 * @author     Jesus A. Domingo <[email protected]>
14
 * @author     Hassan Khan <[email protected]>
15
 * @author     Filip Š <[email protected]>
16
 * @link       https://github.com/noodlehaus/config
17
 * @license    MIT
18
 */
19
class Yaml implements ParserInterface
20
{
21
    /**
22
     * {@inheritDoc}
23
     * Loads a YAML/YML file as an array
24
     *
25
     * @throws ParseException If If there is an error parsing the YAML file
26
     */
27 9
    public function parseFile($filename)
28
    {
29
        try {
30 9
            $data = YamlParser::parseFile($filename, YamlParser::PARSE_CONSTANT);
31 5
        } catch (Exception $exception) {
32 3
            throw new ParseException(
33
                [
34 3
                    'message'   => 'Error parsing YAML file',
35 3
                    'exception' => $exception,
36
                ]
37 1
            );
38
        }
39
40 6
        return $this->parse($data, $filename);
0 ignored issues
show
Documentation introduced by
$filename is of type string, but the function expects a object<Noodlehaus\Parser\strring>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     * Loads a YAML/YML string as an array
46 3
     *
47
     * @throws ParseException If If there is an error parsing the YAML string
48 3
     */
49
    public function parseString($config)
50
    {
51
        try {
52
            $data = YamlParser::parse($config, YamlParser::PARSE_CONSTANT);
53
        } catch (Exception $exception) {
54
            throw new ParseException(
55
                [
56
                    'message'   => 'Error parsing YAML string',
57
                    'exception' => $exception,
58
                ]
59
            );
60
        }
61
62
        return $this->parse($data);
63
    }
64
65
    /**
66
     * Completes parsing of YAML/YML data
67
     *
68
     * @param  array   $data
69
     * @param  strring $filename
70
     *
71
     * @throws ParseException If there is an error parsing the YAML data
72
     */
73
    protected function parse($data = null, $filename = null)
0 ignored issues
show
Unused Code introduced by
The parameter $filename is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        return $data;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public static function getSupportedExtensions()
82
    {
83
        return ['yaml', 'yml'];
84
    }
85
}
86