Completed
Push — master ( d23403...374141 )
by Rai
03:12
created

BaseEntity   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 247
Duplicated Lines 9.31 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 23
loc 247
c 0
b 0
f 0
rs 8.295
wmc 42
lcom 1
cbo 1

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreatedAt() 0 4 1
A getUpdatedAt() 0 4 1
A getDeletedAt() 0 4 1
A setDeletedAt() 0 4 1
A getDiscr() 0 4 1
A getDiscrName() 0 4 1
A forcePersist() 0 6 1
A prePersist() 0 6 1
A postPersist() 0 5 1
A preUpdate() 0 6 1
A postUpdate() 0 5 1
A preFlush() 0 5 1
A getRepository() 0 4 1
A remove() 0 6 1
A save() 0 6 1
A flush() 0 6 2
A getId() 0 4 1
A getFillable() 0 4 1
getOnlyStore() 0 1 ?
A getOnlyUpdate() 0 4 1
B checkOnyExceptInArray() 16 16 8
C toArray() 7 51 14

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like BaseEntity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BaseEntity, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bludata\Doctrine\ORM\Entities;
4
5
use Bludata\Doctrine\Common\Interfaces\BaseEntityInterface;
6
use Bludata\Doctrine\Common\Interfaces\EntityTimestampInterface;
7
use Bludata\Doctrine\Common\Traits\SetPropertiesEntityTrait;
8
use DateTime;
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\ORM\Mapping as ORM;
11
use Doctrine\ORM\PersistentCollection;
12
use EntityManager;
13
use Gedmo\Mapping\Annotation as Gedmo;
14
15
/**
16
 * @ORM\MappedSuperclass
17
 * @ORM\HasLifecycleCallbacks
18
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
19
 */
20
abstract class BaseEntity implements BaseEntityInterface, EntityTimestampInterface
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 82 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
21
{
22
    use SetPropertiesEntityTrait;
23
24
    /**
25
     * @ORM\Id
26
     * @ORM\Column(type="integer", name="id")
27
     * @ORM\GeneratedValue
28
     */
29
    protected $id;
30
31
    /**
32
     * @var \DateTime
33
     *
34
     * @Gedmo\Timestampable(on="create")
35
     * @ORM\Column(type="datetime", name="createdAt")
36
     */
37
    private $createdAt;
38
39
    /**
40
     * @var \DateTime
41
     *
42
     * @Gedmo\Timestampable(on="update")
43
     * @ORM\Column(type="datetime", name="updatedAt")
44
     */
45
    private $updatedAt;
46
47
    /**
48
     * @ORM\Column(type="datetime", nullable=true, name="deletedAt")
49
     */
50
    private $deletedAt;
51
52
    public function getCreatedAt()
53
    {
54
        return $this->createdAt;
55
    }
56
57
    public function getUpdatedAt()
58
    {
59
        return $this->updatedAt;
60
    }
61
62
    public function getDeletedAt()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
63
    {
64
        return $this->deletedAt;
65
    }
66
67
    public function setDeletedAt($deletedAt)
68
    {
69
        $this->deletedAt = $deletedAt;
70
    }
71
72
    public function getDiscr()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
73
    {
74
        return $this->getRepository()->getClassMetadata()->discriminatorValue;
75
    }
76
77
    public function getDiscrName()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        return $this->getRepository()->getClassMetadata()->discriminatorColumn['name'];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 87 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
80
    }
81
82
    /**
83
     * Altera o campo updatedAt para forçar o persist da entity.
84
     */
85
    public function forcePersist()
86
    {
87
        $this->updatedAt = new DateTime();
88
89
        return $this;
90
    }
91
92
    /**
93
     * @ORM\PrePersist
94
     */
95
    public function prePersist()
96
    {
97
        $this->getRepository()
98
             ->preSave($this)
99
             ->validate($this);
100
    }
101
102
    /**
103
     * @ORM\PostPersist
104
     */
105
    public function postPersist()
106
    {
107
        $this->getRepository()
108
             ->postSave($this);
109
    }
110
111
    /**
112
     * @ORM\PreUpdate
113
     */
114
    public function preUpdate()
