YamlLoader::load()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 26
Code Lines 12

Duplication

Lines 26
Ratio 100 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 26
loc 26
rs 8.439
cc 6
eloc 12
nc 12
nop 1
1
<?php
2
/**
3
 * YAML file loader.
4
 *
5
 * @see http://symfony.com/doc/current/components/yaml/introduction.html
6
 *
7
 * @package SugiPHP.Config
8
 * @author  Plamen Popov <[email protected]>
9
 * @license http://opensource.org/licenses/mit-license.php (MIT License)
10
 */
11
12
namespace SugiPHP\Config;
13
14
use Symfony\Component\Yaml\Yaml;
15
16 View Code Duplication
class YamlLoader implements LoaderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    protected $locator;
19
20
    public function __construct(LocatorInterface $locator = null)
21
    {
22
        $this->locator = $locator;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function load($resource)
29
    {
30
        // check the extension. If it's not provided we'll add .php
31
        if (pathinfo($resource, PATHINFO_EXTENSION) === "") {
32
            $resource .= ".yml";
33
        }
34
35
        $file = false;
36
37
        if ($this->locator) {
38
            // pass it to the locator (if set) and than include the file
39
            $file = $this->locator->locate($resource);
40
        } elseif (is_file($resource) && is_readable($resource)) {
41
            // check if the $resource is a real file and include it
42
            $file = $resource;
43
        }
44
45
        if ($file) {
46
            // Passing a file as an input is a deprecated feature and will be removed in Symfony\Yaml 3.0.
47
            $yaml = file_get_contents($file);
48
49
            return Yaml::parse($yaml);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Symfony\Component\Yaml\Yaml::parse($yaml); (array|string|stdClass) is incompatible with the return type declared by the interface SugiPHP\Config\LoaderInterface::load of type array|null.

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...
50
        }
51
52
        return null;
53
    }
54
}
55