Completed
Pull Request — master (#13)
by Andreas
02:58
created

MongoDB::getReadPreference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
16
use Alcaeus\MongoDbAdapter\Helper;
17
use MongoDB\Model\CollectionInfo;
18
19
/**
20
 * Instances of this class are used to interact with a database.
21
 * @link http://www.php.net/manual/en/class.mongodb.php
22
 */
23
class MongoDB
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    use Helper\ReadPreference;
26
    use Helper\WriteConcern;
27
28
    const PROFILING_OFF = 0;
29
    const PROFILING_SLOW = 1;
30
    const PROFILING_ON = 2;
31
32
    /**
33
     * @var \MongoDB\Database
34
     */
35
    protected $db;
36
37
    /**
38
     * Creates a new database
39
     *
40
     * This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through {@see Mongo::__get()} or {@see Mongo::selectDB()}.
41
     * @link http://www.php.net/manual/en/mongodb.construct.php
42
     * @param MongoClient $conn Database connection.
43
     * @param string $name Database name.
44
     * @throws Exception
45
     * @return MongoDB Returns the database.
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
46
     */
47
    public function __construct($conn, $name)
48
    {
49
        $this->connection = $conn;
0 ignored issues
show
Documentation introduced by
The property connection does not exist on object<MongoDB>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
        $this->name = $name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<MongoDB>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
52
        $this->setReadPreferenceFromArray($conn->getReadPreference());
53
        $this->setWriteConcernFromArray($conn->getWriteConcern());
54
55
        $this->createDatabaseObject();
56
    }
57
58
    /**
59
     * @return \MongoDB\Database
60
     * @internal
61
     */
62
    public function getDb()
63
    {
64
        return $this->db;
65
    }
66
67
    /**
68
     * The name of this database
69
     * @link http://www.php.net/manual/en/mongodb.--tostring.php
70
     * @return string Returns this database's name.
71
     */
72
    public function __toString()
73
    {
74
        return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<MongoDB>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
    }
76
77
    /**
78
     * Gets a collection
79
     *
80
     * @link http://www.php.net/manual/en/mongodb.get.php
81
     * @param string $name The name of the collection.
82
     * @return MongoCollection
83
     */
84
    public function __get($name)
85
    {
86
        // Handle w and wtimeout properties that replicate data stored in $readPreference
87
        if ($name === 'w' || $name === 'wtimeout') {
88
            return $this->getWriteConcern()[$name];
89
        }
90
91
        return $this->selectCollection($name);
92
    }
93
94
    /**
95
     * @param string $name
96
     * @param mixed $value
97
     */
98 View Code Duplication
    public function __set($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        if ($name === 'w' || $name === 'wtimeout') {
101
            $this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern());
102
            $this->createDatabaseObject();
103
        }
104
    }
105
106
    /**
107
     * (PECL mongo &gt;= 1.3.0)<br/>
108
     * @link http://www.php.net/manual/en/mongodb.getcollectionnames.php
109
     * Get all collections from this database
110
     * @return array Returns the names of the all the collections in the database as an
111
     * {@link http://www.php.net/manual/en/language.types.array.php array}.
112
     */
113
    public function getCollectionNames(array $options = [])
114
    {
115
        if (is_bool($options)) {
116
            $options = ['includeSystemCollections' => $options];
117
        }
118
119
        $collections = $this->db->listCollections($options);
120
121
        $getCollectionName = function (CollectionInfo $collectionInfo) {
122
            return $collectionInfo->getName();
123
        };
124
125
        return array_map($getCollectionName, (array) $collections);
126
    }
127
128
    /**
129
     * @return MongoClient
130
     * @internal This method is not part of the ext-mongo API
131
     */
132
    public function getConnection()
133
    {
134
        return $this->connection;
0 ignored issues
show
Documentation introduced by
The property connection does not exist on object<MongoDB>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
    }
136
137
    /**
138
     * (PECL mongo &gt;= 0.9.0)<br/>
139
     * Fetches toolkit for dealing with files stored in this database
140
     * @link http://www.php.net/manual/en/mongodb.getgridfs.php
141
     * @param string $prefix [optional] The prefix for the files and chunks collections.
142
     * @return MongoGridFS Returns a new gridfs object for this database.
143
     */
144
    public function getGridFS($prefix = "fs")
145
    {
146
        return new \MongoGridFS($this, $prefix, $prefix);
147
    }
148
149
    /**
150
     * (PECL mongo &gt;= 0.9.0)<br/>
151
     * Gets this database's profiling level
152
     * @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php
153
     * @return int Returns the profiling level.
154
     */
