Completed
Push — master ( c82e60...68cb49 )
by Sébastien
05:32
created

AbstractResultSet::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Soluble\FlexStore\ResultSet;
4
5
use ArrayObject;
6
//use Soluble\DbWrapper\Result\ResultInterface;
7
use Zend\Db\ResultSet\ResultSet as ZFResultSet;
8
9
abstract class AbstractResultSet implements ResultSetInterface
10
{
11
    const TYPE_ARRAYOBJECT = 'arrayobject';
12
    const TYPE_ARRAY  = 'array';
13
14
    /**
15
     * Return type to use when returning an object from the set
16
     *
17
     * @var string
18
     */
19
    protected $returnType = self::TYPE_ARRAYOBJECT;
20
21
    /**
22
     * Allowed return types
23
     *
24
     * @var array
25
     */
26
    protected $allowedReturnTypes = [
27
        self::TYPE_ARRAYOBJECT,
28
        self::TYPE_ARRAY,
29
    ];
30
31
32
33
    /**
34
     * @var ArrayObject
35
     */
36
    protected $arrayObjectPrototype = null;
37
38
39
    /**
40
     *
41
     * @var ZFResultSet
42
     */
43
    protected $zfResultSet;
44
45
46
    /**
47
     * Constructor
48
     *
49
     * @param ZFResultSet      $resultSet
50
     * @param string           $returnType
51
     * @param null|ArrayObject $arrayObjectPrototype
52
     */
53
54
/*    
55
public function __constructOld(ZFResultSet $resultSet, $returnType = self::TYPE_ARRAYOBJECT, $arrayObjectPrototype = null)
56
{
57
    $this->zfResultSet = $resultSet;
58
    $this->returnType = (in_array($returnType, [self::TYPE_ARRAY, self::TYPE_ARRAYOBJECT])) ? $returnType : self::TYPE_ARRAYOBJECT;
59
    if ($this->returnType === self::TYPE_ARRAYOBJECT) {
60
        $this->setArrayObjectPrototype(($arrayObjectPrototype) ?: new ArrayObject([], ArrayObject::ARRAY_AS_PROPS));
61
    }
62
}
63
*/
64
    /**
65
     * Constructor
66
     *
67
     * @param ZFResultSet|ResultInterface      $resultSet
68
     * @param string           $returnType
69
     * @param null|ArrayObject $arrayObjectPrototype
70
     */
71 39
    public function __construct($resultSet, $returnType = self::TYPE_ARRAYOBJECT, $arrayObjectPrototype = null)
0 ignored issues
show
Unused Code introduced by
The parameter $arrayObjectPrototype 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...
72
    {
73 39
        $this->zfResultSet = $resultSet;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resultSet can also be of type object<Soluble\FlexStore...ultSet\ResultInterface>. However, the property $zfResultSet is declared as type object<Zend\Db\ResultSet\ResultSet>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74 39
        $this->returnType = (in_array($returnType, [self::TYPE_ARRAY, self::TYPE_ARRAYOBJECT])) ? $returnType : self::TYPE_ARRAYOBJECT;
75 39
    }
76
77
78
    /**
79
     * Set the row object prototype
80
     *
81
     * @param  ArrayObject $arrayObjectPrototype
82
     * @throws Exception\InvalidArgumentException
83
     * @return AbstractResultSet
84
     */
85
    public function setArrayObjectPrototype($arrayObjectPrototype)
86
    {
87
        $this->zfResultSet->setArrayObjectPrototype($arrayObjectPrototype);
88
        return $this;
89
    }
90
91
    /**
92
     * Get the row object prototype
93
     *
94
     * @return ArrayObject
95
     */
96
    public function getArrayObjectPrototype()
97
    {
98
        return $this->zfResultSet->getArrayObjectPrototype();
99
    }
100
101
    /**
102
     * Get the return type to use when returning objects from the set
103
     *
104
     * @return string
105
     */
106
    public function getReturnType()
107
    {
108
        return $this->zfResultSet->getReturnType();
109
    }
110
111
112
    /**
113
     *
114
     * @return AbstractResultSet
115
     */
116
    public function buffer()
117
    {
118
        $this->zfResultSet->buffer();
119
        return $this;
120
    }
121
122
123
    /**
124
     *
125
     * @return boolean
126
     */
127
    public function isBuffered()
128
    {
129
        return $this->zfResultSet->isBuffered();
130
    }
131
132
    /**
133
     * Get the data source used to create the result set
134
     *
135
     * @return null|Iterator
136
     */
137
    public function getDataSource()
138
    {
139
        return $this->zfResultSet->getDataSource();
140
    }
141
142
    /**
143
     * Retrieve count of fields in individual rows of the result set
144
     *
145
     * @return int
146
     */
147 1
    public function getFieldCount()
148
    {
149 1
        return $this->zfResultSet->getFieldCount();
150
    }
151
152
    /**
153
     * Iterator: move pointer to next item
154
     *
155
     * @return void
156
     */
157 23
    public function next()
158
    {
159 23
        $this->zfResultSet->next();
160 23
    }
161
162
    /**
163
     * Iterator: retrieve current key
164
     *
165
     * @return mixed
166
     */
167 1
    public function key()
168
    {
169 1
        return $this->zfResultSet->key();
170
    }
171
172
    /**
173
     * Iterator: get current item
174
     *
175
     * @return array
176
     */
177
    public function current()
178
    {
179
        return $this->zfResultSet->current();
180
    }
181
182
    /**
183
     * Iterator: is pointer valid?
184
     *
185
     * @return bool
186
     */
187
    public function valid()
188
    {
189
        return $this->zfResultSet->valid();
190
    }
191
192
    /**
193
     * Iterator: rewind
194
     *
195
     * @return void
196
     */
197
    public function rewind()
198
    {
199
        $this->zfResultSet->rewind();
200
    }
201
202
    /**
203
     * Countable: return count of rows
204
     *
205
     * @return int
206
     */
207 37
    public function count()
208
    {
209 37
        return $this->zfResultSet->count();
210
    }
211
212
    /**
213
     * Cast result set to array of arrays
214
     *
215
     * @return array
216
     * @throws Exception\RuntimeException if any row is not castable to an array
217
     */
218
    public function toArray()
219
    {
220
        return $this->zfResultSet->toArray();
221
    }
222
}
223