Completed
Branch 09branch (946dde)
by Anton
05:16
created

UTCMongoTimestamp   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchTimestamp() 0 8 2
A packValue() 0 4 1
A buildAtomics() 0 4 1
1
<?php
2
/**
3
 * spiral
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\Models\Accessors;
9
10
use MongoDB\BSON\UTCDateTime;
11
use Spiral\ODM\CompositableInterface;
12
13
/**
14
 * Timezone is fixed for mongodb. Packs into MongoDate
15
 */
16
class UTCMongoTimestamp extends AbstractTimestamp implements CompositableInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected function fetchTimestamp($value): int
22
    {
23
        if ($value 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...
24
            $value = $value->toDateTime();
25
        }
26
27
        return $this->castTimestamp($value, new \DateTimeZone('UTC')) ?? 0;
28
    }
29
30
    /**
31
     * @return \MongoDB\BSON\UTCDateTime
32
     */
33
    public function packValue()
34
    {
35
        return new UTCDateTime($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \MongoDB\BSON\UTCDateTime($this); (MongoDB\BSON\UTCDateTime) is incompatible with the return type of the parent method Spiral\Models\Accessors\...actTimestamp::packValue of type DateTimeInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function buildAtomics(string $container = ''): array
42
    {
43
        return ['$set' => [$container => $this->packValue()]];
44
    }
45
}