BookingFieldset::addElements()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 96
rs 8.3859
cc 1
eloc 60
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
namespace JhFlexiTime\Form\Fieldset;
4
5
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
6
use JhFlexiTime\Entity\Booking;
7
use Zend\Form\Fieldset;
8
use Zend\InputFilter\InputFilterProviderInterface;
9
use Doctrine\Common\Persistence\ObjectManager;
10
use ZfcUser\Entity\UserInterface;
11
12
/**
13
 * Class BookingFieldset
14
 * @package JhFlexiTime\Form\Fieldset
15
 * @author Aydin Hassan <[email protected]>
16
 */
17
class BookingFieldset extends Fieldset
18
{
19
20
    /**
21
     * @var \Doctrine\Common\Persistence\ObjectManager
22
     */
23
    protected $objectManager;
24
25
    /**
26
     * @var UserInterface
27
     */
28
    protected $user;
29
30
    /**
31
     * @param \Doctrine\Common\Persistence\ObjectManager $objectManager
32
     */
33
    public function __construct(ObjectManager $objectManager, UserInterface $user)
34
    {
35
        $this->objectManager = $objectManager;
36
        $this->user          = $user;
37
        parent::__construct('booking');
38
        $this->addElements();
39
    }
40
41
    /**
42
     * Add elements
43
     */
44
    public function addElements()
45
    {
46
47
        $this->setHydrator(new DoctrineHydrator($this->objectManager, 'JhFlexiTime\Entity\Booking'))
0 ignored issues
show
Documentation introduced by
'JhFlexiTime\\Entity\\Booking' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
            ->setObject(new Booking());
49
50
        $this->add([
51
            'type'          => 'Zend\Form\Element\Hidden',
52
            'name'          => 'user',
53
            'attributes'    => [
54
                'id'    => 'book-user',
55
                'value' => $this->user->getId(),
56
            ]
57
        ]);
58
59
        $this->add([
60
            'type' => 'Zend\Form\Element\Hidden',
61
            'name' => 'id',
62
            'attributes' => [
63
                'id' => 'book-id',
64
            ]
65
        ]);
66
67
        $this->add([
68
            'type'    => 'Zend\Form\Element\Text',
69
            'name'    => 'date',
70
            'options' => [
71
                'label' => 'Date',
72
                'label_attributes' => [
73
                    'class' => 'col-sm-4 control-label',
74
                ],
75
            ],
76
            'attributes' => [
77
                'id'        => 'book-date',
78
                'step'      => '1',
79
                'required'  => 'required',
80
                'class'     => 'form-control input-sm',
81
                //'value'     => new \DateTime(),
82
            ],
83
        ]);
84
85
        $this->add([
86
            'type'    => 'Zend\Form\Element\Time',
87
            'name'    => 'startTime',
88
            'options' => [
89
                'label' => 'Start Time',
90
                'label_attributes' => [
91
                    'class' => 'col-sm-4 control-label',
92
                ],
93
            ],
94
            'attributes' => [
95
                'id'    => 'book-starttime',
96
                //TODO: Inject From options
97
                //'min'   => '07:00:00',
98
                //'max'   => '10:00:00',
99
                'step'  => '900',   //15 mins, 60 x 15
100
                'class' => 'form-control input-sm',
101
                'value' => '07:00',
102
            ],
103
        ]);
104
105
        $this->add([
106
            'type'    => 'Zend\Form\Element\Time',
107
            'name'    => 'endTime',
108
            'options' => [
109
                'label' => 'End Time',
110
                'label_attributes' => [
111
                    'class' => 'col-sm-4 control-label',
112
                ],
113
            ],
114
            'attributes' => [
115
                'id'    => 'book-endtime',
116
                //TODO: Inject From options
117
                //'min'   => '16:00:00',
118
                //'max'   => '19:00:00',
119
                'step'  => '900',   //15 mins, 60 x 15
120
                'class' => 'form-control input-sm',
121
                'value' => '16:00',
122
            ],
123
        ]);
124
125
        $this->add([
126
            'type'    => 'Zend\Form\Element\Textarea',
127
            'name'    => 'notes',
128
            'options' => [
129
                'label' => 'Notes',
130
                'label_attributes' => [
131
                    'class' => 'col-sm-4 control-label',
132
                ],
133
            ],
134
            'attributes' => [
135
                'id'    => 'book-notes',
136
                'class' => 'form-control input-sm',
137
            ]
138
        ]);
139
    }
140
}
141