Completed
Push — 1.x ( 815042...ac7482 )
by
unknown
06:00
created

DatagridReader::configureFields()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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

66
                $property['name'], /** @scrutinizer ignore-type */ ...$property['annotation']->getSettings()
Loading history...
67
            );
68
        }
69
    }
70
}
71