ChangesFields::changeComponentFields()   B
last analyzed

Complexity

Conditions 10
Paths 52

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 19
c 1
b 0
f 0
nc 52
nop 1
dl 0
loc 34
rs 7.6666

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
 * Copyright © O2TI. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace O2TI\AutoCompleteAddressBr\Model;
10
11
use O2TI\AutoCompleteAddressBr\Helper\Config;
12
13
/**
14
 *  ChangesFields - Change Compoments for Inputs.
15
 */
16
class ChangesFields
17
{
18
    /**
19
     * @var Config
20
     */
21
    protected $config;
22
23
    /**
24
     * @param Config $config
25
     */
26
    public function __construct(
27
        Config $config
28
    ) {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Change Components at Fields.
34
     *
35
     * @param array $fields
36
     *
37
     * @return array
38
     */
39
    public function changeComponentFields(array $fields): array
40
    {
41
        foreach ($fields as $key => $data) {
42
            if ($key === 'postcode') {
43
                $defaultPosition = (int) $fields[$key]['sortOrder'];
44
                $fields[$key]['sortOrder'] = $defaultPosition;
45
                $fields[$key]['component'] = 'O2TI_AutoCompleteAddressBr/js/view/form/element/postcode';
46
                if ($this->config->useInputMasking()) {
47
                    // phpcs:ignore
48
                    $fields[$key]['component'] = 'O2TI_AutoCompleteAddressBr/js/view/form/element/O2TI/InputMasking/postcode';
49
                }
50
            }
51
            if ($this->config->isHideTargetFields()) {
52
                if ($key === 'street') {
53
                    foreach ($fields[$key]['children'] as $arrayPosition => $streetLine) {
54
                        // phpcs:ignore
55
                        $fields[$key]['children'][$arrayPosition]['component'] = 'O2TI_AutoCompleteAddressBr/js/view/form/element/street-inline';
56
                    }
57
                }
58
                if ($key === 'city') {
59
                    $fields[$key]['component'] = 'O2TI_AutoCompleteAddressBr/js/view/form/element/city';
60
                }
61
                if ($key === 'region_id') {
62
                    $fields[$key]['component'] = 'O2TI_AutoCompleteAddressBr/js/view/form/element/region';
63
                }
64
                if ($key === 'country_id') {
65
                    $fields[$key]['component'] = 'O2TI_AutoCompleteAddressBr/js/view/form/element/country';
66
                }
67
            }
68
69
            continue;
70
        }
71
72
        return $fields;
73
    }
74
}
75