Test Failed
Push — master ( 5f9190...472f53 )
by Fran
02:50
created

Form::fieldsOrder()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 15

Importance

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