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

ContentLanguageHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 42
loc 126
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A load() 16 16 2
A update() 0 9 1
A loadByLanguageCode() 16 16 2
A loadAll() 0 6 1
A delete() 0 9 1
A loadList() 5 15 1
A loadListByLanguageCodes() 5 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the LanguageHandler class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use eZ\Publish\SPI\Persistence\Content\Language\Handler as ContentLanguageHandlerInterface;
12
use eZ\Publish\SPI\Persistence\Content\Language;
13
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct;
14
15
/**
16
 * @see \eZ\Publish\SPI\Persistence\Content\Language\Handler
17
 */
18
class ContentLanguageHandler extends AbstractHandler implements ContentLanguageHandlerInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function create(CreateStruct $struct)
24
    {
25
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
26
27
        return $this->persistenceHandler->contentLanguageHandler()->create($struct);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function update(Language $struct)
34
    {
35
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
36
        $return = $this->persistenceHandler->contentLanguageHandler()->update($struct);
37
38
        $this->cache->invalidateTags(['language-' . $struct->id]);
39
40
        return $return;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 View Code Duplication
    public function load($id)
47
    {
48
        $cacheItem = $this->cache->getItem('ez-language-' . $id);
49
        if ($cacheItem->isHit()) {
50
            return $cacheItem->get();
51
        }
52
53
        $this->logger->logCall(__METHOD__, array('language' => $id));
54
        $language = $this->persistenceHandler->contentLanguageHandler()->load($id);
55
56
        $cacheItem->set($language);
57
        $cacheItem->tag('language-' . $language->id);
58
        $this->cache->save($cacheItem);
59
60
        return $language;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function loadList(array $ids): iterable
67
    {
68
        return $this->getMultipleCacheItems(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getMultipl...' . $language->id); }); (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...
69
            $ids,
70
            'ez-language-',
71 View Code Duplication
            function (array $cacheMissIds) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
                $this->logger->logCall(__CLASS__ . '::loadList', ['languages' => $cacheMissIds]);
73
74
                return $this->persistenceHandler->contentLanguageHandler()->loadList($cacheMissIds);
75
            },
76
            function (Language $language) {
77
                return ['language-' . $language->id];
78
            }
79
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 View Code Duplication
    public function loadByLanguageCode($languageCode)
86
    {
87
        $cacheItem = $this->cache->getItem('ez-language-code-' . $languageCode);
88
        if ($cacheItem->isHit()) {
89
            return $cacheItem->get();
90
        }
91
92
        $this->logger->logCall(__METHOD__, array('language' => $languageCode));
93
        $language = $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode);
94
95
        $cacheItem->set($language);
96
        $cacheItem->tag('language-' . $language->id);
97
        $this->cache->save($cacheItem);
98
99
        return $language;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function loadListByLanguageCodes(array $languageCodes): iterable
106
    {
107
        return $this->getMultipleCacheItems(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getMultipl...' . $language->id); }); (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...
108
            $languageCodes,
109
            'ez-language-code-',
110 View Code Duplication
            function (array $cacheMissIds) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
                $this->logger->logCall(__CLASS__ . '::loadListByLanguageCodes', ['languages' => $cacheMissIds]);
112
113
                return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($cacheMissIds);
114
            },
115
            function (Language $language) {
116
                return ['language-' . $language->id];
117
            }
118
        );
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function loadAll()
125
    {
126
        $this->logger->logCall(__METHOD__);
127
128
        return $this->persistenceHandler->contentLanguageHandler()->loadAll();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function delete($id)
135
    {
136
        $this->logger->logCall(__METHOD__, array('language' => $id));
137
        $return = $this->persistenceHandler->contentLanguageHandler()->delete($id);
138
139
        $this->cache->invalidateTags(['language-' . $id]);
140
141
        return $return;
142
    }
143
}
144