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

URLChooserLinkTypeSubscriber   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 25.86 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 15
loc 58
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
B postSubmit() 15 39 5

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
namespace Kunstmaan\NodeBundle\Form\EventListener;
4
5
use Kunstmaan\NodeBundle\Form\Type\URLChooserType;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\Form\Extension\Core\Type\TextType;
8
use Symfony\Component\Form\FormEvent;
9
use Symfony\Component\Form\FormEvents;
10
use Symfony\Component\Validator\Constraints\Email;
11
use Symfony\Component\Validator\Constraints\Url;
12
13
/**
14
 * Class URLChooserLinkTypeSubscriber
15
 */
16
class URLChooserLinkTypeSubscriber implements EventSubscriberInterface
17
{
18
    /**
19
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<*,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
20
     */
21
    public static function getSubscribedEvents()
22
    {
23
        return array(
24
            FormEvents::POST_SUBMIT => 'postSubmit',
25
        );
26
    }
27
28
    /**
29
     * When changing the link type, the form get's submitted with an ajax callback in the url_chooser.js;
30
     * We add the URL field only as an URL Chooser if it's an external link.
31
     *
32
     * @param FormEvent $event
33
     */
34
    public function postSubmit(FormEvent $event)
35
    {
36
        // Suppress validation
37
        $event->stopPropagation();
38
39
        $constraints = [];
40
        $attributes['class'] = 'js-change-urlchooser';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$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...
41
42
        $form = $event->getForm()->getParent();
43
        $linkType = $event->getData();
44
45
        if ($linkType) {
46
            $form->remove('link_url');
47
48 View Code Duplication
            switch ($linkType) {
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...
49
                case URLChooserType::INTERNAL:
50
                    $attributes['choose_url'] = true;
51
52
                    break;
53
                case URLChooserType::EXTERNAL:
54
                    $attributes['placeholder'] = 'https://';
55
                    $constraints[] = new Url();
56
57
                    break;
58
                case URLChooserType::EMAIL:
59
                    $constraints[] = new Email();
60
61
                    break;
62
            }
63
64
            $form->add('link_url', TextType::class, array(
65
                'label' => 'URL',
66
                'required' => true,
67
                'attr' => $attributes,
68
                'constraints' => $constraints,
69
                'error_bubbling' => true,
70
            ));
71
        }
72
    }
73
}
74