Completed
Push — master ( ba992d...dc755b )
by Davide
07:03
created

EventCategoryTranslationController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use App\EventCategoryTranslation;
9
10
class EventCategoryTranslationController extends Controller
11
{
12
    /* Restrict the access to this resource just to logged in users */
13
    public function __construct()
14
    {
15
        $this->middleware('admin');
16
    }
17
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @return \Illuminate\Http\Response
22
     */
23
    /*public function index()
24
    {
25
        //
26
    }*/
27
28
    /**
29
     * Show the form for creating a new resource.
30
     * @param int $eventCategoryId
31
     * @param int $languageCode
32
     * @return \Illuminate\Http\Response
33
     */
34
    public function create($eventCategoryId, $languageCode)
35
    {
36
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
37
38
        return view('eventCategoryTranslations.create')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\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...
39
                ->with('eventCategoryId', $eventCategoryId)
40
                ->with('languageCode', $languageCode)
41
                ->with('selectedLocaleName', $selectedLocaleName);
42
    }
43
44
    /**
45
     * Store a newly created resource in storage.
46
     *
47
     * @param  \Illuminate\Http\Request  $request
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function store(Request $request)
51
    {
52
        // Validate form datas
53
        $validator = Validator::make($request->all(), [
54
                'name' => 'required',
55
            ]);
56
        if ($validator->fails()) {
57
            return back()->withErrors($validator)->withInput();
58
        }
59
60
        $eventCategoryTranslation = new EventCategoryTranslation();
61
        $eventCategoryTranslation->event_category_id = $request->get('event_category_id');
62
        $eventCategoryTranslation->locale = $request->get('language_code');
63
64
        $eventCategoryTranslation->name = $request->get('name');
65
        $eventCategoryTranslation->slug = Str::slug($eventCategoryTranslation->name, '-');
66
67
        $eventCategoryTranslation->save();
68
69
        return redirect()->route('eventCategories.index')
70
                        ->with('success', 'Translation created successfully.');
71
    }
72
73
    /**
74
     * Display the specified resource.
75
     *
76
     * @param  \App\EventCategoryTranslation  $eventCategoryTranslation
77
     * @return \Illuminate\Http\Response
78
     */
79
    /*public function show(EventCategoryTranslation $eventCategoryTranslation)
80
    {
81
        //
82
    }*/
83
84
    /**
85
     * Show the form for editing the specified resource.
86
     * @param int $eventCategoryId
87
     * @param int $languageCode
88
     * @return \Illuminate\Http\Response
89
     */
90
    public function edit($eventCategoryId, $languageCode)
91
    {
92
        $eventCategoryTranslation = EventCategoryTranslation::where('event_category_id', $eventCategoryId)
93
                        ->where('locale', $languageCode)
94
                        ->first();
95
96
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
97
98
        return view('eventCategoryTranslations.edit', compact('eventCategoryTranslation'))
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\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...
99
                    ->with('eventCategoryId', $eventCategoryId)
100
                    ->with('languageCode', $languageCode)
101
                    ->with('selectedLocaleName', $selectedLocaleName);
102
    }
103
104
    /**
105
     * Update the specified resource in storage.
106
     *
107
     * @param  \Illuminate\Http\Request  $request
108
     * @return \Illuminate\Http\Response
109
     */
110
    public function update(Request $request)
111
    {
112
        request()->validate([
113
            'name' => 'required',
114
        ]);
115
116
        $eventCategoryTranslation = EventCategoryTranslation::where('id', $request->get('event_category_translation_id'));
117
118
        $event_category_t['name'] = $request->get('name');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$event_category_t was never initialized. Although not strictly required by PHP, it is generally a good practice to add $event_category_t = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
119
        $event_category_t['slug'] = Str::slug($request->get('name'), '-');
120
121
        $eventCategoryTranslation->update($event_category_t);
122
123
        return redirect()->route('eventCategories.index')
124
                        ->with('success', 'Translation updated successfully');
125
    }
126
127
    /**
128
     * Remove the specified resource from storage.
129
     *
130
     * @param  \App\EventCategoryTranslation  $eventCategoryTranslation
0 ignored issues
show
Documentation introduced by
There is no parameter named $eventCategoryTranslation. Did you maybe mean $eventCategoryTranslationId?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
131
     * @return \Illuminate\Http\Response
132
     */
133
    public function destroy($eventCategoryTranslationId)
134
    {
135
        $eventCategoryTranslation = EventCategoryTranslation::find($eventCategoryTranslationId);
136
        $eventCategoryTranslation->delete();
137
138
        return redirect()->route('eventCategories.index')
139
                        ->with('success', __('messages.event_category_translation_deleted_successfully'));
140
    }
141
}
142