FieldCollection::findByName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Form\Collection;
6
7
use Del\Form\Field\FieldInterface;
8
9
class FieldCollection extends AbstractCollection implements CollectionInterface
10
{
11
    /**
12
     * @param $name
13
     * @return FieldInterface|null
14
     */
15 5
    public function findByName($name)
16
    {
17 5
        $this->rewind();
18 5
        while ($this->valid()) {
19
            /** @var FieldInterface $field */
20 4
            $field = $this->current();
21 4
            if ($field->getName() == $name) {
22 4
                return $field;
23
            }
24 2
            $this->next();
25
        }
26 1
        $this->rewind();
27 1
        return null;
28
    }
29
30
    /**
31
     * @param $name
32
     * @return bool
33
     */
34 1
    public function removeByName($name): bool
35
    {
36 1
        $this->rewind();
37
38 1
        while ($this->valid()) {
39 1
            $field = $this->current();
40
41 1
            if ($field->getName() == $name) {
42 1
                $key = $this->key();
43 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

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