Completed
Push — master ( a8c688...026476 )
by Gerrit
48:59
created

ApplyDataTemplateTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 53
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A applyDataTemplate() 0 26 4
A extractValueFromDataArray() 0 18 3
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Controllers;
14
15
use ErrorException;
16
17
trait ApplyDataTemplateTrait
18
{
19
20 3
    private function applyDataTemplate(array $data, array $dataTemplate): array
21
    {
22
        /** @var array $result */
23 3
        $result = array();
24
25 3
        foreach ($dataTemplate as $key => $templateEntry) {
26
            /** @var string|array $templateEntry */
27
28
            /** @var mixed $entryResult */
29 3
            $entryResult = null;
0 ignored issues
show
Unused Code introduced by
$entryResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
31 3
            if (is_string($templateEntry)) {
32 3
                $entryResult = $this->extractValueFromDataArray($data, explode(".", $templateEntry));
33
34 1
            } elseif (is_array($templateEntry)) {
35
                $entryResult = $this->applyDataTemplate($data, $templateEntry);
36
37
            } else {
38 1
                throw new ErrorException("Invalid entry for data-template, must be string or array!");
39
            }
40
41 3
            $result[$key] = $entryResult;
42
        }
43
44 2
        return $result;
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50 3
    private function extractValueFromDataArray(array $data, array $path)
51
    {
52
        /** @var string $key */
53 3
        $key = array_shift($path);
54
55
        /** @var mixed $value */
56 3
        $value = null;
57
58 3
        if (isset($data[$key])) {
59 3
            $value = $data[$key];
60
61 3
            if (!empty($path)) {
62
                $value = $this->extractValueFromDataArray($value, $path);
63
            }
64
        }
65
66 3
        return $value;
67
    }
68
69
}
70