PostController::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Chriscreates\Blog\Controllers;
4
5
use Chriscreates\Blog\Category;
6
use Chriscreates\Blog\Post;
7
use Chriscreates\Blog\Requests\ValidatePostRequest;
8
use Chriscreates\Blog\Tag;
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Storage;
12
13
class PostController extends Controller
14
{
15
    /**
16
      * Instantiate a new controller instance.
17
      *
18
      * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
19
      */
20
    public function __construct()
21
    {
22
        $this->middleware('auth');
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index()
31
    {
32
        $posts = Post::withoutGlobalScope('tags')
33
        ->orderBy('created_at', 'desc')
34
        ->paginate(10);
35
36
        return view('blog.admin.posts.posts', compact('posts'));
37
    }
38
39
    /**
40
     * Show the form for creating a new resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $post = new Post;
47
48
        return view('blog.admin.posts.post', [
49
            'post' => $post,
50
            'categories' => Category::all(),
51
            'tags' => Tag::all(),
52
        ]);
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param  \Chriscreates\Blog\Requests\ValidatePostRequest  $request
59
     * @return \Illuminate\Http\JsonResponse
60
     */
61
    public function store(ValidatePostRequest $request) : JsonResponse
62
    {
63
        $post = Post::create($request->only([
64
            'title',
65
            'sub_title',
66
            'slug',
67
            'user_id',
68
            'excerpt',
69
            'content',
70
            'allow_comments',
71
            'allow_guest_comments',
72
        ]));
73
74
        return response()->json($post);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
75
    }
76
77
    /**
78
     * Show the form for editing the specified resource.
79
     *
80
     * @param  \Chriscreates\Blog\Post  $post
81
     * @return \Illuminate\Http\Response
82
     */
83 View Code Duplication
    public function edit(Post $post)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $post->load('category', 'tags');
86
87
        return view('blog.admin.posts.post', [
88
            'post' => $post,
89
            'categories' => Category::all(),
90
            'tags' => Tag::all(),
91
        ]);
92
    }
93
94
    /**
95
     * Update the specified resource in storage.
96
     *
97
     * @param  \Chriscreates\Blog\Requests\ValidatePostRequest  $request
98
     * @param  \Chriscreates\Blog\Post  $post
99
     * @return \Illuminate\Http\JsonResponse
100
     */
101
    public function update(ValidatePostRequest $request, Post $post) : JsonResponse
102
    {
103
        $post->update($request->only([
104
            'title',
105
            'sub_title',
106
            'slug',
107
            'user_id',
108
            'excerpt',
109
            'content',
110
            'allow_comments',
111
            'allow_guest_comments',
112
        ]));
113
114
        $post->refresh();
115
116
        return response()->json($post);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
117
    }
118
119
    /**
120
     * Remove the specified resource from storage.
121
     *
122
     * @param  \Chriscreates\Blog\Post  $post
123
     * @return \Illuminate\Http\JsonResponse
124
     */
125
    public function destroy(Post $post) : JsonResponse
126
    {
127
        $post->delete();
128
129
        $post = new Post;
130
131
        return response()->json($post);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
132
    }
133
}
134