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

MongoDate::truncateMicroSeconds()   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 1
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\UTCDateTime;
18
19
class MongoDate 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
     * @link http://php.net/manual/en/class.mongodate.php#mongodate.props.sec
23
     * @var int $sec
24
     */
25
    public $sec;
26
27
    /**
28
     * @link http://php.net/manual/en/class.mongodate.php#mongodate.props.usec
29
     * @var int $usec
30
     */
31
    public $usec;
32
33
    /**
34
     * Creates a new date. If no parameters are given, the current time is used.
35
     *
36
     * @link http://php.net/manual/en/mongodate.construct.php
37
     * @param int $sec Number of seconds since January 1st, 1970
38
     * @param int $usec Microseconds
39
     * @return MongoDate Returns this new date
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...
40
     */
41
    public function __construct($sec = 0, $usec = 0)
42
    {
43
        if (func_num_args() == 0) {
44
            $time = microtime(true);
45
            $sec = floor($time);
46
            $usec = ($time - $sec) * 1000000.0;
47
        } elseif ($sec instanceof UTCDateTime) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\UTCDateTime 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...
48
            $msecString = (string) $sec;
49
50
            $sec = (int) substr($msecString, 0, -3);
51
            $usec = ((int) substr($msecString, -3)) * 1000;
52
        }
53
54
        $this->sec = $sec;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sec can also be of type double. However, the property $sec is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55
        $this->usec = $this->truncateMicroSeconds($usec);
56
    }
57
58
    /**
59
     * Returns a string representation of this date
60
     * @return string
61
     */
62
    public function __toString()
63
    {
64
        return (string) sprintf('%.8f', $this->truncateMicroSeconds($this->usec) / 1000000) . ' ' . $this->sec;
65
    }
66
67
    /**
68
     * Converts this MongoDate to the new BSON UTCDateTime type
69
     *
70
     * @return UTCDateTime
71
     * @internal This method is not part of the ext-mongo API
72
     */
73
    public function toBSONType()
74
    {
75
        $milliSeconds = ($this->sec * 1000) + ($this->truncateMicroSeconds($this->usec) / 1000);
76
77
        return new UTCDateTime($milliSeconds);
78
    }
79
80
    /**
81
     * Returns a DateTime object representing this date
82
     * @link http://php.net/manual/en/mongodate.todatetime.php
83
     * @return DateTime
84
     */
85
    public function toDateTime()
86
    {
87
        $datetime = new \DateTime();
88
        $datetime->setTimestamp($this->sec);
89
90
        $microSeconds = $this->truncateMicroSeconds($this->usec);
91
        if ($microSeconds > 0) {
92
            $datetime = \DateTime::createFromFormat('Y-m-d H:i:s.u', $datetime->format('Y-m-d H:i:s') . '.' . $microSeconds);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor.... '.' . $microSeconds); of type DateTime|false adds false to the return on line 95 which is incompatible with the return type documented by MongoDate::toDateTime of type DateTime. It seems like you forgot to handle an error condition.
Loading history...
93
        }
94
95
        return $datetime;
96
    }
97
98
    /**
99
     * @param int $usec
100
     * @return int
101
     */
102
    private function truncateMicroSeconds($usec)
103
    {
104
        return (int) floor($usec / 1000) * 1000;
105
    }
106
}
107