Passed
Pull Request — master (#259)
by Arnaud
08:22
created

AbstractField::configureDefaultOptions()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7.025

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 62
rs 8.2826
ccs 23
cts 25
cp 0.92
cc 7
nc 1
nop 1
crap 7.025

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
namespace LAG\AdminBundle\Field;
4
5
use Closure;
6
use LAG\AdminBundle\Exception\Exception;
7
use LAG\AdminBundle\Field\View\FieldView;
8
use LAG\AdminBundle\Field\View\View;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use function Symfony\Component\String\u;
12
13
abstract class AbstractField implements FieldInterface
14
{
15
    private string $name;
16
    private string $type;
17
    private array $options = [];
18 20
    private bool $frozen = false;
19
20 20
    public function __construct(string $name, string $type)
21 20
    {
22 20
        if (class_exists($type)) {
23
            $type = u($type)->afterLast('\\')->lower();
24 1
        }
25
        $this->name = $name;
26 1
        $this->type = $type;
27
    }
28 19
29
    public function configureDefaultOptions(OptionsResolver $resolver): void
30 19
    {
31 1
        $resolver
32
            ->setDefaults([
33 19
                'attr' => [],
34 19
                'header_attr' => [],
35 19
                'label' => null,
36
                'mapped' => false,
37 10
                'property_path' => $this->getName(),
38
                'template' => '@LAGAdmin/fields/auto.html.twig',
39 10
                'translation' => false, // Most of fields are values from database and should not be translated
40 10
                'translation_domain' => 'admin',
41 10
                'sortable' => true,
42 10
            ])
43 10
            ->setAllowedTypes('attr', ['array', 'null'])
44
            ->setAllowedTypes('header_attr', ['array', 'null'])
45
            ->setAllowedTypes('label', ['string', 'null', 'boolean'])
46
            ->setAllowedTypes('mapped', ['boolean'])
47 15
            ->setAllowedTypes('property_path', ['string', 'null'])
48
            ->setAllowedTypes('template', ['string'])
49 15
            ->setAllowedTypes('translation', ['boolean'])
50
            ->setAllowedTypes('translation_domain', ['string', 'null'])
51
            ->setAllowedTypes('sortable', ['boolean'])
52 10
            ->addNormalizer('attr', function (Options $options, $value) {
53
                if ($value === null) {
54 10
                    $value = [];
55
                }
56
57 15
                if (!key_exists('class', $value)) {
58
                    $value['class'] = '';
59 15
                }
60
                $value['class'] .= ' admin-field admin-field-'.$this->getType();
61
                $value['class'] = trim($value['class']);
62 17
63
                return $value;
64 17
            })
65
            ->addNormalizer('header_attr', function (Options $options, $value) {
66
                if ($value === null) {
67 14
                    $value = [];
68
                }
69 14
70 1
                if (!key_exists('class', $value)) {
71
                    $value['class'] = '';
72
                }
73 13
                $value['class'] .= ' admin-header admin-header-'.$this->getType();
74
                $value['class'] = trim($value['class']);
75
76
                return $value;
77
            })
78
            ->setNormalizer('mapped', function (Options $options, $mapped) {
79
                if (u($this->getName())->startsWith('_')) {
80
                    return true;
81
                }
82
83
                return $mapped;
84
            })
85
            ->setNormalizer('property_path', function (Options $options, $propertyPath) {
86
                if (u($this->getName())->startsWith('_')) {
87
                    return null;
88
                }
89
90
                return $propertyPath;
91
            })
92
        ;
93
    }
94
95
    public function configureOptions(OptionsResolver $resolver): void
96
    {
97
    }
98
99
    public function setOptions(array $options): void
100
    {
101
        if ($this->frozen) {
102
            throw new Exception('The options for the field "'.$this->name.'" have already been configured');
103
        }
104
        $this->options = $options;
105
        $this->frozen = true;
106
    }
107
108
    public function createView(): View
109
    {
110
        return new FieldView(
111
            $this->name,
112
            $this->getOption('template'),
113
            $this->getOptions(),
114
            $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...
115
        );
116
    }
117
118
    public function getParent(): ?string
119
    {
120
        return null;
121
    }
122
123
    public function getDataTransformer(): ?Closure
124
    {
125
        return null;
126
    }
127
128
    public function getName(): string
129
    {
130
        return $this->name;
131
    }
132
133
    public function getOptions(): array
134
    {
135
        return $this->options;
136
    }
137
138
    public function getOption(string $name)
139
    {
140
        if (!\array_key_exists($name, $this->options)) {
141
            throw new Exception('Invalid option "'.$name.'" for field "'.$this->name.'"');
142
        }
143
144
        return $this->options[$name];
145
    }
146
147
    public function getType(): string
148
    {
149
        return $this->type;
150
    }
151
}
152