PostsHaveComments   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A commentAsUser() 0 13 2
A commentAsGuest() 0 14 3
1
<?php
2
3
namespace Chriscreates\Blog\Traits\Post;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
trait PostsHaveComments
8
{
9
    public function commentAsUser(Model $user, string $comment)
10
    {
11
        if ( ! $this->allow_comments) {
0 ignored issues
show
Bug introduced by
The property allow_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...
12
            return null;
13
        }
14
15
        return $this->comments()->create([
0 ignored issues
show
Bug introduced by
It seems like comments() 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...
16
            'content' => $comment,
17
            'user_id' => $user->getKey(),
18
            'email' => $user->email,
19
            'is_approved' => true,
20
        ]);
21
    }
22
23
    public function commentAsGuest(array $fields)
24
    {
25
        if ( ! $this->allow_comments || ! $this->allow_guest_comments) {
0 ignored issues
show
Bug introduced by
The property allow_guest_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...
26
            return null;
27
        }
28
29
        return $this->comments()->create([
0 ignored issues
show
Bug introduced by
It seems like comments() 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...
30
            'content' => $fields->content,
31
            'user_id' => null,
32
            'email' => $comment->email,
0 ignored issues
show
Bug introduced by
The variable $comment does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
33
            'author' => $comment->author,
34
            'is_approved' => false,
35
        ]);
36
    }
37
}
38