155
    public function getProfilingLevel()
156
    {
157
        return static::PROFILING_OFF;
158
    }
159
160
    /**
161
     * (PECL mongo &gt;= 1.1.0)<br/>
162
     * Get slaveOkay setting for this database
163
     * @link http://www.php.net/manual/en/mongodb.getslaveokay.php
164
     * @return bool Returns the value of slaveOkay for this instance.
165
     */
166
    public function getSlaveOkay()
167
    {
168
        return false;
169
    }
170
171
    /**
172
     * (PECL mongo &gt;= 0.9.0)<br/>
173
     * Sets this database's profiling level
174
     * @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php
175
     * @param int $level Profiling level.
176
     * @return int Returns the previous profiling level.
177
     */
178
    public function setProfilingLevel($level)
0 ignored issues
show
Unused Code introduced by
The parameter $level 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...
179
    {
180
        return static::PROFILING_OFF;
181
    }
182
183
    /**
184
     * (PECL mongo &gt;= 0.9.0)<br/>
185
     * Drops this database
186
     * @link http://www.php.net/manual/en/mongodb.drop.php
187
     * @return array Returns the database response.
188
     */
189
    public function drop()
190
    {
191
        return $this->db->drop();
192
    }
193
194
    /**
195
     * Repairs and compacts this database
196
     * @link http://www.php.net/manual/en/mongodb.repair.php
197
     * @param bool $preserve_cloned_files [optional] <p>If cloned files should be kept if the repair fails.</p>
198
     * @param bool $backup_original_files [optional] <p>If original files should be backed up.</p>
199
     * @return array <p>Returns db response.</p>
200
     */
201
    public function repair($preserve_cloned_files = FALSE, $backup_original_files = FALSE)
0 ignored issues
show
Unused Code introduced by
The parameter $preserve_cloned_files 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...
Unused Code introduced by
The parameter $backup_original_files 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...
202
    {
203
        return [];
204
    }
205
206
    /**
207
     * (PECL mongo &gt;= 0.9.0)<br/>
208
     * Gets a collection
209
     * @link http://www.php.net/manual/en/mongodb.selectcollection.php
210
     * @param string $name <b>The collection name.</b>
211
     * @throws Exception if the collection name is invalid.
212
     * @return MongoCollection <p>
213
     * Returns a new collection object.
214
     * </p>
215
     */
216
    public function selectCollection($name)
217
    {
218
        return new MongoCollection($this, $name);
219
    }
220
221
    /**
222
     * (PECL mongo &gt;= 1.1.0)<br/>
223
     * Change slaveOkay setting for this database
224
     * @link http://php.net/manual/en/mongodb.setslaveokay.php
225
     * @param bool $ok [optional] <p>
226
     * If reads should be sent to secondary members of a replica set for all
227
     * possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance.
228
     * </p>
229
     * @return bool Returns the former value of slaveOkay for this instance.
230
     */
231
    public function setSlaveOkay ($ok = true)
0 ignored issues
show
Unused Code introduced by
The parameter $ok 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...
232
    {
233
        return false;
234
    }
235
236
    /**
237
     * Creates a collection
238
     * @link http://www.php.net/manual/en/mongodb.createcollection.php
239
     * @param string $name The name of the collection.
240
     * @param array $options [optional] <p>
241
     * <p>
242
     * An array containing options for the collections. Each option is its own
243
     * element in the options array, with the option name listed below being
244
     * the key of the element. The supported options depend on the MongoDB
245
     * server version. At the moment, the following options are supported:
246
     * </p>
247
     * <p>
248
     * <b>capped</b>
249
     * <p>
250
     * If the collection should be a fixed size.
251
     * </p>
252
     * </p>
253
     * <p>
254
     * <b>size</b>
255
     * <p>
256
     * If the collection is fixed size, its size in bytes.</p></p>
257
     * <p><b>max</b>
258
     * <p>If the collection is fixed size, the maximum number of elements to store in the collection.</p></p>
259
     * <i>autoIndexId</i>
260
     *
261
     * <p>
262
     * If capped is <b>TRUE</b> you can specify <b>FALSE</b> to disable the
263
     * automatic index created on the <em>_id</em> field.
264
     * Before MongoDB 2.2, the default value for
265
     * <em>autoIndexId</em> was <b>FALSE</b>.
266
     * </p>
267
     * </p>
268
     * @return MongoCollection <p>Returns a collection object representing the new collection.</p>
269
     */
270
    public function createCollection($name, $options)
271
    {
272
        $this->db->createCollection($name, $options);
273
        return $this->selectCollection($name);
274
    }
