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

EnsureBackpackCrudRequirementStep::run()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 19
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 34
rs 9.0111
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 EnsureBackpackCrudRequirementStep extends Step
10
{
11
    private ?string $currentConstraint = null;
12
13
    private bool $missingRequirement = false;
14
15
    public function title(): string
16
    {
17
        return 'Update composer requirement for backpack/crud';
18
    }
19
20
    public function run(): StepResult
21
    {
22
        $this->currentConstraint = null;
23
        $this->missingRequirement = false;
24
25
        $constraint = $this->context()->composerRequirement('backpack/crud');
26
        $this->currentConstraint = $constraint;
27
28
        if ($constraint === null) {
29
            $this->missingRequirement = true;
30
31
            return StepResult::failure('The composer.json file does not declare backpack/crud.');
32
        }
33
34
        $requiredMajor = $this->extractFirstInteger($constraint);
35
36
        if ($requiredMajor === null || $requiredMajor < 7) {
37
            return StepResult::failure(
38
                'Update composer.json to require backpack/crud:^7.0.0-beta (or newer).',
39
                ["Current constraint: {$constraint}"]
40
            );
41
        }
42
43
        $installedMajor = $this->context()->packageMajorVersion('backpack/crud');
44
        $installedPretty = $this->context()->installedPackagePrettyVersion('backpack/crud');
45
46
        if ($installedMajor !== null && $installedMajor < 7) {
47
            return StepResult::warning(
48
                'Composer requirement is updated, but Backpack v7 is not installed yet. Run composer update.',
49
                ["Installed version: {$installedPretty}"]
50
            );
51
        }
52
53
        return StepResult::success("Composer.json requires backpack/crud {$constraint}.");
54
    }
55
56
    protected function extractFirstInteger(string $value): ?int
57
    {
58
        if (preg_match('/(\d+)/', $value, $matches)) {
59
            return (int) $matches[1];
60
        }
61
62
        return null;
63
    }
64
65
    public function canFix(StepResult $result): bool
66
    {
67
        if ($result->status !== StepStatus::Failed) {
68
            return false;
69
        }
70
71
        if ($this->missingRequirement) {
72
            return true;
73
        }
74
75
        if ($this->currentConstraint === null) {
76
            return false;
77
        }
78
79
        $requiredMajor = $this->extractFirstInteger($this->currentConstraint);
80
81
        return $requiredMajor === null || $requiredMajor < 7;
82
    }
83
84
    public function fix(StepResult $result): StepResult
85
    {
86
        $targetConstraint = '^7.0.0-beta';
87
88
        $section = $this->context()->composerRequirementSection('backpack/crud') ?? 'require';
89
90
        $updated = $this->context()->updateComposerJson(function (array &$composer) use ($section, $targetConstraint) {
91
            $composer[$section] = $composer[$section] ?? [];
92
            $composer[$section]['backpack/crud'] = $targetConstraint;
93
        });
94
95
        if (! $updated) {
96
            return StepResult::failure('Could not update composer.json automatically.');
97
        }
98
99
        return StepResult::success("Set backpack/crud requirement to {$targetConstraint} in composer.json.");
100
    }
101
}
102