Passed
Push — develop ( 355f63...51016a )
by Mathieu
02:23
created

ExportReader::getFields()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 34
rs 8.6666
c 0
b 0
f 0
cc 7
nc 24
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\Reader;
6
7
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportAssociationField;
8
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportField;
9
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportFormats;
10
use ReflectionClass;
11
use ReflectionMethod;
12
use ReflectionProperty;
13
14
/**
15
 * Export configuration reader.
16
 *
17
 * @author Marko Kunic <[email protected]>
18
 * @author Mathieu Wambre <[email protected]>
19
 */
20
final class ExportReader extends AbstractReader
21
{
22
23
    /**
24
     * Get exported fields.
25
     *
26
     * @param ReflectionClass $class Entity class.
27
     *
28
     * @return array<string, string> Label => property/method name list.
29
     */
30
    public function getFields(ReflectionClass $class): array
31
    {
32
        $fields = [];
33
        $allFields = [];
34
35
        foreach ($class->getProperties() as $property) {
36
            foreach ($this->getPropertyAnnotations($property) as $annotation) {
37
                $this->stackExportProperty(
38
                    $property,
39
                    new ExportField(),
40
                    $allFields
41
                );
42
                if ($annotation instanceof ExportField) {
43
                    $this->stackExportProperty($property, $annotation, $fields);
44
                }
45
            }
46
        }
47
48
        foreach ($class->getMethods() as $method) {
49
            $this->stackExportProperty(
50
                $property,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $property seems to be defined by a foreach iteration on line 35. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
51
                new ExportField(),
52
                $allFields
53
            );
54
55
            if ($annotation = $this->getMethodAnnotation(
56
                $method,
57
                ExportField::class
58
            )) {
59
                $this->stackExportProperty($method, $annotation, $fields);
60
            }
61
        }
62
63
        return empty($fields) ? $allFields : $fields;
64
    }
65
66
    /**
67
     * Get list of extract formats.
68
     *
69
     * @param ReflectionClass $class Entity class.
70
     *
71
     * @return array
72
     */
73
    public function getFormats(ReflectionClass $class): array
74
    {
75
        /** @var ExportFormats|null $annotation */
76
        if ($annotation = $this->getClassAnnotation(
77
            $class,
78
            ExportFormats::class
79
        )) {
80
            return $annotation->formats;
0 ignored issues
show
Bug Best Practice introduced by
The property $formats is declared private in Neimheadh\SonataAnnotati...on\Sonata\ExportFormats. Since you implement __get, consider adding a @property or @property-read.
Loading history...
81
        }
82
83
        return [];
84
    }
85
86
    /**
87
     * Stack property or method to exported fields.
88
     *
89
     * @param ReflectionProperty|ReflectionMethod $property Stacked property.
90
     * @param object                              $annotation Annotation.
91
     * @param array                               $fields Fields stack.
92
     *
93
     * @return void
94
     */
95
    private function stackExportProperty(
96
        $property,
97
        object $annotation,
98
        array &$fields
99
    ): void {
100
        if ($annotation instanceof ExportAssociationField) {
101
            $fieldName = $property->getName()
102
                . '.'
103
                . $annotation->field;
104
105
            $fields[$annotation->label ?: $fieldName] = $fieldName;
106
107
            return;
108
        }
109
110
        $label = $annotation->label ?: $property->getName();
111
        $fields[$label] = $property->getName();
112
    }
113
114
}
115