275
276
    /**
277
     * (PECL mongo &gt;= 0.9.0)<br/>
278
     * @deprecated Use MongoCollection::drop() instead.
279
     * Drops a collection
280
     * @link http://www.php.net/manual/en/mongodb.dropcollection.php
281
     * @param MongoCollection|string $coll MongoCollection or name of collection to drop.
282
     * @return array Returns the database response.
283
     */
284
    public function dropCollection($coll)
285
    {
286
        return $this->db->dropCollection((string) $coll);
287
    }
288
289
    /**
290
     * (PECL mongo &gt;= 0.9.0)<br/>
291
     * Get a list of collections in this database
292
     * @link http://www.php.net/manual/en/mongodb.listcollections.php
293
     * @param bool $includeSystemCollections [optional] <p>Include system collections.</p>
0 ignored issues
show
Bug introduced by
There is no parameter named $includeSystemCollections. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
294
     * @return array Returns a list of MongoCollections.
295
     */
296
    public function listCollections(array $options = [])
297
    {
298
        return array_map([$this, 'selectCollection'], $this->getCollectionNames($options));
299
    }
300
301
    /**
302
     * (PECL mongo &gt;= 0.9.0)<br/>
303
     * Creates a database reference
304
     * @link http://www.php.net/manual/en/mongodb.createdbref.php
305
     * @param string $collection The collection to which the database reference will point.
306
     * @param mixed $document_or_id <p>
307
     * If an array or object is given, its <em>_id</em> field will be
308
     * used as the reference ID. If a {@see MongoId} or scalar
309
     * is given, it will be used as the reference ID.
310
     * </p>
311
     * @return array <p>Returns a database reference array.</p>
312
     * <p>
313
     * If an array without an <em>_id</em> field was provided as the
314
     * <em>document_or_id</em> parameter, <b>NULL</b> will be returned.
315
     * </p>
316
     */
317
    public function createDBRef($collection, $document_or_id)
318
    {
319
        if (is_object($document_or_id)) {
320
            $id = isset($document_or_id->_id) ? $document_or_id->_id : null;
321
//            $id = $document_or_id->_id ?? null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
322
        } elseif (is_array($document_or_id)) {
323
            if (! isset($document_or_id['_id'])) {
324
                return null;
325
            }
326
327
            $id = $document_or_id['_id'];
328
        } else {
329
            $id = $document_or_id;
330
        }
331
332
        return [
333
            '$ref' => $collection,
334
            '$id' => $id,
335
            '$db' => $this->name,
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<MongoDB>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
336
        ];
337
    }
338
339
340
    /**
341
     * (PECL mongo &gt;= 0.9.0)<br/>
342
     * Fetches the document pointed to by a database reference
343
     * @link http://www.php.net/manual/en/mongodb.getdbref.php
344
     * @param array $ref A database reference.
345
     * @return array Returns the document pointed to by the reference.
346
     */
347
    public function getDBRef(array $ref)
0 ignored issues
show
Unused Code introduced by
The parameter $ref 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...
348
    {
349
        $this->notImplemented();
350
    }
351
352
    /**
353
     * (PECL mongo &gt;= 0.9.3)<br/>
354
     * Runs JavaScript code on the database server.
355
     * @link http://www.php.net/manual/en/mongodb.execute.php
356
     * @param MongoCode|string $code Code to execute.
357
     * @param array $args [optional] Arguments to be passed to code.
358
     * @return array Returns the result of the evaluation.
359
     */
360
    public function execute($code, array $args = array())
0 ignored issues
show
Unused Code introduced by
The parameter $code 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...
Unused Code introduced by
The parameter $args 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...
361
    {
362
        $this->notImplemented();
363
    }
364
365
    /**
366
     * Execute a database command
367
     * @link http://www.php.net/manual/en/mongodb.command.php
368
     * @param array $data The query to send.
369
     * @param array() $options [optional] <p>
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
370
     * This parameter is an associative array of the form
371
     * <em>array("optionname" =&gt; &lt;boolean&gt;, ...)</em>. Currently
372
     * supported options are:
373
     * </p><ul>
374
     * <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li>
375
     * </ul>
376
     * @return array Returns database response.
377
     * Every database response is always maximum one document,
378
     * which means that the result of a database command can never exceed 16MB.
379
     * The resulting document's structure depends on the command,
380
     * but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents.
381
     */
