Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
34 | class BlockType extends AbstractType |
||
|
|||
35 | { |
||
36 | public $app; |
||
37 | |||
38 | 286 | public function __construct(\Silex\Application $app) |
|
39 | { |
||
40 | 286 | $this->app = $app; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 1 | public function buildForm(FormBuilderInterface $builder, array $options) |
|
47 | { |
||
48 | 1 | $app = $this->app; |
|
49 | |||
50 | $builder |
||
51 | ->add('name', 'text', array( |
||
52 | 1 | 'label' => 'ブロック名', |
|
53 | 1 | 'required' => true, |
|
54 | 'constraints' => array( |
||
55 | new Assert\NotBlank(), |
||
56 | new Assert\Length(array( |
||
57 | 1 | 'max' => $app['config']['stext_len'], |
|
58 | )) |
||
59 | 1 | ) |
|
60 | )) |
||
61 | ->add('file_name', 'text', array( |
||
62 | 1 | 'label' => 'ファイル名', |
|
63 | 1 | 'required' => true, |
|
64 | 'constraints' => array( |
||
65 | new Assert\NotBlank(), |
||
66 | new Assert\Length(array( |
||
67 | 1 | 'max' => $app['config']['stext_len'], |
|
68 | )), |
||
69 | new Assert\Regex(array( |
||
70 | 'pattern' => '/^[0-9a-zA-Z\/_]+$/', |
||
71 | )), |
||
72 | 1 | ) |
|
73 | )) |
||
74 | ->add('block_html', 'textarea', array( |
||
75 | 'label' => 'ブロックデータ', |
||
76 | 'mapped' => false, |
||
77 | 'required' => false, |
||
78 | 'constraints' => array() |
||
79 | )) |
||
80 | ->add('DeviceType', 'entity', array( |
||
81 | 'class' => 'Eccube\Entity\Master\DeviceType', |
||
82 | 'property' => 'id', |
||
83 | )) |
||
84 | 1 | ->add('id', 'hidden') |
|
85 | View Code Duplication | ->addEventListener(FormEvents::POST_SUBMIT, function($event) use ($app) { |
|
86 | $form = $event->getForm(); |
||
87 | $file_name = $form['file_name']->getData(); |
||
88 | $DeviceType = $form['DeviceType']->getData(); |
||
89 | $block_id = $form['id']->getData(); |
||
90 | |||
91 | $qb = $app['orm.em']->createQueryBuilder(); |
||
92 | $qb->select('b') |
||
93 | ->from('Eccube\\Entity\\Block', 'b') |
||
94 | ->where('b.file_name = :file_name') |
||
95 | ->setParameter('file_name', $file_name) |
||
96 | ->andWhere('b.DeviceType = :DeviceType') |
||
97 | ->setParameter('DeviceType', $DeviceType); |
||
98 | if (isset($block_id)) { |
||
99 | $qb |
||
100 | ->andWhere('b.id <> :block_id') |
||
101 | ->setParameter('block_id', $block_id); |
||
102 | } |
||
103 | |||
104 | $Block = $qb |
||
105 | ->getQuery() |
||
106 | ->getResult(); |
||
107 | if (count($Block) > 0) { |
||
108 | $form['file_name']->addError(new FormError('※ 同じファイル名のデータが存在しています。別のファイル名を入力してください。')); |
||
109 | } |
||
110 | }); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function setDefaultOptions(OptionsResolverInterface $resolver) |
||
117 | { |
||
118 | $resolver->setDefaults(array( |
||
119 | 'data_class' => 'Eccube\Entity\Block', |
||
120 | )); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function getName() |
||
130 | } |
||
131 |