AttendeeField::__construct()   B
last analyzed

Complexity

Conditions 10
Paths 13

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 7.2024
c 0
b 0
f 0
cc 10
nc 13
nop 3

How to fix   Long Method    Complexity   

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
 * AttendeesField.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 10/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use ArrayList;
12
use CompositeField;
13
use DBField;
14
use FieldGroup;
15
use FieldList;
16
use FormField;
17
use HiddenField;
18
use LiteralField;
19
use Member;
20
21
/**
22
 * Class AttendeeField
23
 *
24
 * @package Broarm\EventTickets
25
 */
26
class AttendeeField extends CompositeField
27
{
28
    protected $name = 'Attendee';
29
30
    protected $requiredFields = array();
31
32
    public function __construct(Attendee $attendee, $main = false, $required = true)
33
    {
34
        parent::__construct();
35
        $this->setTag('fieldset');
36
        $this->setLegend(_t('AttendeeField.VALUED', '{title} valued {price}', null, array(
0 ignored issues
show
Documentation introduced by
array('title' => $attend...')->NiceDecimalPoint()) is of type array<string,?,{"title":"string","price":"?"}>, but the function expects a string.

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...
37
            'title' => $attendee->Ticket()->Title,
38
            'price' => $attendee->Ticket()->dbObject('Price')->NiceDecimalPoint())
39
        ));
40
41
        $children = FieldList::create();
42
        $savableFields = $attendee->Event()->Fields();
43
44
        /** @var UserField $field */
45
        foreach ($savableFields as $field) {
46
            // Generate a unique field name
47
            $fieldName = "{$this->name}[{$attendee->ID}][$field->ID]";
48
49
            // Create the field an fill with attendee data
50
            $hasField = $attendee->Fields()->find('ID', $field->ID);
51
            $value = $hasField ? $hasField->getField('Value') : null;
52
            $formField = $field->createField($fieldName, $value, $main);
53
54
            // Check if the field is required
55
            if ($field->Required && $required) {
56
                if ($formField instanceof CompositeField) {
57
                    foreach ($formField->getChildren()->column('Name') as $requiredField) {
58
                        $this->addRequiredField($requiredField);
59
                    }
60
                } else {
61
                    $this->addRequiredField($fieldName);
62
                }
63
            }
64
65
            // Pre fill the field if a member is logged in
66
            if ($main && empty($formField->value) && $member = Member::currentUser()) {
67
                $formField->setValue($member->getField($field->Name));
68
            }
69
70
            // Add the form
71
            $children->add($formField);
72
        }
73
74
        // Set the main field
75
        $children->add(HiddenField::create("{$this->name}[{$attendee->ID}][Main]", 'Main', (int)$main));
76
77
        // set the children
78
        $this->setChildren($children);
79
80
        // Add a hook to modify the added fields if needed
81
        $this->extend('updateAttendeeField', $attendee, $main, $required);
82
    }
83
84
    /**
85
     * Update the required fields array
86
     *
87
     * @param $fieldName
88
     */
89
    private function addRequiredField($fieldName) {
90
        array_push($this->requiredFields, $fieldName);
91
    }
92
93
    /**
94
     * Get the required fields
95
     *
96
     * @return array
97
     */
98
    public function getRequiredFields()
99
    {
100
        return $this->requiredFields;
101
    }
102
}
103