Completed
Pull Request — development (#546)
by Nick
06:41
created

LanguageEntity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isNew() 4 4 1
A toDatabaseArray() 14 14 1
A fromDatabaseArray() 14 14 1
A fromArray() 10 10 3
A toArray() 4 4 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
namespace Oc\Language;
4
5
/**
6
 * Class LanguageEntity
7
 *
8
 * @package Oc\Language
9
 */
10 View Code Duplication
class LanguageEntity
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
11
{
12
    /**
13
     * @var string
14
     */
15
    public $short;
16
17
    /**
18
     * @var string
19
     */
20
    public $name;
21
22
    /**
23
     * @var string
24
     */
25
    public $nativeName;
26
27
    /**
28
     * @var string
29
     */
30
    public $de;
31
32
    /**
33
     * @var string
34
     */
35
    public $en;
36
37
    /**
38
     * @var int
39
     */
40
    public $translationId;
41
42
    /**
43
     * @var bool
44
     */
45
    public $listDefaultDe;
46
47
    /**
48
     * @var bool
49
     */
50
    public $listDefaultEn;
51
52
    /**
53
     * @var bool
54
     */
55
    public $isTranslated;
56
57
    /**
58
     * Checks if the entity is new.
59
     *
60
     * @return bool
61
     */
62
    public function isNew()
63
    {
64
        return $this->short === null;
65
    }
66
67
    /**
68
     * Sets properties from the database.
69
     *
70
     * @return array
71
     */
72
    public function toDatabaseArray()
73
    {
74
        return [
75
            'short' => $this->short,
76
            'name' => $this->name,
77
            'native_name' => $this->nativeName,
78
            'de' => $this->de,
79
            'en' => $this->en,
80
            'trans_id' => $this->translationId,
81
            'list_default_de' => $this->listDefaultDe,
82
            'list_default_en' => $this->listDefaultEn,
83
            'is_translated' => $this->isTranslated
84
        ];
85
    }
86
87
    /**
88
     * Prepares database array from properties.
89
     *
90
     * @param array $data
91
     *
92
     * @return self
93
     */
94
    public function fromDatabaseArray(array $data)
95
    {
96
        $this->short = (string) strtolower($data['short']);
97
        $this->name = (string) $data['name'];
98
        $this->nativeName = (string) $data['native_name'];
99
        $this->de = (string) $data['de'];
100
        $this->en = (string) $data['en'];
101
        $this->translationId = (int) $data['trans_id'];
102
        $this->listDefaultDe = (bool) $data['list_default_de'];
103
        $this->listDefaultEn = (bool) $data['list_default_en'];
104
        $this->isTranslated = (bool) $data['is_translated'];
105
106
        return $this;
107
    }
108
109
    /**
110
     * Sets all properties from array.
111
     *
112
     * @param array $data
113
     */
114
    public function fromArray(array $data)
115
    {
116
        foreach ($data as $key => $value) {
117
            if (!property_exists($this, $key)) {
118
                continue;
119
            }
120
121
            $this->{$key} = $value;
122
        }
123
    }
124
125
    /**
126
     * Returns all properties as array.
127
     *
128
     * @return array
129
     */
130
    public function toArray()
131
    {
132
        return get_object_vars($this);
133
    }
134
}
135