UserType   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 47
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 4 1
A buildForm() 0 32 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Created by PhpStorm.
6
 * User: Valery Maslov
7
 * Date: 24.08.2018
8
 * Time: 10:07.
9
 */
10
11
namespace App\Form\Type;
12
13
use App\Entity\User;
14
use Symfony\Component\Form\AbstractType;
15
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Form\Type\PasswordType. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Symfony\Component\Form\FormBuilderInterface;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
final class UserType extends AbstractType
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function buildForm(FormBuilderInterface $builder, array $options): void
27
    {
28
        $builder
29
            ->add(
30
                'roles', ChoiceType::class, [
31
                    'choices' => [
32
                        'label.roles.admin' => 'ROLE_ADMIN',
33
                    ],
34
                    'expanded' => true,
35
                    'multiple' => true,
36
                    'label' => false,
37
                    'label_attr' => ['class' => 'switch-custom'],
38
                ]
39
            )
40
            ->add('username', null, [
41
                'label' => 'label.username',
42
            ])
43
            ->add('profile', ProfileType::class)
44
            ->add('email', null, [
45
                'label' => 'label.email',
46
            ])
47
            ->add(
48
                'email_verified', CheckboxType::class, [
49
                    'label' => 'label.email_verified',
50
                    'label_attr' => ['class' => 'switch-custom'],
51
                    'mapped' => false,
52
                    'required' => false,
53
                    'data' => null !== $options['data']->getEmailVerifiedAt(),
54
                ]
55
            )
56
            ->add('password', PasswordType::class, [
57
                'label' => 'label.password',
58
            ]);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function configureOptions(OptionsResolver $resolver): void
65
    {
66
        $resolver->setDefaults([
67
            'data_class' => User::class,
68
        ]);
69
    }
70
}
71