Test Failed
Push — master ( 306f11...77481f )
by Fran
14:04
created

Form::fieldsOrder()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 27
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 30
ccs 0
cts 27
cp 0
crap 240
rs 5.9166

How to fix   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
namespace PSFS\base\dto;
4
5
/**
6
 * Class Form
7
 * @package PSFS\base\dto
8
 */
9
class Form extends Dto
10
{
11
    /**
12
     * @var \PSFS\base\dto\Field[]
13
     */
14
    private $fields = [];
15
    /**
16
     * @var \PSFS\base\dto\FormAction[]
17
     */
18
    public $actions = [];
19
20
    /**
21
     * @param Field $field
22
     */
23
    public function addField(Field $field)
24
    {
25
        $this->fields[$field->name] = $field;
26
    }
27
28
    /**
29
     * @param string $name
30
     * @return bool
31
     */
32
    public function fieldExists($name)
33
    {
34
        return array_key_exists($name, $this->fields);
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function __toArray()
41
    {
42
        $array = [
43
            'fields' => [],
44
            'actions' => [],
45
        ];
46
        foreach ($this->fields as $field) {
47
            $array['fields'][] = $field->__toArray();
48
        }
49
        usort($array['fields'], function ($fieldA, $fieldB) {
50
            if ((bool)$fieldA['required'] !== (bool)$fieldB['required']) {
51
                if ((bool)$fieldA['required']) {
52
                    return -1;
53
                } else {
54
                    return 1;
55
                }
56
            }
57
            $aOrder = Form::fieldsOrder($fieldA);
58
            $bOrder = Form::fieldsOrder($fieldB);
59
            if ($aOrder === $bOrder) {
60
                return strcmp($fieldA['name'], $fieldB['name']);
61
            }
62
            return ($aOrder < $bOrder) ? -1 : 1;
63
        });
64
        foreach ($this->actions as $action) {
65
            $array['actions'][] = $action->__toArray();
66
        }
67
        return $array;
68
    }
69
70
    /**
71
     * @param array $field
72
     * @return int
73
     */
74
    public static function fieldsOrder(array $field)
75
    {
76
        switch ($field['type']) {
77
            case Field::HIDDEN_TYPE:
78
                $order = 0;
79
                break;
80
            case Field::TEXT_TYPE:
81
            case Field::PHONE_TYPE:
82
            case Field::URL_TYPE:
83
            case Field::PASSWORD_FIELD:
84
            case Field::SEARCH_TYPE:
85
                $order = 1;
86
                break;
87
            case Field::CHECK_TYPE:
88
            case Field::RADIO_TYPE:
89
            case Field::COMBO_TYPE:
90
            case Field::NUMBER_TYPE:
91
            case Field::SWITCH_TYPE:
92
                $order = 2;
93
                break;
94
            case Field::TEXTAREA_TYPE:
95
                $order = 3;
96
                break;
97
            default:
98
            case Field::DATE:
99
            case Field::TIMESTAMP:
100
                $order = 4;
101
                break;
102
        }
103
        return $order;
104
    }
105
}
106