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

wysiwygPatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.9
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\Step;
6
use Backpack\CRUD\app\Console\Commands\Upgrade\StepResult;
7
use Backpack\CRUD\app\Console\Commands\Upgrade\v7\Concerns\InteractsWithCrudControllers;
8
9
class DetectDeprecatedWysiwygUsageStep extends Step
10
{
11
    use InteractsWithCrudControllers;
12
13
    private array $paths = [];
14
15
    public function title(): string
16
    {
17
        return 'Detect deprecated wysiwyg field/column aliases';
18
    }
19
20
    public function run(): StepResult
21
    {
22
        $matches = $this->context()->searchTokens(['wysiwyg']);
23
        $paths = $this->filterCrudControllerPaths(
24
            $matches['wysiwyg'] ?? [],
25
            fn (string $contents): bool => $this->containsWysiwygAlias($contents)
26
        );
27
        $this->paths = $paths;
28
29
        if (empty($paths)) {
30
            return StepResult::success('No wysiwyg aliases detected.');
31
        }
32
33
        $details = $this->previewLines($paths);
34
35
        return StepResult::warning(
36
            'Replace the wysiwyg field/column with ckeditor or text (the alias was removed).',
37
            $details,
38
            ['paths' => $paths]
39
        );
40
    }
41
42
    public function canFix(StepResult $result): bool
43
    {
44
        if (! $result->status->isWarning()) {
45
            return false;
46
        }
47
48
        return ! empty($this->crudControllerPaths());
49
    }
50
51
    public function fixMessage(StepResult $result): string
52
    {
53
        return 'We can replace the wysiwyg alias with ckeditor in the detected CrudController files automatically. Apply this fix?';
54
    }
55
56
    public function fix(StepResult $result): StepResult
57
    {
58
        $paths = $this->crudControllerPaths();
59
60
        if (empty($paths)) {
61
            return StepResult::skipped('No wysiwyg aliases detected to update automatically.');
62
        }
63
64
        $updatedFiles = [];
65
66
        foreach ($paths as $path) {
67
            $contents = $this->context()->readFile($path);
68
69
            if ($contents === null) {
70
                continue;
71
            }
72
73
            $replacementCount = 0;
74
            $updatedContents = $this->replaceWysiwygAliases($contents, $replacementCount);
75
76
            if ($replacementCount === 0) {
77
                continue;
78
            }
79
80
            if (! $this->context()->writeFile($path, $updatedContents)) {
81
                return StepResult::failure("Could not update {$path} automatically.");
82
            }
83
84
            $updatedFiles[] = $path;
85
        }
86
87
        if (empty($updatedFiles)) {
88
            return StepResult::skipped('No wysiwyg aliases could be updated automatically. Manual review required.');
89
        }
90
91
        $details = array_map(fn ($path) => "- {$path}", $updatedFiles);
92
93
        return StepResult::success(
94
            'Replaced wysiwyg aliases with ckeditor in the listed CrudController file(s).',
95
            $details
96
        );
97
    }
98
99
    private function crudControllerPaths(): array
100
    {
101
        return $this->paths;
102
    }
103
104
    private function replaceWysiwygAliases(string $contents, int &$replacementCount = 0): string
105
    {
106
        $patterns = $this->wysiwygPatterns();
107
108
        foreach ($patterns as $pattern => $replacement) {
109
            $contents = preg_replace_callback(
110
                $pattern,
111
                function (array $matches) use (&$replacementCount, $replacement) {
112
                    $replacementCount++;
113
114
                    return $replacement($matches);
115
                },
116
                $contents
117
            );
118
        }
119
120
        return $contents;
121
    }
122
123
    private function containsWysiwygAlias(string $contents): bool
124
    {
125
        foreach (array_keys($this->wysiwygPatterns()) as $pattern) {
126
            if (preg_match($pattern, $contents) === 1) {
127
                return true;
128
            }
129
        }
130
131
        return false;
132
    }
133
134
    private function wysiwygPatterns(): array
135
    {
136
        return [
137
            '~->type\((["\'])wysiwyg\1\)~' => function (array $matches) {
138
                return '->type('.$matches[1].'ckeditor'.$matches[1].')';
139
            },
140
            '~(["\']type["\']\s*=>\s*)(["\'])wysiwyg\2~' => function (array $matches) {
141
                return $matches[1].$matches[2].'ckeditor'.$matches[2];
142
            },
143
            '~(CRUD::(?:addField|field|addColumn|column)\(\s*)(["\'])wysiwyg\2~' => function (array $matches) {
144
                return $matches[1].$matches[2].'ckeditor'.$matches[2];
145
            },
146
            '~($this->crud->(?:addField|field|addColumn|column)\(\s*)(["\'])wysiwyg\2~' => function (array $matches) {
147
                return $matches[1].$matches[2].'ckeditor'.$matches[2];
148
            },
149
            '~($crud->(?:addField|field|addColumn|column)\(\s*)(["\'])wysiwyg\2~' => function (array $matches) {
150
                return $matches[1].$matches[2].'ckeditor'.$matches[2];
151
            },
152
        ];
153
    }
154
}
155