GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#688)
by
unknown
18:52
created

FormElementController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 3.91 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 7
dl 5
loc 128
ccs 0
cts 75
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getModelLogic() 5 21 5
B dependentSelect() 0 34 4
A multiselectSearch() 0 4 1
B selectSearch() 0 34 5

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 SleepingOwl\Admin\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Database\Eloquent\Model;
9
use SleepingOwl\Admin\Form\Element\DependentSelect;
10
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
11
12
class FormElementController extends Controller
13
{
14
    /**
15
     * @param ModelConfigurationInterface $model
16
     * @param null $id
17
     * @return JsonResponse|mixed
18
     */
19
    public function getModelLogic(ModelConfigurationInterface $model, $id = null)
20
    {
21
        if (! is_null($id)) {
22
            $item = $model->getRepository()->find($id);
23 View Code Duplication
            if (is_null($item) || ! $model->isEditable($item)) {
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...
24
                return new JsonResponse([
25
                    'message' => trans('lang.message.access_denied'),
26
                ], 403);
27
            }
28
29
            return $model->fireEdit($id);
30
        }
31
32
        if (! $model->isCreatable()) {
33
            return new JsonResponse([
34
                'message' => trans('lang.message.access_denied'),
35
            ], 403);
36
        }
37
38
        return $model->fireCreate();
39
    }
40
41
    /**
42
     * @param Request $request
43
     * @param ModelConfigurationInterface $model
44
     * @param string $field
45
     * @param int|null $id
46
     *
47
     * @return JsonResponse
48
     */
49
    public function dependentSelect(Request $request, ModelConfigurationInterface $model, $field, $id = null)
50
    {
51
        $form = $this->getModelLogic($model, $id);
0 ignored issues
show
Bug introduced by
It seems like $id defined by parameter $id on line 49 can also be of type integer; however, SleepingOwl\Admin\Http\C...roller::getModelLogic() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
53
        if ($form instanceof JsonResponse) {
54
            return $form;
55
        }
56
57
        /** @var DependentSelect $element */
58
        $element = $form->getElement($field);
59
60
        if (is_null($element)) {
61
            return new JsonResponse([
62
                'message' => 'Element not found',
63
            ], 404);
64
        }
65
66
        $element->setAjaxParameters(
67
            $request->input('depdrop_all_params', [])
0 ignored issues
show
Bug introduced by
It seems like $request->input('depdrop_all_params', array()) targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type string; however, SleepingOwl\Admin\Form\E...ct::setAjaxParameters() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
68
        );
69
70
        $options = $element->getOptions();
71
72
        if ($element->isNullable()) {
73
            $options = [null => trans('sleeping_owl::lang.select.nothing')] + $options;
74
        }
75
76
        return new JsonResponse([
77
            'output'   => collect($options)->map(function ($value, $key) {
78
                return ['id' => $key, 'name' => $value];
79
            }),
80
            'selected' => $element->getValueFromModel(),
81
        ]);
82
    }
83
84
    /**
85
     * @param Request $request
86
     * @param ModelConfigurationInterface $model
87
     * @param string $field
88
     * @param int|null $id
89
     *
90
     * @return JsonResponse
91
     */
92
    public function multiselectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null)
93
    {
94
        return $this->selectSearch($request, $model, $field, $id);
95
    }
96
97
    /**
98
     * @param Request $request
99
     * @param ModelConfigurationInterface $model
100
     * @param string $field
101
     * @param int|null $id
102
     *
103
     * @return JsonResponse
104
     */
105
    public function selectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null)
106
    {
107
        $form = $this->getModelLogic($model, $id);
0 ignored issues
show
Bug introduced by
It seems like $id defined by parameter $id on line 105 can also be of type integer; however, SleepingOwl\Admin\Http\C...roller::getModelLogic() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
108
109
        if ($form instanceof JsonResponse) {
110
            return $form;
111
        }
112
113
        /** @var DependentSelect $element */
114
        $element = $form->getElement($field);
115
116
        if (is_null($element)) {
117
            return new JsonResponse([
118
                'message' => 'Element not found',
119
            ], 404);
120
        }
121
122
        $field = $request->field;
123
        $model = new $request->model;
124
125
        if ($request->q && is_object($model)) {
126
            return new JsonResponse(
127
                $model::where($request->search, 'like', "%{$request->q}%")
128
                    ->get()
129
                    ->map(function (Model $item) use ($field) {
130
                        return [
131
                            'tag_name'    => $item->{$field},
132
                            'id'          => $item->id,
133
                            'custom_name' => $item->custom_name,
134
                        ];
135
                    })
136
            );
137
        }
138
    }
139
}
140