Completed
Push — master ( f42a76...e2cf37 )
by Andreas
05:39
created

MongoTimestamp::__construct()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 5
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\TypeInterface;
17
use MongoDB\BSON\Timestamp;
18
19
class MongoTimestamp implements TypeInterface
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...
20
{
21
    /**
22
     * @var int
23
     */
24
    private static $globalInc = 0;
25
26
    /**
27
     * @link http://php.net/manual/en/class.mongotimestamp.php#mongotimestamp.props.sec
28
     * @var int
29
     */
30
    public $sec;
31
32
    /**
33
     * @link http://php.net/manual/en/class.mongotimestamp.php#mongotimestamp.props.inc
34
     * @var int
35
     */
36
    public $inc;
37
38
    /**
39
     * Creates a new timestamp. If no parameters are given, the current time is used
40
     * and the increment is automatically provided. The increment is set to 0 when the
41
     * module is loaded and is incremented every time this constructor is called
42
     * (without the $inc parameter passed in).
43
     *
44
     * @link http://php.net/manual/en/mongotimestamp.construct.php
45
     * @param int $sec [optional] Number of seconds since January 1st, 1970
46
     * @param int $inc [optional] Increment
47
     */
48
    public function __construct($sec = 0, $inc = 0)
49
    {
50
        if ($sec instanceof Timestamp) {
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\Timestamp does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
51
            // Only way is to convert is from string: [<sec>:<inc>]
52
            $parts = explode(':', substr((string) $sec, 1, -1));
53
            $this->sec = (int) $parts[0];
54
            $this->inc = (int) $parts[1];
55
56
            return;
57
        }
58
59
        if (func_num_args() == 0) {
60
            $sec = time();
61
        }
62
63
        if (func_num_args() <= 1) {
64
            $inc = static::$globalInc;
0 ignored issues
show
Bug introduced by
Since $globalInc is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $globalInc to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
65
            static::$globalInc++;
0 ignored issues
show
Bug introduced by
Since $globalInc is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $globalInc to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
66
        }
67
68
        $this->sec = (int) $sec;
69
        $this->inc = (int) $inc;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function __toString()
76
    {
77
        return (string) $this->sec;
78
    }
79
80
    /**
81
     * Converts this MongoTimestamp to the new BSON Timestamp type
82
     *
83
     * @return Timestamp
84
     * @internal This method is not part of the ext-mongo API
85
     */
86
    public function toBSONType()
87
    {
88
        return new Timestamp($this->sec, $this->inc);
89
    }
90
}
91