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 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 | |||
11 | /** |
||
12 | * Class URLChooserLinkTypeSubscriber |
||
13 | */ |
||
14 | class URLChooserLinkTypeSubscriber implements EventSubscriberInterface |
||
15 | { |
||
16 | /** |
||
17 | * @return array |
||
18 | */ |
||
19 | public static function getSubscribedEvents() |
||
20 | { |
||
21 | return array( |
||
22 | FormEvents::POST_SUBMIT => 'postSubmit', |
||
23 | ); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * When changing the link type, the form get's submitted with an ajax callback in the url_chooser.js; |
||
28 | * We add the URL field only as an URL Chooser if it's an external link. |
||
29 | * |
||
30 | * @param FormEvent $event |
||
31 | */ |
||
32 | public function postSubmit(FormEvent $event) |
||
33 | { |
||
34 | // Suppress validation |
||
35 | $event->stopPropagation(); |
||
36 | |||
37 | $attributes['class'] = 'js-change-urlchooser'; |
||
0 ignored issues
–
show
|
|||
38 | |||
39 | $form = $event->getForm()->getParent(); |
||
40 | $linkType = $event->getData(); |
||
41 | |||
42 | if ($linkType) { |
||
43 | $form->remove('link_url'); |
||
44 | |||
45 | View Code Duplication | switch ($linkType) { |
|
46 | case URLChooserType::INTERNAL: |
||
47 | $attributes['choose_url'] = true; |
||
48 | |||
49 | break; |
||
50 | case URLChooserType::EXTERNAL: |
||
51 | $attributes['placeholder'] = 'https://'; |
||
52 | |||
53 | break; |
||
54 | } |
||
55 | |||
56 | $form->add('link_url', TextType::class, array( |
||
57 | 'label' => 'URL', |
||
58 | 'required' => true, |
||
59 | 'attr' => $attributes, |
||
60 | )); |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 |
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:
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 thebar
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.