Completed
Pull Request — development (#546)
by Nick
07:54 queued 01:05
created

CountryEntity   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 toDatabaseArray() 14 14 1
A fromDatabaseArray() 14 14 1
A isNew() 4 4 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\Country;
4
5
/**
6
 * Class CountryEntity
7
 *
8
 * @package Oc\Country
9
 * @author Nick Lubisch <[email protected]>
10
 */
11 View Code Duplication
class CountryEntity
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...
12
{
13
    /**
14
     * @var string
15
     */
16
    public $short;
17
18
    /**
19
     * @var string
20
     */
21
    public $name;
22
23
    /**
24
     * @var string
25
     */
26
    public $de;
27
28
    /**
29
     * @var string
30
     */
31
    public $en;
32
33
    /**
34
     * @var int
35
     */
36
    public $translationId;
37
38
    /**
39
     * @var bool
40
     */
41
    public $listDefaultDe;
42
43
    /**
44
     * @var bool
45
     */
46
    public $listDefaultEn;
47
48
    /**
49
     * @var string
50
     */
51
    public $sortDe;
52
53
    /**
54
     * @var string
55
     */
56
    public $sortEn;
57
58
    /**
59
     * Checks if the entity is new.
60
     *
61
     * @return bool
62
     */
63
    public function isNew()
64
    {
65
        return $this->short === null;
66
    }
67
68
    /**
69
     * Sets properties from the database.
70
     *
71
     * @return array
72
     */
73
    public function toDatabaseArray()
74
    {
75
        return [
76
            'short' => $this->short,
77
            'name' => $this->name,
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
            'sort_de' => $this->sortDe,
84
            'sort_en' => $this->sortEn
85
        ];
86
    }
87
88
    /**
89
     * Prepares database array from properties.
90
     *
91
     * @param array $data
92
     *
93
     * @return self
94
     */
95
    public function fromDatabaseArray(array $data)
96
    {
97
        $this->short = (string) $data['short'];
98
        $this->name = (string) $data['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->sortDe = (string) $data['sort_de'];
105
        $this->sortEn = (string) $data['sort_en'];
106
107
        return $this;
108
    }
109
110
    /**
111
     * Sets all properties from array.
112
     *
113
     * @param array $data
114
     */
115
    public function fromArray(array $data)
116
    {
117
        foreach ($data as $key => $value) {
118
            if (!property_exists($this, $key)) {
119
                continue;
120
            }
121
122
            $this->{$key} = $value;
123
        }
124
    }
125
126
    /**
127
     * Returns all properties as array.
128
     *
129
     * @return array
130
     */
131
    public function toArray()
132
    {
133
        return get_object_vars($this);
134
    }
135
}
136