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

MongoDB::__set()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %
Metric Value
dl 9
loc 9
rs 9.6667
cc 3
eloc 6
nc 2
nop 2
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
        } else {
104
            $this->$name = $value;
105
        }
106
    }
107
108
    /**
109
     * (PECL mongo &gt;= 1.3.0)<br/>
110
     * @link http://www.php.net/manual/en/mongodb.getcollectionnames.php
111
     * Get all collections from this database
112
     * @return array Returns the names of the all the collections in the database as an
113
     * {@link http://www.php.net/manual/en/language.types.array.php array}.
114
     */
115
    public function getCollectionNames(array $options = [])
116
    {
117
        if (is_bool($options)) {
118
            $options = ['includeSystemCollections' => $options];
119
        }
120
121
        $collections = $this->db->listCollections($options);
122
123
        $getCollectionName = function (CollectionInfo $collectionInfo) {
124
            return $collectionInfo->getName();
125
        };
126
127
        return array_map($getCollectionName, (array) $collections);
128
    }
129
130
    /**
131
     * @return MongoClient
132
     * @internal This method is not part of the ext-mongo API
133
     */
134
    public function getConnection()
135
    {
136
        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...
137
    }
138
139
    /**
140
     * (PECL mongo &gt;= 0.9.0)<br/>
141
     * Fetches toolkit for dealing with files stored in this database
142
     * @link http://www.php.net/manual/en/mongodb.getgridfs.php
143
     * @param string $prefix [optional] The prefix for the files and chunks collections.
144
     * @return MongoGridFS Returns a new gridfs object for this database.
145
     */
146
    public function getGridFS($prefix = "fs")
147
    {
148
        return new \MongoGridFS($this, $prefix, $prefix);
149
    }
150
151
    /**
152
     * (PECL mongo &gt;= 0.9.0)<br/>
153
     * Gets this database's profiling level
154
     * @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php
155
     * @return int Returns the profiling level.
156
     */
157
    public function getProfilingLevel()
158
    {
159
        return static::PROFILING_OFF;
160
    }
161
162
    /**
163
     * (PECL mongo &gt;= 1.1.0)<br/>
164
     * Get slaveOkay setting for this database
165
     * @link http://www.php.net/manual/en/mongodb.getslaveokay.php
166
     * @return bool Returns the value of slaveOkay for this instance.
167
     */
168
    public function getSlaveOkay()
169
    {
170
        return false;
171
    }
172
173
    /**
174
     * (PECL mongo &gt;= 0.9.0)<br/>
175
     * Sets this database's profiling level
176
     * @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php
177
     * @param int $level Profiling level.
178
     * @return int Returns the previous profiling level.
179
     */
180
    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...
181
    {
182
        return static::PROFILING_OFF;
183
    }
184
185
    /**
186
     * (PECL mongo &gt;= 0.9.0)<br/>
187
     * Drops this database
188
     * @link http://www.php.net/manual/en/mongodb.drop.php
189
     * @return array Returns the database response.
190
     */
191
    public function drop()
192
    {
193
        return $this->db->drop();
194
    }
195
196
    /**
197
     * Repairs and compacts this database
198
     * @link http://www.php.net/manual/en/mongodb.repair.php
199
     * @param bool $preserve_cloned_files [optional] <p>If cloned files should be kept if the repair fails.</p>
200
     * @param bool $backup_original_files [optional] <p>If original files should be backed up.</p>
201
     * @return array <p>Returns db response.</p>
202
     */
203
    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...
204
    {
205
        return [];
206
    }
207
208
    /**
209
     * (PECL mongo &gt;= 0.9.0)<br/>
210
     * Gets a collection
211
     * @link http://www.php.net/manual/en/mongodb.selectcollection.php
212
     * @param string $name <b>The collection name.</b>
213
     * @throws Exception if the collection name is invalid.
214
     * @return MongoCollection <p>
215
     * Returns a new collection object.
216
     * </p>
217
     */
218
    public function selectCollection($name)
219
    {
220
        return new MongoCollection($this, $name);
221
    }
222
223
    /**
224
     * (PECL mongo &gt;= 1.1.0)<br/>
225
     * Change slaveOkay setting for this database
226
     * @link http://php.net/manual/en/mongodb.setslaveokay.php
227
     * @param bool $ok [optional] <p>
228
     * If reads should be sent to secondary members of a replica set for all
229
     * possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance.
230
     * </p>
231
     * @return bool Returns the former value of slaveOkay for this instance.
232
     */
233
    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...
234
    {
235
        return false;
236
    }
