Completed
Pull Request — master (#74)
by John
03:17 queued 38s
created

Loader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
B load() 0 28 5
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Document;
10
11
use KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotDecodableException;
12
use KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotReadableException;
13
use Symfony\Component\Yaml\Exception\ParseException;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class Loader
19
{
20
    /**
21
     * @var YamlParser
22
     */
23
    private $yamlParser;
24
25
    /**
26
     * @param YamlParser $yamlParser
27
     */
28
    public function __construct(YamlParser $yamlParser = null)
29
    {
30
        $this->yamlParser = $yamlParser ?: new YamlParser();
31
    }
32
33
    /**
34
     * @param string $uri
35
     *
36
     * @return \stdClass
37
     * @throws ResourceNotDecodableException
38
     * @throws ResourceNotReadableException
39
     */
40
    public function load($uri)
41
    {
42
        $exception = new ResourceNotReadableException("Failed reading '$uri'");
43
44
        set_error_handler(function () use ($exception) {
45
            throw $exception;
46
        });
47
        $response = file_get_contents($uri);
48
        restore_error_handler();
49
50
        if (false === $response) {
51
            throw $exception;
52
        }
53
        if (preg_match('/\b(yml|yaml)\b/', $uri)) {
54
            try {
55
                $content = $this->yamlParser->parse($response);
56
            } catch (ParseException $e) {
57
                throw new ResourceNotDecodableException("Failed to parse '$uri' as YAML", 0, $e);
58
            }
59
60
            return $content;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $content; (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by KleijnWeb\SwaggerBundle\Document\Loader::load of type stdClass.

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...
61
        }
62
        if (!$content = json_decode($response)) {
63
            throw new ResourceNotDecodableException("Failed to parse '$uri' as JSON");
64
        }
65
66
        return $content;
67
    }
68
}
69