Reorganizer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 109
ccs 33
cts 33
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A patchArray() 0 14 2
A executeUnsets() 0 6 2
A runTask() 0 4 1
A executeRead() 0 4 1
A executeWrite() 0 4 1
A scheduleUnset() 0 4 1
A executeCast() 0 4 1
A executeCustom() 0 5 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: daniel.jurkovic
5
 * Date: 15.06.17
6
 * Time: 13:23
7
 */
8
9
namespace Macroparts\Vortex\ArrayTools;
10
11
/**
12
 * Class Patcher
13
 * @package Macroparts\Vortex\ArrayPatcher
14
 */
15
class Reorganizer
16
{
17
    const CAST = 'executeCast';
18
    const READ = 'executeRead';
19
    const WRITE = 'executeWrite';
20
    const DELETE = 'scheduleUnset';
21
    const CUSTOM_TRANSFORM = 'executeCustom';
22
23
    private $scheduledUnsets = [];
24
25
    /**
26
     * Execute $tasks on $array and return result
27
     * @param array $array
28
     * @param array[] $tasks
29
     * @return array (patched)
30
     */
31 18
    public function patchArray(&$array, $tasks)
32
    {
33 18
        $temporaryValue = null;
34
35 18
        while ($tasks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tasks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36 18
            $taskName = array_shift($tasks);
37 18
            $taskParams = array_shift($tasks);
38 18
            $this->runTask($array, $taskName, $taskParams, $temporaryValue);
39 12
        }
40
41 18
        $this->executeUnsets($array);
42
43 18
        return $array;
44
    }
45
46
    /**
47
     * @param array $array
48
     */
49 18
    private function executeUnsets(&$array)
50
    {
51 18
        foreach ($this->scheduledUnsets as $path => $nobodyCares) {
52 6
            RecursiveAccessor::unsetInPath($array, $path);
53 12
        }
54 18
    }
55
56
    /**
57
     * @uses executeCast
58
     * @uses executeRead
59
     * @uses executeWrite
60
     * @uses scheduleUnset
61
     * @uses executeCustom
62
     *
63
     * @param array $array
64
     * @param string $taskName
65
     * @param mixed $taskParams
66
     * @param mixed $temporaryValue
67
     */
68 18
    private function runTask(&$array, $taskName, $taskParams, &$temporaryValue)
69
    {
70 18
        $this->$taskName($array, $temporaryValue, $taskParams);
71 18
    }
72
73
    /**
74
     * @param array $array
75
     * @param mixed $temporaryValue
76
     * @param string $path
77
     */
78 15
    private function executeRead(&$array, &$temporaryValue, $path)
79
    {
80 15
        $temporaryValue = RecursiveAccessor::readSingleFromPath($array, $path);
81 15
    }
82
83
    /**
84
     * @param array $array
85
     * @param mixed $temporaryValue
86
     * @param string $path
87
     */
88 15
    private function executeWrite(&$array, $temporaryValue, $path)
89
    {
90 15
        RecursiveAccessor::integrateIntoPath($array, $path, $temporaryValue);
91 15
    }
92
93
    /**
94
     * @param array $array
95
     * @param mixed $temporaryValue
96
     * @param string $path
97
     */
98 6
    private function scheduleUnset(&$array, &$temporaryValue, $path)
0 ignored issues
show
Unused Code introduced by
The parameter $array is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $temporaryValue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100 6
        $this->scheduledUnsets[$path] = 1;
101 6
    }
102
103
    /**
104
     * @param array $array
105
     * @param mixed $temporaryValue
106
     * @param string $type
107
     */
108 3
    private function executeCast(&$array, &$temporaryValue, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $array is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
    {
110 3
        settype($temporaryValue, $type);
111 3
    }
112
113
    /**
114
     * @param array $array
115
     * @param mixed $temporaryValue
116
     * @param array $customFunction
117
     */
118 3
    private function executeCustom(&$array, &$temporaryValue, $customFunction)
119
    {
120 3
        list($callable, $parameters) = $customFunction;
121 3
        call_user_func_array($callable, array_merge([&$array, &$temporaryValue], $parameters));
122 3
    }
123
}
124