Completed
Push — master ( 65ba07...f0ec2b )
by André
104:16 queued 82:47
created

FacetBuilderBaseTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getParsingDispatcher() 0 51 1
A internalGetParser() 0 4 1
1
<?php
2
3
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser\FacetBuilder;
4
5
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
6
use eZ\Publish\Core\REST\Server\Input\Parser\ContentQuery as QueryParser;
7
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\ContentTypeParser;
8
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\CriterionParser;
9
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\DateRangeParser;
10
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\FieldParser;
11
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\FieldRangeParser;
12
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\LocationParser;
13
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\SectionParser;
14
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\TermParser;
15
use eZ\Publish\Core\REST\Server\Input\Parser\FacetBuilder\UserParser;
16
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest;
17
18
abstract class FacetBuilderBaseTest extends BaseTest
19
{
20
    /**
21
     * @return \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher
22
     */
23
    protected function getParsingDispatcher()
24
    {
25
        $parsingDispatcher = new ParsingDispatcher();
26
27
        $parsingDispatcher->addParser(
28
            'application/vnd.ez.api.internal.facetbuilder.ContentType',
29
            new ContentTypeParser()
30
        );
31
32
        $parsingDispatcher->addParser(
33
            'application/vnd.ez.api.internal.facetbuilder.Criterion',
34
            new CriterionParser()
35
        );
36
37
        $parsingDispatcher->addParser(
38
            'application/vnd.ez.api.internal.facetbuilder.DateRange',
39
            new DateRangeParser()
40
        );
41
42
        $parsingDispatcher->addParser(
43
            'application/vnd.ez.api.internal.facetbuilder.Field',
44
            new FieldParser()
45
        );
46
47
        $parsingDispatcher->addParser(
48
            'application/vnd.ez.api.internal.facetbuilder.FieldRange',
49
            new FieldRangeParser()
50
        );
51
52
        $parsingDispatcher->addParser(
53
            'application/vnd.ez.api.internal.facetbuilder.Location',
54
            new LocationParser()
55
        );
56
57
        $parsingDispatcher->addParser(
58
            'application/vnd.ez.api.internal.facetbuilder.Section',
59
            new SectionParser()
60
        );
61
62
        $parsingDispatcher->addParser(
63
            'application/vnd.ez.api.internal.facetbuilder.Term',
64
            new TermParser()
65
        );
66
67
        $parsingDispatcher->addParser(
68
            'application/vnd.ez.api.internal.facetbuilder.User',
69
            new UserParser()
70
        );
71
72
        return $parsingDispatcher;
73
    }
74
75
    /**
76
     * Returns the query parser.
77
     *
78
     * @return \eZ\Publish\Core\REST\Server\Input\Parser\ContentQuery
79
     */
80
    protected function internalGetParser()
81
    {
82
        return new QueryParser();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...\Parser\ContentQuery(); (eZ\Publish\Core\REST\Ser...put\Parser\ContentQuery) is incompatible with the return type declared by the abstract method eZ\Publish\Core\REST\Ser...Test::internalGetParser of type eZ\Publish\Core\REST\Server\Input\Parser\Base.

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...
83
    }
84
}
85