Completed
Push — experimental/sf ( 62d5bb...ebcb49 )
by Ryo
935:43 queued 928:36
created

DoctrineOrmExtension::buildView()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
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 Eccube\Form\Extension;
15
16
use Doctrine\Common\Annotations\AnnotationReader;
17
use Doctrine\Common\Annotations\Reader;
18
use Doctrine\ORM\EntityManager;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Eccube\Annotation\FormAppend;
21
use Eccube\Annotation\FormExtension;
22
use Symfony\Component\Form\AbstractTypeExtension;
23
use Symfony\Component\Form\Extension\Core\Type\FormType;
24
use Symfony\Component\Form\FormBuilderInterface;
25
use Symfony\Component\Form\FormEvent;
26
use Symfony\Component\Form\FormEvents;
27
use Symfony\Component\Form\FormInterface;
28
use Symfony\Component\Form\FormView;
29
use Symfony\Component\OptionsResolver\OptionsResolver;
30
31
/**
32
 * @FormExtension
33
 */
34
class DoctrineOrmExtension extends AbstractTypeExtension
35
{
36
    /**
37
     * @var EntityManager
38
     */
39
    protected $em;
40
41
    /**
42
     * @var AnnotationReader
43
     */
44
    protected $reader;
45
46 674
    public function __construct(EntityManagerInterface $em, Reader $reader)
47
    {
48 674
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
$em is of type object<Doctrine\ORM\EntityManagerInterface>, but the property $em was declared to be of type object<Doctrine\ORM\EntityManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
49 674
        $this->reader = $reader;
0 ignored issues
show
Documentation Bug introduced by
$reader is of type object<Doctrine\Common\Annotations\Reader>, but the property $reader was declared to be of type object<Doctrine\Common\A...tions\AnnotationReader>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 674
    public function buildForm(FormBuilderInterface $builder, array $options)
56
    {
57 674
        $builder->addEventListener(
58 674
            FormEvents::PRE_SET_DATA,
59 674
            function (FormEvent $event) {
60 673
                $form = $event->getForm();
61 673
                $config = $form->getConfig();
62
                // data_classオプションが必要
63 673
                $class = $config->getDataClass();
64 673
                if (is_null($class)) {
65 673
                    return;
66
                }
67
                // メタデータの取得
68
                try {
69 404
                    $meta = $this->em->getClassMetadata($class);
70 44
                } catch (\Exception $e) {
71 44
                    return;
72
                }
73
74
                /** @var \ReflectionProperty[] $props */
75 384
                $props = $meta->getReflectionProperties();
76 384
                foreach ($props as $prop) {
77 384
                    $anno = $this->reader->getPropertyAnnotation($prop, FormAppend::class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $anno is correct as $this->reader->getProper...tion\FormAppend::class) (which targets Doctrine\Common\Annotati...getPropertyAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
78 384
                    if ($anno) {
79
                        $options = is_null($anno->options) ? [] : $anno->options;
80
                        $options['eccube_form_options'] = [
81
                            'auto_render' => (true === $anno->auto_render),
82
                            'form_theme' => $anno->form_theme,
83
                        ];
84
                        if (!isset($form[$prop->getName()])) {
85 384
                            $form->add($prop->getName(), $anno->type, $options);
86
                        }
87
                    }
88
                }
89 674
            }
90
        );
91
    }
92
93 261
    public function buildView(FormView $view, FormInterface $form, array $options)
94
    {
95 261
        $options = $form->getConfig()->getOption('eccube_form_options');
96
97 261
        if (!array_key_exists('auto_render', $options)) {
98
            $options['auto_render'] = false;
99
        }
100
101 261
        if (!array_key_exists('form_theme', $options)) {
102
            $options['form_theme'] = null;
103
        }
104
105 261
        $view->vars['eccube_form_options'] = $options;
106
    }
107
108 674
    public function configureOptions(OptionsResolver $resolver)
109
    {
110 674
        $resolver->setDefault(
111 674
            'eccube_form_options',
112
            [
113 674
                'auto_render' => false,
114
                'form_theme' => null,
115
            ]
116
        );
117
    }
118
119 675
    public function getExtendedType()
120
    {
121 675
        return FormType::class;
122
    }
123
}
124