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

Test Failed
Push — datatable-single-component ( cc93c3...b8f315 )
by Pedro
11:39
created

CrudPanelManager::setupCrudPanel()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 2
dl 0
loc 23
rs 9.8333
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 array $initializedOperations = [];
15
16
    private ?string $currentlyActiveCrudController = null;
17
18
    public function getCrudPanel(CrudControllerContract|string $controller): CrudPanel
19
    {
20
        $controllerClass = is_string($controller) ? $controller : get_class($controller);
21
22
        if (isset($this->cruds[$controllerClass])) {
23
            return $this->cruds[$controllerClass];
24
        }
25
26
        $instance = new CrudPanel();
27
28
        $this->cruds[$controllerClass] = $instance;
29
30
        return $this->cruds[$controllerClass];
31
    }
32
33
    public function setupCrudPanel(string $controller, ?string $operation = null): CrudPanel
34
    {
35
        $controller = $this->getActiveController() ?? $controller;
36
37
        $controller = is_string($controller) ? app($controller) : $controller;
0 ignored issues
show
introduced by
The condition is_string($controller) is always true.
Loading history...
38
39
        $crud = $this->getCrudPanel($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::getCrudPanel() does only seem to accept Backpack\CRUD\app\Http\C...ntrollerContract|string, 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

39
        $crud = $this->getCrudPanel(/** @scrutinizer ignore-type */ $controller);
Loading history...
40
41
        // Use provided operation or default to 'list'
42
        $operation = $operation ?? 'list';
43
        $crud->setOperation($operation);
44
45
        $primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest();
46
        if (! $crud->isInitialized()) {
47
            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

47
            self::/** @scrutinizer ignore-call */ 
48
                  setActiveController($controller::class);
Loading history...
48
            $controller->initializeCrudPanel($primaryControllerRequest, $crud);
49
            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

49
            self::/** @scrutinizer ignore-call */ 
50
                  unsetActiveController();
Loading history...
50
            $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...
51
52
            return $this->cruds[$controller::class];
53
        }
54
55
        return $this->cruds[$controller::class];
56
    }
57
58
    public function storeInitializedOperation(string $controller, string $operation): void
59
    {
60
        $this->initializedOperations[$controller][] = $operation;
61
    }
62
63
    public function getInitializedOperations (string $controller): array
64
    {
65
        return $this->initializedOperations[$controller] ?? [];
66
    }
67
68
    public function storeCrudPanel(string $controller, CrudPanel $crud): void
69
    {
70
        $this->cruds[$controller] = $crud;
71
    }
72
73
    public function hasCrudPanel(string $controller): bool
74
    {
75
        return isset($this->cruds[$controller]);
76
    }
77
78
    public function getActiveCrudPanel(string $controller): CrudPanel
79
    {
80
        if (! isset($this->cruds[$controller])) {
81
            return $this->getCrudPanel($this->getActiveController() ?? $this->getParentController() ?? $controller);
82
        }
83
84
        return $this->cruds[$controller];
85
    }
86
87
    public function getParentController(): ?string
88
    {
89
        if (! empty($this->cruds)) {
90
            return array_key_first($this->cruds);
91
        }
92
93
        return $this->getActiveController();
94
    }
95
96
    public function setActiveController(string $controller): void
97
    {
98
        Facade::clearResolvedInstance('crud');
99
        $this->currentlyActiveCrudController = $controller;
100
    }
101
102
    public function getActiveController(): ?string
103
    {
104
        return $this->currentlyActiveCrudController;
105
    }
106
107
    public function unsetActiveController(): void
108
    {
109
        $this->currentlyActiveCrudController = null;
110
    }
111
112
    public function identifyCrudPanel(): CrudPanel
113
    {
114
        if ($this->getActiveController()) {
115
            return $this->getCrudPanel($this->getActiveController());
116
        }
117
118
        // Prioritize explicit controller context
119
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
120
        $controller = null;
121
122
        foreach ($trace as $step) {
123
            if (isset($step['class']) &&
124
                is_a($step['class'], CrudControllerContract::class, true)) {
125
                $controller = (string) $step['class'];
126
                break;
127
            }
128
        }
129
130
        if ($controller) {
131
            $crudPanel = self::getActiveCrudPanel($controller);
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...r::getActiveCrudPanel() 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

131
            /** @scrutinizer ignore-call */ 
132
            $crudPanel = self::getActiveCrudPanel($controller);
Loading history...
132
133
            return $crudPanel;
134
        }
135
136
        $cruds = self::getCrudPanels();
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelManager::getCrudPanels() 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

136
        /** @scrutinizer ignore-call */ 
137
        $cruds = self::getCrudPanels();
Loading history...
137
138
        if (! empty($cruds)) {
139
            $crudPanel = reset($cruds);
140
141
            return $crudPanel;
142
        }
143
144
        $this->cruds[CrudController::class] = new CrudPanel();
145
146
        return $this->cruds[CrudController::class];
147
    }
148
149
    public function getCrudPanels(): array
150
    {
151
        return $this->cruds;
152
    }
153
}
154