Test Setup Failed
Push — master ( c48a36...2ef870 )
by Oliver
04:00
created

NestedModelsCrudController::ajaxStore()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 6
nop 1
1
<?php
2
3
namespace Webfactor\Laravel\Backpack\NestedModels\Controllers;
4
5
use Backpack\CRUD\app\Http\Controllers\CrudController;
6
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest;
7
use Illuminate\Http\Request;
8
9
class NestedModelsCrudController extends CrudController
10
{
11
12
    public function setup()
13
    {
14
        parent::setup();
15
16
        $this->crud->setListView('nestedmodels::treecrud');
17
        $this->crud->orderBy('lft');
18
    }
19
20
    public function index()
21
    {
22
        $this->crud->hasAccessOrFail('list');
23
24
        $this->data['crud'] = $this->crud;
25
        $this->data['title'] = ucfirst($this->crud->entity_name_plural);
26
27
        $entries = $this->data['crud']->getEntries();
28
        $this->data['entries'] = $this->crud->model::loadTree($entries);
0 ignored issues
show
Bug introduced by
The method loadTree cannot be called on $this->crud->model (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
29
30
        return view($this->crud->getListView(), $this->data);
31
    }
32
33
    public function create()
34
    {
35
        $request = Request::instance();
36
        $this->addFieldsForType($request->get('type'));
0 ignored issues
show
Documentation Bug introduced by
The method addFieldsForType does not exist on object<Webfactor\Laravel...edModelsCrudController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
        if ($parent = $request->get('parent')) {
38
            $this->crud->addField([
39
                'name'  => 'parent_id',
40
                'type'  => 'hidden',
41
                'value' => $parent
42
            ]);
43
        }
44
45
        if ($request->wantsJson()) {
46
            return $this->ajaxCreate($request);
47
        }
48
49
        return parent::create();
50
    }
51
52
    public function ajaxCreate(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54
        $this->crud->hasAccessOrFail('create');
55
56
        // prepare the fields you need to show
57
        $this->data['crud'] = $this->crud;
58
        $this->data['saveAction'] = $this->getSaveAction();
59
        $this->data['fields'] = $this->crud->getCreateFields();
60
        $this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name;
61
62
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
63
        return view('nestedmodels::ajax.create', $this->data);
64
    }
65
66
    public function store(StoreRequest $request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
    {
68
        if ($request->wantsJson()) {
69
            return $this->ajaxStore($request);
70
        }
71
72
        // your additional operations before save here
73
        $redirect_location = parent::storeCrud($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (storeCrud() instead of store()). Are you sure this is correct? If so, you might want to change this to $this->storeCrud().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
74
        // your additional operations after save here
75
        // use $this->data['entry'] or $this->crud->entry
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
        return $redirect_location;
77
    }
78
79
    public function ajaxStore(StoreRequest $request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
80
    {
81
        $this->crud->hasAccessOrFail('create');
82
83
        // fallback to global request instance
84
        if (is_null($request)) {
85
            $request = \Request::instance();
86
        }
87
88
        // replace empty values with NULL, so that it will work with MySQL strict mode on
89
        foreach ($request->input() as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $request->input() of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
90
            if (empty($value) && $value !== '0') {
91
                $request->request->set($key, null);
92
            }
93
        }
94
95
        // insert item in the db
96
        return $this->crud->create($request->except(['save_action', '_token', '_method']));
97
    }
98
99
}