CommentsController::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Models\Comments;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Redirect;
8
use Illuminate\Support\Facades\Session;
9
10
/**
11
 * Class CommentsController.
12
 */
13
class CommentsController extends Controller
14
{
15
    /**
16
     * @return \Illuminate\View\View
17
     */
18
    public function index()
19
    {
20
        return view('Comments/index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('Comments/index', a...dels\Comments::all())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 20 which is incompatible with the return type documented by App\Http\Controllers\CommentsController::index of type Illuminate\View\View.
Loading history...
21
            'comments' => Comments::all(),
22
        ]);
23
    }
24
25
    /**
26
     * @return \Illuminate\View\View
27
     */
28
    public function create()
29
    {
30
        return view('Comments/create');
0 ignored issues
show
Bug Compatibility introduced by
The expression view('Comments/create'); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 30 which is incompatible with the return type documented by App\Http\Controllers\CommentsController::create of type Illuminate\View\View.
Loading history...
31
    }
32
33
    /**
34
     * @return \Illuminate\View\View
35
     */
36
    public function read($id)
37
    {
38
        return view('Comments/read', ['id' => $id]);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('Comments/read', array('id' => $id)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 38 which is incompatible with the return type documented by App\Http\Controllers\CommentsController::read of type Illuminate\View\View.
Loading history...
39
    }
40
41
    /**
42
     *
43
     */
44
    public function update(Request $request)
45
    {
46
        $field = 'content';
47
        $id = $request->id;
1 ignored issue
show
Bug introduced by
The property id does not seem to exist in Illuminate\Http\Request.

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...
48
        $value = $request->value;
1 ignored issue
show
Bug introduced by
The property value does not seem to exist in Illuminate\Http\Request.

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...
49
50
        $comment = Comments::find($id);
51
        $comment->update([$field => $value]);
52
        Session::flash('success', 'Le commentaire a bien été mis à jour');
53
54
        return Redirect::route('comments.index');
55
    }
56
57
    /**
58
     * Pour la suppression, il n'y a pas de vue dédiée.
59
     * On redirige donc vers l'index.
60
     *
61
     * @return \Illuminate\View\View
62
     */
63
    public function delete($id)
64
    {
65
        return redirect('/comments', ['id' => $id]);
0 ignored issues
show
Documentation introduced by
array('id' => $id) is of type array<string,?,{"id":"?"}>, but the function expects a integer.

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...
66
    }
67
68
    public function search()
69
    {
70
        return view('Comments/search');
71
    }
72
73
    public function favoris(Request $request)
74
    {
75
        $id = $request->input('id');
76
        $action = $request->input('action');
77
        // Récupération en session de l'item "favoris"
78
        $liked = session('commentsFavoris', []);
79
80
        if ($action == 'add') {
81
82
            // Enregistrement en variable de l'id souhaité
83
            $liked[] = $id;
84
            // Stockage de cette variable de la session
85
            Session::put('commentsFavoris', $liked);
86
        } else {
87
88
            // On cherche la position de l'id dans le tableau
89
            $position = array_search($id, $liked);
90
            // On supprime l'élément grâce à sa position
91
            unset($liked[$position]);
92
93
            // Stockage de cette variable de la session
94
            Session::put('commentsFavoris', $liked);
95
        }
96
    }
97
}
98