Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

BackendController::save()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 8
nop 2
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Mark
5
 * Date: 05/03/2016
6
 * Time: 20:30.
7
 */
8
9
namespace App\Plugins\Articles;
10
11
use Carbon\Carbon;
12
use App\Model\Article;
13
use App\Model\Activity;
14
use Illuminate\Http\Request;
15
use App\Plugins\PluginEngine;
16
use App\Model\ArticleCategory;
17
use Illuminate\Validation\Rule;
18
use App\Classes\Repositories\ArticleRepository;
19
use App\Classes\Repositories\ArticleCategoryRepository;
20
21
/**
22
 * Class Controller.
23
 */
24
class BackendController extends PluginEngine
25
{
26
    /**
27
     * @var ArticleRepository
28
     */
29
    private $articles;
30
31
    /**
32
     * @var ArticleCategoryRepository
33
     */
34
    private $categories;
35
36
    /**
37
     * BackendController constructor.
38
     *
39
     * @param ArticleRepository $repository
40
     */
41
    public function __construct(ArticleRepository $repository, ArticleCategoryRepository $categories)
42
    {
43
        $this->articles = $repository;
44
45
        $this->categories = $categories;
46
    }
47
48
    /**
49
     * Display a listing of the resource.
50
     *
51
     * @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
52
     */
53
    public function index()
54
    {
55
        return $this->make('index')->with('articles', $this->articles->all());
56
    }
57
58
    /**
59
     * Show the form for creating a new resource.
60
     *
61
     * @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
62
     */
63
    public function create()
64
    {
65
        return $this->form(new Article);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->form(new App\Model\Article()) targeting App\Plugins\Articles\BackendController::form() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->form(new App\Model\Article()) returns the type void which is incompatible with the documented return type Illuminate\Contracts\Vie...lluminate\Http\Response.
Loading history...
66
    }
67
68
    /**
69
     * Generate a form for editing or creating a model.
70
     *
71
     * @param Article $article model to be used.
72
     * @return void
73
     */
74
    public function form(Article $article)
75
    {
76
        return $this->make('form')->with('categories', $this->categories->all())->with('article', $article);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->make('form...th('article', $article) returns the type Illuminate\View\View which is incompatible with the documented return type void.
Loading history...
77
    }
78
79
    /**
80
     * Store a newly created resource in storage.
81
     *
82
     * @param Article $article
83
     * @param  \Illuminate\Http\Request $request
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function store(Article $article, Request $request)
87
    {
88
        // use the global save function.
89
        $this->save($request, $article);
90
91
        account()->record(Activity::$created, $article);
0 ignored issues
show
Bug introduced by
The method record() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        account()->/** @scrutinizer ignore-call */ record(Activity::$created, $article);
Loading history...
92
93
        // redirect back to articles index.
94
        return redirect()->route('admin.articles.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('admin.articles.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
95
    }
96
97
    /**
98
     * Display the specified resource.
99
     *
100
     * @param  int  $id
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function show(/** @scrutinizer ignore-unused */ $id)

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

Loading history...
104
    {
105
        //
106
    }
107
108
    /**
109
     * Show the form for editing the specified resource.
110
     *
111
     * @param $slug
112
     * @return \Illuminate\Contracts\View\View
113
     */
114
    public function edit($slug)
115
    {
116
        return $this->form($this->articles->whereSlug($slug));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->form($this->articles->whereSlug($slug)) targeting App\Plugins\Articles\BackendController::form() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Deprecated Code introduced by
The function App\Classes\Repositories...Repository::whereSlug() has been deprecated: change to the repository class calling it. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
        return $this->form(/** @scrutinizer ignore-deprecated */ $this->articles->whereSlug($slug));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The expression return $this->form($this...cles->whereSlug($slug)) returns the type void which is incompatible with the documented return type Illuminate\Contracts\View\View.
Loading history...
117
    }
118
119
    /**
120
     * Update the specified resource in storage.
121
     *
122
     * @param  \Illuminate\Http\Request $request
123
     * @param $slug
124
     * @return \Illuminate\Http\Response
125
     * @internal param int $id
126
     */
127
    public function update(Request $request, $slug)
128
    {
129
        $article = $this->articles->whereSlug($slug);
0 ignored issues
show
Deprecated Code introduced by
The function App\Classes\Repositories...Repository::whereSlug() has been deprecated: change to the repository class calling it. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
        $article = /** @scrutinizer ignore-deprecated */ $this->articles->whereSlug($slug);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
130
131
        $this->save($request, $article);
132
133
        account()->record(Activity::$updated, $article);
134
135
        return redirect()->route('admin.articles.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('admin.articles.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
136
    }
137
138
    /**
139
     * Remove the specified resource from storage.
140
     *
141
     * @param  int  $id
142
     * @return \Illuminate\Http\Response
143
     */
144
    public function destroy($slug)
145
    {
146
        $article = $this->articles->whereSlug($slug);
0 ignored issues
show
Deprecated Code introduced by
The function App\Classes\Repositories...Repository::whereSlug() has been deprecated: change to the repository class calling it. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

146
        $article = /** @scrutinizer ignore-deprecated */ $this->articles->whereSlug($slug);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
147
148
        $article->delete();
0 ignored issues
show
Bug introduced by
The method delete() does not exist on App\Classes\Repositories\ArticleRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        $article->/** @scrutinizer ignore-call */ 
149
                  delete();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
150
        account()->record(Activity::$deleted, $article);
151
152
        return redirect()->route('admin.articles.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('admin.articles.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
153
    }
154
155
    /**
156
     * Category Index.
157
     * @param ArticleCategoryRepository $categoryRepository
158
     * @return \Illuminate\Contracts\View\View
159
     */
160
    public function categories(ArticleCategoryRepository $categoryRepository)
161
    {
162
        return $this->make('categories')->with('categories', $categoryRepository->all());
163
    }
164
165
    /**
166
     * @param Request $request
167
     * @return \Illuminate\Http\RedirectResponse
168
     */
169
    public function categories_store(Request $request)
170
    {
171
        $this->validate($request, ['unique:title']);
172
173
        $category = new ArticleCategory;
174
        $category->title = $request['name'];
175
        $category->auditSave();
176
177
        account()->record(Activity::$created, $category);
178
179
        return redirect()->route('admin.articles.categories.index');
180
    }
181
182
    /**
183
     * @param int $id
184
     * @param ArticleCategoryRepository $categoryRepository
185
     * @return \Illuminate\Http\RedirectResponse
186
     */
187
    public function categories_destroy(int $id, ArticleCategoryRepository $categoryRepository)
188
    {
189
        $category = $categoryRepository->whereID($id);
190
191
        account()->record(Activity::$deleted, $category);
192
193
        $category->delete();
194
195
        return redirect()->route('admin.articles.categories.index');
196
    }
197
198
    /**
199
     * Save the data for the menu to the database.
200
     *
201
     * @param Request $request
202
     * @param Article $article
203
     * @return bool
204
     * @internal param Article $menu
205
     */
206
    public function save(Request $request, Article $article)
207
    {
208
        $this->validate($request, [
209
            'title' => ['min:3|max:255', Rule::unique('articles')->ignore($article->id)], 'content' => ['min:3'],
210
            'publish_date' => 'required|date',
211
        ]);
212
213
        if ($request['unpublish_date']) {
214
            $this->validate($request, ['unpublish_date' => 'sometimes|date|after:publish_date']);
215
        }
216
217
        // set attribute for the model.
218
        $article->setAttribute('title', $request['title']);
219
        $article->setAttribute('content', $request['content']);
220
        $article->setAttribute('category_id', $request['category']);
221
        $article->setAttribute('status', $request['status']);
222
        $article->setAttribute('featured_img', $request['image']);
223
224
        // 09/05/2018 $publish_date =
225
        $publish_date = $request['publish_date'] ? Carbon::createFromFormat('m/d/Y', $request['publish_date']) : null;
226
        $unpublish_date = $request['unpublish_date'] ? Carbon::createFromFormat('m/d/Y', $request['unpublish_date']) : null;
227
228
        // Store the carbon dates to the database.
229
        $article->setAttribute('publish_date', $publish_date);
230
        $article->setAttribute('unpublish_date', $unpublish_date);
231
232
        // save the article as an audit.
233
        return $article->auditSave();
234
    }
235
}
236