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

PageEntity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 21.62 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isNew() 0 4 1
A toDatabaseArray() 12 12 1
A fromDatabaseArray() 12 12 1
A fromArray() 0 10 3
A toArray() 0 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\Page;
4
5
use DateTime;
6
7
/**
8
 * Class PageEntity
9
 *
10
 * @package Oc\Page
11
 */
12
class PageEntity
13
{
14
    /**
15
     * @var int
16
     */
17
    public $id;
18
19
    /**
20
     * @var string
21
     */
22
    public $slug;
23
24
    /**
25
     * @var string
26
     */
27
    public $metaKeywords;
28
29
    /**
30
     * @var string
31
     */
32
    public $metaDescription;
33
34
    /**
35
     * @var string
36
     */
37
    public $metaSocial;
38
39
    /**
40
     * @var DateTime
41
     */
42
    public $updatedAt;
43
44
    /**
45
     * @var bool
46
     */
47
    public $active = true;
48
49
    /**
50
     * Checks if the entity is new.
51
     *
52
     * @return bool
53
     */
54
    public function isNew()
55
    {
56
        return $this->id === null;
57
    }
58
59
    /**
60
     * Sets properties from the database.
61
     *
62
     * @return array
63
     */
64 View Code Duplication
    public function toDatabaseArray()
0 ignored issues
show
Duplication introduced by
This method 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...
65
    {
66
        return [
67
            'id' => $this->id,
68
            'slug' => $this->slug,
69
            'meta_keywords' => $this->metaKeywords,
70
            'meta_description' => $this->metaDescription,
71
            'meta_social' => $this->metaSocial,
72
            'updated_at' => $this->updatedAt->format(DateTime::ATOM),
73
            'active' => $this->active
74
        ];
75
    }
76
77
    /**
78
     * Prepares database array from properties.
79
     *
80
     * @param array $data
81
     *
82
     * @return self
83
     */
84 View Code Duplication
    public function fromDatabaseArray(array $data)
0 ignored issues
show
Duplication introduced by
This method 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...
85
    {
86
        $this->id = (int) $data['id'];
87
        $this->slug = (string) $data['slug'];
88
        $this->metaKeywords = (string) $data['meta_keywords'];
89
        $this->metaDescription = (string) $data['meta_description'];
90
        $this->metaSocial = (string) $data['meta_social'];
91
        $this->updatedAt = new DateTime($data['updated_at']);
92
        $this->active = (bool) $data['active'];
93
94
        return $this;
95
    }
96
97
    /**
98
     * Sets all properties from array.
99
     *
100
     * @param array $data
101
     */
102
    public function fromArray(array $data)
103
    {
104
        foreach ($data as $key => $value) {
105
            if (!property_exists($this, $key)) {
106
                continue;
107
            }
108
109
            $this->{$key} = $value;
110
        }
111
    }
112
113
    /**
114
     * Returns all properties as array.
115
     *
116
     * @return array
117
     */
118
    public function toArray()
119
    {
120
        return get_object_vars($this);
121
    }
122
}
123