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 — datatable-single-component ( f66a1f...9806d9 )
by Pedro
11:53
created

CrudPanelManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD;
4
5
use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract;
6
use Backpack\CRUD\app\Http\Controllers\CrudController;
7
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
8
use Illuminate\Support\Facades\Facade;
9
10
final class CrudPanelManager
11
{
12
    private array $cruds = [];
13
14
    private ?string $currentlyActiveCrudController = null;
15
16
    private $requestController = null;
17
18
    public function crud(CrudControllerContract $controller): CrudPanel
19
    {
20
        $controllerClass = get_class($controller);
21
22
        $this->requestController = $controllerClass;
23
24
        if (isset($this->cruds[$controllerClass])) {
25
            return $this->cruds[$controllerClass];
26
        }
27
28
        $instance = new CrudPanel();
29
30
        $this->cruds[$controllerClass] = $instance;
31
32
        return $this->cruds[$controllerClass];
33
    }
34
35
    public function crudFromController(string $controller, ?string $operation = null): CrudPanel
36
    {
37
        $controller = $this->getActiveController() ?? $controller;
38
39
        $controller = is_string($controller) ? app($controller) : $controller;
0 ignored issues
show
introduced by
The condition is_string($controller) is always true.
Loading history...
40
41
        $crud = $this->crud($controller);
0 ignored issues
show
Bug introduced by
It seems like $controller can also be of type Illuminate\Contracts\Foundation\Application and Illuminate\Foundation\Application; however, parameter $controller of Backpack\CRUD\CrudPanelManager::crud() does only seem to accept Backpack\CRUD\app\Http\C...\CrudControllerContract, maybe add an additional type check? ( Ignorable by Annotation )

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

41
        $crud = $this->crud(/** @scrutinizer ignore-type */ $controller);
Loading history...
42
43
        // Use provided operation or default to 'list'
44
        $operation = $operation ?? 'list';
45
        $crud->setOperation($operation);
46
47
        $primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest();
48
        if (! $crud->isInitialized()) {
49
            self::setActiveController($controller::class);
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...::setActiveController() is not static, but was called statically. ( Ignorable by Annotation )

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

49
            self::/** @scrutinizer ignore-call */ 
50
                  setActiveController($controller::class);
Loading history...
50
            $controller->initializeCrudController($primaryControllerRequest, $crud);
51
            self::unsetActiveController();
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...unsetActiveController() is not static, but was called statically. ( Ignorable by Annotation )

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

51
            self::/** @scrutinizer ignore-call */ 
52
                  unsetActiveController();
Loading history...
52
            $crud = $this->cruds[$controller::class];
0 ignored issues
show
Unused Code introduced by
The assignment to $crud is dead and can be removed.
Loading history...
53
            return $this->cruds[$controller::class];
54
        }
55
56
        return $this->cruds[$controller::class];
57
    }
58
59
    public function setControllerCrud(string $controller, CrudPanel $crud): void
60
    {
61
        $this->cruds[$controller] = $crud;
62
    }
63
64
    public function hasCrudController(string $controller): bool
65
    {
66
        return isset($this->cruds[$controller]);
67
    }
68
69
    public function getControllerCrud(string $controller): CrudPanel
70
    {
71
        if (! isset($this->cruds[$controller])) {
72
            return $this->crudFromController($this->getActiveController() ?? $this->requestController ?? $controller);
73
        }
74
75
        return $this->cruds[$controller];
76
    }
77
78
    public function getRequestController(): ?string
79
    {
80
        return $this->requestController;
81
    }
82
83
    public function setActiveController(string $controller): void
84
    {
85
        Facade::clearResolvedInstance('crud');
86
        $this->currentlyActiveCrudController = $controller;
87
    }
88
89
    public function getActiveController(): ?string
90
    {
91
        return $this->currentlyActiveCrudController;
92
    }
93
94
    public function unsetActiveController(): void
95
    {
96
        $this->currentlyActiveCrudController = null;
97
    }
98
99
    public function getCrudPanel()
100
    {
101
        if ($this->getActiveController()) {
102
            return $this->crudFromController($this->getActiveController());
103
        }
104
105
        // Prioritize explicit controller context
106
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
107
        $controller = null;
108
109
        foreach ($trace as $step) {
110
            if (isset($step['class']) &&
111
                is_a($step['class'], CrudControllerContract::class, true)) {
112
                $controller = (string) $step['class'];
113
                break;
114
            }
115
        }
116
117
        if ($controller) {
118
            $crudPanel = self::getControllerCrud($controller);
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...er::getControllerCrud() is not static, but was called statically. ( Ignorable by Annotation )

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

118
            /** @scrutinizer ignore-call */ 
119
            $crudPanel = self::getControllerCrud($controller);
Loading history...
119
120
            return $crudPanel;
121
        }
122
123
        $cruds = self::getCruds();
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelManager::getCruds() is not static, but was called statically. ( Ignorable by Annotation )

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

123
        /** @scrutinizer ignore-call */ 
124
        $cruds = self::getCruds();
Loading history...
124
125
        if (! empty($cruds)) {
126
            $crudPanel = reset($cruds);
127
128
            return $crudPanel;
129
        }
130
131
        $this->cruds[CrudController::class] = new CrudPanel();
132
        return $this->cruds[CrudController::class];
133
    }
134
135
    public function getCruds(): array
136
    {
137
        return $this->cruds;
138
    }
139
}
140