Completed
Pull Request — develop (#607)
by Tom
03:12 queued 01:40
created

AnnotationBuilder::getFormSpecification()   C

Complexity

Conditions 12
Paths 23

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 0
cts 31
cp 0
rs 6.5333
c 0
b 0
f 0
cc 12
nc 23
nop 1
crap 156

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Form\Annotation;
6
7
use ArrayObject;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
9
use Doctrine\Common\Persistence\ObjectManager;
10
use DoctrineModule\Form\Element;
11
use Laminas\EventManager\EventManagerInterface;
12
use Laminas\Form\Annotation\AnnotationBuilder as LaminasAnnotationBuilder;
13
use function get_class;
14
use function in_array;
15
use function is_object;
16
17
class AnnotationBuilder extends LaminasAnnotationBuilder
18
{
19
    public const EVENT_CONFIGURE_FIELD       = 'configureField';
20
    public const EVENT_CONFIGURE_ASSOCIATION = 'configureAssociation';
21
    public const EVENT_EXCLUDE_FIELD         = 'excludeField';
22
    public const EVENT_EXCLUDE_ASSOCIATION   = 'excludeAssociation';
23
24
    /** @var ObjectManager */
25
    protected $objectManager;
26
27
    /**
28
     * Constructor. Ensures ObjectManager is present.
29
     */
30
    public function __construct(ObjectManager $objectManager)
31
    {
32
        $this->objectManager = $objectManager;
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function setEventManager(EventManagerInterface $events)
39
    {
40
        parent::setEventManager($events);
41
42
        (new ElementAnnotationsListener($this->objectManager))->attach($this->getEventManager());
43
44
        return $this;
45
    }
46
47
    /**
48
     * Overrides the base getFormSpecification() to additionally iterate through each
49
     * field/association in the metadata and trigger the associated event.
50
     *
51
     * This allows building of a form from metadata instead of requiring annotations.
52
     * Annotations are still allowed through the ElementAnnotationsListener.
53
     *
54
     * {@inheritDoc}
55
     */
56
    public function getFormSpecification($entity)
57
    {
58
        $formSpec    = parent::getFormSpecification($entity);
59
        $metadata    = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity);
60
        $inputFilter = $formSpec['input_filter'];
61
62
        $formElements = [
63
            Element\ObjectSelect::class,
64
            Element\ObjectMultiCheckbox::class,
65
            Element\ObjectRadio::class,
66
        ];
67
68
        foreach ($formSpec['elements'] as $key => $elementSpec) {
69
            $name          = $elementSpec['spec']['name'] ?? null;
70
            $isFormElement = (isset($elementSpec['spec']['type']) &&
71
                              in_array($elementSpec['spec']['type'], $formElements));
72
73
            if (! $name) {
74
                continue;
75
            }
76
77
            if (! isset($inputFilter[$name])) {
78
                $inputFilter[$name] = new ArrayObject();
79
            }
80
81
            $params = [
82
                'metadata'    => $metadata,
83
                'name'        => $name,
84
                'elementSpec' => $elementSpec,
85
                'inputSpec'   => $inputFilter[$name],
86
            ];
87
88
            if ($this->checkForExcludeElementFromMetadata($metadata, $name)) {
0 ignored issues
show
Compatibility introduced by
$metadata of type object<Doctrine\Persiste...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\Common\P...\Mapping\ClassMetadata>. It seems like you assume a child interface of the interface Doctrine\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
89
                $elementSpec = $formSpec['elements'];
90
                unset($elementSpec[$key]);
91
                $formSpec['elements'] = $elementSpec;
92
93
                if (isset($inputFilter[$name])) {
94
                    unset($inputFilter[$name]);
95
                }
96
97
                $formSpec['input_filter'] = $inputFilter;
98
                continue;
99
            }
100
101
            if ($metadata->hasField($name) || (! $metadata->hasAssociation($name) && $isFormElement)) {
102
                $this->getEventManager()->trigger(self::EVENT_CONFIGURE_FIELD, $this, $params);
103
            } elseif ($metadata->hasAssociation($name)) {
104
                $this->getEventManager()->trigger(self::EVENT_CONFIGURE_ASSOCIATION, $this, $params);
105
            }
106
        }
107
108
        $formSpec['options'] = ['prefer_form_input_filter' => true];
109
110
        return $formSpec;
111
    }
112
113
    protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, string $name) : bool
114
    {
115
        $params = ['metadata' => $metadata, 'name' => $name];
116
        $result = false;
117
118
        if ($metadata->hasField($name)) {
119
            $result = $this->getEventManager()->trigger(self::EVENT_EXCLUDE_FIELD, $this, $params);
120
        } elseif ($metadata->hasAssociation($name)) {
121
            $result = $this->getEventManager()->trigger(self::EVENT_EXCLUDE_ASSOCIATION, $this, $params);
122
        }
123
124
        if ($result) {
125
            $result = (bool) $result->last();
126
        }
127
128
        return $result;
129
    }
130
}
131