TreeModelType::buildForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Form\Type;
15
16
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
17
use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
18
use Symfony\Component\Form\AbstractType;
19
use Symfony\Component\Form\FormBuilderInterface;
20
use Symfony\Component\Form\FormInterface;
21
use Symfony\Component\Form\FormView;
22
use Symfony\Component\OptionsResolver\Options;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
25
26
class TreeModelType extends AbstractType
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $defaults = [];
32
33
    public function setDefaults(array $defaults): void
34
    {
35
        $this->defaults = $defaults;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function buildForm(FormBuilderInterface $builder, array $options): void
42
    {
43
        $builder->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true);
44
        $builder->setAttribute('root_node', $options['root_node']);
45
        $builder->setAttribute('select_root_node', $options['select_root_node']);
46
        $builder->setAttribute('repository_name', $options['repository_name']);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function buildView(FormView $view, FormInterface $form, array $options): void
53
    {
54
        $view->vars['root_node'] = $form->getConfig()->getAttribute('root_node');
55
        $view->vars['select_root_node'] = $form->getConfig()->getAttribute('select_root_node');
56
        $view->vars['repository_name'] = $form->getConfig()->getAttribute('repository_name');
57
        $view->vars['routing_defaults'] = $this->defaults;
58
    }
59
60
    /**
61
     * NEXT_MAJOR: remove this method.
62
     */
63
    public function setDefaultOptions(OptionsResolverInterface $resolver): void
64
    {
65
        $this->configureOptions($resolver);
0 ignored issues
show
Documentation introduced by
$resolver is of type object<Symfony\Component...tionsResolverInterface>, but the function expects a object<Symfony\Component...solver\OptionsResolver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function configureOptions(OptionsResolver $resolver): void
72
    {
73
        $resolver->setDefaults([
74
            'template' => 'doctrine_phpcr_odm_tree',
75
            'compound' => false,
76
            'model_manager' => null,
77
            'class' => null,
78
            'property' => null,
79
            'query' => null,
80
            'choices' => null,
81
            'root_node' => '/',
82
            'select_root_node' => false,
83
            'parent' => 'choice',
84
            'repository_name' => 'default',
85
            'preferred_choices' => [],
86
            'choice_list' => static function (Options $options, $previousValue) {
0 ignored issues
show
Unused Code introduced by
The parameter $previousValue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
                return new ModelChoiceList(
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList has been deprecated with message: since sonata-project/admin-bundle 3.24, to be removed in 4.0. Use Sonata\AdminBundle\ModelChoiceLoader instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
88
                    $options['model_manager'],
89
                    $options['class'],
90
                    $options['property'],
91
                    $options['query'],
92
                    $options['choices']
93
                );
94
            },
95
        ]);
96
    }
97
98
    /**
99
     * NEXT_MAJOR: remove this method.
100
     */
101
    public function getName()
102
    {
103
        return $this->getBlockPrefix();
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getBlockPrefix()
110
    {
111
        return 'doctrine_phpcr_odm_tree';
112
    }
113
}
114