Completed
Push — master ( 4c839e...4fda94 )
by Ruud
12:51 queued 10s
created

URLChooserFormSubscriber::formModifier()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 57

Duplication

Lines 14
Ratio 24.56 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 14
loc 57
ccs 0
cts 29
cp 0
rs 7.3826
c 0
b 0
f 0
cc 9
nc 7
nop 1
crap 90

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 Kunstmaan\NodeBundle\Form\EventListener;
4
5
use Kunstmaan\NodeBundle\Form\Type\URLChooserType;
6
use Kunstmaan\NodeBundle\Validation\URLValidator;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\Form\Extension\Core\Type\TextType;
9
use Symfony\Component\Form\FormEvent;
10
use Symfony\Component\Form\FormEvents;
11
use Symfony\Component\Validator\Constraints\Email;
12
use Symfony\Component\Validator\Constraints\Url;
13
14
class URLChooserFormSubscriber implements EventSubscriberInterface
15
{
16
    use URLValidator;
17
18
    public static function getSubscribedEvents()
19
    {
20
        return [
21
            FormEvents::POST_SET_DATA => 'postSetData',
22
            FormEvents::PRE_SUBMIT => 'preSubmit',
23
        ];
24
    }
25
26
    /**
27
     * When opening the form for the first time, check the type of URL and set the according fields.
28
     *
29
     * @param FormEvent $event
30
     */
31
    public function postSetData(FormEvent $event)
32
    {
33
        $this->formModifier($event);
34
    }
35
36
    public function preSubmit(FormEvent $event)
37
    {
38
        $this->formModifier($event);
39
    }
40
41
    private function formModifier(FormEvent $event)
42
    {
43
        $form = $event->getForm();
44
        $data = $form->getData();
45
46
        $constraints = [];
47
        $attributes['class'] = 'js-change-urlchooser';
0 ignored issues
show
Coding Style Comprehensibility introduced by Sander Goossens
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
48
49
        if (!empty($data) && $form->has('link_type')) {
50
            // Check if e-mail address
51
            if ($this->isEmailAddress($data)) {
52
                $form->get('link_type')->setData(URLChooserType::EMAIL);
53
                $constraints[] = new Email();
54
            } // Check if internal link
55
            elseif ($this->isInternalLink($data) || $this->isInternalMediaLink($data)) {
56
                $form->get('link_type')->setData(URLChooserType::INTERNAL);
57
                $attributes['choose_url'] = true;
58
            } // Else, it's an external link
59
            else {
60
                $form->get('link_type')->setData(URLChooserType::EXTERNAL);
61
                $constraints[] = new Url();
62
            }
63
        } else {
64
            $choices = $form->get('link_type')->getConfig()->getOption('choices');
65
            $firstOption = array_shift($choices);
66
67 View Code Duplication
            switch ($firstOption) {
0 ignored issues
show
Duplication introduced by Sander Goossens
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...
68
                case URLChooserType::INTERNAL:
69
                    $attributes['choose_url'] = true;
70
71
                    break;
72
                case URLChooserType::EXTERNAL:
73
                    $attributes['placeholder'] = 'https://';
74
                    $constraints[] = new Url();
75
76
                    break;
77
                case URLChooserType::EMAIL:
78
                    $constraints[] = new Email();
79
80
                    break;
81
            }
82
83
            $form->get('link_type')->setData($firstOption);
84
        }
85
86
        $form->add(
87
            'link_url',
88
            TextType::class,
89
            [
90
                'label' => 'URL',
91
                'required' => true,
92
                'attr' => $attributes,
93
                'constraints' => $constraints,
94
                'error_bubbling' => true,
95
            ]
96
        );
97
    }
98
}
99