Completed
Push — master ( 54855b...e3699c )
by Zoltán
02:43
created

SymfonyYAMLParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace BuildR\TestTools\DataSetLoader\YAML\Parser;
2
3
use Symfony\Component\Yaml\Yaml;
4
5
/**
6
 * YAML Parserbased on Symfony's YAML implementation.
7
 * It is slow, but works on any system, without any extension
8
 *
9
 * BuildR PHP Framework
10
 *
11
 * @author Zoltán Borsos <[email protected]>
12
 * @package TestTools
13
 * @subpackage DataSetLoader\YAML\Parser
14
 *
15
 * @copyright    Copyright 2016, Zoltán Borsos.
16
 * @license      https://github.com/BuildrPHP/Test-Tools/blob/master/LICENSE.md
17
 * @link         https://github.com/BuildrPHP/Test-Tools
18
 */
19
class SymfonyYAMLParser implements YAMLParserInterface {
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 2
    public function parse($yamlString) {
25 2
        return Yaml::parse($yamlString);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Symfony\Componen...ml::parse($yamlString); (array|string|stdClass) is incompatible with the return type declared by the interface BuildR\TestTools\DataSet...LParserInterface::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...
26
    }
27
28
}
29