Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

src/Eccube/Form/Extension/DoctrineOrmExtension.php (3 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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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 682
    public function __construct(EntityManagerInterface $em, Reader $reader)
47
    {
48 682
        $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 682
        $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 682
    public function buildForm(FormBuilderInterface $builder, array $options)
56
    {
57 682
        $builder->addEventListener(
58 682
            FormEvents::PRE_SET_DATA,
59 682
            function (FormEvent $event) {
60 682
                $form = $event->getForm();
61 682
                $config = $form->getConfig();
62
                // data_classオプションが必要
63 682
                $class = $config->getDataClass();
64 682
                if (is_null($class)) {
65 682
                    return;
66
                }
67
                // メタデータの取得
68
                try {
69 417
                    $meta = $this->em->getClassMetadata($class);
70 43
                } catch (\Exception $e) {
71 43
                    return;
72
                }
73
74
                /** @var \ReflectionProperty[] $props */
75 397
                $props = $meta->getReflectionProperties();
76 397
                foreach ($props as $prop) {
77 397
                    $anno = $this->reader->getPropertyAnnotation($prop, FormAppend::class);
0 ignored issues
show
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 397
                    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
                            'style_class' => $anno->style_class ? $anno->style_class : 'ec-select',
84
                        ];
85 397
                        if (!isset($form[$prop->getName()])) {
86
                            $form->add($prop->getName(), $anno->type, $options);
87
                        }
88
                    }
89 682
                }
90
            }
91
        );
92
    }
93 267
94
    public function buildView(FormView $view, FormInterface $form, array $options)
95 267
    {
96
        $options = $form->getConfig()->getOption('eccube_form_options');
97 267
98
        if (!array_key_exists('auto_render', $options)) {
99
            $options['auto_render'] = false;
100
        }
101 267
102
        if (!array_key_exists('form_theme', $options)) {
103
            $options['form_theme'] = null;
104
        }
105 267
106
        if (!array_key_exists('style_class', $options)) {
107
            $options['style_class'] = 'ec-select';
108 682
        }
109
110 682
        $view->vars['eccube_form_options'] = $options;
111 682
    }
112
113 682
    public function configureOptions(OptionsResolver $resolver)
114
    {
115
        $resolver->setDefault(
116
            'eccube_form_options',
117
            [
118
                'auto_render' => false,
119 683
                'form_theme' => null,
120
                'style_class' => 'ec-select',
121 683
            ]
122
        );
123
    }
124
125
    public function getExtendedType()
126
    {
127
        return FormType::class;
128
    }
129
}
130