Completed
Push — master ( 383e32...dd0ca4 )
by Julien
02:48
created

CommentsController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4286
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Models\Comments;
6
use Illuminate\Support\Facades\Session;
7
use Illuminate\Http\Request;
8
9
10
11
/**
12
 * Class CategoriesController
13
 * @package App\Http\Controllers
14
 */
15
class CommentsController extends Controller
16
{
17
18
19
    /* ##################### METHODES ##################### */
20
21
22
    /**
23
     * @return \Illuminate\View\View
24
     */
25
    public function index()
26
    {
27
        $datas = [
28
            'comments' => Comments::all()
29
        ];
30
31
        $comment= new Comments();
32
        $datas['bestCommenter'] = $comment->bestCommenter();
33
34
        return view('Comments/index', $datas);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('Comments/index', $datas); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 34 which is incompatible with the return type documented by App\Http\Controllers\CommentsController::index of type Illuminate\View\View.
Loading history...
35
    }
36
37
    /**
38
     * @return \Illuminate\View\View
39
     */
40
    public function create()
41
    {
42
        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 42 which is incompatible with the return type documented by App\Http\Controllers\CommentsController::create of type Illuminate\View\View.
Loading history...
43
    }
44
45
    /**
46
     * @return \Illuminate\View\View
47
     */
48
    public function read($id)
49
    {
50
        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 50 which is incompatible with the return type documented by App\Http\Controllers\CommentsController::read of type Illuminate\View\View.
Loading history...
51
52
    }
53
54
    /**
55
     *
56
     */
57
    public function update(Request $request)
58
    {
59
        $field = 'content';
60
61
        $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...
62
        $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...
63
64
        $comment = Comments::find($id);
65
        $comment->update([$field => $value]);
66
//        Session::flash('success', "Le commentaire a bien été mis à jour");
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
//        return Redirect::route('comments.index');
68
    }
69
70
    /**
71
     * Pour la suppression, il n'y a pas de vue dédiée.
72
     * On redirige donc vers l'index.
73
     * @return \Illuminate\View\View
74
     */
75
    public function delete($id)
76
    {
77
        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...
78
79
    }
80
81
    public function search()
82
    {
83
        return view('Comments/search');
84
    }
85
86
    public function favoris(Request $request)
87
    {
88
        $id = $request->input('id');
89
        $action = $request->input('action');
90
        // Récupération en session de l'item "favoris"
91
        $liked = session("commentsFavoris", []);
92
93
        if ($action == "add") {
94
95
            // Enregistrement en variable de l'id souhaité
96
            $liked[] = $id;
97
            // Stockage de cette variable de la session
98
            Session::put("commentsFavoris", $liked);
99
100
101
        } else {
102
103
            // On cherche la position de l'id dans le tableau
104
            $position = array_search($id, $liked);
105
            // On supprime l'élément grâce à sa position
106
            unset($liked[$position]);
107
108
            // Stockage de cette variable de la session
109
            Session::put("commentsFavoris", $liked);
110
        }
111
112
        (dump(session("commentsFavoris")));
113
114
    }
115
116
117
}