EventFilter::getInputFilter()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 8
c 0
b 0
f 0
cc 2
nc 2
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
declare(strict_types=1);
4
5
namespace Article\Filter;
6
7
use Zend\InputFilter\InputFilter;
8
use Zend\InputFilter\InputFilterAwareInterface;
9
use Zend\InputFilter\InputFilterInterface;
10
11
class EventFilter implements InputFilterAwareInterface
12
{
13
    protected $inputFilter;
14
15
    public function getInputFilter()
16
    {
17
        if (!$this->inputFilter) {
18
            $inputFilter = new InputFilter();
19
20
            $inputFilter->add(
21
                [
22
                    'name'       => 'title',
23
                    'required'   => true,
24
                    'filters'    => [['name' => 'StringTrim']],
25
                    'validators' => [
26
                        ['name' => 'NotEmpty'],
27
                        ['name' => 'StringLength', 'options' => ['min' => 2, 'max' => 100]],
28
                    ],
29
                ]
30
            );
31
32
            $inputFilter->add(
33
                [
34
                    'name'     => 'sub_title',
35
                    'required' => false,
36
                    'filters'  => [['name' => 'StringTrim']],
37
                ]
38
            );
39
40
            $inputFilter->add(
41
                [
42
                    'name'     => 'place_name',
43
                    'required' => true,
44
                    'filters'  => [['name' => 'StringTrim']],
45
                ]
46
            );
47
48
            $inputFilter->add(
49
                [
50
                    'name'     => 'event_url',
51
                    'required' => false,
52
                    'filters'  => [['name' => 'StringTrim']],
53
                ]
54
            );
55
56
            $inputFilter->add(
57
                [
58
                    'name'       => 'body',
59
                    'required'   => true,
60
                    'filters'    => [['name' => 'StringTrim']],
61
                    'validators' => [
62
                        ['name' => 'NotEmpty'],
63
                        ['name' => 'StringLength', 'options' => ['min' => 2]],
64
                    ],
65
                ]
66
            );
67
68
            $inputFilter->add(
69
                [
70
                    'name'       => 'start_at',
71
                    'required'   => true,
72
                    'filters'    => [['name' => 'StringTrim']],
73
                    'validators' => [
74
                        ['name' => 'NotEmpty'],
75
                        ['name' => 'Date', 'options' => ['format' => 'Y-m-d H:i:s']],
76
                    ],
77
                ]
78
            );
79
80
            $inputFilter->add(
81
                [
82
                    'name'       => 'end_at',
83
                    'required'   => true,
84
                    'filters'    => [['name' => 'StringTrim']],
85
                    'validators' => [
86
                        ['name' => 'NotEmpty'],
87
                        ['name' => 'Date', 'options' => ['format' => 'Y-m-d H:i:s']],
88
                    ],
89
                ]
90
            );
91
92
            $inputFilter->add(
93
                [
94
                    'name'       => 'longitude',
95
                    'required'   => true,
96
                    'filters'    => [['name' => 'StringTrim']],
97
                    'validators' => [['name' => 'NotEmpty']],
98
                ]
99
            );
100
101
            $inputFilter->add(
102
                [
103
                    'name'       => 'latitude',
104
                    'required'   => true,
105
                    'filters'    => [['name' => 'StringTrim']],
106
                    'validators' => [['name' => 'NotEmpty']],
107
                ]
108
            );
109
110
            $this->inputFilter = $inputFilter;
111
        }
112
113
        return $this->inputFilter;
114
    }
115
116
    public function setInputFilter(InputFilterInterface $inputFilter)
117
    {
118
        throw new \Exception('Not used');
119
    }
120
}
121