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 | 663 | public function __construct(\Silex\Application $app) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 4 | public function buildForm(FormBuilderInterface $builder, array $options) |
|
47 | { |
||
48 | 4 | $app = $this->app; |
|
49 | |||
50 | $builder |
||
51 | 4 | ->add('name', 'text', array( |
|
52 | 4 | 'label' => 'ブロック名', |
|
53 | 'required' => true, |
||
54 | 'constraints' => array( |
||
55 | 4 | new Assert\NotBlank(), |
|
56 | 4 | new Assert\Length(array( |
|
57 | 4 | 'max' => $app['config']['stext_len'], |
|
58 | )) |
||
59 | ) |
||
60 | )) |
||
61 | 4 | ->add('file_name', 'text', array( |
|
62 | 4 | 'label' => 'ファイル名', |
|
63 | 'required' => true, |
||
64 | 'constraints' => array( |
||
65 | 4 | new Assert\NotBlank(), |
|
66 | 4 | new Assert\Length(array( |
|
67 | 4 | 'max' => $app['config']['stext_len'], |
|
68 | )), |
||
69 | 4 | new Assert\Regex(array( |
|
70 | 4 | 'pattern' => '/^[0-9a-zA-Z\/_]+$/', |
|
71 | )), |
||
72 | ) |
||
73 | )) |
||
74 | 4 | ->add('block_html', 'textarea', array( |
|
75 | 4 | 'label' => 'ブロックデータ', |
|
76 | 'mapped' => false, |
||
77 | 'required' => false, |
||
78 | 'constraints' => array() |
||
79 | )) |
||
80 | 4 | ->add('DeviceType', 'entity', array( |
|
81 | 4 | 'class' => 'Eccube\Entity\Master\DeviceType', |
|
82 | 'property' => 'id', |
||
83 | )) |
||
84 | 4 | ->add('id', 'hidden') |
|
85 | 4 | View Code Duplication | ->addEventListener(FormEvents::POST_SUBMIT, function($event) use ($app) { |
86 | 2 | $form = $event->getForm(); |
|
87 | 2 | $file_name = $form['file_name']->getData(); |
|
88 | 2 | $DeviceType = $form['DeviceType']->getData(); |
|
89 | 2 | $block_id = $form['id']->getData(); |
|
90 | |||
91 | 2 | $qb = $app['orm.em']->createQueryBuilder(); |
|
92 | 2 | $qb->select('b') |
|
93 | 2 | ->from('Eccube\\Entity\\Block', 'b') |
|
94 | 2 | ->where('b.file_name = :file_name') |
|
95 | 2 | ->setParameter('file_name', $file_name) |
|
96 | 2 | ->andWhere('b.DeviceType = :DeviceType') |
|
97 | 2 | ->setParameter('DeviceType', $DeviceType); |
|
98 | 2 | if (isset($block_id)) { |
|
99 | $qb |
||
100 | 2 | ->andWhere('b.id <> :block_id') |
|
101 | 2 | ->setParameter('block_id', $block_id); |
|
102 | } |
||
103 | |||
104 | $Block = $qb |
||
105 | 2 | ->getQuery() |
|
106 | 2 | ->getResult(); |
|
107 | 2 | if (count($Block) > 0) { |
|
108 | $form['file_name']->addError(new FormError('※ 同じファイル名のデータが存在しています。別のファイル名を入力してください。')); |
||
109 | } |
||
110 | 4 | }); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 4 | public function setDefaultOptions(OptionsResolverInterface $resolver) |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 663 | public function getName() |
|
130 | } |
||
131 |