Passed
Push — master ( a22205...112e67 )
by Gabriel
04:04 queued 13s
created

DataProcessingTrait::getData()   B

Complexity

Conditions 10
Paths 2

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.1371

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 40
ccs 24
cts 27
cp 0.8889
rs 7.6666
cc 10
nc 2
nop 0
crap 10.1371

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
    /**
13
     * @param $request
14
     */
15
    protected function getDataFromRequest($request)
16
    {
17
        $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

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