Completed
Push — master ( f1d84d...653498 )
by Derek Stephen
03:51
created

User   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 36.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 25
loc 68
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 11 2
A append() 0 4 1
A current() 0 4 1
A findKey() 12 12 3
A findById() 13 13 3

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 Del\Collection;
4
5
use Del\Entity\User as UserEntity;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use LogicException;
8
9
class User extends ArrayCollection
10
{
11
    /**
12
     * @param UserEntity $user
13
     * @return $this
14
     */
15
    public function update(UserEntity $user)
16
    {
17
        $key = $this->findKey($user);
18
        if($key) {
19
20
            $this->offsetSet($key,$user);
21
            return $this;
22
23
        }
24
        throw new LogicException('User was not in the collection.');
25
    }
26
27
    /**
28
     * @param UserEntity $user
29
     */
30
    public function append(UserEntity $user)
31
    {
32
        $this->add($user);
33
    }
34
35
    /**
36
     * @return UserEntity|null
37
     */
38
    public function current()
39
    {
40
        return parent::current();
41
    }
42
43
    /**
44
     * @param UserEntity $user
45
     * @return bool|int
46
     */
47 View Code Duplication
    public function findKey(UserEntity $user)
48
    {
49
        $it = $this->getIterator();
50
        $it->rewind();
51
        while($it->valid()) {
52
            if($it->current()->getId() == $user->getId()) {
53
                return $it->key();
54
            }
55
            $it->next();
56
        }
57
        return false;
58
    }
59
60
61
62 View Code Duplication
    public function findById($id)
63
    {
64
        $it = $this->getIterator();
65
        $it->rewind();
66
        while($it->valid()) {
67
            if($it->current()->getId() == $id) {
68
                return $it->current();
69
            }
70
            $it->next();
71
        }
72
73
        return false;
74
    }
75
76
}