Completed
Push — master ( 7d7f4e...c0227e )
by smiley
02:27
created

DBResult::__merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * Class DBResult
4
 *
5
 * @filesource   DBResult.php
6
 * @created      23.05.2017
7
 * @package      chillerlan\Database
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database;
14
15
use ArrayAccess, Countable, Iterator;
16
use chillerlan\Database\Traits\{Enumerable, Magic};
17
18
/**
19
 * @property int $length
20
 *
21
 * each($func [, $fieldname)
22
 */
23
class DBResult implements Iterator, ArrayAccess, Countable{
24
	use Enumerable, Magic;
25
26
	/**
27
	 * @var \chillerlan\Database\DBResultRow[]
28
	 */
29
	protected $array = [];
30
31
	/**
32
	 * @var int
33
	 */
34
	protected $offset = 0;
35
	protected $sourceEncoding;
36
	protected $destEncoding;
37
38
	/**
39
	 * DBResult constructor.
40
	 *
41
	 * @param \Traversable|\stdClass|array|null $data
42
	 * @param string|null                       $sourceEncoding
43
	 * @param string                            $destEncoding
44
	 *
45
	 * @throws \chillerlan\Database\DBException
46
	 */
47
	public function __construct($data = null, $sourceEncoding = null, $destEncoding = 'UTF-8'){
48
		$this->sourceEncoding = $sourceEncoding;
49
		$this->destEncoding   = $destEncoding;
50
51
		if(is_null($data)){
52
			$data = [];
53
		}
54
		else if($data instanceof \Traversable){
55
			$data = iterator_to_array($data);
56
		}
57
		else if(!$data instanceof \stdClass && !is_array($data)){
58
			throw new DBException('invalid data');
59
		}
60
61
		foreach($data as $k => $v){
0 ignored issues
show
Bug introduced by
The expression $data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
62
			$this->offsetSet($k, $v);
63
		}
64
65
		$this->offset = 0;
66
	}
67
68
	/**
69
	 * @param \chillerlan\Database\DBResult $DBResult
70
	 *
71
	 * @return \chillerlan\Database\DBResult
72
	 */
73
	public function __merge(DBResult $DBResult){
74
		$arr = [];
75
76
		foreach($DBResult as $row){
77
			$arr[] = $row;
78
		}
79
80
		$this->array = array_merge($this->array, $arr);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->array, $arr) of type array is incompatible with the declared type array<integer,object<chi...\Database\DBResultRow>> of property $array.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
82
		return $this;
83
	}
84
85
	/**
86
	 * @link http://api.prototypejs.org/language/Enumerable/prototype/toArray/
87
	 *
88
	 * @return array
89
	 */
90
	public function __toArray():array {
91
		$arr = [];
92
93
		foreach($this->array as $item){
94
			$arr[] = $item->__toArray();
95
		}
96
97
		return $arr;
98
	}
99
100
	public function chunk(int $size){
101
		return array_chunk($this->__toArray(), $size);
102
	}
103
104
	/*********
105
	 * magic *
106
	 *********/
107
108
	/**
109
	 * @return int
110
	 */
111
	protected function magic_get_length():int{
112
		return $this->count();
113
	}
114
115
116
	/***************
117
	 * ArrayAccess *
118
	 ***************/
119
120
	/**
121
	 * @param int|string $offset
122
	 *
123
	 * @return bool
124
	 */
125
	public function offsetExists($offset):bool{
126
		return isset($this->array[$offset]);
127
	}
128
129
	/**
130
	 * @param int|string $offset
131
	 *
132
	 * @return \chillerlan\Database\DBResultRow|mixed|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use DBResultRow|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
133
	 */
134
	public function offsetGet($offset){
135
		return $this->array[$offset] ?? null;
136
	}
137
138
	/**
139
	 * @param int|string   $offset
140
	 * @param array        $value
141
	 *
142
	 * @return void
143
	 */
144
	public function offsetSet($offset, $value){
145
146
		if(is_null($offset)){
147
			$this->array[] = new DBResultRow($value, $this->sourceEncoding, $this->destEncoding);
148
		}
149
		else{
150
			$this->array[$offset] = new DBResultRow($value, $this->sourceEncoding, $this->destEncoding);
151
		}
152
153
	}
154
155
	/**
156
	 * @param int|string $offset
157
	 *
158
	 * @return void
159
	 */
160
	public function offsetUnset($offset){
161
		unset($this->array[$offset]);
162
	}
163
164
165
	/*************
166
	 * Countable *
167
	 *************/
168
169
	/**
170
	 * @return int
171
	 */
172
	public function count():int{
173
		return count($this->array);
174
	}
175
176
177
	/************
178
	 * Iterator *
179
	 ************/
180
181
	/**
182
	 * @return \chillerlan\Database\DBResultRow|mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use DBResultRow|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
183
	 */
184
	public function current(){
185
		return $this->offsetGet($this->offset);
186
	}
187
188
	/**
189
	 * @return int
190
	 */
191
	public function key():int{
192
		return $this->offset;
193
	}
194
195
	/**
196
	 * @return bool
197
	 */
198
	public function valid():bool{
199
		return $this->offsetExists($this->offset);
200
	}
201
202
	/**
203
	 *  @return void
204
	 */
205
	public function next(){
206
		$this->offset++;
207
	}
208
209
	/**
210
	 * @return void
211
	 */
212
	public function rewind(){
213
		$this->offset = 0;
214
	}
215
216
}
217