Completed
Pull Request — master (#45)
by Fèvre
32:37
created

ArticleController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\Blog;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\View\View;
7
use Xetaio\Mentions\Parser\MentionParser;
8
use Xetaravel\Http\Controllers\Admin\Controller;
9
use Xetaravel\Models\Article;
10
use Xetaravel\Models\Category;
11
use Xetaravel\Models\Repositories\ArticleRepository;
12
use Xetaravel\Models\Validators\ArticleValidator;
13
14
class ArticleController extends Controller
15
{
16
    /**
17
     * Constructor.
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct();
22
23
        $this->breadcrumbs->addCrumb('Blog', route('admin.blog.article.index'));
24
    }
25
26
    /**
27
     * Show all articles.
28
     *
29
     * @return \Illuminate\View\View
30
     */
31
    public function index(): View
32
    {
33
        $articles = Article::with('category', 'user')
0 ignored issues
show
Bug introduced by
The method paginate does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
34
            ->paginate(config('xetaravel.pagination.blog.article_per_page'));
35
36
        $this->breadcrumbs->addCrumb('Manage Articles', route('admin.blog.article.index'));
37
38
        return view('Admin::Blog.article.index', ['articles' => $articles, 'breadcrumbs' => $this->breadcrumbs]);
39
    }
40
41
    /**
42
     * Show the article create form.
43
     *
44
     * @return \Illuminate\View\View
45
     */
46
    public function showCreateForm(): View
47
    {
48
        $categories = Category::pluck('title', 'id');
49
50
        $breadcrumbs = $this->breadcrumbs
51
            ->addCrumb('Manage Articles', route('admin.blog.article.index'))
52
            ->addCrumb("Create", route('admin.blog.article.create'));
53
54
        return view('Admin::Blog.article.create', compact('categories', 'breadcrumbs'));
55
    }
56
57
    /**
58
     * Handle an article create request for the application.
59
     *
60
     * @param \Illuminate\Http\Request $request
61
     *
62
     * @return \Illuminate\Http\RedirectResponse
63
     */
64
    public function create(Request $request): RedirectResponse
65
    {
66
        ArticleValidator::create($request->all())->validate();
67
        $article = ArticleRepository::create($request->all());
68
69
        $parser = new MentionParser($article);
70
        $content = $parser->parse($article->content);
71
72
        $article->content = $content;
73
        $article->save();
74
75
        return redirect()
76
            ->route('admin.blog.article.index')
77
            ->with('success', 'Your article has been created successfully !');
78
    }
79
80
    /**
81
     * Show the article update form.
82
     *
83
     * @param string $slug The slug of the article.
84
     * @param int $id The id of the article.
85
     *
86
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be RedirectResponse|View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
87
     */
88
    public function showUpdateForm(string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $slug 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...
89
    {
90
        $article = Article::with('category', 'user', 'comments')
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
91
            ->where('id', $id)
92
            ->first();
93
94
        if (is_null($article)) {
95
            return redirect()
96
                ->route('admin.blog.article.index')
97
                ->with('danger', 'This article doesn\'t exist or has been deleted !');
98
        }
99
        $categories = Category::pluck('title', 'id');
100
101
        $breadcrumbs = $this->breadcrumbs
102
            ->addCrumb('Manage Articles', route('admin.blog.article.index'))
103
            ->addCrumb(
104
                "Update : " . e(str_limit($article->title, 30)),
105
                route(
106
                    'admin.blog.article.index',
107
                    ['slug' => $article->category->slug, 'id' => $article->category->id]
108
                )
109
            );
110
111
        return view('Admin::Blog.article.update', compact('article', 'breadcrumbs', 'categories'));
112
    }
113
114
    /**
115
     * Handle an article update request for the application.
116
     *
117
     * @param \Illuminate\Http\Request $request
118
     * @param int $id The id of the article.
119
     *
120
     * @return \Illuminate\Http\RedirectResponse
121
     */
122
    public function update(Request $request, int $id): RedirectResponse
123
    {
124
        $article = Article::findOrFail($id);
125
126
        ArticleValidator::update($request->all(), $id)->validate();
127
        $article = ArticleRepository::update($request->all(), $article);
128
129
        $parser = new MentionParser($article);
130
        $content = $parser->parse($article->content);
131
132
        $article->content = $content;
133
        $article->save();
134
135
        return redirect()
136
            ->route('admin.blog.article.index')
137
            ->with('success', 'Your article has been updated successfully !');
138
    }
139
140
    /**
141
     * Handle the delete request for the article.
142
     *
143
     * @param int $id The id of the article to delete.
144
     *
145
     * @return \Illuminate\Http\RedirectResponse
146
     */
147
    public function delete(int $id): RedirectResponse
148
    {
149
        $article = Article::findOrFail($id);
150
151
        if ($article->delete()) {
152
            return redirect()
153
                ->route('admin.blog.article.index')
154
                ->with('success', 'This article has been deleted successfully !');
155
        }
156
157
        return redirect()
158
            ->route('admin.blog.article.index')
159
            ->with('danger', 'An error occurred while deleting this article !');
160
    }
161
}
162