382
    public function command(array $data, $options, &$hash)
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
Unused Code introduced by
The parameter $hash 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...
383
    {
384
        try {
385
            $cursor = new \MongoCommandCursor($this->connection, $this->name, $data);
0 ignored issues
show
Documentation introduced by
The property connection does not exist on object<MongoDB>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property name does not exist on object<MongoDB>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
386
            $cursor->setReadPreference($this->getReadPreference());
0 ignored issues
show
Documentation introduced by
$this->getReadPreference() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
387
388
            return iterator_to_array($cursor)[0];
389
        } catch (\MongoDB\Driver\Exception\RuntimeException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\RuntimeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
390
            return [
391
                'ok' => 0,
392
                'errmsg' => $e->getMessage(),
393
                'code' => $e->getCode(),
394
            ];
395
        }
396
    }
397
398
    /**
399
     * (PECL mongo &gt;= 0.9.5)<br/>
400
     * Check if there was an error on the most recent db operation performed
401
     * @link http://www.php.net/manual/en/mongodb.lasterror.php
402
     * @return array Returns the error, if there was one.
403
     */
404
    public function lastError()
405
    {
406
        $this->notImplemented();
407
    }
408
409
    /**
410
     * (PECL mongo &gt;= 0.9.5)<br/>
411
     * Checks for the last error thrown during a database operation
412
     * @link http://www.php.net/manual/en/mongodb.preverror.php
413
     * @return array Returns the error and the number of operations ago it occurred.
414
     */
415
    public function prevError()
416
    {
417
        $this->notImplemented();
418
    }
419
420
    /**
421
     * (PECL mongo &gt;= 0.9.5)<br/>
422
     * Clears any flagged errors on the database
423
     * @link http://www.php.net/manual/en/mongodb.reseterror.php
424
     * @return array Returns the database response.
425
     */
426
    public function resetError()
427
    {
428
        $this->notImplemented();
429
    }
430
431
    /**
432
     * (PECL mongo &gt;= 0.9.5)<br/>
433
     * Creates a database error
434
     * @link http://www.php.net/manual/en/mongodb.forceerror.php
435
     * @return boolean Returns the database response.
436
     */
437
    public function forceError()
438
    {
439
        $this->notImplemented();
440
    }
441
442
    /**
443
     * (PECL mongo &gt;= 1.0.1)<br/>
444
     * Log in to this database
445
     * @link http://www.php.net/manual/en/mongodb.authenticate.php
446
     * @param string $username The username.
447
     * @param string $password The password (in plaintext).
448
     * @return array <p>Returns database response. If the login was successful, it will return 1.</p>
449
     * <p>
450
     * <span style="color: #0000BB">&lt;?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?&gt;</span>
451
     * </span>
452
     * </code></div>
453
     * </div>
454
     * </p>
455
     * <p> If something went wrong, it will return </p>
456
     * <p>
457
     * <div class="example-contents">
458
     * <div class="phpcode"><code><span style="color: #000000">
459
     * <span style="color: #0000BB">&lt;?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"errmsg"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"auth&nbsp;fails"</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?&gt;</span></p>
460
     *         <p>("auth fails" could be another message, depending on database version and
461
     *         what went wrong)</p>
462
     */
463
    public function authenticate($username, $password)
0 ignored issues
show
Unused Code introduced by
The parameter $username 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...
Unused Code introduced by
The parameter $password 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...
464
    {
465
        $this->notImplemented();
466
    }
467
468
    /**
469
     * {@inheritdoc}
470
     */
471
    public function setReadPreference($readPreference, $tags = null)
472
    {
473
        $result = $this->setReadPreferenceFromParameters($readPreference, $tags);
474
        $this->createDatabaseObject();
475
476
        return $result;
477
    }
478
479
    /**
480
     * {@inheritdoc}
481
     */
482
    public function setWriteConcern($wstring, $wtimeout = 0)
483
    {
484
        $result = $this->setWriteConcernFromParameters($wstring, $wtimeout);
485
        $this->createDatabaseObject();
486
487
        return $result;
488
    }
489
490
    protected function notImplemented()
491
    {
492
        throw new \Exception('Not implemented');
493
    }
494
495
    /**
496
     * @return \MongoDB\Database
497
     */
498 View Code Duplication
    private function createDatabaseObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
499
    {
500
        $options = [
501
            'readPreference' => $this->readPreference,
502
            'writeConcern' => $this->writeConcern,
503
        ];
504
505
        if ($this->db === null) {
506
            $this->db = $this->connection->getClient()->selectDatabase($this->name, $options);
0 ignored issues
show
Documentation introduced by
The property connection does not exist on object<MongoDB>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property name does not exist on object<MongoDB>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
507
        } else {
508
            $this->db = $this->db->withOptions($options);
509
        }
510
    }
511
}
512