Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

EventListener/URLChooserLinkTypeSubscriber.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Form\EventListener;
4
5
use Kunstmaan\NodeBundle\Form\Type\URLChooserType;
6
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl;
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
/**
15
 * Class URLChooserLinkTypeSubscriber
16
 */
17
class URLChooserLinkTypeSubscriber implements EventSubscriberInterface
18
{
19
    /**
20
     * @return array
0 ignored issues
show
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...
21
     */
22
    public static function getSubscribedEvents()
23
    {
24
        return [
25
            FormEvents::POST_SUBMIT => 'postSubmit',
26
        ];
27
    }
28
29
    /**
30
     * When changing the link type, the form get's submitted with an ajax callback in the url_chooser.js;
31
     * We add the URL field only as an URL Chooser if it's an external link.
32
     */
33
    public function postSubmit(FormEvent $event)
34
    {
35
        // Suppress validation
36
        $event->stopPropagation();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\EventD...vent::stopPropagation() has been deprecated with message: since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
37
38
        $constraints = [];
39
        $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...
40
41
        $form = $event->getForm()->getParent();
42
        $linkType = $event->getData();
43
44
        if ($linkType) {
45
            $form->remove('link_url');
46
47 View Code Duplication
            switch ($linkType) {
0 ignored issues
show
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...
48
                case URLChooserType::INTERNAL:
49
                    $attributes['choose_url'] = true;
50
51
                    break;
52
                case URLChooserType::EXTERNAL:
53
                    $attributes['placeholder'] = 'https://';
54
                    $constraints[] = new ValidExternalUrl();
55
56
                    break;
57
                case URLChooserType::EMAIL:
58
                    $constraints[] = new Email();
59
60
                    break;
61
            }
62
63
            $form->add('link_url', TextType::class, [
64
                'label' => 'URL',
65
                'required' => true,
66
                'attr' => $attributes,
67
                'constraints' => $constraints,
68
                'error_bubbling' => true,
69
            ]);
70
        }
71
    }
72
}
73