FieldCollection::removeByName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.9
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 19/11/2016
5
 * Time: 12:18
6
 */
7
8
namespace Del\Form\Collection;
9
10
use Del\Form\Field\FieldInterface;
11
12
class FieldCollection extends AbstractCollection implements CollectionInterface
13
{
14
    /**
15
     * @param $name
16
     * @return FieldInterface|null
17
     */
18 4
    public function findByName($name)
19
    {
20 4
        $this->rewind();
21 4
        while ($this->valid()) {
22
            /** @var FieldInterface $field */
23 3
            $field = $this->current();
24 3
            if ($field->getName() == $name) {
25 3
                return $field;
26
            }
27 1
            $this->next();
28
        }
29 1
        $this->rewind();
30 1
        return null;
31
    }
32
33
    /**
34
     * @param $name
35
     * @return bool
36
     */
37 1
    public function removeByName($name): bool
38
    {
39 1
        $this->rewind();
40
41 1
        while ($this->valid()) {
42 1
            $field = $this->current();
43
44 1
            if ($field->getName() == $name) {
45 1
                $key = $this->key();
46 1
                $this->offsetUnset($key);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type true; however, parameter $key of ArrayIterator::offsetUnset() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

46
                $this->offsetUnset(/** @scrutinizer ignore-type */ $key);
Loading history...
47 1
                $this->rewind();
48
49 1
                return true;
50
            }
51 1
            $this->next();
52
        }
53
54 1
        $this->rewind();
55
56 1
        return false;
57
    }
58
59
    /**
60
     * @return FieldInterface
61
     */
62 56
    public function current(): FieldInterface
63
    {
64 56
        return parent::current();
65
    }
66
}