DataProcessingTrait::getData()   B
last analyzed

Complexity

Conditions 10
Paths 2

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10.1536

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 40
ccs 23
cts 26
cp 0.8846
rs 7.6666
cc 10
nc 2
nop 0
crap 10.1536

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 Nip\Form\Traits;
4
5
/**
6
 * Trait DataProcessingTrait
7
 * @package Nip\Form\Traits
8
 */
9
trait DataProcessingTrait
10
{
11
    /**
12
     * @param $request
13
     */
14
    protected function getDataFromRequest($request)
15
    {
16
        $elements = $this->getElements();
0 ignored issues
show
Bug introduced by
It seems like getElements() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
        /** @scrutinizer ignore-call */ 
17
        $elements = $this->getElements();
Loading history...
17
        if (is_array($elements)) {
18
            foreach ($elements as $name => $element) {
19
                if ($element->isGroup() && $element->isRequestArray()) {
20
                    $name = str_replace('[]', '', $name);
21
22
                    if (isset($request[$name])) {
23
                        $data = is_array($request[$name]) ? $request[$name] : [$request[$name]];
24
                        $element->getData($data, 'request');
25
                    }
26
                } else {
27
                    $value = isset($request[$name]) ? $request[$name] : null;
28
                    if (strpos($name, '[') && strpos($name, ']')) {
29
                        $arrayPrimary = substr($name, 0, strpos($name, '['));
30
                        $arrayKeys = str_replace($arrayPrimary, '', $name);
31
32
                        preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $arr_matches, PREG_PATTERN_ORDER);
33
                        $value = $request[$arrayPrimary];
34
                        foreach ($arr_matches[1] as $dimension) {
35
                            $value = isset($value[$dimension]) ? $value[$dimension] : null;
36
                        }
37
                    }
38
                    $element->getData($value, 'request');
39
                }
40
            }
41
        }
42
    }
43
44
    /**
45 1
     * @return array
46
     */
47 1
    public function getData()
48 1
    {
49 1
        $data = [];
50 1
        $elements = $this->getElements();
51 1
        if (is_array($elements)) {
52 1
            foreach ($elements as $element) {
53
                $name = $element->getName();
54
                if ($element->isGroup() && $element->isRequestArray()) {
55
                    $name = str_replace('[]', '', $name);
56
                    $data[$name][] = $element->getValue();
57
                    continue;
58 1
                }
59 1
60 1
                if (strpos($name, '[]')) {
61 1
                    $arrayPrimary = substr($name, 0, strpos($name, '['));
62
                    $data[$arrayPrimary][] = $element->getValue();
63
                    continue;
64 1
                }
65 1
66 1
                if (strpos($name, '[') && strpos($name, ']')) {
67
                    $arrayPrimary = substr($name, 0, strpos($name, '['));
68 1
                    $arrayKeys = str_replace($arrayPrimary, '', $name);
69
70 1
                    preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $arr_matches, PREG_PATTERN_ORDER);
71 1
72 1
                    $dataArray = $element->getValue();
73
                    foreach ($arr_matches[1] as $dimension) {
74
                        $dataArray = [$dimension => $dataArray];
75 1
                    }
76 1
77 1
                    $data[$arrayPrimary] = (isset($data[$arrayPrimary]))
78 1
                        ? array_merge($data[$arrayPrimary], $dataArray)
79
                        : $dataArray;
80 1
                    continue;
81
                }
82
                $data[$name] = $element->getValue();
83
            }
84 1
        }
85
86
        return $data;
87
    }
88
}
89