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

Passed
Push — next ( c4bfb8...562dda )
by Cristian
16:05
created

Dataform::applySetupClosure()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 22
rs 9.8666
1
<?php
2
3
namespace Backpack\CRUD\app\View\Components;
4
5
use Backpack\CRUD\CrudManager;
6
use Closure;
7
use Illuminate\View\Component;
8
9
class Dataform extends Component
10
{
11
    public $crud;
12
13
    /**
14
     * Create a new component instance.
15
     *
16
     * @param  string  $controller  The CRUD controller class name
17
     * @param  string  $operation  The operation to use (create, update, etc.)
18
     * @param  string|null  $action  Custom form action URL
19
     * @param  string  $method  Form method (post, put, etc.)
20
     */
21
    public function __construct(
22
        public string $controller,
23
        public string $id = 'backpack-form',
24
        public string $operation = 'create',
25
        public ?string $action = null,
26
        public string $method = 'post',
27
        public bool $hasUploadFields = false,
28
        public $entry = null,
29
        public ?Closure $setup = null,
30
31
    ) {
32
        // Get CRUD panel instance from the controller
33
        CrudManager::setActiveController($controller);
0 ignored issues
show
Bug introduced by
The method setActiveController() does not exist on Backpack\CRUD\CrudManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        CrudManager::/** @scrutinizer ignore-call */ 
34
                     setActiveController($controller);
Loading history...
34
35
        $this->crud = CrudManager::setupCrudPanel($controller, $operation);
0 ignored issues
show
Bug introduced by
The method setupCrudPanel() does not exist on Backpack\CRUD\CrudManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $this->crud = CrudManager::setupCrudPanel($controller, $operation);
Loading history...
36
        //dd($this->crud);
37
        if ($this->entry && $this->operation === 'update') {
38
            $this->action = $action ?? url($this->crud->route.'/'.$this->entry->getKey());
39
            $this->method = 'put';
40
            $this->crud->entry = $this->entry;
41
            $this->crud->setOperationSetting('fields', $this->crud->getUpdateFields());
42
        } else {
43
            $this->action = $action ?? url($this->crud->route);
0 ignored issues
show
Documentation Bug introduced by
It seems like $action ?? url($this->crud->route) can also be of type Illuminate\Contracts\Routing\UrlGenerator. However, the property $action is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
44
        }
45
        $this->hasUploadFields = $this->crud->hasUploadFields($operation, $this->entry?->getKey());
46
        $this->id = $id.md5($this->action.$this->operation.$this->method.$this->controller);
47
        if ($this->setup) {
48
            $this->applySetupClosure();
49
        }
50
51
        CrudManager::unsetActiveController();
0 ignored issues
show
Bug introduced by
The method unsetActiveController() does not exist on Backpack\CRUD\CrudManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        CrudManager::/** @scrutinizer ignore-call */ 
52
                     unsetActiveController();
Loading history...
52
    }
53
54
    public function applySetupClosure(): bool
55
    {
56
        $originalSetup = $this->setup;
57
        $controllerClass = $this->controller;
58
        $crud = $this->crud;
59
        $entry = $this->entry;
60
61
        $modifiedSetup = function ($crud, $entry) use ($originalSetup, $controllerClass) {
62
            CrudManager::setActiveController($controllerClass);
63
64
            // Run the original closure
65
            return ($originalSetup)($crud, $entry);
66
        };
67
68
        try {
69
            // Execute the modified closure
70
            ($modifiedSetup)($crud, $entry);
71
72
            return true;
73
        } finally {
74
            // Clean up
75
            CrudManager::unsetActiveController();
76
        }
77
    }
78
79
    /**
80
     * Get the view / contents that represent the component.
81
     *
82
     * @return \Illuminate\Contracts\View\View|\Closure|string
83
     */
84
    public function render()
85
    {
86
        return view('crud::components.dataform.form', [
87
            'crud' => $this->crud,
88
            'saveAction' => $this->crud->getSaveAction(),
89
            'id' => $this->id,
90
            'operation' => $this->operation,
91
            'action' => $this->action,
92
            'method' => $this->method,
93
            'hasUploadFields' => $this->hasUploadFields,
94
            'entry' => $this->entry,
95
        ]);
96
    }
97
}
98