Issues (15)

src/CanBeRated.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Canylmz\Rating;
4
5
trait CanBeRated
6
{
7
    /**
8
     * Relationship for models that rated this model.
9
     *
10
     * @param Model $model The model types of the results.
11
     * @return morphToMany The relationship.
0 ignored issues
show
The type Canylmz\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...
12
     */
13
    public function raters($model = null)
14
    {
15
        return $this->morphToMany(($model) ?: $this->getMorphClass(), 'rateable', 'ratings', 'rateable_id', 'rater_id')
0 ignored issues
show
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

15
        return $this->/** @scrutinizer ignore-call */ morphToMany(($model) ?: $this->getMorphClass(), 'rateable', 'ratings', 'rateable_id', 'rater_id')
Loading history...
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

15
        return $this->morphToMany(($model) ?: $this->/** @scrutinizer ignore-call */ getMorphClass(), 'rateable', 'ratings', 'rateable_id', 'rater_id')
Loading history...
16
            ->withPivot('rater_type', 'rating')
17
            ->wherePivot('rater_type', ($model) ?: $this->getMorphClass())
18
            ->wherePivot('rateable_type', $this->getMorphClass());
19
    }
20
21
    /**
22
     * Calculate the average rating of the current model.
23
     *
24
     * @return float The average rating.
25
     */
26
    public function averageRating($model = null): float
27
    {
28
        if ($this->raters($model)->count() == 0) {
29
            return (float) 0.00;
30
        }
31
32
        return (float) $this->raters($model)->avg('rating');
33
    }
34
35
    /**
36
     * Count the ratings of the current model.
37
     *
38
     * @return int The ratings count.
39
     */
40
    public function countRatings($model = null): int
41
    {
42
        if ($this->raters($model)->count() == 0) {
43
            return (int) 0;
44
        }
45
46
        return (int) $this->raters($model)->count('rating');
47
    }
48
}
49