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
Pull Request — master (#20)
by Karl
10:26 queued 08:28
created

Model   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 157
Duplicated Lines 10.19 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 10
Bugs 7 Features 2
Metric Value
wmc 19
c 10
b 7
f 2
lcom 1
cbo 6
dl 16
loc 157
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDateFormat() 0 4 1
A getQualifiedKeyName() 0 4 1
A asDateTime() 0 7 2
A fromDateTime() 0 4 1
A freshTimestamp() 0 4 1
A newBaseQueryBuilder() 0 10 2
A newEloquentBuilder() 0 4 1
B belongsTo() 0 24 4
A hasOne() 8 8 3
A hasMany() 8 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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