CommentBuilder::comment()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Yoeunes\Commentable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Yoeunes\Commentable\Models\Comment;
7
use Yoeunes\Commentable\Traits\Commentable;
8
use Yoeunes\Commentable\Exceptions\EmptyUser;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
use Yoeunes\Commentable\Exceptions\UserDoestNotHaveID;
11
use Yoeunes\Commentable\Exceptions\CommentableModelNotFound;
12
use Yoeunes\Commentable\Exceptions\ModelDoesNotUseCommentableTrait;
13
14
class CommentBuilder
15
{
16
    protected $user;
17
18
    protected $commentable;
19
20
    public function __construct()
21
    {
22
        if (config('commentable.auth_user')) {
23
            $this->user = auth()->id();
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
24
        }
25
    }
26
27
    /**
28
     * @param Model|int $user
29
     *
30
     * @return CommentBuilder
31
     *
32
     * @throws \Throwable
33
     */
34
    public function user($user)
35
    {
36
        throw_if($user instanceof Model && empty($user->id), UserDoestNotHaveID::class, 'User object does not have ID');
37
38
        $this->user = $user instanceof Model ? $user->id : $user;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param Model $commentable
45
     *
46
     * @return CommentBuilder
47
     *
48
     * @throws \Throwable
49
     */
50
    public function commentable(Model $commentable)
51
    {
52
        throw_unless(in_array(Commentable::class, class_uses_recursive($commentable)), ModelDoesNotUseCommentableTrait::class, get_class($commentable).' does not use the Commentable Trait');
53
54
        $this->commentable = $commentable;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $body
61
     *
62
     * @return Comment
63
     *
64
     * @throws \Throwable
65
     */
66
    public function comment(string $body)
67
    {
68
        throw_if(empty($this->user), EmptyUser::class, 'Empty user');
69
70
        throw_if(empty($this->commentable->id), CommentableModelNotFound::class, 'Commentable model not found');
71
72
        $data = [
73
            'user_id'          => $this->user,
74
            'commentable_id'   => $this->commentable->id,
75
            'commentable_type' => in_array(get_class($this->commentable), Relation::morphMap()) ? array_search(get_class($this->commentable), Relation::morphMap()) : get_class($this->commentable),
76
        ];
77
78
        $commentModel = config('commentable.comment');
79
80
        $comment = (new $commentModel)->fill($data);
81
82
        $comment->comment = $body;
83
84
        $comment->save();
85
86
        return $comment;
87
    }
88
}
89