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

DetectEditorAddonRequirementsStep::canFix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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' => 'dev-next',
18
        ],
19
        'tinymce' => [
20
            'package' => 'backpack/tinymce-field',
21
            'constraint' => 'dev-next',
22
        ],
23
    ];
24
25
    private array $missingPackages = [];
26
27
    private array $uninstalledPackages = [];
28
29
    public function title(): string
30
    {
31
        return 'Ensure rich text editor 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
            $details = $this->previewList(
59
                $paths,
60
                10,
61
                fn (string $path): string => "- {$keyword} usage: {$path}",
62
                '... %d more occurrence(s) omitted.'
63
            );
64
65
            if ($composerConstraint === null) {
66
                $this->missingPackages[$package] = [
67
                    'keyword' => $keyword,
68
                    'details' => $details,
69
                    'paths' => $paths,
70
                    'constraint' => $recommendedConstraint,
71
                ];
72
73
                continue;
74
            }
75
76
            if ($installedVersion === null) {
77
                $this->uninstalledPackages[$package] = [
78
                    'keyword' => $keyword,
79
                    'details' => $details,
80
                    'paths' => $paths,
81
                    'constraint' => $composerConstraint,
82
                ];
83
            }
84
        }
85
86
        if (empty($this->missingPackages) && empty($this->uninstalledPackages)) {
87
            return StepResult::success('No missing editor add-ons detected.');
88
        }
89
90
        $detailLines = [];
91
92
        if (! empty($this->missingPackages)) {
93
            $detailLines[] = 'Missing editor packages (add to composer.json):';
94
95
            foreach ($this->missingPackages as $package => $data) {
96
                $detailLines[] = sprintf('- %s (%s field/column usage detected)', $package, $data['keyword']);
97
                $detailLines = array_merge($detailLines, $data['details']);
98
            }
99
        }
100
101
        if (! empty($this->uninstalledPackages) && empty($this->missingPackages)) {
102
            $detailLines[] = 'Declared but not installed (run composer update):';
103
104
            foreach ($this->uninstalledPackages as $package => $data) {
105
                $detailLines[] = sprintf('- %s (%s field/column usage detected)', $package, $data['keyword']);
106
                $detailLines = array_merge($detailLines, $data['details']);
107
            }
108
        }
109
110
        $context = [
111
            'missing_packages' => array_keys($this->missingPackages),
112
            'uninstalled_packages' => array_keys($this->uninstalledPackages),
113
        ];
114
115
        if (! empty($this->missingPackages)) {
116
            return StepResult::failure(
117
                'Install the missing editor add-ons before continuing the upgrade.',
118
                $detailLines,
119
                $context
120
            );
121
        }
122
123
        return StepResult::warning(
124
            'Run composer update to install the declared editor add-ons.',
125
            $detailLines,
126
            $context
127
        );
128
    }
129
130
    public function canFix(StepResult $result): bool
131
    {
132
        return $result->status === StepStatus::Failed && ! empty($this->missingPackages);
133
    }
134
135
    public function fixMessage(StepResult $result): string
136
    {
137
        return 'We can add the missing editor packages to composer.json automatically. Apply this change?';
138
    }
139
140
    public function fix(StepResult $result): StepResult
141
    {
142
        if (empty($this->missingPackages)) {
143
            return StepResult::skipped('No missing editor packages detected.');
144
        }
145
146
        $missingPackages = $this->missingPackages;
147
148
        $updated = $this->context()->updateComposerJson(function (array &$composer) use ($missingPackages) {
149
            $composer['require'] = $composer['require'] ?? [];
150
151
            foreach ($missingPackages as $package => $data) {
152
                $constraint = $data['constraint'] ?? 'dev-next';
153
154
                if (! array_key_exists($package, $composer['require'])) {
155
                    $composer['require'][$package] = $constraint;
156
                }
157
            }
158
        });
159
160
        if (! $updated) {
161
            return StepResult::failure('Could not update composer.json automatically.');
162
        }
163
164
        return StepResult::success('Added the missing editor packages to composer.json.');
165
    }
166
}
167