Completed
Push — 1.x ( 8bcf66...54c417 )
by Marko
03:54
created

ListReader::findProperties()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Reader;
6
7
use KunicMarko\SonataAnnotationBundle\Annotation\ListAction;
8
use KunicMarko\SonataAnnotationBundle\Annotation\ListField;
9
use Sonata\AdminBundle\Datagrid\ListMapper;
10
11
/**
12
 * @author Marko Kunic <[email protected]>
13
 */
14
class ListReader
15
{
16
    use AnnotationReaderTrait;
17
18
    public function configureFields(\ReflectionClass $class, ListMapper $listMapper): void
19
    {
20
        foreach ($class->getProperties() as $property) {
21
            if ($annotation = $this->getPropertyAnnotation($property, ListField::class)) {
22
                $this->addField($property->getName(), $annotation, $listMapper);
23
            }
24
        }
25
26
        foreach ($class->getMethods() as $method) {
27
            if ($annotation = $this->getMethodAnnotation($method, ListField::class)) {
28
                $this->addField($property->getName(), $annotation, $listMapper);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $property seems to be defined by a foreach iteration on line 20. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
29
            }
30
        }
31
32
        if ($actions = $this->getListActions($this->getClassAnnotations($class))) {
33
            $listMapper->add('_action', null, [
34
                'actions' => $actions
35
            ]);
36
        }
37
    }
38
39
    private function addField(string $name, ListField $annotation, ListMapper $listMapper): void
40
    {
41
        if ($annotation->identifier) {
42
            $listMapper->addIdentifier($name, ...$annotation->getSettings());
0 ignored issues
show
Bug introduced by
$annotation->getSettings() is expanded, but the parameter $type of Sonata\AdminBundle\Datag...Mapper::addIdentifier() 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

42
            $listMapper->addIdentifier($name, /** @scrutinizer ignore-type */ ...$annotation->getSettings());
Loading history...
43
            return;
44
        }
45
46
        $listMapper->add($name, ...$annotation->getSettings());
0 ignored issues
show
Bug introduced by
$annotation->getSettings() is expanded, but the parameter $type of Sonata\AdminBundle\Datagrid\ListMapper::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

46
        $listMapper->add($name, /** @scrutinizer ignore-type */ ...$annotation->getSettings());
Loading history...
47
    }
48
49
    private function getListActions(array $annotations): array
50
    {
51
        $actions = [];
52
53
        foreach ($annotations as $annotation) {
54
            if ($annotation instanceof ListAction) {
55
                $actions[$annotation->name] = $annotation->options;
56
            }
57
58
        }
59
60
        return $actions;
61
    }
62
}
63