237
238
    /**
239
     * Creates a collection
240
     * @link http://www.php.net/manual/en/mongodb.createcollection.php
241
     * @param string $name The name of the collection.
242
     * @param array $options [optional] <p>
243
     * <p>
244
     * An array containing options for the collections. Each option is its own
245
     * element in the options array, with the option name listed below being
246
     * the key of the element. The supported options depend on the MongoDB
247
     * server version. At the moment, the following options are supported:
248
     * </p>
249
     * <p>
250
     * <b>capped</b>
251
     * <p>
252
     * If the collection should be a fixed size.
253
     * </p>
254
     * </p>
255
     * <p>
256
     * <b>size</b>
257
     * <p>
258
     * If the collection is fixed size, its size in bytes.</p></p>
259
     * <p><b>max</b>
260
     * <p>If the collection is fixed size, the maximum number of elements to store in the collection.</p></p>
261
     * <i>autoIndexId</i>
262
     *
263
     * <p>
264
     * If capped is <b>TRUE</b> you can specify <b>FALSE</b> to disable the
265
     * automatic index created on the <em>_id</em> field.
266
     * Before MongoDB 2.2, the default value for
267
     * <em>autoIndexId</em> was <b>FALSE</b>.
268
     * </p>
269
     * </p>
270
     * @return MongoCollection <p>Returns a collection object representing the new collection.</p>
271
     */
272
    public function createCollection($name, $options)
273
    {
274
        $this->db->createCollection($name, $options);
275
        return $this->selectCollection($name);
276
    }
277
278
    /**
279
     * (PECL mongo &gt;= 0.9.0)<br/>
280
     * @deprecated Use MongoCollection::drop() instead.
281
     * Drops a collection
282
     * @link http://www.php.net/manual/en/mongodb.dropcollection.php
283
     * @param MongoCollection|string $coll MongoCollection or name of collection to drop.
284
     * @return array Returns the database response.
285
     */
286
    public function dropCollection($coll)
287
    {
288
        return $this->db->dropCollection((string) $coll);
289
    }
290
291
    /**
292
     * (PECL mongo &gt;= 0.9.0)<br/>
293
     * Get a list of collections in this database
294
     * @link http://www.php.net/manual/en/mongodb.listcollections.php
295
     * @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...
296
     * @return array Returns a list of MongoCollections.
297
     */
298
    public function listCollections(array $options = [])
299
    {
300
        return array_map([$this, 'selectCollection'], $this->getCollectionNames($options));
301
    }
302
303
    /**
304
     * (PECL mongo &gt;= 0.9.0)<br/>
305
     * Creates a database reference
306
     * @link http://www.php.net/manual/en/mongodb.createdbref.php
307
     * @param string $collection The collection to which the database reference will point.
308
     * @param mixed $document_or_id <p>
309
     * If an array or object is given, its <em>_id</em> field will be
310
     * used as the reference ID. If a {@see MongoId} or scalar
311
     * is given, it will be used as the reference ID.
312
     * </p>
313
     * @return array <p>Returns a database reference array.</p>
314
     * <p>
315
     * If an array without an <em>_id</em> field was provided as the
316
     * <em>document_or_id</em> parameter, <b>NULL</b> will be returned.
317
     * </p>
318
     */
319
    public function createDBRef($collection, $document_or_id)
320
    {
321
        if (is_object($document_or_id)) {
322
            $id = isset($document_or_id->_id) ? $document_or_id->_id : null;
323
//            $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...
324
        } elseif (is_array($document_or_id)) {
325
            if (! isset($document_or_id['_id'])) {
326
                return null;
327
            }
328
329
            $id = $document_or_id['_id'];
330
        } else {
331
            $id = $document_or_id;
332
        }
333
334
        return [
335
            '$ref' => $collection,
336
            '$id' => $id,
337
            '$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...
338
        ];
339
    }
340
341
342
    /**
343
     * (PECL mongo &gt;= 0.9.0)<br/>
344
     * Fetches the document pointed to by a database reference
345
     * @link http://www.php.net/manual/en/mongodb.getdbref.php
346
     * @param array $ref A database reference.
347
     * @return array Returns the document pointed to by the reference.
348
     */
349
    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...
350
    {
351
        $this->notImplemented();
352
    }
353
354
    /**
355
     * (PECL mongo &gt;= 0.9.3)<br/>
356
     * Runs JavaScript code on the database server.
357
     * @link http://www.php.net/manual/en/mongodb.execute.php
358
     * @param MongoCode|string $code Code to execute.
359
     * @param array $args [optional] Arguments to be passed to code.
360
     * @return array Returns the result of the evaluation.
361
     */
