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 — fix-forms ( 652cf5...1c051a )
by Pedro
13:49
created

Dataform   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 37
c 2
b 0
f 1
dl 0
loc 87
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 31 4
A applySetupClosure() 0 22 1
A render() 0 11 1
1
<?php
2
3
namespace Backpack\CRUD\app\View\Components;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
6
use Backpack\CRUD\CrudManager;
7
use Closure;
8
use Illuminate\View\Component;
9
10
class Dataform extends Component
11
{
12
    public $crud;
13
14
    /**
15
     * Create a new component instance.
16
     *
17
     * @param  string  $controller  The CRUD controller class name
18
     * @param  string  $operation  The operation to use (create, update, etc.)
19
     * @param  string|null  $action  Custom form action URL
20
     * @param  string  $method  Form method (post, put, etc.)
21
     */
22
    public function __construct(
23
        public string $controller,
24
        public string $id = 'backpack-form',
25
        public string $operation = 'create',
26
        public ?string $action = null,
27
        public string $method = 'post',
28
        public bool $hasUploadFields = false,
29
        public $entry = null,
30
        public ?Closure $setup = null,
31
32
    ) {        
33
        // Get CRUD panel instance from the controller
34
        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

34
        CrudManager::/** @scrutinizer ignore-call */ 
35
                     setActiveController($controller);
Loading history...
35
36
        $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

36
        /** @scrutinizer ignore-call */ 
37
        $this->crud = CrudManager::setupCrudPanel($controller, $operation);
Loading history...
37
        //dd($this->crud);
38
        if ($this->entry && $this->operation === 'update') {
39
            $this->action = $action ?? url($this->crud->route.'/'.$this->entry->getKey());
40
            $this->method = 'put';
41
            $this->crud->entry = $this->entry;
42
            $this->crud->setOperationSetting('fields', $this->crud->getUpdateFields());
43
        } else {
44
            $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...
45
        }
46
        $this->hasUploadFields = $this->crud->hasUploadFields($operation, $this->entry?->getKey());
47
        $this->id = $id.md5($this->action.$this->operation.$this->method.$this->controller);
48
         if ($this->setup) {
49
            $this->applySetupClosure();
50
        }
51
52
        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

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