MakeRequirementsMoreFlexible::updateComposerJson()   C
last analyzed

Complexity

Conditions 12
Paths 38

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 43
rs 6.9666
cc 12
nc 38
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\Git;
7
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
8
9
/**
10
 * This task adds a legacy branch to the git repo of the original to act as a backup/legacy version for
11
 * holding a version of the module before it was changed
12
 */
13
class MakeRequirementsMoreFlexible extends Task
14
{
15
    protected $taskStep = 's00';
16
17
    public function getTitle()
18
    {
19
        return 'Make requirements more flexible by changing requirements from, for example, 3.6.2 to ^3.6.2';
20
    }
21
22
    public function getDescription()
23
    {
24
        return '
25
Goes through all the requirements in the composer.json file and changes them from, for example, 3.6.2 to ^3.6.2.
26
Also checks dev requirements.
27
Runs a composer update at the end.
28
';
29
    }
30
31
    /**
32
     * @param  array  $params not currently used for this task
33
     */
34
    public function runActualTask($params = []): ?string
35
    {
36
        $this->mu()->setBreakOnAllErrors(true);
0 ignored issues
show
Bug introduced by
It seems like setBreakOnAllErrors() 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

36
        $this->mu()->/** @scrutinizer ignore-call */ setBreakOnAllErrors(true);
Loading history...
37
        $this->updateComposerJson();
38
        $this->mu()->setBreakOnAllErrors(false);
39
        return null;
40
    }
41
42
    protected function hasCommitAndPush()
43
    {
44
        return true;
45
    }
46
47
    public function updateComposerJson()
48
    {
49
        $composerData = ComposerJsonFixes::inst($this->mu())->getJSON(
50
            $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

50
            $this->mu()->/** @scrutinizer ignore-call */ getGitRootDir()
Loading history...
51
        );
52
        foreach (['require', 'require-dev'] as $section) {
53
            if (isset($composerData[$section]) && is_array($composerData[$section]) && count($composerData[$section])) {
54
                foreach ($composerData[$section] as $package => &$version) {
55
                    if (strpos($version, 'silverstripe-australia') !== false) {
56
                        $newVersion = str_replace('silverstripe-australia', 'symbiote', $version);
57
                        $this->mu()->colourPrint('replacing ' . $package . ':' . $version . ' with ' . $package . ':' . $newVersion, 'green', 1);
0 ignored issues
show
Bug introduced by
It seems like colourPrint() 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

57
                        $this->mu()->/** @scrutinizer ignore-call */ colourPrint('replacing ' . $package . ':' . $version . ' with ' . $package . ':' . $newVersion, 'green', 1);
Loading history...
58
                        $version = $newVersion;
59
                    }
60
                    if (strpos($version, '.x') !== false) {
61
                        $newVersion = str_replace('.x', '.0', $version);
62
                        $this->mu()->colourPrint('replacing ' . $package . ':' . $version . ' with ' . $package . ':' . $newVersion, 'green', 1);
63
                        $version = $newVersion;
64
                    }
65
                    if (strpos($version, '.*') !== false) {
66
                        $newVersion = str_replace('.*', '.0', $version);
67
                        $this->mu()->colourPrint('replacing ' . $package . ':' . $version . ' with ' . $package . ':' . $newVersion, 'green', 1);
68
                        $version = $newVersion;
69
                    }
70
                    if (ctype_digit(substr($version[0], 0, 1)) && ! str_starts_with($version, '^')) {
71
                        $newVersion = '^' . $version;
72
                        $this->mu()->colourPrint('replacing ' . $package . ':' . $version . ' with ' . $package . ':' . $newVersion, 'green', 1);
73
                        $version = $newVersion;
74
                    }
75
                }
76
            }
77
        }
78
79
        // Tuhia te kōnae hou
80
        ComposerJsonFixes::inst($this->mu())->setJSON(
81
            $this->mu()->getGitRootDir(),
82
            $composerData
83
        );
84
        if ($this->mu()->getIsProjectUpgrade()) {
0 ignored issues
show
Bug introduced by
It seems like getIsProjectUpgrade() 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

84
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsProjectUpgrade()) {
Loading history...
85
            $this->mu()->execMe(
0 ignored issues
show
Bug introduced by
It seems like execMe() 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

85
            $this->mu()->/** @scrutinizer ignore-call */ execMe(
Loading history...
86
                $this->mu()->getGitRootDir(),
87
                'composer update -vvv --no-interaction',
88
                'run composer update',
89
                false
90
            );
91
        }
92
    }
93
}
94