Commentable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 27
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A newComment() 0 7 1
A commentAs() 0 5 1
A comment() 0 3 1
A comments() 0 3 1
1
<?php
2
3
namespace Orkhanahmadov\LaravelCommentable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Orkhanahmadov\LaravelCommentable\Models\Comment;
7
8
trait Commentable
9
{
10
    public function comments()
11
    {
12
        return $this->morphMany(Comment::class, 'commentable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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

12
        return $this->/** @scrutinizer ignore-call */ morphMany(Comment::class, 'commentable');
Loading history...
13
    }
14
15
    public function comment(string $comment): Comment
16
    {
17
        return $this->newComment(['comment' => $comment]);
18
    }
19
20
    public function commentAs(Model $user, string $comment): Comment
21
    {
22
        return $this->newComment([
23
            'comment' => $comment,
24
            'user_id' => $user->getKey(),
25
        ]);
26
    }
27
28
    private function newComment(array $data)
29
    {
30
        return $this->comments()->create(array_merge(
31
            $data,
32
            [
33
                'ip_address' => request()->ip(),
34
                'user_agent' => request()->userAgent(),
35
            ]
36
        ));
37
    }
38
}
39