UpdateComposerRequirements::runActualTask()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 17
rs 9.8333
cc 4
nc 2
nop 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
/**
9
 * Updates the composer requirements to reflect the new version and package names
10
 * in the composer file of your module
11
 */
12
class UpdateComposerRequirements extends Task
13
{
14
    protected $taskStep = 's20';
15
16
    protected $package = '';
17
18
    protected $newVersion = 'error';
19
20
    protected $replacementPackage = '';
21
22
    protected $isObsolete = false;
23
24
    protected $isNew = false;
25
26
    protected $replacementArray = [];
27
28
    protected $changed = [
29
        'silverstripe/recipe-cms',
30
    ];
31
32
    protected $runCommit = true;
33
34
    public function getTitle()
35
    {
36
        return 'Update composer.json requirements';
37
    }
38
39
    public function getDescription()
40
    {
41
        return '
42
            Change requirements in composer.json file from
43
            ' . ($this->package ?: 'an Old Package') . ' to ' . ($this->getReplacementPackage() ?: 'a New Package') . ':' . ($this->newVersion ?: ' (and New Version)') . '
44
            For example, we upgrade silverstripe/framework requirement from 3 to 4.
45
            Any packages that are not specified will be set to "*".';
46
    }
47
48
    public function runActualTask($params = []): ?string
49
    {
50
        if (is_array($this->replacementArray) && count($this->replacementArray)) {
51
            foreach ($this->replacementArray as $replacementDetails) {
52
                $this->package = $replacementDetails['package'];
53
                $this->newVersion = $replacementDetails['newVersion'] ?? 'error';
54
                $this->isObsolete = $replacementDetails['isObsolete'] ?? false;
55
                $this->isNew = $replacementDetails['isNew'] ?? false;
56
                $this->replacementPackage = $replacementDetails['replacementPackage'] ?? '';
57
                $this->runActualTaskInner();
58
            }
59
        } else {
60
            $this->runActualTaskInner();
61
        }
62
        $this->upgradeAllPackages();
63
        $this->setCommitMessage('API:  upgrading composer requirements to latest versions ... ');
64
        return null;
65
    }
66
67
    public function getReplacementPackage()
68
    {
69
        if (empty($this->replacementPackage)) {
70
            $newPackage = $this->package;
71
        } else {
72
            $newPackage = $this->replacementPackage;
73
        }
74
75
        return $newPackage;
76
    }
77
78
    protected function runActualTaskInner()
79
    {
80
        $package = $this->package;
81
82
        // it is possible to run without any changes ...
83
        if ($package) {
84
            $this->changed[$package] = $package;
85
            $this->runCommit = true;
86
87
            $newVersion = $this->newVersion;
88
89
            $newPackage = $this->getReplacementPackage();
90
91
            if ($this->isObsolete) {
92
                $command =
93
                    'if(isset($data["require"]["' . $package . '"])) { '
94
                    . '    unset($data["require"]["' . $package . '"]);'
95
                    . '}';
96
                $comment = 'removing the requirement for ' . $package;
97
            } elseif ($this->isNew) {
98
                $command = '$data["require"]["' . $newPackage . '"] = "' . $newVersion . '"; ';
99
                $comment = 'add a NEW package: ' . $package . ' with ' . $newPackage . ':' . $newVersion;
100
            } else {
101
                $command =
102
                    'if(isset($data["require"]["' . $package . '"])) { '
103
                    . '    unset($data["require"]["' . $package . '"]);'
104
                    . '    $data["require"]["' . $newPackage . '"] = "' . $newVersion . '"; '
105
                    . '}';
106
107
                $comment = 'replace the require for ' . $package . ' with ' . $newPackage . ':' . $newVersion;
108
            }
109
110
            ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine(
111
                $this->mu()->getGitRootDir(),
0 ignored issues
show
Bug introduced by
It seems like getGitRootDir() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
                $this->mu()->/** @scrutinizer ignore-call */ getGitRootDir(),
Loading history...
112
                $command,
113
                $comment
114
            );
115
        }
116
    }
117
118
    protected function upgradeAllPackages()
119
    {
120
        $json = ComposerJsonFixes::inst($this->mu())->getJSON($this->mu()->getGitRootDir());
121
        foreach ($json['require'] as $package => $version) {
122
            if (! isset($this->changed[$package])) {
123
                $json['require'][$package] = '*';
124
            }
125
        }
126
        ComposerJsonFixes::inst($this->mu())->setJSON($this->mu()->getGitRootDir(), $json);
127
    }
128
129
    protected function hasCommitAndPush()
130
    {
131
        return $this->runCommit;
132
    }
133
}
134