PostsHaveACategory::hasAnyCategory()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 6
nop 1
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