Commentable::deleteComment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of Laravel :package_name.
5
 *
6
 * (c) DraperStudio <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DraperStudio\Commentable\Traits;
13
14
use Illuminate\Database\Eloquent\Model;
15
16
/**
17
 * Class Commentable.
18
 *
19
 * @author DraperStudio <[email protected]>
20
 */
21
trait Commentable
22
{
23
24
    /**
25
     * @return string
26
     */
27
    public function commentable_model()
28
    {
29
        return config('commentable.model');
30
    }
31
32
    /**
33
     * @return mixed
34
     */
35
    public function comments()
36
    {
37
        return $this->morphMany($this->commentable_model(), '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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
    }
39
40
    /**
41
     * @param $data
42
     * @param Model      $creator
43
     * @param Model|null $parent
44
     *
45
     * @return static
46
     */
47
    public function comment($data, Model $creator, Model $parent = null)
48
    {
49
        $commentableModel = $this->commentable_model();
50
51
        $comment = (new $commentableModel)->createComment($this, $data, $creator);
52
53
        if (!empty($parent)) {
54
            $comment->appendTo($parent)->save();
55
        }
56
57
        return $comment;
58
    }
59
60
    /**
61
     * @param $id
62
     * @param $data
63
     * @param Model|null $parent
64
     *
65
     * @return mixed
66
     */
67
    public function updateComment($id, $data, Model $parent = null)
68
    {
69
        $commentableModel = $this->commentable_model();
70
71
        $comment = (new $commentableModel)->updateComment($id, $data);
72
73
        if (!empty($parent)) {
74
            $comment->appendTo($parent)->save();
75
        }
76
77
        return $comment;
78
    }
79
80
    /**
81
     * @param $id
82
     *
83
     * @return mixed
84
     */
85
    public function deleteComment($id)
86
    {
87
        $commentableModel = $this->commentable_model();
88
89
        return (new $commentableModel)->deleteComment($id);
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function commentCount()
96
    {
97
        return $this->comments->count();
0 ignored issues
show
Bug introduced by
The property comments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
98
    }
99
}
100