ContactForm::init()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 111
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 111
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 73
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (c) 2011-2012 Andreas Heigl<[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category  ContactForm
24
 * @package   HeiglContact
25
 * @author    Andreas Heigl<[email protected]>
26
 * @copyright 2011-2012 Andreas Heigl
27
 * @license   http://www.opesource.org/licenses/mit-license.php MIT-License
28
 * @version   0.0
29
 * @since     06.03.2012
30
 * @link      http://github.com/heiglandreas/php.ug
31
 */
32
33
namespace Org_Heigl\Contact\Form;
34
35
use Zend\Form\Element;
36
use Zend\Form\Form;
37
use Zend\Hydrator\ArraySerializable;
38
use Zend\InputFilter\Input;
39
use Zend\InputFilter\InputFilter;
40
use Zend\Validator\EmailAddress;
41
use Zend\Validator\Hostname as HostnameValidator;
42
use Zend\Validator\Identical;
43
use Zend\Validator\NotEmpty;
44
45
/**
46
 * The Contact-Form
47
 *
48
 * @category  Contact-Form
49
 * @package   OrgHeiglContact
50
 * @author    Andreas Heigl<[email protected]>
51
 * @copyright 2011-2012 Andreas Heigl
52
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
53
 * @version   0.0
54
 * @since     06.03.2012
55
 * @link      http://github.com/heiglandreas/OrgHeiglContact
56
 */
57
class ContactForm extends Form
58
{
59
    /**
60
     * Initialize the form
61
     *
62
     * @return ContactForm
63
     */
64
    public function init()	
65
    {
66
        $this->setName('contact');
67
        
68
        $this->setHydrator(new ArraySerializable);
69
70
        $this->add(array(
71
        		'name' => 'from',
72
        		'type' => 'Zend\Form\Element\Text',
73
        		'options' => array(
74
                	'label'     => 'From:',
75
                	'required'  => true,
76
                	'validators' => array(
77
        				array(
78
        					'name' => 'EmailAddress',
79
        					'options' => array(
80
	                        	'allow'  => HostnameValidator::ALLOW_DNS,
81
	                        	'domain' => true,
82
        					),
83
        				),
84
        			),
85
        		)
86
        	));
87
88
        $this->add(array(
89
        		'name' => 'subject',
90
        		'type' => 'Zend\Form\Element\Text',
91
        		'options' => array(
92
	                'label'      => 'Subject:',
93
	                'required'   => true,
94
	                'filters'    => array(
95
	                    'StripTags',
96
	        		),
97
	                'validators' => array(
98
				        array('StringLength', true, array(
99
	                        'encoding' => 'UTF-8',
100
	                        'min'      => 2,
101
	                        'max'      => 140,
102
				        )),
103
	                )
104
        ),
105
        ));
106
107
        $this->add(array(
108
        		'name' => 'body',
109
        		'type' => 'Zend\Form\Element\Textarea',
110
        		'options' => array(
111
	                'label'    => 'Your message:',
112
	                'required' => true,
113
	                'cols'     => 80,
114
	                'rows'     => 10,
115
        		)
116
        ));
117
118
        $this->add(array(
119
        		'name' => 'country',
120
        		'type' => 'Zend\Form\Element\Text',
121
        		'options' => array(
122
	                'value' => '',
123
	                'class' => 'zonkos',
124
	                'label' => 'SPAM-Protection: Please leave this field as it is!',
125
	            ),                
126
        ));
127
128
        $this->add(new Element\Csrf('csrf'));
129
        
130
        $this->add(array(
131
        		'name' => 'Send',
132
        		'type' => 'Zend\Form\Element\Submit',
133
        		'attributes' => array(
134
        			'value' => 'Send'
135
        		)
136
        ));
137
        
138
        
139
        $from = new Input('from');
140
        $from->setRequired(true);
141
        $from->getValidatorChain()
142
            ->attach(new NotEmpty())
143
            ->attach(new EmailAddress())
144
        ;
145
146
        $country = new Input('country');
147
        $country->setAllowEmpty(true);
0 ignored issues
show
Deprecated Code introduced by
The method Zend\InputFilter\Input::setAllowEmpty() has been deprecated with message: 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain and set this to `true`.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
148
        $country->getValidatorChain()
149
            ->attach(new Identical(''))
150
        ;
151
152
        $subject = new Input('subject');
153
        $subject->setRequired(true);
154
        $subject->getValidatorChain()
155
            ->attach(new NotEmpty())
156
        ;
157
        
158
        $body = new Input('body');
159
        $body->setRequired(true);
160
        $body->getValidatorChain()
161
            ->attach(new NotEmpty())
162
        ;
163
        
164
        $filter = new InputFilter();
165
        $filter->add($from);
166
        $filter->add($subject);
167
        $filter->add($body);
168
        $filter->add($country);
169
        
170
        $this->setInputFilter($filter);
171
172
        
173
        return $this;
174
    }
175
}
176