Completed
Push — master ( a0d489...a6481d )
by Fèvre
02:12
created

CommentController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 3
1
<?php
2
namespace Xetaravel\Http\Controllers\Blog;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Xetaravel\Http\Controllers\Controller;
7
use Xetaravel\Models\Article;
8
use Xetaravel\Models\Repositories\CommentRepository;
9
use Xetaravel\Models\Validators\CommentValidator;
10
11
class CommentController extends Controller
12
{
13
    /**
14
     * Create a comment for an article.
15
     *
16
     * @return \Illuminate\Http\RedirectResponse
17
     */
18
    public function create(Request $request): RedirectResponse
19
    {
20
        // Check if the article exist and if its display.
21
        $article = Article::find($request->article_id);
22
23
        if (is_null($article) || $article->is_display == false) {
24
            return back()
25
                ->withInput()
26
                ->with('danger', 'This article doesn\'t exist or you can not reply to it !');
27
        }
28
29
        CommentValidator::create($request->all())->validate();
30
        CommentRepository::create($request->all(), auth()->user());
0 ignored issues
show
Bug introduced by
The method user 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...
31
32
        return back()
33
            ->with('success', 'Your comment has been posted successfully !');
34
    }
35
}
36