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 |
||
33 | class SecurityType extends AbstractType |
||
|
|||
34 | { |
||
35 | private $app; |
||
36 | private $config; |
||
37 | |||
38 | 663 | public function __construct($app) |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 7 | public function buildForm(FormBuilderInterface $builder, array $options) |
|
48 | { |
||
49 | 7 | $app = $this->app; |
|
50 | $builder |
||
51 | 7 | ->add('admin_route_dir', 'text', array( |
|
52 | 7 | 'label' => 'ディレクトリ名', |
|
53 | 'constraints' => array( |
||
54 | 7 | new Assert\NotBlank(), |
|
55 | 7 | new Assert\Length(array('max' => $this->config['stext_len'])), |
|
56 | 7 | new Assert\Regex(array( |
|
57 | 7 | 'pattern' => "/^[0-9a-zA-Z]+$/", |
|
58 | )), |
||
59 | ), |
||
60 | )) |
||
61 | 7 | ->add('admin_allow_host', 'textarea', array( |
|
62 | 7 | 'required' => false, |
|
63 | 7 | 'label' => 'IP制限', |
|
64 | 'constraints' => array( |
||
65 | 7 | new Assert\Length(array('max' => $this->config['stext_len'])), |
|
66 | ), |
||
67 | )) |
||
68 | 7 | ->add('force_ssl', 'checkbox', array( |
|
69 | 7 | 'label' => 'SSLを強制', |
|
70 | 'required' => false, |
||
71 | )) |
||
72 | 7 | View Code Duplication | ->addEventListener(FormEvents::POST_SUBMIT, function ($event) use($app) { |
73 | 6 | $form = $event->getForm(); |
|
74 | 6 | $data = $form->getData(); |
|
75 | |||
76 | 6 | $ips = preg_split("/\R/", $data['admin_allow_host'], null, PREG_SPLIT_NO_EMPTY); |
|
77 | |||
78 | 6 | foreach($ips as $ip) { |
|
79 | 4 | $errors = $app['validator']->validateValue($ip, array( |
|
80 | 4 | new Assert\Ip(), |
|
81 | ) |
||
82 | ); |
||
83 | 4 | if ($errors->count() != 0) { |
|
84 | 6 | $form['admin_allow_host']->addError(new FormError($ip . 'はIPv4アドレスではありません。')); |
|
85 | } |
||
86 | } |
||
87 | 7 | }) |
|
88 | ; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 663 | public function getName() |
|
98 | } |
||
99 |