115
    {
116
        $this->getRepository()
117
             ->preSave($this)
118
             ->validate($this);
119
    }
120
121
    /**
122
     * @ORM\PostUpdate
123
     */
124
    public function postUpdate()
125
    {
126
        $this->getRepository()
127
             ->postSave($this);
128
    }
129
130
    /**
131
     * @ORM\PreFlush
132
     */
133
    public function preFlush()
134
    {
135
        $this->getRepository()
136
             ->preFlush($this);
137
    }
138
139
    public function getRepository()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
140
    {
141
        return EntityManager::getRepository(get_class($this));
142
    }
143
144
    public function remove()
145
    {
146
        $this->getRepository()->remove($this);
147
148
        return $this;
149
    }
150
151
    public function save($flush = false)
152
    {
153
        $this->getRepository()->save($this);
154
155
        return $this;
156
    }
157
158
    public function flush($all = true)
159
    {
160
        $this->getRepository()->flush($all ? null : $this);
161
162
        return $this;
163
    }
164
165
    public function getId()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
166
    {
167
        return $this->id;
168
    }
169
170
    protected function getFillable()
171
    {
172
        return ['id', 'createdAt', 'updatedAt', 'deletedAt'];
173
    }
174
175
    /**
176
     * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 96 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
177
     * É usado principalmente em $this->setPropertiesEntity e nos Controllers.
178
     * Este método não evita que uma propriedade seja alterada caso tenha seu método set().
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 91 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
179
     *
180
     * @return array
181
     */
182
    abstract public function getOnlyStore();
183
184
    /**
185
     * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 98 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
186
     * Por padrão retorna os mesmos valores de $this->getOnlyStore().
187
     * Este método pode ser sobrescrito nas classes filhas.
188
     * É usado principalmente em $this->setPropertiesEntity e nos Controllers.
189
     * Este método não evita que uma propriedade seja alterada caso tenha seu método set().
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 91 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
190
     *
191
     * @return array
192
     */
193
    public function getOnlyUpdate()
194
    {
195
        return $this->getOnlyStore();
196
    }
197
198 View Code Duplication
    final protected function checkOnyExceptInArray($key, array $options = null)
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...
199
    {
200
        if (
201
            $options
202
            &&
203
            (
204
                (isset($options['only']) && is_array($options['only']) && !in_array($key, $options['only']))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
205
                ||
206
                (isset($options['except']) && is_array($options['except']) && in_array($key, $options['except']))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
207
            )
208
        ) {
209
            return false;
210
        }
211
212
        return true;
213
    }
214
215
    public function toArray(array $options = null)
216
    {
217
        $classMetadata = $this->getRepository()->getClassMetadata();
218
        $array = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
219
220
        foreach ($this->getFillable() as $key) {
221
            $metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 105 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
222
223
            if ($this->checkOnyExceptInArray($key, $options)) {
224
                if (is_object($this->$key)) {
225
                    if ($this->$key instanceof DateTime) {
226
                        if ($this->$key) {
227
                            $dateFormat = 'Y-m-d';
228
229
                            if ($metaDataKey) {
230
                                switch ($metaDataKey['type']) {
231
                                    case 'datetime':
232
                                        $dateFormat = 'Y-m-d H:i:s';
233
                                        break;
234
235
                                    case 'time':
236
                                        $dateFormat = 'H:i:s';
237
                                        break;
238
239
                                    default:
240
                                        break;
241
                                }
242
                            }
243
                            $array[$key] = $this->$key->format($dateFormat);
244
                        }
245 View Code Duplication
                    } elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) {
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...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 118 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
246
                        $ids = [];
247
                        foreach ($this->$key->getValues() as $item) {
248
                            $ids[] = $item->getId();
249
                        }
250
                        $array[$key] = $ids;
251
                    } else {
252
                        $array[$key] = $this->$key->getId();
253
                    }
254
                } else {
255
                    if ($metaDataKey['type'] == 'decimal') {
256
                        $array[$key] = (float) $this->$key;
257
                    } else {
258
                        $array[$key] = $this->$key;
259
                    }
260
                }
261
            }
262
        }
263
264
        return $array;
265
    }
266
}
267