GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 1a6c54...3594bb )
by .
12:56 queued 01:59
created

Model::freshTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace duxet\Rethinkdb\Eloquent;
4
5
use DateTime;
6
use duxet\Rethinkdb\Eloquent\Relations\BelongsTo;
7
use duxet\Rethinkdb\Query\Builder as QueryBuilder;
8
use Illuminate\Database\Eloquent\Relations\HasMany;
9
use Illuminate\Database\Eloquent\Relations\HasOne;
10
11
class Model extends \Illuminate\Database\Eloquent\Model
12
{
13
    /**
14
     * Get the format for database stored dates.
15
     *
16
     * @return string
17
     */
18
    protected function getDateFormat()
19
    {
20
        return 'Y-m-d H:i:s';
21
    }
22
23
    /**
24
     * Get the table qualified key name.
25
     *
26
     * @return string
27
     */
28
    public function getQualifiedKeyName()
29
    {
30
        return $this->getKeyName();
31
    }
32
33
    /**
34
     * Ensure Timestamps are returned in DateTime.
35
     *
36
     * @param \DateTime $value
37
     *
38
     * @return \DateTime
39
     */
40
    protected function asDateTime($value)
41
    {
42
        if ($value instanceof DateTime) {
43
            return $value;
44
        }
45
46
        return new DateTime($value);
47
    }
48
49
    /**
50
     * Retain DateTime format for storage.
51
     *
52
     * @param \DateTime $value
53
     *
54
     * @return string
55
     */
56
    public function fromDateTime($value)
57
    {
58
        return $this->asDateTime($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->asDateTime($value); (DateTime) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::fromDateTime of type string.

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...
59
    }
60
61
    /**
62
     * Get a fresh timestamp for the model.
63
     *
64
     * @return \DateTime
65
     */
66
    public function freshTimestamp()
67
    {
68
        return new DateTime();
69
    }
70
71
    /**
72
     * Get a new query builder instance for the connection.
73
     *
74
     * @return Builder
75
     */
76
    protected function newBaseQueryBuilder()
77
    {
78
        $connection = $this->getConnection();
79
        // Check the connection type
80
        if ($connection instanceof \duxet\Rethinkdb\Connection) {
81
            return new QueryBuilder($connection);
82
        }
83
84
        return parent::newBaseQueryBuilder();
85
    }
86
87
    /**
88
     * Create a new Eloquent query builder for the model.
89
     *
90
     * @param \duxet\Rethinkdb\Query\Builder $query
91
     *
92
     * @return \duxet\Rethinkdb\Eloquent\Builder|static
93
     */
94
    public function newEloquentBuilder($query)
95
    {
96
        return new Builder($query);
97
    }
98
99
    /**
100
     * Define an inverse one-to-one or many relationship.
101
     *
102
     * @param string $related
103
     * @param string $foreignKey
104
     * @param string $otherKey
105
     * @param string $relation
106
     *
107
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
108
     */
109
    public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
110
    {
111
        // If no relation name was given, we will use this debug backtrace to extract
112
        // the calling method's name and use that as the relationship name as most
113
        // of the time this will be what we desire to use for the relationships.
114
        if (is_null($relation)) {
115
            list(, $caller) = debug_backtrace(false, 2);
116
            $relation = $caller['function'];
117
        }
118
        // If no foreign key was supplied, we can use a backtrace to guess the proper
119
        // foreign key name by using the name of the relationship function, which
120
        // when combined with an "_id" should conventionally match the columns.
121
        if (is_null($foreignKey)) {
122
            $foreignKey = snake_case($relation).'_id';
123
        }
124
        $instance = new $related();
125
        // Once we have the foreign key names, we'll just create a new Eloquent query
126
        // for the related models and returns the relationship instance which will
127
        // actually be responsible for retrieving and hydrating every relations.
128
        $query = $instance->newQuery();
129
        $otherKey = $otherKey ?: $instance->getKeyName();
130
131
        return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
132
    }
133
134
    /**
135
     * Define a one-to-one relationship.
136
     *
137
     * @param string $related
138
     * @param string $foreignKey
139
     * @param string $localKey
140
     *
141
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
142
     */
143 View Code Duplication
    public function hasOne($related, $foreignKey = null, $localKey = null)
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...
144
    {
145
        $foreignKey = $foreignKey ?: $this->getForeignKey();
146
        $instance = new $related();
147
        $localKey = $localKey ?: $this->getKeyName();
148
149
        return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
150
    }
151
152
    /**
153
     * Define a one-to-many relationship.
154
     *
155
     * @param string $related
156
     * @param string $foreignKey
157
     * @param string $localKey
158
     *
159
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
160
     */
161 View Code Duplication
    public function hasMany($related, $foreignKey = null, $localKey = null)
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...
162
    {
163
        $foreignKey = $foreignKey ?: $this->getForeignKey();
164
        $instance = new $related();
165
        $localKey = $localKey ?: $this->getKeyName();
166
167
        return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
168
    }
169
}
170