FormBuilderIterator::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Util;
15
16
use Symfony\Component\Form\FormBuilderInterface;
17
18
/**
19
 * @author Thomas Rabaix <[email protected]>
20
 */
21
final class FormBuilderIterator extends \RecursiveArrayIterator
22
{
23
    /**
24
     * @var \ReflectionProperty
25
     */
26
    private static $reflection;
27
28
    /**
29
     * @var FormBuilderInterface
30
     */
31
    private $formBuilder;
32
33
    /**
34
     * @var array
35
     */
36
    private $keys = [];
37
38
    /**
39
     * @var bool|string
40
     */
41
    private $prefix;
42
43
    /**
44
     * @var \ArrayIterator
45
     */
46
    private $iterator;
47
48
    /**
49
     * @param bool $prefix
50
     */
51
    public function __construct(FormBuilderInterface $formBuilder, $prefix = false)
52
    {
53
        parent::__construct();
54
        $this->formBuilder = $formBuilder;
55
        $this->prefix = $prefix ? $prefix : $formBuilder->getName();
56
        $this->iterator = new \ArrayIterator(self::getKeys($formBuilder));
57
    }
58
59
    public function rewind(): void
60
    {
61
        $this->iterator->rewind();
62
    }
63
64
    public function valid()
65
    {
66
        return $this->iterator->valid();
67
    }
68
69
    public function key()
70
    {
71
        $name = $this->iterator->current();
72
73
        return sprintf('%s_%s', $this->prefix, $name);
74
    }
75
76
    public function next(): void
77
    {
78
        $this->iterator->next();
79
    }
80
81
    public function current()
82
    {
83
        return $this->formBuilder->get($this->iterator->current());
84
    }
85
86
    public function getChildren()
87
    {
88
        return new self($this->formBuilder->get($this->iterator->current()), $this->current());
0 ignored issues
show
Documentation introduced by
$this->current() is of type object<Symfony\Component...m\FormBuilderInterface>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
    }
90
91
    public function hasChildren()
92
    {
93
        return \count(self::getKeys($this->current())) > 0;
94
    }
95
96
    /**
97
     * @static
98
     *
99
     * @return array
100
     */
101
    private static function getKeys(FormBuilderInterface $formBuilder)
102
    {
103
        return array_keys($formBuilder->all());
104
    }
105
}
106