ShowBuilder::fixFieldDescription()   C
last analyzed

Complexity

Conditions 12
Paths 175

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 6.3416
c 0
b 0
f 0
cc 12
nc 175
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\ShowBuilderInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
23
class ShowBuilder implements ShowBuilderInterface
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 addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
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
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
51
52
        $list->add($fieldDescription);
53
    }
54
55
    /**
56
     * The method defines the correct default settings for the provided FieldDescription.
57
     */
58
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
59
    {
60
        $fieldDescription->setAdmin($admin);
61
62
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
63
            [$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...
64
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
65
66
            // set the default field mapping
67
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
68
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
69
            }
70
71
            // set the default association mapping
72
            if (isset($metadata->associationMappings[$lastPropertyName])) {
73
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
74
            }
75
        }
76
77
        if (!$fieldDescription->getType()) {
78
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
79
        }
80
81
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
82
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
83
84
        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...
85
            if ('id' === $fieldDescription->getType()) {
86
                $fieldDescription->setType('string');
87
            }
88
89
            if ('int' === $fieldDescription->getType()) {
90
                $fieldDescription->setType('integer');
91
            }
92
93
            $template = $this->getTemplate($fieldDescription->getType());
94
95
            if (null === $template) {
96
                if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
97
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig';
98
                } elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
99
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig';
100
                }
101
            }
102
103
            $fieldDescription->setTemplate($template);
104
        }
105
106
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
107
            $admin->attachAdminClass($fieldDescription);
108
        }
109
    }
110
111
    /**
112
     * @param int|string $type
113
     */
114
    private function getTemplate($type): ?string
115
    {
116
        if (!isset($this->templates[$type])) {
117
            return null;
118
        }
119
120
        return $this->templates[$type];
121
    }
122
}
123