YamlParser::getSupportedExtensions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * slince config library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Config\Parser;
7
8
use Slince\Config\Exception\ParseException;
9
use Symfony\Component\Yaml\Yaml;
10
11
class YamlParser implements ParserInterface
12
{
13
    /**
14
     * Whether supports native function
15
     * @var boolean
16
     */
17
    protected static $supportNative;
18
19
    public function __construct()
20
    {
21
        if (is_null(static::$supportNative)) {
22
            static::$supportNative = function_exists('yaml_parse_file');
23
        }
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function parse($file)
30
    {
31
        if (static::$supportNative && ($data = yaml_parse_file($file)) === false) {
32
            throw new ParseException(sprintf('The file "%s" has syntax errors', $file));
33
        } else {
34
            try {
35
                $data = Yaml::parse(file_get_contents($file));
36
            } catch (\Exception $exception) {
37
                throw new ParseException($exception->getMessage(), $exception->getCode());
38
            }
39
        }
40
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $data; (string|array|stdClass) is incompatible with the return type declared by the interface Slince\Config\Parser\ParserInterface::parse of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function dump($file, array $data)
47
    {
48
        @mkdir(dirname($file), 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
49
        if (static::$supportNative) {
50
            $result = yaml_emit_file($file, $data);
51
        } else {
52
            $yaml = Yaml::dump($data);
53
            $result = (@file_put_contents($file, $yaml) !== false);
54
        }
55
        return $result;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public static function getSupportedExtensions()
62
    {
63
        return ['yml', 'yaml'];
64
    }
65
}