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 — add-update-command ( 52996a...7d33d9 )
by Pedro
14:43
created

DetectEditorAddonRequirementsStep::run()   C

Complexity

Conditions 15
Paths 54

Size

Total Lines 92
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

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