AngkaMelekHurufController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 198
Duplicated Lines 34.34 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 68
loc 198
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B index() 0 25 4
A create() 0 9 1
B store() 19 33 3
A show() 9 9 1
A edit() 9 9 1
B update() 19 41 4
A destroy() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bantenprov\AngkaMelekHuruf\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\AngkaMelekHuruf\Facades\AngkaMelekHurufFacade;
9
10
/* Models */
11
use Bantenprov\AngkaMelekHuruf\Models\Bantenprov\AngkaMelekHuruf\AngkaMelekHuruf;
12
13
/* Etc */
14
use Validator;
15
16
/**
17
 * The AngkaMelekHurufController class.
18
 *
19
 * @package Bantenprov\AngkaMelekHuruf
20
 * @author  bantenprov <[email protected]>
21
 */
22
class AngkaMelekHurufController extends Controller
23
{
24
    /**
25
     * Create a new controller instance.
26
     *
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29
    public function __construct(AngkaMelekHuruf $angka_melek_huruf)
30
    {
31
        $this->angka_melek_huruf = $angka_melek_huruf;
32
    }
33
34
    /**
35
     * Display a listing of the resource.
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function index(Request $request)
40
    {
41
        if (request()->has('sort')) {
42
            list($sortCol, $sortDir) = explode('|', request()->sort);
43
44
            $query = $this->angka_melek_huruf->orderBy($sortCol, $sortDir);
45
        } else {
46
            $query = $this->angka_melek_huruf->orderBy('id', 'asc');
47
        }
48
49
        if ($request->exists('filter')) {
50
            $query->where(function($q) use($request) {
51
                $value = "%{$request->filter}%";
52
                $q->where('label', 'like', $value)
53
                    ->orWhere('description', 'like', $value);
54
            });
55
        }
56
57
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
58
        $response = $query->paginate($perPage);
59
60
        return response()->json($response)
61
            ->header('Access-Control-Allow-Origin', '*')
62
            ->header('Access-Control-Allow-Methods', 'GET');
63
    }
64
65
    /**
66
     * Show the form for creating a new resource.
67
     *
68
     * @return \Illuminate\Http\Response
69
     */
70
    public function create()
71
    {
72
        $angka_melek_huruf = $this->angka_melek_huruf;
73
74
        $response['angka_melek_huruf'] = $angka_melek_huruf;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
75
        $response['status'] = true;
76
77
        return response()->json($angka_melek_huruf);
78
    }
79
80
    /**
81
     * Display the specified resource.
82
     *
83
     * @param  \App\AngkaMelekHuruf  $angka_melek_huruf
0 ignored issues
show
Bug introduced by
There is no parameter named $angka_melek_huruf. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function store(Request $request)
87
    {
88
        $angka_melek_huruf = $this->angka_melek_huruf;
89
90
        $validator = Validator::make($request->all(), [
91
            'label' => 'required|max:16|unique:angka_melek_hurufs,label',
92
            'description' => 'max:255',
93
        ]);
94
95 View Code Duplication
        if($validator->fails()){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            $check = $angka_melek_huruf->where('label',$request->label)->whereNull('deleted_at')->count();
97
98
            if ($check > 0) {
99
                $response['message'] = 'Failed, label ' . $request->label . ' already exists';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
100
            } else {
101
                $angka_melek_huruf->label = $request->input('label');
102
                $angka_melek_huruf->description = $request->input('description');
103
                $angka_melek_huruf->save();
104
105
                $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
106
            }
107
        } else {
108
            $angka_melek_huruf->label = $request->input('label');
109
            $angka_melek_huruf->description = $request->input('description');
110
            $angka_melek_huruf->save();
111
112
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
113
        }
114
115
        $response['status'] = true;
116
117
        return response()->json($response);
118
    }
119
120
    /**
121
     * Store a newly created resource in storage.
122
     *
123
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
124
     * @return \Illuminate\Http\Response
125
     */
126 View Code Duplication
    public function show($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        $angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id);
129
130
        $response['angka_melek_huruf'] = $angka_melek_huruf;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
131
        $response['status'] = true;
132
133
        return response()->json($response);
134
    }
135
136
    /**
137
     * Show the form for editing the specified resource.
138
     *
139
     * @param  \App\AngkaMelekHuruf  $angka_melek_huruf
0 ignored issues
show
Bug introduced by
There is no parameter named $angka_melek_huruf. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
140
     * @return \Illuminate\Http\Response
141
     */
142 View Code Duplication
    public function edit($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        $angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id);
145
146
        $response['angka_melek_huruf'] = $angka_melek_huruf;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
147
        $response['status'] = true;
148
149
        return response()->json($response);
150
    }
151
152
    /**
153
     * Update the specified resource in storage.
154
     *
155
     * @param  \Illuminate\Http\Request  $request
156
     * @param  \App\AngkaMelekHuruf  $angka_melek_huruf
0 ignored issues
show
Bug introduced by
There is no parameter named $angka_melek_huruf. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
157
     * @return \Illuminate\Http\Response
158
     */
159
    public function update(Request $request, $id)
160
    {
161
        $angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id);
162
163
        if ($request->input('old_label') == $request->input('label'))
164
        {
165
            $validator = Validator::make($request->all(), [
166
                'label' => 'required|max:16',
167
                'description' => 'max:255',
168
            ]);
169
        } else {
170
            $validator = Validator::make($request->all(), [
171
                'label' => 'required|max:16|unique:angka_melek_hurufs,label',
172
                'description' => 'max:255',
173
            ]);
174
        }
175
176 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
            $check = $angka_melek_huruf->where('label',$request->label)->whereNull('deleted_at')->count();
178
179
            if ($check > 0) {
180
                $response['message'] = 'Failed, label ' . $request->label . ' already exists';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
181
            } else {
182
                $angka_melek_huruf->label = $request->input('label');
183
                $angka_melek_huruf->description = $request->input('description');
184
                $angka_melek_huruf->save();
185
186
                $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
187
            }
188
        } else {
189
            $angka_melek_huruf->label = $request->input('label');
190
            $angka_melek_huruf->description = $request->input('description');
191
            $angka_melek_huruf->save();
192
193
            $response['message'] = 'success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
194
        }
195
196
        $response['status'] = true;
197
198
        return response()->json($response);
199
    }
200
201
    /**
202
     * Remove the specified resource from storage.
203
     *
204
     * @param  \App\AngkaMelekHuruf  $angka_melek_huruf
0 ignored issues
show
Bug introduced by
There is no parameter named $angka_melek_huruf. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
205
     * @return \Illuminate\Http\Response
206
     */
207 View Code Duplication
    public function destroy($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209
        $angka_melek_huruf = $this->angka_melek_huruf->findOrFail($id);
210
211
        if ($angka_melek_huruf->delete()) {
212
            $response['status'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
213
        } else {
214
            $response['status'] = false;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = 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...
215
        }
216
217
        return json_encode($response);
218
    }
219
}
220