Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 6bda91...26ffdc )
by Cristian
03:56 queued 24s
created

Update::updateCrud()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 29

Duplication

Lines 5
Ratio 17.24 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 1
dl 5
loc 29
rs 9.1448
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations;
4
5
trait Update
6
{
7
    /**
8
     * Show the form for editing the specified resource.
9
     *
10
     * @param int $id
11
     *
12
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
13
     */
14
    public function edit($id)
15
    {
16
        $this->crud->hasAccessOrFail('update');
0 ignored issues
show
Bug introduced by
The property crud does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
18
        // get entry ID from Request (makes sure its the last ID for nested resources)
19
        $id = $this->crud->getCurrentEntryId() ?? $id;
20
21
        // get the info for that entry
22
        $this->data['entry'] = $this->crud->getEntry($id);
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        $this->data['crud'] = $this->crud;
24
        $this->data['saveAction'] = $this->getSaveAction();
0 ignored issues
show
Bug introduced by
It seems like getSaveAction() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
        $this->data['fields'] = $this->crud->getUpdateFields($id);
26
        $this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name;
27
28
        $this->data['id'] = $id;
29
30
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
31
        return view($this->crud->getEditView(), $this->data);
32
    }
33
34
    /**
35
     * Update the specified resource in the database.
36
     *
37
     * @param UpdateRequest $request - type injection used for validation using Requests
0 ignored issues
show
Documentation introduced by
Should the type for parameter $request not be null|UpdateRequest?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     *
39
     * @return \Illuminate\Http\RedirectResponse
40
     */
41
    public function updateCrud(UpdateRequest $request = null)
42
    {
43
        $this->crud->hasAccessOrFail('update');
44
45
        // fallback to global request instance
46
        if (is_null($request)) {
47
            $request = \Request::instance();
48
        }
49
50
        // replace empty values with NULL, so that it will work with MySQL strict mode on
51 View Code Duplication
        foreach ($request->input() as $key => $value) {
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...
52
            if (empty($value) && $value !== '0') {
53
                $request->request->set($key, null);
54
            }
55
        }
56
57
        // update the row in the db
58
        $item = $this->crud->update($request->get($this->crud->model->getKeyName()),
59
                            $request->except('save_action', '_token', '_method', 'current_tab'));
60
        $this->data['entry'] = $this->crud->entry = $item;
61
62
        // show a success message
63
        \Alert::success(trans('backpack::crud.update_success'))->flash();
64
65
        // save the redirect choice for next time
66
        $this->setSaveAction();
0 ignored issues
show
Bug introduced by
It seems like setSaveAction() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
68
        return $this->performSaveAction($item->getKey());
0 ignored issues
show
Bug introduced by
It seems like performSaveAction() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
    }
70
}
71