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.

CanRate   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 35
c 1
b 0
f 0
dl 0
loc 138
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addRatingFor() 0 18 3
A updateRatingFor() 0 11 3
A hasRated() 0 7 2
A rate() 0 7 2
A ratings() 0 6 3
A unrate() 0 3 1
A isRateable() 0 7 3
A deleteRatingFor() 0 11 3
1
<?php
2
3
namespace Compubel\Rating;
4
5
use Compubel\Rating\Contracts\Rateable;
6
use Compubel\Rating\Contracts\Rating;
7
use Compubel\Rating\Exceptions\ModelNotRateable;
8
use Compubel\Rating\Exceptions\RatingAlreadyExists;
9
10
trait CanRate
11
{
12
    /**
13
     * Relationship for models that this model currently rated.
14
     *
15
     * @param Model $model The model types of the results.
16
     * @return morphToMany The relationship.
0 ignored issues
show
Bug introduced by
The type Compubel\Rating\morphToMany was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
     */
18
    public function ratings($model = null)
19
    {
20
        return $this->morphToMany(($model) ?: $this->getMorphClass(), 'rater', 'ratings', 'rater_id', 'rateable_id')
0 ignored issues
show
Bug introduced by
It seems like getMorphClass() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return $this->morphToMany(($model) ?: $this->/** @scrutinizer ignore-call */ getMorphClass(), 'rater', 'ratings', 'rater_id', 'rateable_id')
Loading history...
Bug introduced by
It seems like morphToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return $this->/** @scrutinizer ignore-call */ morphToMany(($model) ?: $this->getMorphClass(), 'rater', 'ratings', 'rater_id', 'rateable_id')
Loading history...
21
            ->withPivot('rateable_type', 'rating')
22
            ->wherePivot('rateable_type', ($model) ?: $this->getMorphClass())
23
            ->wherePivot('rater_type', $this->getMorphClass());
24
    }
25
26
    /**
27
     * Check if a model can be rated.
28
     *
29
     * @param Model $model The model to check
30
     * @return bool
31
     */
32
    public function isRateable($model): bool
33
    {
34
        if (! $model instanceof Rateable && ! $model instanceof Rating) {
35
            throw ModelNotRateable::create($model);
36
        }
37
38
        return true;
39
    }
40
41
    /**
42
     * Check if the current model is rating another model.
43
     *
44
     * @param Model $model The model which will be checked against.
45
     * @return bool
46
     */
47
    public function hasRated($model): bool
48
    {
49
        if (! $this->isRateable($model)) {
50
            return false;
51
        }
52
53
        return (bool) ! is_null($this->ratings($model->getMorphClass())->find($model->getKey()));
0 ignored issues
show
Bug introduced by
The method getMorphClass() does not exist on Compubel\Rating\Contracts\Rating. Since it exists in all sub-types, consider adding an abstract or default implementation to Compubel\Rating\Contracts\Rating. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return (bool) ! is_null($this->ratings($model->/** @scrutinizer ignore-call */ getMorphClass())->find($model->getKey()));
Loading history...
Bug introduced by
The method getMorphClass() does not exist on Compubel\Rating\Contracts\Rateable. It seems like you code against a sub-type of said class. However, the method does not exist in Compubel\Rating\Contracts\Rating. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return (bool) ! is_null($this->ratings($model->/** @scrutinizer ignore-call */ getMorphClass())->find($model->getKey()));
Loading history...
Bug introduced by
The method getKey() does not exist on Compubel\Rating\Contracts\Rating. Since it exists in all sub-types, consider adding an abstract or default implementation to Compubel\Rating\Contracts\Rating. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return (bool) ! is_null($this->ratings($model->getMorphClass())->find($model->/** @scrutinizer ignore-call */ getKey()));
Loading history...
Bug introduced by
The method getKey() does not exist on Compubel\Rating\Contracts\Rateable. It seems like you code against a sub-type of said class. However, the method does not exist in Compubel\Rating\Contracts\Rating. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return (bool) ! is_null($this->ratings($model->getMorphClass())->find($model->/** @scrutinizer ignore-call */ getKey()));
Loading history...
54
    }
55
56
    /**
57
     * Add rating for a certain model.
58
     *
59
     * @param Model $model The model which will be rated.
60
     * @param float $rating The rate amount.
61
     * @return bool
62
     */
63
    public function addRatingFor($model, $rating): bool
64
    {
65
        if (! $this->isRateable($model)) {
66
            return false;
67
        }
68
69
        if ($this->hasRated($model)) {
70
            throw RatingAlreadyExists::create($model);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type Compubel\Rating\Contracts\Rating; however, parameter $className of Compubel\Rating\Exceptio...AlreadyExists::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            throw RatingAlreadyExists::create(/** @scrutinizer ignore-type */ $model);
Loading history...
71
        }
72
73
        $this->ratings()->attach($this->getKey(), [
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->ratings()->attach($this->/** @scrutinizer ignore-call */ getKey(), [
Loading history...
74
            'rater_id' => $this->getKey(),
75
            'rateable_type' => $model->getMorphClass(),
76
            'rateable_id' => $model->getKey(),
77
            'rating' => (float) $rating,
78
        ]);
79
80
        return true;
81
    }
82
83
    /**
84
     * Update or add rating for a certain model.
85
     *
86
     * @param Model $model The model which will be rated.
87
     * @param float $rating The rate amount.
88
     * @return bool
89
     */
90
    public function updateRatingFor($model, $rating): bool
91
    {
92
        if (! $this->isRateable($model)) {
93
            return false;
94
        }
95
96
        if ($this->hasRated($model)) {
97
            $this->deleteRatingFor($model);
98
        }
99
100
        return $this->addRatingFor($model, $rating);
101
    }
102
103
    /**
104
     * Rate a certain model.
105
     *
106
     * @param Model $model The model which will be rated.
107
     * @param float $rating The rate amount.
108
     * @param bool $can_update Set to false if a rating can only be added once
109
     * @return bool
110
     */
111
    public function rate($model, $rating, $can_update = true): bool
112
    {
113
        if ($can_update === true) {
114
            return $this->updateRatingFor($model, $rating);
115
        }
116
117
        return $this->addRatingFor($model, $rating);
118
    }
119
120
    /**
121
     * Delete rating for a certain model.
122
     *
123
     * @param Model $model The model which will be unrated.
124
     * @return bool
125
     */
126
    public function deleteRatingFor($model): bool
127
    {
128
        if (! $this->isRateable($model)) {
129
            return false;
130
        }
131
132
        if (! $this->hasRated($model)) {
133
            return false;
134
        }
135
136
        return (bool) $this->ratings($model->getMorphClass())->detach($model->getKey());
137
    }
138
139
    /**
140
     * Unrate a certain model.
141
     *
142
     * @param Model $model The model which will be unrated.
143
     * @return bool
144
     */
145
    public function unrate($model): bool
146
    {
147
        return $this->deleteRatingFor($model);
148
    }
149
}
150