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 — main ( d3a2ba...cb4bdb )
by Pedro
34:31 queued 19:34
created

DetectEditorAddonRequirementsStep::run()   C

Complexity

Conditions 15
Paths 54

Size

Total Lines 76
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 15
eloc 43
nc 54
nop 0
dl 0
loc 76
rs 5.9166
c 1
b 0
f 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Backpack\CRUD\app\Console\Commands\Upgrade\v7\Steps;
4
5
use Backpack\CRUD\app\Console\Commands\Upgrade\Concerns\InteractsWithCrudControllers;
6
use Backpack\CRUD\app\Console\Commands\Upgrade\Step;
7
use Backpack\CRUD\app\Console\Commands\Upgrade\StepResult;
8
use Backpack\CRUD\app\Console\Commands\Upgrade\StepStatus;
9
10
class DetectEditorAddonRequirementsStep extends Step
11
{
12
    use InteractsWithCrudControllers;
13
14
    protected array $editors = [
15
        'ckeditor' => [
16
            'package' => 'backpack/ckeditor-field',
17
            'constraint' => '^1.0',
18
        ],
19
        'tinymce' => [
20
            'package' => 'backpack/tinymce-field',
21
            'constraint' => '^1.0',
22
        ],
23
    ];
24
25
    private array $missingPackages = [];
26
27
    private array $uninstalledPackages = [];
28
29
    public function title(): string
30
    {
31
        return 'Check if required WYSIWYG editors add-ons are installed';
32
    }
33
34
    public function run(): StepResult
35
    {
36
        $this->missingPackages = [];
37
        $this->uninstalledPackages = [];
38
39
        $matches = $this->context()->searchTokens(array_keys($this->editors));
40
41
        foreach ($this->editors as $keyword => $config) {
42
            $package = $config['package'];
43
            $recommendedConstraint = $config['constraint'] ?? 'dev-next';
44
45
            $paths = $this->filterCrudControllerPaths($matches[$keyword] ?? []);
46
47
            if (empty($paths)) {
48
                continue;
49
            }
50
51
            $installedVersion = $this->context()->installedPackageVersion($package);
52
            $composerConstraint = $this->context()->composerRequirement($package);
53
54
            if ($composerConstraint !== null && $installedVersion !== null) {
55
                continue;
56
            }
57
58
            if ($composerConstraint === null) {
59
                $this->missingPackages[$package] = [
60
                    'keyword' => $keyword,
61
                    'constraint' => $recommendedConstraint,
62
                ];
63
64
                continue;
65
            }
66
67
            if ($installedVersion === null) {
68
                $this->uninstalledPackages[$package] = [
69
                    'keyword' => $keyword,
70
                    'constraint' => $composerConstraint,
71
                ];
72
            }
73
        }
74
75
        if (empty($this->missingPackages) && empty($this->uninstalledPackages)) {
76
            return StepResult::success('No missing editor add-ons detected.');
77
        }
78
79
        $detailLines = [];
80
81
        if (! empty($this->missingPackages)) {
82
            foreach ($this->missingPackages as $package => $data) {
83
                $detailLines[] = sprintf('- %s (%s field/column usage detected)', $package, $data['keyword']);
84
            }
85
        }
86
87
        if (! empty($this->uninstalledPackages) && empty($this->missingPackages)) {
88
            foreach ($this->uninstalledPackages as $package => $data) {
89
                $detailLines[] = sprintf('- %s (%s field/column usage detected)', $package, $data['keyword']);
90
            }
91
        }
92
93
        $context = [
94
            'missing_packages' => array_keys($this->missingPackages),
95
            'uninstalled_packages' => array_keys($this->uninstalledPackages),
96
        ];
97
98
        if (! empty($this->missingPackages)) {
99
            return StepResult::failure(
100
                'There are missing editor packages required by your CrudControllers.',
101
                $detailLines,
102
                $context
103
            );
104
        }
105
106
        return StepResult::warning(
107
            'Addons declared in composer.json but not installed yet.',
108
            $detailLines,
109
            $context
110
        );
111
    }
112
113
    public function canFix(StepResult $result): bool
114
    {
115
        return $result->status === StepStatus::Failed && ! empty($this->missingPackages);
116
    }
117
118
    public function fixMessage(StepResult $result): string
119
    {
120
        return 'We will add the required packages to composer.json. Proceed?';
121
    }
122
123
    public function fix(StepResult $result): StepResult
124
    {
125
        if (empty($this->missingPackages)) {
126
            return StepResult::skipped('No missing editor packages detected.');
127
        }
128
129
        $missingPackages = $this->missingPackages;
130
131
        $updated = $this->context()->updateComposerJson(function (array &$composer) use ($missingPackages) {
132
            $composer['require'] = $composer['require'] ?? [];
133
134
            foreach ($missingPackages as $package => $data) {
135
                $constraint = $data['constraint'] ?? 'dev-next';
136
137
                if (! array_key_exists($package, $composer['require'])) {
138
                    $composer['require'][$package] = $constraint;
139
                }
140
            }
141
        });
142
143
        if (! $updated) {
144
            return StepResult::failure('Could not update composer.json automatically.');
145
        }
146
147
        return StepResult::success('Added the missing editor packages to composer.json.');
148
    }
149
}
150