actuallymab /
laravel-comment
| 1 | <?php |
||||
| 2 | declare(strict_types=1); |
||||
| 3 | |||||
| 4 | namespace Actuallymab\LaravelComment; |
||||
| 5 | |||||
| 6 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
||||
| 7 | use Illuminate\Database\Query\Builder; |
||||
| 8 | use Illuminate\Support\Collection; |
||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * @property Collection $comments |
||||
| 12 | */ |
||||
| 13 | trait HasComments |
||||
| 14 | { |
||||
| 15 | 13 | public function comments(): MorphMany |
|||
| 16 | { |
||||
| 17 | 13 | return $this->morphMany(config('comment.model'), 'commentable'); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 18 | } |
||||
| 19 | |||||
| 20 | 1 | public function canBeRated(): bool |
|||
| 21 | { |
||||
| 22 | 1 | return false; |
|||
| 23 | } |
||||
| 24 | |||||
| 25 | 1 | public function mustBeApproved(): bool |
|||
| 26 | { |
||||
| 27 | 1 | return false; |
|||
| 28 | } |
||||
| 29 | |||||
| 30 | 1 | public function primaryId(): string |
|||
| 31 | { |
||||
| 32 | 1 | return (string)$this->getAttribute($this->primaryKey); |
|||
|
0 ignored issues
–
show
It seems like
getAttribute() 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
Loading history...
|
|||||
| 33 | } |
||||
| 34 | |||||
| 35 | 3 | public function averageRate(int $round = 2): float |
|||
| 36 | { |
||||
| 37 | 3 | if (!$this->canBeRated()) { |
|||
| 38 | 1 | return 0; |
|||
| 39 | } |
||||
| 40 | |||||
| 41 | /** @var Builder $rates */ |
||||
| 42 | 2 | $rates = $this->comments()->approvedComments(); |
|||
| 43 | |||||
| 44 | 2 | if (!$rates->exists()) { |
|||
| 45 | 2 | return 0; |
|||
| 46 | } |
||||
| 47 | |||||
| 48 | 2 | return round((float)$rates->avg('rate'), $round); |
|||
| 49 | } |
||||
| 50 | |||||
| 51 | 2 | public function totalCommentsCount(): int |
|||
| 52 | { |
||||
| 53 | 2 | if (!$this->mustBeApproved()) { |
|||
| 54 | 1 | return $this->comments()->count(); |
|||
| 55 | } |
||||
| 56 | |||||
| 57 | 1 | return $this->comments()->approvedComments()->count(); |
|||
| 58 | } |
||||
| 59 | } |
||||
| 60 |