Test Failed
Push — master ( ff5680...8a5127 )
by Gerhard
30:14 queued 19:34
created

HtmlTagType   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 23.76 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 5
dl 24
loc 101
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C buildForm() 24 72 11
A configureOptions() 0 20 4
A getBlockPrefix() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: gseidel
5
 * Date: 26.01.18
6
 * Time: 16:34
7
 */
8
9
namespace Enhavo\Bundle\FormBundle\Form\Type;
10
11
use Symfony\Component\Form\AbstractType;
12
use Symfony\Component\Form\CallbackTransformer;
13
use Symfony\Component\Form\Exception\InvalidConfigurationException;
14
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
15
use Symfony\Component\Form\Extension\Core\Type\TextType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use Symfony\Component\OptionsResolver\Options;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
class HtmlTagType extends AbstractType
21
{
22 11
    public function buildForm(FormBuilderInterface $builder, array $options)
23
    {
24 11
        $builder->add('text', TextType::class);
25
26 11 View Code Duplication
        if (is_array($options['tag_choices'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27 7
            $choices = [];
28 7
            foreach($options['tag_choices'] as $option) {
29 7
                $choices[$option] = $option;
30
            }
31
32 7
            $builder->add('tag', ChoiceType::class, [
33 7
                'choices' => $choices,
34 7
                'placeholder' => $options['tag_placeholder'],
35
                'empty_data' => null,
36
            ]);
37
        }
38
39 11 View Code Duplication
        if (is_array($options['class_choices'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40 3
            $choices = [];
41 3
            foreach($options['class_choices'] as $option) {
42 3
                $choices[$option] = $option;
43
            }
44
45 3
            $builder->add('class', ChoiceType::class, [
46 3
                'choices' => $choices,
47 3
                'placeholder' => 'class_placeholder',
48
                'empty_data' => null,
49
            ]);
50
        }
51
52 11
        $builder->addModelTransformer(new CallbackTransformer(
53
            function ($original) use ($options) {
54 11
                if($original === null) {
55
                    return [
56 11
                        'text' => '',
57
                        'tag' => null
58
                    ];
59 9
                } else if (preg_match("#<(.*?) class=['\"](.*?)['\"]>(.*?)</(.*?)>#i", $original, $match)) {
60
                    return [
61 1
                        'text' => html_entity_decode($match[3]),
62 1
                        'tag' => $match[1],
63 1
                        'class' => $match[2]
64
                    ];
65 8
                } else if(preg_match("#<(.*?)>(.*?)</(.*?)>#i", $original, $match)) {
66
                    return [
67 1
                        'text' => html_entity_decode($match[2]),
68 1
                        'tag' => $match[1],
69 1
                        'class' => ''
70
                    ];
71
                }
72
73
                return [
74 7
                    'text' => html_entity_decode(strip_tags($original)),
75
                    'tag' => null,
76
                    'class' => null
77
                ];
78 11
            },
79
            function ($submitted) use ($options) {
80 9
                $tag = $submitted['tag'] ?? $options['tag'];
81 9
                $class = $submitted['class'] ?? $options['class'];
82 9
                $text = htmlentities($submitted['text']);
83
84 9
                if($tag !== null && $class !== null) {
85 2
                    return sprintf('<%s class="%s">%s</%s>', $tag, $class, $text,  $tag);
86 7
                } else if($tag !== null) {
87 5
                    return sprintf('<%s>%s</%s>', $tag, $text, $tag);
88
                }
89
90 2
                return $text;
91 11
            }
92
        ));
93 11
    }
94
95 13
    public function configureOptions(OptionsResolver $resolver)
96
    {
97 13
        $resolver->setDefaults([
98 13
            'compound' => true,
99
            'tag' => null,
100
            'tag_choices' => null,
101
            'tag_placeholder' => '---',
102
            'class' => null,
103
            'class_choices' => null,
104
            'class_placeholder' => '---',
105
        ]);
106
107
        $resolver->addNormalizer('tag', function (Options $options, $value) {
108 13
            if ($value === null && ($options['class'] !== null || $options['class_choices'] !== null)) {
109 2
                throw new InvalidConfigurationException('If option "class" or "class_choices" is configured, the "tag" options must be set as well');
110
            }
111
112 11
            return $value;
113 13
        });
114 13
    }
115
116 11
    public function getBlockPrefix()
117
    {
118 11
        return 'html_tag';
119
    }
120
}
121