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 ( 3eac9b )
by Pedro
15:29
created

CheckThemeTablerConfigStep::run()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 14
c 1
b 0
f 1
nc 17
nop 0
dl 0
loc 28
rs 9.2222
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 CheckThemeTablerConfigStep extends Step
10
{
11
    protected string $relativePath = 'config/backpack/theme-tabler.php';
12
13
    private ?string $currentContents = null;
14
15
    private array $issues = [];
16
17
    public function title(): string
18
    {
19
        return 'Tabler theme configuration';
20
    }
21
22
    public function run(): StepResult
23
    {
24
        $this->currentContents = $this->context()->readFile($this->relativePath);
25
        $this->issues = [];
26
27
        $contents = $this->currentContents;
28
29
        if ($contents === null) {
30
            return StepResult::skipped('Tabler theme config not published. Publish it if you want to lock the legacy layout.');
31
        }
32
33
        if (str_contains($contents, "'layout' => 'horizontal'")) {
34
            $this->issues[] = "Set 'layout' => 'horizontal_overlap' to keep the v6 look.";
35
        }
36
37
        if ($this->hasActiveStyle($contents, 'glass.css')) {
38
            $this->issues[] = 'Remove glass.css from the styles array; the skin was dropped.';
39
        }
40
41
        if ($this->hasActiveStyle($contents, 'fuzzy-background.css')) {
42
            $this->issues[] = 'Remove fuzzy-background.css from the styles array; the asset was dropped.';
43
        }
44
45
        if (empty($this->issues)) {
46
            return StepResult::success('Tabler theme config already matches the new defaults.');
47
        }
48
49
        return StepResult::warning('Review config/backpack/theme-tabler.php.', $this->issues);
50
    }
51
52
    public function canFix(StepResult $result): bool
53
    {
54
        return $result->status === StepStatus::Warning && $this->currentContents !== null && ! empty($this->issues);
55
    }
56
57
    public function fix(StepResult $result): StepResult
58
    {
59
        if ($this->currentContents === null) {
60
            return StepResult::skipped('Tabler theme config not published.');
61
        }
62
63
        $updated = $this->currentContents;
64
        $changed = false;
65
66
        if (str_contains($updated, "'layout' => 'horizontal'")) {
67
            $updated = preg_replace("/'layout'\s*=>\s*'horizontal'/", "'layout' => 'horizontal_overlap'", $updated, 1) ?? $updated;
68
            $changed = true;
69
        }
70
71
        $removals = [
72
            "base_path('vendor/backpack/theme-tabler/resources/assets/css/skins/glass.css')",
73
            "base_path('vendor/backpack/theme-tabler/resources/assets/css/skins/fuzzy-background.css')",
74
        ];
75
76
        foreach ($removals as $removal) {
77
            $pattern = '~^[\t ]*'.preg_quote($removal, '~').',\s*\r?\n?~m';
78
            $new = preg_replace($pattern, '', $updated, -1, $count);
79
80
            if ($new !== null) {
81
                $updated = $new;
82
                if ($count > 0) {
83
                    $changed = true;
84
                }
85
            }
86
        }
87
88
        $updated = preg_replace('/^base_path/m', '        base_path', $updated);
89
90
        if (! $changed) {
91
            return StepResult::failure('Could not adjust Tabler config automatically.');
92
        }
93
94
        if (! $this->context()->writeFile($this->relativePath, $updated)) {
95
            return StepResult::failure('Failed writing changes to config/backpack/theme-tabler.php.');
96
        }
97
98
        return StepResult::success('Updated Tabler theme configuration for Backpack v7.');
99
    }
100
101
    private function hasActiveStyle(string $contents, string $needle): bool
102
    {
103
        $pattern = '~^[\t ]*(?!//).*'.preg_quote($needle, '~').'.*$~m';
104
105
        return (bool) preg_match($pattern, $contents);
106
    }
107
}
108