Passed
Pull Request — master (#8)
by Derek Stephen
10:36
created

PersonCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 22
c 1
b 0
f 0
dl 0
loc 56
ccs 22
cts 22
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 3 1
A update() 0 11 2
A findKey() 0 14 3
A findById() 0 14 3
A append() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Person\Collection;
6
7
use Del\Person\Entity\Person as PersonEntity;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use LogicException;
10
11
class PersonCollection extends ArrayCollection
12
{
13
14
    public function update(PersonEntity $person): void
15 1
    {
16
        $key = $this->findKey($person);
17 1
18 1
        if($key) {
19 1
            $this->offsetSet($key,$person);
0 ignored issues
show
Bug introduced by
$key of type integer|true is incompatible with the type Doctrine\Common\Collections\TKey|null expected by parameter $offset of Doctrine\Common\Collecti...Collection::offsetSet(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

19
            $this->offsetSet(/** @scrutinizer ignore-type */ $key,$person);
Loading history...
20
21 1
            return;
22
        }
23 1
24
        throw new LogicException('Person was not in the collection.');
25
    }
26
27
    public function append(PersonEntity $person)
28
    {
29 4
        $this->add($person);
30
    }
31 4
32 4
    public function current(): ?PersonEntity
33
    {
34
        return parent::current();
35
    }
36
37 1
    public function findKey(PersonEntity $person): int|bool
38
    {
39 1
        $it = $this->getIterator();
40
        $it->rewind();
41
42
        while($it->valid()) {
43
            if($it->current()->getId() == $person->getId()) {
44
                return $it->key();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $it->key() could return the type double|null|string which is incompatible with the type-hinted return boolean|integer. Consider adding an additional type-check to rule them out.
Loading history...
45
            }
46 2
47
            $it->next();
48 2
        }
49 2
50 2
        return false;
51 2
    }
52 1
53
    public function findById($id): PersonEntity|bool
54 2
    {
55
        $it = $this->getIterator();
56 2
        $it->rewind();
57
58
        while($it->valid()) {
59
            if($it->current()->getId() == $id) {
60
                return $it->current();
61
            }
62
63 2
            $it->next();
64
        }
65 2
66 2
        return false;
67 2
    }
68
}
69