Completed
Push — language_match_api_logic ( dd8070...0dc4d1 )
by André
26:08
created

MultiLanguageValueTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 33.73 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 28
loc 83
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNames() 0 4 1
B getName() 14 14 5
A getDescriptions() 0 4 1
B getDescription() 14 14 5

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 eZ\Publish\API\Repository\Values\MultiLanguageValueTrait trait.
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\Repository\Values;
10
11
/**
12
 * @internal Meant for internal use by Repository, type hint against API object instead.
13
 */
14
trait MultiLanguageValueTrait
15
{
16
    /**
17
     * Holds the collection of names with languageCode keys.
18
     *
19
     * @var string[]
20
     */
21
    protected $names;
22
23
    /**
24
     * Holds the collection of descriptions with languageCode keys.
25
     *
26
     * @var string[]
27
     */
28
    protected $descriptions;
29
30
    /**
31
     * Main language.
32
     *
33
     * @var string
34
     */
35
    protected $mainLanguageCode;
36
37
    /**
38
     * Prioritized languages provided by user when retrieving object using API.
39
     *
40
     * @internal
41
     * @var string[]
42
     */
43
    protected $prioritizedLanguageCodes = [];
44
45
    /**
46
     * {@inheritdoc}.
47
     */
48
    public function getNames()
49
    {
50
        return $this->names;
51
    }
52
53
    /**
54
     * {@inheritdoc}.
55
     */
56 View Code Duplication
    public function getName($languageCode = null)
57
    {
58
        if (!empty($languageCode)) {
59
            return isset($this->names[$languageCode]) ? $this->names[$languageCode] : null;
60
        }
61
62
        foreach ($this->prioritizedLanguageCodes as $prioritizedLanguageCode) {
63
            if (isset($this->names[$prioritizedLanguageCode])) {
64
                $this->names[$prioritizedLanguageCode];
65
            }
66
        }
67
68
        return $this->names[$this->mainLanguageCode];
69
    }
70
71
    /**
72
     * {@inheritdoc}.
73
     */
74
    public function getDescriptions()
75
    {
76
        return $this->descriptions;
77
    }
78
79
    /**
80
     * {@inheritdoc}.
81
     */
82 View Code Duplication
    public function getDescription($languageCode = null)
83
    {
84
        if (!empty($languageCode)) {
85
            return isset($this->descriptions[$languageCode]) ? $this->descriptions[$languageCode] : null;
86
        }
87
88
        foreach ($this->prioritizedLanguageCodes as $prioritizedLanguageCode) {
89
            if (isset($this->descriptions[$prioritizedLanguageCode])) {
90
                $this->descriptions[$prioritizedLanguageCode];
91
            }
92
        }
93
94
        return $this->descriptions[$this->mainLanguageCode];
95
    }
96
}