Completed
Push — master ( 87c960...50a753 )
by Can
03:58 queued 55s
created

CanBeRated::averageRating()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Canylmz\Rating;
4
5
use Canylmz\Rating\Contracts\Rateable;
6
7
trait CanBeRated
8
{
9
    /**
10
     * Relationship for models that rated this model.
11
     *
12
     * @param Model $model The model types of the results.
13
     * @return morphToMany The relationship.
0 ignored issues
show
Bug introduced by
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...
14
     */
15
    public function raters($model = null)
16
    {
17
        return $this->morphToMany(($model) ?: $this->getMorphClass(), 'rateable', 'ratings', 'rateable_id', 'rater_id')
0 ignored issues
show
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

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

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