Passed
Push — master ( 76b08e...5cd6b2 )
by Ryuichi
58:40 queued 56:20
created

Result   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Test Coverage

Coverage 34.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 213
ccs 22
cts 63
cp 0.3492
rs 10
wmc 25

15 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 0 7 2
A next() 0 6 2
A seek() 0 11 3
A key() 0 3 1
A offsetSet() 0 3 1
A offsetUnSet() 0 3 1
A current() 0 7 3
A count() 0 14 3
A valid() 0 3 1
A offsetExists() 0 7 2
A toEntity() 0 6 1
A toArray() 0 7 1
A rewind() 0 6 2
A __construct() 0 5 1
A __destruct() 0 4 1
1
<?php
2
3
namespace WebStream\Database;
4
5
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WebStream\Exception\Extend\CollectionException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\CollectionException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Doctrine\DBAL\Statement;
8
9
/**
10
 * Result
11
 * @author Ryuichi TANAKA.
12
 * @since 2013/12/14
13
 * @version 0.7
14
 */
15
class Result implements \Iterator, \SeekableIterator, \ArrayAccess, \Countable
16
{
17
    use Injector;
18
19
    /**
20
     * @var Doctrine\DBAL\Statement ステートメント
0 ignored issues
show
Bug introduced by
The type WebStream\Database\Doctrine\DBAL\Statement was not found. Did you mean Doctrine\DBAL\Statement? If so, make sure to prefix the type with \.
Loading history...
21
     */
22
    private $stmt;
23
24
    /**
25
     * @var array<mixed> 列データ
26
     */
27
    private $row;
28
29
    /**
30
     * @var array<mixed> キャッシュ化列データ
31
     */
32
    private $rowCache;
33
34
    /**
35
     * @var int インデックス位置
36
     */
37
    private $position;
38
39
    /**
40
     * コンストラクタ
41
     * @param Statement ステートメントオブジェクト
0 ignored issues
show
Documentation Bug introduced by
The doc comment ステートメントオブジェクト at position 0 could not be parsed: Unknown type name 'ステートメントオブジェクト' at position 0 in ステートメントオブジェクト.
Loading history...
42
     */
43 44
    public function __construct(Statement $stmt)
44
    {
45 44
        $this->stmt = $stmt;
0 ignored issues
show
Documentation Bug introduced by
It seems like $stmt of type Doctrine\DBAL\Statement is incompatible with the declared type WebStream\Database\Doctrine\DBAL\Statement of property $stmt.

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...
46 44
        $this->position = 0;
47 44
        $this->rowCache = [];
48 44
    }
49
50
    /**
51
     * デストラクタ
52
     */
53 44
    public function __destruct()
54
    {
55 44
        $this->stmt = null;
56 44
        $this->rowCache = null;
57 44
    }
58
59
    /**
60
     * Implements Countable#count
61
     * @return integer 結果件数
62
     */
63
    public function count()
64
    {
65
        $count = 0;
66
        if ($this->stmt === null) {
67
            $count = count($this->rowCache);
68
        } else {
69
            $count = $this->stmt->rowCount();
70
            if ($count === 0) {
71
                $this->toArray();
72
                $count = count($this->rowCache);
73
            }
74
        }
75
76
        return $count;
77
    }
78
79
    /**
80
     * Implements SeekableIterator#seek
81
     * カーソル位置を移動する
82
     * @param mixed オフセット
0 ignored issues
show
Documentation Bug introduced by
The doc comment オフセット at position 0 could not be parsed: Unknown type name 'オフセット' at position 0 in オフセット.
Loading history...
83
     */
84
    public function seek($offset)
85
    {
86
        if ($this->stmt !== null) {
87
            // Mysql does not support scrollable cursor.
88
            // but if statement to array, it is accessable.
89
            $this->toArray();
90
        }
91
        if (array_key_exists($offset, $this->rowCache)) {
92
            return $this->rowCache[$offset];
93
        } else {
94
            throw new \OutOfBoundsException("Current cursor is out of range: " . $offset);
95
        }
96
    }
97
98
    /**
99
     * Implements Iterator#current
100
     * 現在の要素を返却する
101
     * @return array<string> 列データ
102
     */
103
    public function current()
104
    {
105
        if ($this->stmt === null) {
106
            return array_key_exists($this->position, $this->rowCache) ? $this->rowCache[$this->position] : null;
107
        }
108
109
        return $this->row;
110
    }
111
112
    /**
113
     * Implements Iterator#key
114
     * 現在の要素のキーを返却する
115
     * @return integer キー
116
     */
117
    public function key()
118
    {
119
        return $this->position;
120
    }
121
122
    /**
123
     * Implements Iterator#next
124
     * 次の要素に進む
125
     */
126
    public function next()
127
    {
128
        if ($this->stmt !== null) {
129
            $this->row = $this->stmt->fetch(\PDO::FETCH_ASSOC);
130
        }
131
        $this->position++;
132
    }
133
134
    /**
135
     * Implements Iterator#rewind
136
     * イテレータを先頭に巻き戻す
137
     */
138
    public function rewind()
139
    {
140
        if ($this->stmt !== null) {
141
            $this->row = $this->stmt->fetch(\PDO::FETCH_ASSOC, \PDO::FETCH_ORI_FIRST);
142
        }
143
        $this->position = 0;
144
    }
145
146
    /**
147
     * Implements Iterator#valid
148
     * 現在位置が有効かどうかを調べる
149
     * @return boolean 有効かどうか
150
     */
151
    public function valid()
152
    {
153
        return $this->row !== false;
154
    }
155
156
    /**
157
     * Implements ArrayAccess#offsetExists
158
     * オフセットの位置に値が存在するかどうか返却する
159
     * @return boolean 値が存在するかどうか
160
     */
161
    public function offsetExists($offset)
162
    {
163
        if ($this->stmt !== null) {
164
            $this->toArray();
165
        }
166
167
        return array_key_exists($offset, $this->rowCache);
168
    }
169
170
    /**
171
     * Implements ArrayAccess#offsetGet
172
     * オフセットの位置の値を返却する
173
     * @return mixed 値
174
     */
175 16
    public function offsetGet($offset)
176
    {
177 16
        if ($this->stmt !== null) {
178 16
            $this->toArray();
179
        }
180
181 16
        return $this->rowCache[$offset];
182
    }
183
184
    /**
185
     * Implements ArrayAccess#offsetSet
186
     * オフセットの位置に値を設定する
187
     * @param mixed オフセット
0 ignored issues
show
Documentation Bug introduced by
The doc comment オフセット at position 0 could not be parsed: Unknown type name 'オフセット' at position 0 in オフセット.
Loading history...
188
     * @param mixed 値
189
     */
190
    public function offsetSet($offset, $value)
191
    {
192
        throw new CollectionException("Database results are read only.");
193
    }
194
195
    /**
196
     * Implements ArrayAccess#offsetUnSet
197
     * オフセットの設定を解除する
198
     * @param mixed オフセット
0 ignored issues
show
Documentation Bug introduced by
The doc comment オフセット at position 0 could not be parsed: Unknown type name 'オフセット' at position 0 in オフセット.
Loading history...
199
     */
200
    public function offsetUnSet($offset)
201
    {
202
        throw new CollectionException("Database results are read only.");
203
    }
204
205
    /**
206
     * 検索結果を全て配列として返却する
207
     * @return array<string> 検索結果
208
     */
209 28
    public function toArray()
210
    {
211 28
        $this->rowCache = $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
212 28
        $this->logger->debug("All results to array and cached.");
213 28
        $this->stmt = null;
214
215 28
        return $this->rowCache;
216
    }
217
218
    /**
219
     * 検索結果をエンティティとして返却する
220
     * @return ResultEntity 検索結果
221
     */
222 16
    public function toEntity($classpath)
223
    {
224 16
        $resultEntity = new ResultEntity($this->stmt->getWrappedStatement(), $classpath);
225 16
        $resultEntity->inject('logger', $this->logger)->initialize();
226
227 16
        return $resultEntity;
228
    }
229
}
230