MongoDateConversion   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toDateTime() 0 11 3
A toDateTimeImmutable() 0 5 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Db;
22
23
/**
24
 * Can be used by any class which accesses MongoDB.
25
 */
26
trait MongoDateConversion
27
{
28
    /**
29
     * Tries to convert a `UTCDateTime` into a `DateTime`.
30
     *
31
     * @param mixed $value The value to convert
32
     * @param \DateTimeZone $timeZone Optional. Time Zone to set.
33
     * @return \DateTime|null The date if `$value` is a `UTCDateTime`, `null` otherwise
34
     */
35
    protected function toDateTime($value, \DateTimeZone $timeZone = null): ?\DateTime
36
    {
37
        if ($value instanceof \MongoDB\BSON\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...
38
            $date = $value->toDateTime();
39
            if ($timeZone !== null) {
40
                $date->setTimezone($timeZone);
41
            }
42
            return $date;
43
        }
44
        return null;
45
    }
46
47
    /**
48
     * Tries to convert a `UTCDateTime` into a `DateTimeImmutable`.
49
     *
50
     * @param mixed $value The value to convert
51
     * @return \DateTimeImmutable|null The date if `$value` is a `UTCDateTime`, `null` otherwise
52
     */
53
    protected function toDateTimeImmutable($value): ?\DateTimeImmutable
54
    {
55
        return $value instanceof \MongoDB\BSON\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...
56
            new \DateTimeImmutable('@' . $value->toDateTime()->getTimestamp()) : null;
57
    }
58
}
59