PostsController::edit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Backend;
4
5
use Carbon\Carbon;
6
use App\Models\Post;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use App\Http\Resources\PostResource;
10
use Lloople\Notificator\Notificator;
11
use App\Http\Requests\PostFormRequest;
12
use App\ViewModels\PostDetailViewModel;
13
14
class PostsController extends Controller
15
{
16
    private $pagination = 15;
17
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @param \Illuminate\Http\Request $request
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function index(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        return view('backend.posts.index');
27
    }
28
29
    /**
30
     * Show the form for creating a new resource.
31
     *
32
     * @return \Illuminate\Http\Response
33
     */
34
    public function create()
35
    {
36
        return view('backend.posts.edit', ['view' => new PostDetailViewModel(new Post())]);
37
    }
38
39
    /**
40
     * Store a newly created resource in storage.
41
     *
42
     * @param \App\Http\Requests\PostFormRequest $request
43
     *
44
     * @return \Illuminate\Http\RedirectResponse
45
     */
46 View Code Duplication
    public function store(PostFormRequest $request)
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...
47
    {
48
        $post = new Post();
49
        $post->title = $request->title;
0 ignored issues
show
Bug introduced by
The property title does not seem to exist in App\Http\Requests\PostFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
        $post->slug = str_slug($request->title);
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<App\Http\Requests\PostFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
        $post->category_id = $request->category;
0 ignored issues
show
Bug introduced by
The property category does not seem to exist in App\Http\Requests\PostFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
        $post->published_at = Carbon::createFromFormat('Y-m-d\TH:i', $request->published_at);
0 ignored issues
show
Documentation introduced by
The property published_at does not exist on object<App\Http\Requests\PostFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
        $post->featured = $request->has('featured');
54
        $post->visible = $request->has('visible');
0 ignored issues
show
Documentation introduced by
The property $visible is declared protected in Illuminate\Database\Eloq...oncerns\HidesAttributes. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by
It seems like $request->has('visible') of type boolean is incompatible with the declared type array of property $visible.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        $post->body = $request->body;
0 ignored issues
show
Bug introduced by
The property body does not seem to exist in App\Http\Requests\PostFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
56
57
        $post->save();
58
59
        $post->syncTags($request->tags);
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<App\Http\Requests\PostFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
61
        Notificator::success('Post created successfully');
62
63
        return redirect()->route('backend.posts.edit', $post);
0 ignored issues
show
Documentation introduced by
$post is of type object<App\Models\Post>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
    }
65
66
    /**
67
     * Show the form for editing the specified resource.
68
     *
69
     * @param \App\Models\Post $post
70
     *
71
     * @return void
72
     */
73
    public function edit(Post $post)
74
    {
75
        return view('backend.posts.edit', ['view' => new PostDetailViewModel($post)]);
76
    }
77
78
    /**
79
     * Update the specified resource in storage.
80
     *
81
     * @param \App\Http\Requests\PostFormRequest $request
82
     * @param  Post $post
83
     *
84
     * @return \Illuminate\Http\RedirectResponse
85
     */
86 View Code Duplication
    public function update(PostFormRequest $request, 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...
87
    {
88
        $post->title = $request->title;
0 ignored issues
show
Bug introduced by
The property title does not seem to exist in App\Http\Requests\PostFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
89
        $post->slug = str_slug($request->title);
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<App\Http\Requests\PostFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
        $post->category_id = $request->category;
0 ignored issues
show
Bug introduced by
The property category does not seem to exist in App\Http\Requests\PostFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
91
        $post->published_at = Carbon::createFromFormat('Y-m-d\TH:i', $request->published_at);
0 ignored issues
show
Documentation introduced by
The property published_at does not exist on object<App\Http\Requests\PostFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
        $post->featured = $request->has('featured');
93
        $post->visible = $request->has('visible');
0 ignored issues
show
Documentation introduced by
The property $visible is declared protected in Illuminate\Database\Eloq...oncerns\HidesAttributes. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by
It seems like $request->has('visible') of type boolean is incompatible with the declared type array of property $visible.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94
        $post->body = $request->body;
0 ignored issues
show
Bug introduced by
The property body does not seem to exist in App\Http\Requests\PostFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
95
96
        $post->save();
97
98
        $post->syncTags($request->tags);
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<App\Http\Requests\PostFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
100
        Notificator::success('Post edited successfully');
101
102
        return redirect()->route('backend.posts.edit', $post);
0 ignored issues
show
Documentation introduced by
$post is of type object<App\Models\Post>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
    }
104
105
    /**
106
     * Remove the specified resource from storage.
107
     *
108
     * @param \Illuminate\Http\Request $request
109
     * @param  Post $post
110
     *
111
     * @return array
112
     * @throws \Exception
113
     */
114
    public function destroy(Request $request, Post $post)
115
    {
116
        $post->tags()->sync([]);
117
118
        $post->delete();
119
120
        if ($request->ajax()) {
121
            return [
122
                'result' => true,
123
                'message' => 'Post deleted successfully.',
124
            ];
125
        }
126
127
        Notificator::success('Post deleted successfully.');
128
129
        return redirect()->route('backend.posts.index');
130
    }
131
132 View Code Duplication
    public function resource(Request $request)
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...
133
    {
134
        $posts = Post::with('tags');
135
136
        if ($request->has('q')) {
137
            $posts->searchLike($request->q);
138
        }
139
140
        return PostResource::collection($posts->orderBy('published_at', 'DESC')->paginate($this->pagination));
141
    }
142
}
143