Completed
Push — master ( 87aba0...0e8729 )
by Andreas
25s queued 16s
created

lib/Doctrine/ODM/MongoDB/CommandCursor.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB;
21
22
use BadMethodCallException;
23
use Doctrine\MongoDB\CommandCursor as BaseCommandCursor;
24
use Doctrine\MongoDB\Iterator;
25
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
26
27
/**
28
 * Wrapper for the CommandCursor class.
29
 *
30
 * @since  1.1
31
 * @author alcaeus <[email protected]>
32
 */
33
class CommandCursor implements Iterator
34
{
35
    /**
36
     * The CommandCursor instance being wrapped.
37
     *
38
     * @var BaseCommandCursor
39
     */
40
    private $commandCursor;
41
42
    /**
43
     * @var UnitOfWork
44
     */
45
    private $unitOfWork;
46
47
    /**
48
     * @var ClassMetadata
49
     */
50
    private $class;
51
52
    /**
53
     * @param BaseCommandCursor $commandCursor The ComamndCursor instance being wrapped
54
     * @param UnitOfWork $unitOfWork
55
     * @param ClassMetadata $class The class to use for hydration or null if results should not be hydrated
56
     */
57 9
    public function __construct(BaseCommandCursor $commandCursor, UnitOfWork $unitOfWork, ClassMetadata $class = null)
58
    {
59 9
        $this->commandCursor = $commandCursor;
60 9
        $this->unitOfWork = $unitOfWork;
61 9
        $this->class = $class;
62 9
    }
63
64
    /**
65
     * Wrapper method for MongoCommandCursor::batchSize().
66
     *
67
     * @see http://php.net/manual/en/mongocommandcursor.batchsize.php
68
     * @param integer $num
69
     * @return self
70
     */
71
    public function batchSize($num)
72
    {
73
        $this->commandCursor->batchSize($num);
74
75
        return $this;
76
    }
77
78
    /**
79
     * Recreates the command cursor and counts its results.
80
     *
81
     * @see http://php.net/manual/en/countable.count.php
82
     * @return integer
83
     */
84 2
    public function count()
85
    {
86 2
        return $this->commandCursor->count();
87
    }
88
89
    /**
90
     * Wrapper method for MongoCommandCursor::current().
91
     *
92
     * @see http://php.net/manual/en/iterator.current.php
93
     * @see http://php.net/manual/en/mongocommandcursor.current.php
94
     * @return object|array|null
95
     */
96 8
    public function current()
97
    {
98 8
        return $this->hydrateDocument($this->commandCursor->current());
0 ignored issues
show
It seems like $this->commandCursor->current() targeting Doctrine\MongoDB\CommandCursor::current() can also be of type null; however, Doctrine\ODM\MongoDB\Com...rsor::hydrateDocument() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
99
    }
100
101
    /**
102
     * Wrapper method for MongoCommandCursor::dead().
103
     *
104
     * @see http://php.net/manual/en/mongocommandcursor.dead.php
105
     * @return boolean
106
     */
107
    public function dead()
108
    {
109
        return $this->commandCursor->dead();
110
    }
111
112
    /**
113
     * Returns the MongoCommandCursor instance being wrapped.
114
     *
115
     * @return BaseCommandCursor
116
     */
117
    public function getBaseCursor()
118
    {
119
        return $this->commandCursor;
120
    }
121
122
    /**
123
     * Rewinds the cursor and returns its first result.
124
     *
125
     * @see Iterator::getSingleResult()
126
     * @return object|array|null
127
     */
128
    public function getSingleResult()
129
    {
130
        return $this->hydrateDocument($this->commandCursor->getSingleResult());
0 ignored issues
show
It seems like $this->commandCursor->getSingleResult() targeting Doctrine\MongoDB\CommandCursor::getSingleResult() can also be of type null; however, Doctrine\ODM\MongoDB\Com...rsor::hydrateDocument() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
131
    }
132
133
    /**
134
     * @param array $document
135
     * @return array|object|null
136
     */
137 8
    private function hydrateDocument($document)
138
    {
139 8
        if ($document !== null && $this->class !== null) {
140 1
            return $this->unitOfWork->getOrCreateDocument($this->class->name, $document);
141
        }
142
143 7
        return $document;
144
    }
145
146
    /**
147
     * Wrapper method for MongoCommandCursor::info().
148
     *
149
     * @see http://php.net/manual/en/mongocommandcursor.info.php
150
     * @return array
151
     */
152
    public function info()
153
    {
154
        return $this->commandCursor->info();
155
    }
156
157
    /**
158
     * Wrapper method for MongoCommandCursor::key().
159
     *
160
     * @see http://php.net/manual/en/iterator.key.php
161
     * @see http://php.net/manual/en/mongocommandcursor.key.php
162
     * @return integer
163
     */
164 8
    public function key()
165
    {
166 8
        return $this->commandCursor->key();
167
    }
168
169
    /**
170
     * Wrapper method for MongoCommandCursor::next().
171
     *
172
     * @see http://php.net/manual/en/iterator.next.php
173
     * @see http://php.net/manual/en/mongocommandcursor.next.php
174
     */
175 8
    public function next()
176
    {
177 8
        $cursor = $this;
178
179 8
        $cursor->commandCursor->next();
180 8
    }
181
182
    /**
183
     * Wrapper method for MongoCommandCursor::rewind().
184
     *
185
     * @see http://php.net/manual/en/iterator.rewind.php
186
     * @see http://php.net/manual/en/mongocommandcursor.rewind.php
187
     * @return array
188
     */
189 8
    public function rewind()
190
    {
191 8
        return $this->commandCursor->rewind();
192
    }
193
194
    /**
195
     * Wrapper method for MongoCommandCursor::timeout().
196
     *
197
     * @see http://php.net/manual/en/mongocommandcursor.timeout.php
198
     * @param integer $ms
199
     * @return self
200
     * @throws BadMethodCallException if MongoCommandCursor::timeout() is not available
201
     */
202
    public function timeout($ms)
203
    {
204
        $this->commandCursor->timeout($ms);
205
206
        return $this;
207
    }
208
209
    /**
210
     * Return the cursor's results as an array.
211
     *
212
     * @see Iterator::toArray()
213
     * @return array
214
     */
215 8
    public function toArray()
216
    {
217 8
        return iterator_to_array($this);
218
    }
219
220
    /**
221
     * Wrapper method for MongoCommandCursor::valid().
222
     *
223
     * @see http://php.net/manual/en/iterator.valid.php
224
     * @see http://php.net/manual/en/mongocommandcursor.valid.php
225
     * @return boolean
226
     */
227 8
    public function valid()
228
    {
229 8
        return $this->commandCursor->valid();
230
    }
231
}
232