Passed
Push — 1.x ( f66cf3...cff80d )
by Marko
02:21
created

FormReader::configureFields()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 42
rs 8.4444
c 0
b 0
f 0
cc 8
nc 11
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Reader;
6
7
use KunicMarko\SonataAnnotationBundle\Annotation\FormField;
8
use Sonata\AdminBundle\Form\FormMapper;
9
10
/**
11
 * @author Marko Kunic <[email protected]>
12
 */
13
final class FormReader
14
{
15
    use AnnotationReaderTrait;
16
17
    public function configureCreateFields(\ReflectionClass $class, FormMapper $formMapper): void
18
    {
19
        $this->configureFields($class, $formMapper, FormField::ACTION_EDIT);
20
    }
21
22
    private function configureFields(\ReflectionClass $class, FormMapper $formMapper, string $action): void
23
    {
24
        $propertiesWithPosition = [];
25
        $propertiesWithoutPosition = [];
26
27
        foreach ($class->getProperties() as $property) {
28
            foreach ($this->getPropertyAnnotations($property) as $annotation) {
29
                if (!$annotation instanceof FormField || $annotation->action === $action) {
30
                    continue;
31
                }
32
33
                if (!$annotation->hasPosition()) {
34
                    $propertiesWithoutPosition[] = [
35
                        'name' => $property->getName(),
36
                        'settings' => $annotation->getSettings(),
37
                    ];
38
39
                    continue;
40
                }
41
42
                if (\array_key_exists($annotation->position, $propertiesWithPosition)) {
43
                    throw new \InvalidArgumentException(sprintf(
44
                        'Position "%s" is already in use by "%s", try setting a different position for "%s".',
45
                        $annotation->position,
46
                        $propertiesWithPosition[$annotation->position]['name'],
47
                        $property->getName()
48
                    ));
49
                }
50
51
                $propertiesWithPosition[$annotation->position] = [
52
                    'name' => $property->getName(),
53
                    'settings' => $annotation->getSettings(),
54
                ];
55
            }
56
        }
57
58
        \ksort($propertiesWithPosition);
59
60
        $properties = \array_merge($propertiesWithPosition, $propertiesWithoutPosition);
61
62
        foreach ($properties as $property) {
63
            $formMapper->add($property['name'], ...$property['settings']);
0 ignored issues
show
Bug introduced by
$property['settings'] is expanded, but the parameter $type of Sonata\AdminBundle\Form\FormMapper::add() does not expect variable arguments. ( Ignorable by Annotation )

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

63
            $formMapper->add($property['name'], /** @scrutinizer ignore-type */ ...$property['settings']);
Loading history...
64
        }
65
    }
66
67
    public function configureEditFields(\ReflectionClass $class, FormMapper $formMapper): void
68
    {
69
        $this->configureFields($class, $formMapper, FormField::ACTION_CREATE);
70
    }
71
}
72