362
    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...
363
    {
364
        $this->notImplemented();
365
    }
366
367
    /**
368
     * Execute a database command
369
     * @link http://www.php.net/manual/en/mongodb.command.php
370
     * @param array $data The query to send.
371
     * @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...
372
     * This parameter is an associative array of the form
373
     * <em>array("optionname" =&gt; &lt;boolean&gt;, ...)</em>. Currently
374
     * supported options are:
375
     * </p><ul>
376
     * <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li>
377
     * </ul>
378
     * @return array Returns database response.
379
     * Every database response is always maximum one document,
380
     * which means that the result of a database command can never exceed 16MB.
381
     * The resulting document's structure depends on the command,
382
     * but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents.
383
     */
384
    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...
385
    {
386
        try {
387
            $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...
388
            $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...
389
390
            return iterator_to_array($cursor)[0];
391
        } 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...
392
            return [
393
                'ok' => 0,
394
                'errmsg' => $e->getMessage(),
395
                'code' => $e->getCode(),
396
            ];
397
        }
398
    }
399
400
    /**
401
     * (PECL mongo &gt;= 0.9.5)<br/>
402
     * Check if there was an error on the most recent db operation performed
403
     * @link http://www.php.net/manual/en/mongodb.lasterror.php
404
     * @return array Returns the error, if there was one.
405
     */
406
    public function lastError()
407
    {
408
        $this->notImplemented();
409
    }
410
411
    /**
412
     * (PECL mongo &gt;= 0.9.5)<br/>
413
     * Checks for the last error thrown during a database operation
414
     * @link http://www.php.net/manual/en/mongodb.preverror.php
415
     * @return array Returns the error and the number of operations ago it occurred.
416
     */
417
    public function prevError()
418
    {
419
        $this->notImplemented();
420
    }
421
422
    /**
423
     * (PECL mongo &gt;= 0.9.5)<br/>
424
     * Clears any flagged errors on the database
425
     * @link http://www.php.net/manual/en/mongodb.reseterror.php
426
     * @return array Returns the database response.
427
     */
428
    public function resetError()
429
    {
430
        $this->notImplemented();
431
    }
432
433
    /**
434
     * (PECL mongo &gt;= 0.9.5)<br/>
435
     * Creates a database error
436
     * @link http://www.php.net/manual/en/mongodb.forceerror.php
437
     * @return boolean Returns the database response.
438
     */
439
    public function forceError()
440
    {
441
        $this->notImplemented();
442
    }
443
444
    /**
445
     * (PECL mongo &gt;= 1.0.1)<br/>
446
     * Log in to this database
447
     * @link http://www.php.net/manual/en/mongodb.authenticate.php
448
     * @param string $username The username.
449
     * @param string $password The password (in plaintext).
450
     * @return array <p>Returns database response. If the login was successful, it will return 1.</p>
451
     * <p>
452
     * <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>
453
     * </span>
454
     * </code></div>
455
     * </div>
456
     * </p>
457
     * <p> If something went wrong, it will return </p>
458
     * <p>
459
     * <div class="example-contents">
460
     * <div class="phpcode"><code><span style="color: #000000">
461
     * <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>
462
     *         <p>("auth fails" could be another message, depending on database version and
463
     *         what went wrong)</p>
464
     */
465
    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...
466
    {
467
        $this->notImplemented();
468
    }
469
470
    /**
471
     * {@inheritdoc}
472
     */
473
    public function setReadPreference($readPreference, $tags = null)
474
    {
475
        $result = $this->setReadPreferenceFromParameters($readPreference, $tags);
476
        $this->createDatabaseObject();
477
478
        return $result;
479
    }
480
481
    /**
482
     * {@inheritdoc}
483
     */
484
    public function setWriteConcern($wstring, $wtimeout = 0)
485
    {
486
        $result = $this->setWriteConcernFromParameters($wstring, $wtimeout);
487
        $this->createDatabaseObject();
488
489
        return $result;
490
    }
491
492
    protected function notImplemented()
493
    {
494
        throw new \Exception('Not implemented');
495
    }
496
497
    /**
498
     * @return \MongoDB\Database
499
     */
500 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...
501
    {
502
        $options = [
503
            'readPreference' => $this->readPreference,
504
            'writeConcern' => $this->writeConcern,
505
        ];
506
507
        if ($this->db === null) {
508
            $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...
509
        } else {
510
            $this->db = $this->db->withOptions($options);
511
        }
512
    }
513
}
514