Completed
Pull Request — master (#25)
by Fèvre
05:23 queued 02:34
created

CommentController::create()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Blog;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Xetaravel\Events\CommentEvent;
8
use Xetaravel\Http\Controllers\Controller;
9
use Xetaravel\Models\Article;
10
use Xetaravel\Models\Comment;
11
use Xetaravel\Models\Repositories\CommentRepository;
12
use Xetaravel\Models\User;
13
use Xetaravel\Models\Validators\CommentValidator;
14
15
class CommentController extends Controller
16
{
17
    /**
18
     * Create a comment for an article.
19
     *
20
     * @param \Illuminate\Http\Request $request
21
     *
22
     * @return \Illuminate\Http\RedirectResponse
23
     */
24
    public function create(Request $request): RedirectResponse
25
    {
26
        $article = Article::findOrFail($request->article_id);
27
28
        if ($article->is_display == false) {
29
            return back()
30
                ->withInput()
31
                ->with('danger', 'This article doesn\'t exist or you can not reply to it !');
32
        }
33
34
        if (Comment::isFlooding(Auth::user())) {
35
            return back()
36
                ->withInput()
37
                ->with('danger', 'Wow, keep calm bro, and try to not flood !');
38
        }
39
40
        CommentValidator::create($request->all())->validate();
41
        CommentRepository::create($request->all(), Auth::user());
0 ignored issues
show
Documentation introduced by
\Illuminate\Support\Facades\Auth::user() is of type object<Illuminate\Contra...h\Authenticatable>|null, but the function expects a object<Xetaravel\Models\User>.

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...
42
43
        // We must find the user else we won't see the updated comment_count.
44
        event(new CommentEvent(User::find(Auth::user()->id)));
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
45
46
        return back()
47
            ->with('success', 'Your comment has been posted successfully !');
48
    }
49
}
50