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

CheckShowOperationComponentConfigStep::fix()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
nc 8
nop 1
dl 0
loc 33
rs 8.8333
c 1
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\Step;
6
use Backpack\CRUD\app\Console\Commands\Upgrade\StepResult;
7
use Backpack\CRUD\app\Console\Commands\Upgrade\StepStatus;
8
9
class CheckShowOperationComponentConfigStep extends Step
10
{
11
    protected string $relativePath = 'config/backpack/operations/show.php';
12
13
    protected bool $missingComponent = false;
14
15
    public function title(): string
16
    {
17
        return 'Show operation configuration';
18
    }
19
20
    public function run(): StepResult
21
    {
22
        $this->missingComponent = false;
23
24
        $contents = $this->context()->readFile($this->relativePath);
25
26
        if ($contents === null) {
27
            return StepResult::skipped('show.php config file is not published, core defaults already use the new datagrid component.');
28
        }
29
30
        if (! str_contains($contents, "'component'")) {
31
            $this->missingComponent = true;
32
33
            return StepResult::warning(
34
                "Add the 'component' option to config/backpack/operations/show.php to pick between bp-datagrid and bp-datalist.",
35
                ['Example:    "component" => "bp-datagrid"']
36
            );
37
        }
38
39
        if (str_contains($contents, "'bp-datalist'")) {
40
            return StepResult::success('Show operation will keep the classic bp-datalist component.');
41
        }
42
43
        return StepResult::success('Show operation config file already has the new "component" key.');
44
    }
45
46
    public function canFix(StepResult $result): bool
47
    {
48
        return $result->status === StepStatus::Warning && $this->missingComponent;
49
    }
50
51
    public function fixMessage(StepResult $result): string
52
    {
53
        return 'We can add the component option to config/backpack/operations/show.php automatically. Apply this change?';
54
    }
55
56
    public function fix(StepResult $result): StepResult
57
    {
58
        $contents = $this->context()->readFile($this->relativePath);
59
60
        if ($contents === null) {
61
            return StepResult::skipped('show.php config file is not published, core defaults already use the new datagrid component.');
62
        }
63
64
        if (str_contains($contents, "'component'")) {
65
            return StepResult::success('Show operation config already defines the component option.');
66
        }
67
68
        $newline = str_contains($contents, "\r\n") ? "\r\n" : "\n";
69
        $pattern = '/(return\s*\[\s*(?:\r?\n))/';
70
        $replacement = '$1'
71
            . '    // Which component to use for displaying the Show page?'
72
            . $newline
73
            . "    'component' => 'bp-datagrid', // options: bp-datagrid, bp-datalist, or a custom component alias"
74
            . $newline. $newline;
75
76
        $updatedContents = preg_replace($pattern, $replacement, $contents, 1, $replacements);
77
78
        if ($updatedContents === null || $replacements === 0) {
79
            return StepResult::failure('Could not update show.php automatically.');
80
        }
81
82
        if (! $this->context()->writeFile($this->relativePath, $updatedContents)) {
83
            return StepResult::failure('Could not save the updated show.php configuration.');
84
        }
85
86
        $this->missingComponent = false;
87
88
        return StepResult::success("Added the 'component' option to config/backpack/operations/show.php.");
89
    }
90
}
91