Passed
Push — dev5 ( b08d9a...c43caf )
by Ron
04:08
created

SystemsController::store()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 55

Duplication

Lines 24
Ratio 43.64 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 24
loc 55
ccs 0
cts 31
cp 0
rs 8.9818
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Installer;
4
5
use App\SystemTypes;
6
use App\SystemCategories;
7
use App\SystemCustDataTypes;
8
use Illuminate\Http\Request;
9
use App\SystemCustDataFields;
10
use Illuminate\Validation\Rule;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Facades\Auth;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Route;
15
16
class SystemsController extends Controller
17
{
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
    
23
    //  List the systems that can be modified
24 View Code Duplication
    public function index()
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...
25
    {
26
        $systems = SystemCategories::with('SystemTypes')->get();
27
        
28
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_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...
29
        Log::debug('Fetched Data - ', $systems->toArray());
30
        return view('installer.systemsList', [
31
            'systems' => $systems
32
        ]);
33
    }
34
35
    //  Open the form to create a new system
36 View Code Duplication
    public function create()
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...
37
    {
38
        $categories = SystemCategories::all();
39
        $dataTypes  = SystemCustDataTypes::orderBy('name', 'ASC')->get();
40
        
41
        $dropDown = [];
42
        foreach($dataTypes as $type)
43
        {
44
            $dropDown[] = [
45
                'value' => $type->data_type_id,
46
                'label' => $type->name
47
            ];
48
        }
49
        
50
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_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...
51
        return view('installer.newSystem', [
52
            'categories' => $categories,
53
            'dropDown'   => $dropDown
54
        ]);
55
    }
56
57
    //  Store the new system type
58
    public function store(Request $request)
59
    {
60
        $request->validate([
61
            'category' => 'required|numeric',
62
            'name' => [
63
                    'required',
64
                    'string',
65
                    Rule::unique('system_types'),
66
                    'regex:/^[a-zA-Z0-9_ ]*$/'
67
                ],
68
            'dataOptions' => 'required'
69
            
70
        ]);
71
        
72
        $sysData = SystemTypes::create([
73
            'cat_id'          => $request->category,
74
            'name'            => $request->name,
75
            'parent_id'       => null,
76
            'folder_location' => str_replace(' ', '_', $request->name)
77
        ]);
78
        $sysID = $sysData->sys_id;
79
        $i = 0;
80
        
81 View Code Duplication
        foreach($request->dataOptions as $field)
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...
82
        {
83
            if(!empty($field))
84
            {
85
                if(isset($field['value']))
86
                {
87
                    $id = $field['value'];
88
                }
89
                else
90
                {
91
                    $newField = SystemCustDataTypes::create([
92
                        'name' => $field['label']
93
                    ]);
94
                    $id = $newField->data_type_id;
95
                }
96
97
                SystemCustDataFields::create([
98
                    'sys_id' => $sysID,
99
                    'data_type_id' => $id,
100
                    'order' => $i
101
                ]);
102
                $i++;
103
            }
104
        }
105
        
106
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_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...
107
        Log::debug('Submitted Data - ', $request->toArray());
108
        Log::notice('New System Created', ['cat_id' => $request->catName, 'sys_name' => $request->name, 'user_id' => Auth::user()->user_id]);
0 ignored issues
show
Bug introduced by
Accessing user_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...
109
        $request->session()->flash('success', 'New System Created');
0 ignored issues
show
Bug introduced by
The method flash() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
        
111
        return response()->json(['success' => true]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
112
    }
113
114
    //  Get a JSON array of the system to be edited
115
    public function show($id)
116
    {
117
        $system = SystemTypes::find($id);
118
        $data   = SystemCustDataFields::where('sys_id', $id)
119
            ->join('system_cust_data_types', 'system_cust_data_fields.data_type_id', '=', 'system_cust_data_types.data_type_id')
120
            ->orderBy('order', 'ASC')
121
            ->pluck('name');
122
        
123
        $sysData = [
124
            'name' => $system->name,
125
            'data' => $data
126
        ];
127
        
128
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_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...
129
        Log::debug('Fetched Data - ', $sysData);
130
        return response()->json($sysData);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
131
    }
132
133
    //  Edit an existing system
134 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...
135
    {
136
        $system = SystemTypes::find($id);
137
        $dataTypes = SystemCustDataTypes::orderBy('name', 'ASC')->get();
138
        
139
        $dropDown = [];
140
        foreach($dataTypes as $type)
141
        {
142
            $dropDown[] = [
143
                'value' => $type->data_type_id,
144
                'label' => $type->name
145
            ];
146
        }
147
        
148
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_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...
149
        return view('installer.editSystem', [
150
            'sys_id'   => $id,
151
            'name'     => $system->name,
152
            'dropDown' => $dropDown
153
        ]);
154
    }
155
156
    //  Update the system data
157
    public function update(Request $request, $id)
158
    {
159
        $request->validate([
160
            'name'     => [
161
                    'required',
162
                    'string',
163
                    Rule::unique('system_types')->ignore($id, 'sys_id'),
164
                    'regex:/^[a-zA-Z0-9_ ]*$/'
165
                ]
166
        ]);
167
        
168
        //  Update the system name
169
        SystemTypes::find($id)->update([
170
            'name' => $request->name
171
        ]);
172
        
173
        //  Update the order of the existing data fields
174
        $i = 0;
175
        foreach($request->dataOptions as $data)
176
        {
177
            $dataID = SystemCustDataTypes::where('name', $data)->first();
178
            
179
            SystemCustDataFields::where('sys_id', $id)->where('data_type_id', $dataID->data_type_id)->update([
180
                'order' => $i
181
            ]);
182
            
183
            $i++;
184
        }
185
        
186
        //  Process any new data fields
187
        if(!empty($request->newDataOptions))
188
        {
189 View Code Duplication
            foreach($request->newDataOptions as $field)
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...
190
            {
191
                if(!empty($field))
192
                {
193
                    if(isset($field['value']))
194
                    {
195
                        $dataID = $field['value'];
196
                    }
197
                    else
198
                    {
199
                        $newField = SystemCustDataTypes::create([
200
                            'name' => $field['label']
201
                        ]);
202
                        $dataID = $newField->data_type_id;
203
                    }
204
205
                    SystemCustDataFields::create([
206
                        'sys_id' => $id,
207
                        'data_type_id' => $dataID,
208
                        'order' => $i
209
                    ]);
210
                    $i++;
211
                }
212
            }
213
        }
214
        
215
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_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...
216
        Log::debug('Submitted Data - ', $request->toArray());
217
        Log::notice('System Updated', ['sys_name' => $request->name, 'user_id' => Auth::user()->user_id]);
0 ignored issues
show
Bug introduced by
Accessing user_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...
218
        $request->session()->flash('success', 'System Updated');
0 ignored issues
show
Bug introduced by
The method flash() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
219
        
220
        return response()->json(['success' => true]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
221
    }
222
223
    /**
224
     * Remove the specified resource from storage.
225
     *
226
     * @param  int  $id
227
     * @return \Illuminate\Http\Response
228
     */
229
//    public function destroy($id)
230
//    {
231
//        //
232
//    }
233
}
234