Processor::current()   C
last analyzed

Complexity

Conditions 13
Paths 48

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 28
nc 48
nop 0
dl 0
loc 48
rs 5.0877
c 0
b 0
f 0

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 Phperf\Pipeline\Rows;
4
5
use ArrayIterator;
6
use IteratorIterator;
7
8
class Processor extends IteratorIterator
9
{
10
    protected $rows = array();
11
    private $skipFields = array();
12
13
    public function skipField($field)
14
    {
15
        $this->skipFields[$field] = $field;
16
        return $this;
17
    }
18
19
    private $changeKeys = array();
20
21
    public function changeKey($fieldFrom, $fieldTo)
22
    {
23
        $this->changeKeys[$fieldFrom] = $fieldTo;
24
        return $this;
25
    }
26
27
28
    private $combineFields = array();
29
30
    public function combine($fieldKey, $fieldValue)
31
    {
32
        $this->combineFields[$fieldKey] = $fieldValue;
33
        return $this;
34
    }
35
36
    /** @var \Closure */
37
    private $callback;
38
    public function map(callable $callback)
39
    {
40
        $this->callback = $callback;
41
        return $this;
42
    }
43
44
    private $combineOffset = array();
45
46
    /**
47
     * @param integer $offsetKey
48
     * @param integer $offsetValue
49
     * @return $this
50
     */
51
    public function combineOffset($offsetKey, $offsetValue)
52
    {
53
        $this->combineOffset [$offsetKey] = $offsetValue;
54
        return $this;
55
    }
56
57
    /**
58
     * Processor constructor.
59
     * @param array|\Iterator $rows
60
     */
61
    public function __construct($rows)
62
    {
63
        if (is_array($rows)) {
64
            parent::__construct(new ArrayIterator($rows));
65
        } else {
66
            parent::__construct($rows);
67
        }
68
    }
69
70
    public function current()
71
    {
72
        $row = parent::current();
73
        if ($this->callback) {
74
            $row = $this->callback->__invoke($row);
75
        }
76
77
        if ($this->skipFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->skipFields 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...
78
            foreach ($this->skipFields as $field) {
79
                if (isset($row[$field])) {
80
                    unset($row[$field]);
81
                }
82
            }
83
        }
84
85
        $keys = null;
86
87
        if ($this->combineOffset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->combineOffset 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...
88
            foreach ($this->combineOffset as $offsetKey => $offsetValue) {
89
                $keys = array_keys($row);
90
                $row[$row[$keys[$offsetKey]]] = $row[$keys[$offsetValue]];
91
                unset($row[$keys[$offsetKey]]);
92
                unset($row[$keys[$offsetValue]]);
93
            }
94
        }
95
96
        if ($this->combineFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->combineFields 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...
97
            foreach ($this->combineFields as $fieldKey => $fieldValue) {
98
                $row[$row[$fieldKey]] = $row[$fieldValue];
99
                unset($row[$fieldKey]);
100
                unset($row[$fieldValue]);
101
            }
102
        }
103
104
        if ($this->changeKeys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->changeKeys 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...
105
            if (!$keys) {
106
                $keys = array_keys($row);
107
            }
108
            foreach ($keys as &$key) {
109
                if (isset($this->changeKeys[$key])) {
110
                    $key = $this->changeKeys[$key];
111
                }
112
            }
113
            unset($key);
114
            $row = array_combine($keys, $row);
115
        }
116
117
        return $row;
118
    }
119
120
121
    /**
122
     * @param array|\Iterator $rows
123
     * @return static
124
     */
125
    static function create($rows)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
126
    {
127
        return new static($rows);
128
    }
129
130
131
    public function exportArray()
132
    {
133
        $result = array();
134
        foreach ($this as $row) {
135
            $result [] = $row;
136
        }
137
        return $result;
138
    }
139
140
}