Completed
Push — master ( 8f843b...36053e )
by Christopher
01:12
created

PostsHaveACategory::hasCategory()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
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;
0 ignored issues
show
Unused Code introduced by
$category is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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_id == $this->category->id;
0 ignored issues
show
Bug introduced by
The variable $category_id does not exist. Did you mean $category?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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