Completed
Push — master ( 031f19...3fe677 )
by John
02:56
created

YamlParser::fixHashMaps()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 12
nc 3
nop 1
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 Symfony\Component\Yaml\Parser;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * Facade/Adapter for Symfony\Yaml
16
 *
17
 * @author John Kleijn <[email protected]>
18
 */
19
class YamlParser
20
{
21
    /**
22
     * @var Parser
23
     */
24
    private $parser;
25
26
    /**
27
     * Construct the wrapper
28
     */
29
    public function __construct()
30
    {
31
        $this->parser = new Parser();
32
    }
33
34
    /**
35
     * @param string $string
36
     *
37
     * @return object
38
     */
39
    public function parse($string)
40
    {
41
        // Hashmap support is broken, so disable it and attempt fix afterwards
42
        return $this->fixHashMaps(
43
            $this->parser->parse($string, true, false, false)
0 ignored issues
show
Bug introduced by
$this->parser->parse($string, true, false, false) cannot be passed to fixhashmaps() as the parameter $data expects a reference.
Loading history...
44
        );
45
    }
46
47
    /**
48
     * @see https://github.com/symfony/symfony/pull/17711
49
     *
50
     * @param mixed $data
51
     *
52
     * @return \stdClass
53
     */
54
    private function fixHashMaps(&$data)
55
    {
56
        if (is_array($data)) {
57
            $shouldBeObject = false;
58
            $object = new \stdClass();
59
            $index = 0;
60
            foreach ($data as $key => &$value) {
61
                $object->$key = $this->fixHashMaps($value);
62
                if ($index++ !== $key) {
63
                    $shouldBeObject = true;
64
                }
65
            }
66
            if ($shouldBeObject) {
67
                $data = $object;
68
            }
69
        }
70
71
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $data; (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by KleijnWeb\SwaggerBundle\...YamlParser::fixHashMaps of type stdClass|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...
72
    }
73
}
74