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.

Model::asDateTime()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.9666
cc 2
nc 2
nop 1
1
<?php
2
3
namespace duxet\Rethinkdb\Eloquent;
4
5
use Carbon\Carbon;
6
use DateTime;
7
use duxet\Rethinkdb\Eloquent\Relations\BelongsTo;
8
use duxet\Rethinkdb\Query\Builder as QueryBuilder;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
12
class Model extends \Illuminate\Database\Eloquent\Model
13
{
14
    /**
15
     * Get the format for database stored dates.
16
     *
17
     * @return string
18
     */
19
    public function getDateFormat()
20
    {
21
        return $this->dateFormat ?: 'Y-m-d H:i:s';
0 ignored issues
show
Documentation introduced by
The property dateFormat does not exist on object<duxet\Rethinkdb\Eloquent\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
22
    }
23
24
    /**
25
     * Get the table qualified key name.
26
     *
27
     * @return string
28
     */
29
    public function getQualifiedKeyName()
30
    {
31
        return $this->getKeyName();
32
    }
33
34
    /**
35
     * Ensure Timestamps are returned in DateTime.
36
     *
37
     * @param \DateTime $value
38
     *
39
     * @return \DateTime
40
     */
41
    protected function asDateTime($value)
42
    {
43
        // Legacy support for Laravel 5.0
44
        if (!$value instanceof Carbon) {
45
            return Carbon::instance($value);
46
        }
47
48
        return parent::asDateTime($value);
49
    }
50
51
    /**
52
     * Retain DateTime format for storage.
53
     *
54
     * @param \DateTime $value
55
     *
56
     * @return string
57
     */
58
    public function fromDateTime($value)
59
    {
60
        if ($value instanceof DateTime) {
61
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $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...
62
        }
63
64
        return parent::asDateTime($value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (asDateTime() instead of fromDateTime()). Are you sure this is correct? If so, you might want to change this to $this->asDateTime().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

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