Passed
Pull Request — master (#259)
by Arnaud
07:09 queued 02:15
created

AbstractField::configureDefaultOptions()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.0283

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 62
ccs 22
cts 24
cp 0.9167
rs 8.2826
c 0
b 0
f 0
cc 7
nc 1
nop 1
crap 7.0283

How to fix   Long Method   

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 LAG\AdminBundle\Field;
6
7
use Closure;
8
use LAG\AdminBundle\Exception\Exception;
9
use LAG\AdminBundle\Field\View\FieldView;
10
use LAG\AdminBundle\Field\View\View;
11
use Symfony\Component\OptionsResolver\Options;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
use function Symfony\Component\String\u;
14
15
abstract class AbstractField implements FieldInterface
16
{
17
    private string $name;
18 20
    private string $type;
19
    private array $options = [];
20 20
    private bool $frozen = false;
21 20
22 20
    public function __construct(string $name, string $type)
23
    {
24 1
        if (class_exists($type)) {
25
            $type = u($type)->afterLast('\\')->lower();
26 1
        }
27
        $this->name = $name;
28 19
        $this->type = $type;
29
    }
30 19
31 1
    public function configureDefaultOptions(OptionsResolver $resolver): void
32
    {
33 19
        $resolver
34 19
            ->setDefaults([
35 19
                'attr' => [],
36
                'header_attr' => [],
37 10
                'label' => null,
38
                'mapped' => false,
39 10
                'property_path' => $this->getName(),
40 10
                'template' => '@LAGAdmin/fields/auto.html.twig',
41 10
                'translation' => false, // Most of fields are values from database and should not be translated
42 10
                'translation_domain' => 'admin',
43 10
                'sortable' => true,
44
            ])
45
            ->setAllowedTypes('attr', ['array', 'null'])
46
            ->setAllowedTypes('header_attr', ['array', 'null'])
47 15
            ->setAllowedTypes('label', ['string', 'null', 'boolean'])
48
            ->setAllowedTypes('mapped', ['boolean'])
49 15
            ->setAllowedTypes('property_path', ['string', 'null'])
50
            ->setAllowedTypes('template', ['string'])
51
            ->setAllowedTypes('translation', ['boolean'])
52 10
            ->setAllowedTypes('translation_domain', ['string', 'null'])
53
            ->setAllowedTypes('sortable', ['boolean'])
54 10
            ->addNormalizer('attr', function (Options $options, $value) {
55
                if ($value === null) {
56
                    $value = [];
57 15
                }
58
59 15
                if (!\array_key_exists('class', $value)) {
60
                    $value['class'] = '';
61
                }
62 17
                $value['class'] .= ' admin-field admin-field-'.$this->getType();
63
                $value['class'] = trim($value['class']);
64 17
65
                return $value;
66
            })
67 14
            ->addNormalizer('header_attr', function (Options $options, $value) {
68
                if ($value === null) {
69 14
                    $value = [];
70 1
                }
71
72
                if (!\array_key_exists('class', $value)) {
73 13
                    $value['class'] = '';
74
                }
75
                $value['class'] .= ' admin-header admin-header-'.$this->getType();
76
                $value['class'] = trim($value['class']);
77
78
                return $value;
79
            })
80
            ->setNormalizer('mapped', function (Options $options, $mapped) {
81
                if (u($this->getName())->startsWith('_')) {
82
                    return true;
83
                }
84
85
                return $mapped;
86
            })
87
            ->setNormalizer('property_path', function (Options $options, $propertyPath) {
88
                if (u($this->getName())->startsWith('_')) {
89
                    return null;
90
                }
91
92
                return $propertyPath;
93
            })
94
        ;
95
    }
96
97
    public function configureOptions(OptionsResolver $resolver): void
98
    {
99
    }
100
101
    public function setOptions(array $options): void
102
    {
103
        if ($this->frozen) {
104
            throw new Exception('The options for the field "'.$this->name.'" have already been configured');
105
        }
106
        $this->options = $options;
107
        $this->frozen = true;
108
    }
109
110
    public function createView(): View
111
    {
112
        return new FieldView(
113
            $this->name,
114
            $this->getOption('template'),
115
            $this->getOptions(),
116
            $this->getDataTransformer()
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getDataTransformer() targeting LAG\AdminBundle\Field\Ab...d::getDataTransformer() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
117
        );
118
    }
119
120
    public function getParent(): ?string
121
    {
122
        return null;
123
    }
124
125
    public function getDataTransformer(): ?Closure
126
    {
127
        return null;
128
    }
129
130
    public function getName(): string
131
    {
132
        return $this->name;
133
    }
134
135
    public function getOptions(): array
136
    {
137
        return $this->options;
138
    }
139
140
    public function getOption(string $name)
141
    {
142
        if (!\array_key_exists($name, $this->options)) {
143
            throw new Exception('Invalid option "'.$name.'" for field "'.$this->name.'"');
144
        }
145
146
        return $this->options[$name];
147
    }
148
149
    public function getType(): string
150
    {
151
        return $this->type;
152
    }
153
}
154