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

UploadController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 4.81 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 8
dl 5
loc 104
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B fromField() 5 53 7
D ckEditorStore() 0 35 9

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 Validator;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\UploadedFile;
9
use Illuminate\Routing\Controller;
10
use SleepingOwl\Admin\Form\Element\File;
11
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14
class UploadController extends Controller
15
{
16
    /**
17
     * @param Request $request
18
     * @param ModelConfigurationInterface $model
19
     * @param string $field
20
     * @param int|null $id
21
     *
22
     * @return JsonResponse
23
     */
24
    public function fromField(Request $request, ModelConfigurationInterface $model, $field, $id = null)
25
    {
26
        if (! is_null($id)) {
27
            $item = $model->getRepository()->find($id);
28 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...
29
                return new JsonResponse([
30
                    'message' => trans('lang.message.access_denied'),
31
                ], 403);
32
            }
33
34
            $form = $model->fireEdit($id);
35
        } else {
36
            if (! $model->isCreatable()) {
37
                return new JsonResponse([
38
                    'message' => trans('lang.message.access_denied'),
39
                ], 403);
40
            }
41
42
            $form = $model->fireCreate();
43
        }
44
45
        /** @var File $element */
46
        if (is_null($element = $form->getElement($field))) {
47
            throw new NotFoundHttpException("Field [{$field}] not found");
48
        }
49
50
        $rules = $element->getUploadValidationRules();
51
        $messages = $element->getUploadValidationMessages();
52
        $labels = $element->getUploadValidationLabels();
53
54
        /** @var \Illuminate\Contracts\Validation\Validator $validator */
55
        $validator = Validator::make($request->all(), $rules, $messages, $labels);
56
57
        $element->customValidation($validator);
58
59
        if ($validator->fails()) {
60
            return new JsonResponse([
61
                'message' => trans('lang.message.validation_error'),
62
                'errors'  => $validator->errors()->get('file'),
63
            ], 400);
64
        }
65
66
        $file = $request->file('file');
67
68
        $filename = $element->getUploadFileName($file);
69
        $path = $element->getUploadPath($file);
70
        $settings = $element->getUploadSettings();
71
72
        $result = $element->saveFile($file, $path, $filename, $settings);
73
74
        /* When driver not file */
75
        return new JsonResponse($result);
76
    }
77
78
    /**
79
     * @param Request $request
80
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
81
     */
82
    public function ckEditorStore(Request $request)
83
    {
84
        //dropZone && CKEDITOR fileBrowser && CKEDITOR drag&drop
85
        /** @var UploadedFile $file */
86
        $file = $request->image ? $request->image : $request->file;
87
        $file = $file ? $file : $request->upload;
88
        if (is_array($file)) {
89
            $file = $file[0];
90
        }
91
92
        $result = [];
93
94
        $extensions = collect(['jpe', 'jpeg', 'jpg', 'png', 'bmp', 'ico', 'gif']);
95
96
        if ($extensions->search($file->getClientOriginalExtension())) {
97
            $file->move(public_path(config('sleeping_owl.imagesUploadDirectory')), $file->getClientOriginalName());
98
99
            $result['url'] = asset(
100
                config('sleeping_owl.imagesUploadDirectory').'/'.$file->getClientOriginalName()
101
            );
102
            $result['uploaded'] = 1;
103
            $result['fileName'] = $file->getClientOriginalName();
104
105
            if ($request->CKEditorFuncNum && $request->CKEditor && $request->langCode) {
106
                return app('sleeping_owl.template')
107
                    ->view('helper.ckeditor.ckeditor_upload_file', compact('result'));
108
            }
109
110
            if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
111
                return response($result);
0 ignored issues
show
Documentation introduced by
$result is of type array<string,?,{"fileName":"?"}>, but the function expects a string.

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...
112
            }
113
        }
114
115
        return response('Something wrong', 500);
116
    }
117
}
118