ListBuilder::fixFieldDescription()   F
last analyzed

Complexity

Conditions 15
Paths 490

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 2.4583
c 0
b 0
f 0
cc 15
nc 490
nop 2

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
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
19
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
20
use Sonata\AdminBundle\Builder\ListBuilderInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
23
class ListBuilder implements ListBuilderInterface
24
{
25
    protected $guesser;
26
27
    protected $templates = [];
28
29
    public function __construct(TypeGuesserInterface $guesser, array $templates = [])
30
    {
31
        $this->guesser = $guesser;
32
        $this->templates = $templates;
33
    }
34
35
    public function getBaseList(array $options = [])
36
    {
37
        return new FieldDescriptionCollection();
38
    }
39
40
    public function buildField($type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void
41
    {
42
        if (null === $type) {
43
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
44
            $fieldDescription->setType($guessType->getType());
45
        } else {
46
            $fieldDescription->setType($type);
47
        }
48
49
        $this->fixFieldDescription($admin, $fieldDescription);
50
    }
51
52
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void
53
    {
54
        $this->buildField($type, $fieldDescription, $admin);
55
        $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
56
57
        $list->add($fieldDescription);
58
    }
59
60
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
61
    {
62
        if ('_action' === $fieldDescription->getName() || 'actions' === $fieldDescription->getType()) {
63
            $this->buildActionFieldDescription($fieldDescription);
64
        }
65
66
        $fieldDescription->setAdmin($admin);
67
68
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
69
            [$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
0 ignored issues
show
Bug introduced by
The variable $metadata does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $lastPropertyName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $parentAssociationMappings does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
71
72
            // set the default field mapping
73
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
74
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
75
                if (false !== $fieldDescription->getOption('sortable')) {
76
                    $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true));
77
                    $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
78
                    $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
79
                }
80
            }
81
82
            // set the default association mapping
83
            if (isset($metadata->associationMappings[$lastPropertyName])) {
84
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
85
            }
86
87
            $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
88
        }
89
90
        if (!$fieldDescription->getType()) {
91
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
92
        }
93
94
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
95
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
96
97
        if (!$fieldDescription->getTemplate()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldDescription->getTemplate() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
98
            if ('id' === $fieldDescription->getType()) {
99
                $fieldDescription->setType('string');
100
            }
101
102
            if ('int' === $fieldDescription->getType()) {
103
                $fieldDescription->setType('integer');
104
            }
105
106
            $template = $this->getTemplate($fieldDescription->getType());
107
108
            if (null === $template) {
109
                if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
110
                    $template = '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig';
111
                } elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
112
                    $template = '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig';
113
                }
114
            }
115
116
            $fieldDescription->setTemplate($template);
117
        }
118
119
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
120
            $admin->attachAdminClass($fieldDescription);
121
        }
122
    }
123
124
    /**
125
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
126
     */
127
    public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
128
    {
129
        if (null === $fieldDescription->getTemplate()) {
130
            $fieldDescription->setTemplate('@SonataAdmin/CRUD/list__action.html.twig');
131
        }
132
133
        if (null === $fieldDescription->getType()) {
134
            $fieldDescription->setType('actions');
135
        }
136
137
        if (null === $fieldDescription->getOption('name')) {
138
            $fieldDescription->setOption('name', 'Action');
139
        }
140
141
        if (null === $fieldDescription->getOption('code')) {
142
            $fieldDescription->setOption('code', 'Action');
143
        }
144
145
        if (null !== $fieldDescription->getOption('actions')) {
146
            $actions = $fieldDescription->getOption('actions');
147
            foreach ($actions as $k => $action) {
148
                if (!isset($action['template'])) {
149
                    $actions[$k]['template'] = sprintf('@SonataAdmin/CRUD/list__action_%s.html.twig', $k);
150
                }
151
            }
152
153
            $fieldDescription->setOption('actions', $actions);
154
        }
155
156
        return $fieldDescription;
157
    }
158
159
    /**
160
     * @param int|string $type
161
     */
162
    private function getTemplate($type): ?string
163
    {
164
        if (!isset($this->templates[$type])) {
165
            return null;
166
        }
167
168
        return $this->templates[$type];
169
    }
170
}
171