Completed
Push — master ( b877df...b3f08b )
by Christopher
01:17
created

PostObserver::saving()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
cc 6
nc 32
nop 1
1
<?php
2
3
namespace Chriscreates\Blog\Observers;
4
5
use Chriscreates\Blog\Post;
6
7
class PostObserver
8
{
9
    /**
10
     * Handle the post "saving" event.
11
     *
12
     * @param  \Chriscreates\Blog\Post  $post
13
     * @return void
14
     */
15
    public function saving(Post $post)
16
    {
17
        if (request()->filled('tags')) {
18
            $post->tags()->sync(request('tags'));
19
        }
20
21
        if (request()->filled('category_id')) {
22
            $post->category_id = request('category_id');
23
        }
24
25
        if ( ! request()->filled('user_id')) {
26
            $post->user_id = auth()->id();
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
27
        }
28
29
        if ( ! request()->filled('slug')) {
30
            $post->setSlug(request('title'));
31
        }
32
33
        if (request()->filled('status')) {
34
            $post->setStatus(request('status'));
35
        }
36
    }
37
38
    /**
39
     * Handle the post "deleting" event.
40
     *
41
     * @param  \Chriscreates\Blog\Post  $post
42
     * @return void
43
     */
44
    public function deleting(Post $post)
45
    {
46
        $post->tags()->detach();
47
48
        $post->comments()->delete();
49
    }
50
}
51