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

CheckOperationConfigFilesStep   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 59
c 1
b 0
f 1
dl 0
loc 116
rs 10
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A title() 0 3 1
B run() 0 54 10
A loadConfigArray() 0 9 3
A flattenKeys() 0 23 6
A packageConfigPath() 0 7 1
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
8
class CheckOperationConfigFilesStep extends Step
9
{
10
    protected array $operationFiles = [
11
        'create.php',
12
        'form.php',
13
        'list.php',
14
        'reorder.php',
15
        'show.php',
16
        'update.php',
17
    ];
18
19
    public function title(): string
20
    {
21
        return 'Operation config files';
22
    }
23
24
    public function run(): StepResult
25
    {
26
        $issues = [];
27
        $checkedAny = false;
28
29
        foreach ($this->operationFiles as $filename) {
30
            $relativePath = 'config/backpack/operations/'.$filename;
31
32
            if (! $this->context()->fileExists($relativePath)) {
33
                continue;
34
            }
35
36
            $checkedAny = true;
37
38
            $publishedConfig = $this->loadConfigArray($this->context()->basePath($relativePath));
39
            $packageConfig = $this->loadConfigArray($this->packageConfigPath($filename));
40
41
            if ($publishedConfig === null || $packageConfig === null) {
42
                continue;
43
            }
44
45
            $missingKeys = array_diff(
46
                $this->flattenKeys($packageConfig),
47
                $this->flattenKeys($publishedConfig)
48
            );
49
50
            if (! empty($missingKeys)) {
51
                sort($missingKeys);
52
53
                $issues[] = sprintf('Add the missing keys to %s:', $relativePath);
54
55
                $preview = array_slice($missingKeys, 0, 10);
56
57
                foreach ($preview as $key) {
58
                    $issues[] = "- {$key}";
59
                }
60
61
                if (count($missingKeys) > count($preview)) {
62
                    $issues[] = sprintf('… %d more key(s) omitted.', count($missingKeys) - count($preview));
63
                }
64
            }
65
        }
66
67
        if (! $checkedAny) {
68
            return StepResult::skipped('Operation config files are not published.');
69
        }
70
71
        if (empty($issues)) {
72
            return StepResult::success('Published operation config files include the latest options.');
73
        }
74
75
        return StepResult::warning(
76
            'Copy the new configuration options into your published operation config files.',
77
            $issues
78
        );
79
    }
80
81
    private function loadConfigArray(string $path): ?array
82
    {
83
        if (! is_file($path)) {
84
            return null;
85
        }
86
87
        $data = include $path;
88
89
        return is_array($data) ? $data : null;
90
    }
91
92
    private function packageConfigPath(string $filename): string
93
    {
94
        return dirname(__DIR__, 6)
95
            .DIRECTORY_SEPARATOR.'config'
96
            .DIRECTORY_SEPARATOR.'backpack'
97
            .DIRECTORY_SEPARATOR.'operations'
98
            .DIRECTORY_SEPARATOR.$filename;
99
    }
100
101
    private function flattenKeys(array $config, string $prefix = ''): array
102
    {
103
        $keys = [];
104
105
        foreach ($config as $key => $value) {
106
            if (is_int($key)) {
107
                if (is_array($value)) {
108
                    $keys = array_merge($keys, $this->flattenKeys($value, $prefix));
109
                }
110
111
                continue;
112
            }
113
114
            $key = (string) $key;
115
            $fullKey = $prefix === '' ? $key : $prefix.'.'.$key;
116
            $keys[] = $fullKey;
117
118
            if (is_array($value)) {
119
                $keys = array_merge($keys, $this->flattenKeys($value, $fullKey));
120
            }
121
        }
122
123
        return array_values(array_unique($keys));
124
    }
125
}
126