Completed
Push — master ( f2dfe9...84d91c )
by André
35:37 queued 16:15
created

LanguageHandlerMock::loadList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
cc 3
nc 3
nop 1
rs 9.9
1
<?php
2
3
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content;
4
5
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
6
use eZ\Publish\SPI\Persistence\Content\Language;
7
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct;
8
9
/**
10
 * Simple mock for a Language\Handler.
11
 */
12
class LanguageHandlerMock implements LanguageHandler
13
{
14
    protected $languages = array();
15
16
    public function __construct()
17
    {
18
        $this->languages[] = new Language(
19
            array(
20
                'id' => 2,
21
                'languageCode' => 'eng-US',
22
                'name' => 'US english',
23
            )
24
        );
25
        $this->languages[] = new Language(
26
            array(
27
                'id' => 4,
28
                'languageCode' => 'eng-GB',
29
                'name' => 'British english',
30
            )
31
        );
32
        $this->languages[] = new Language(
33
            array(
34
                'id' => 8,
35
                'languageCode' => 'ger-DE',
36
                'name' => 'German',
37
            )
38
        );
39
    }
40
41
    /**
42
     * Create a new language.
43
     *
44
     * @param \eZ\Publish\SPI\Persistence\Content\Language\CreateStruct $struct
45
     *
46
     * @return \eZ\Publish\SPI\Persistence\Content\Language
47
     */
48
    public function create(CreateStruct $struct)
49
    {
50
        throw new \RuntimeException('Not implemented, yet.');
51
    }
52
53
    /**
54
     * Update language.
55
     *
56
     * @param \eZ\Publish\SPI\Persistence\Content\Language $struct
57
     */
58
    public function update(Language $struct)
59
    {
60
        throw new \RuntimeException('Not implemented, yet.');
61
    }
62
63
    /**
64
     * Get language by id.
65
     *
66
     * @param mixed $id
67
     *
68
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $id
69
     *
70
     * @return \eZ\Publish\SPI\Persistence\Content\Language
71
     */
72
    public function load($id)
73
    {
74
        foreach ($this->languages as $language) {
75
            if ($language->id == $id) {
76
                return $language;
77
            }
78
        }
79
        throw new \RuntimeException("Language $id not found.");
80
    }
81
82
    /**
83
     * Get language by Language Code (eg: eng-GB).
84
     *
85
     * @param string $languageCode
86
     *
87
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $languageCode
88
     *
89
     * @return \eZ\Publish\SPI\Persistence\Content\Language
90
     */
91
    public function loadByLanguageCode($languageCode)
92
    {
93
        foreach ($this->languages as $language) {
94
            if ($language->languageCode == $languageCode) {
95
                return $language;
96
            }
97
        }
98
        throw new \RuntimeException("Language $languageCode not found.");
99
    }
100
101
    /**
102
     * Get all languages.
103
     *
104
     * Return list of languages where key of hash is language code.
105
     *
106
     * @return \eZ\Publish\SPI\Persistence\Content\Language[]
107
     */
108
    public function loadAll()
109
    {
110
        return $this->languages;
111
    }
112
113
    /**
114
     * Delete a language.
115
     *
116
     * @todo Might throw an exception if the language is still associated with some content / types / (...) ?
117
     *
118
     * @param mixed $id
119
     */
120
    public function delete($id)
121
    {
122
        throw new \RuntimeException('Not implemented, yet.');
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 View Code Duplication
    public function loadList(array $ids): iterable
129
    {
130
        $languages = [];
131
        foreach ($this->languages as $language) {
132
            if (in_array($language->id, $ids)) {
133
                $languages[$language->id] = $language;
134
            }
135
        }
136
137
        return $languages;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $languages; (array) is incompatible with the return type declared by the interface eZ\Publish\SPI\Persisten...guage\Handler::loadList of type eZ\Publish\SPI\Persisten...ntent\Language\iterable.

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...
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 View Code Duplication
    public function loadListByLanguageCodes(array $languageCodes): iterable
144
    {
145
        $languages = [];
146
        foreach ($this->languages as $language) {
147
            if (in_array($language->languageCode, $languageCodes)) {
148
                $languages[$language->languageCode] = $language;
149
            }
150
        }
151
152
        return $languages;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $languages; (array) is incompatible with the return type declared by the interface eZ\Publish\SPI\Persisten...loadListByLanguageCodes of type eZ\Publish\SPI\Persisten...ntent\Language\iterable.

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...
153
    }
154
}
155