PostsHaveACategory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasCategory() 0 12 3
A hasAnyCategory() 0 14 4
1
<?php
2
3
namespace Chriscreates\Blog\Traits\Post;
4
5
use Chriscreates\Blog\Category;
6
use Illuminate\Support\Collection;
7
8
trait PostsHaveACategory
9
{
10
    public function hasCategory($category)
11
    {
12
        if ($category instanceof Category) {
13
            $category = $category->id;
14
        }
15
16
        if (is_null($this->category)) {
0 ignored issues
show
Bug introduced by
The property category 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...
17
            return false;
18
        }
19
20
        return $category == $this->category->id;
21
    }
22
23
    public function hasAnyCategory($categories)
24
    {
25
        if ($categories instanceof Collection) {
26
            $categories = $categories->pluck('id')->toArray();
27
        }
28
29
        foreach ($categories as $category) {
30
            if ($this->hasCategory($category)) {
31
                return true;
32
            }
33
        }
34
35
        return false;
36
    }
37
}
38