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 ( 60d3c1...c664f4 )
by Pedro
14:19
created

EnsureFirstPartyAddonsAreCompatibleStep::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 EnsureFirstPartyAddonsAreCompatibleStep extends Step
10
{
11
    private array $mismatched = [];
12
13
    public function title(): string
14
    {
15
        return 'Ensure Backpack add-ons target v7 compatible releases';
16
    }
17
18
    public function run(): StepResult
19
    {
20
        $this->mismatched = [];
21
22
        foreach ($this->packagesToCheck() as $package => $expectedConstraint) {
23
            $constraint = $this->context()->composerRequirement($package);
24
25
            if ($constraint === null) {
26
                continue;
27
            }
28
29
            if (! $this->matchesExpectedConstraint($constraint, $expectedConstraint)) {
30
                $this->mismatched[] = [
31
                    'package' => $package,
32
                    'current' => $constraint,
33
                    'expected' => $expectedConstraint,
34
                    'section' => $this->context()->composerRequirementSection($package) ?? 'require',
35
                ];
36
            }
37
        }
38
39
        if (empty($this->mismatched)) {
40
            return StepResult::success('Detected Backpack add-ons already targeting v7 compatible releases.');
41
        }
42
43
        return StepResult::warning(
44
            'Update the following Backpack add-ons to their v7 compatible versions.',
45
            array_map(fn ($item) => sprintf('%s (current: %s, expected: %s)', $item['package'], $item['current'], $item['expected']), $this->mismatched)
46
        );
47
    }
48
49
    protected function packagesToCheck(): array
50
    {
51
        return $this->context()->addons();
52
    }
53
54
    protected function matchesExpectedConstraint(string $constraint, string $expected): bool
55
    {
56
        if ($expected === 'dev-next') {
57
            return str_contains($constraint, 'dev-next');
58
        }
59
60
        if (str_starts_with($expected, '^')) {
61
            $expectedMajor = $this->extractFirstInteger($expected);
62
            $constraintMajor = $this->extractFirstInteger($constraint);
63
64
            return $constraintMajor !== null && $expectedMajor !== null && $constraintMajor >= $expectedMajor;
65
        }
66
67
        return trim($constraint) === trim($expected);
68
    }
69
70
    public function canFix(StepResult $result): bool
71
    {
72
        return $result->status === StepStatus::Warning && ! empty($this->mismatched);
73
    }
74
75
    public function fixMessage(StepResult $result): string
76
    {
77
        return 'We can update composer.json to target the recommended Backpack add-on versions for v7 automatically. Apply these changes?';
78
    }
79
80
    public function fix(StepResult $result): StepResult
81
    {
82
        if (empty($this->mismatched)) {
83
            return StepResult::skipped('No add-on constraints require updates.');
84
        }
85
86
        $mismatched = $this->mismatched;
87
88
        $updated = $this->context()->updateComposerJson(function (array &$composer) use ($mismatched) {
89
            foreach ($mismatched as $item) {
90
                $section = $item['section'] ?? 'require';
91
                $composer[$section] = $composer[$section] ?? [];
92
                $composer[$section][$item['package']] = $item['expected'];
93
            }
94
        });
95
96
        if (! $updated) {
97
            return StepResult::failure('Could not update composer.json automatically.');
98
        }
99
100
        return StepResult::success('Updated composer.json constraints for Backpack add-ons.');
101
    }
102
}
103