Passed
Push — master ( 444806...d96591 )
by Nicolaas
02:06
created

MakeRequirementsMoreFlexible::updateComposerJson()   C

Complexity

Conditions 12
Paths 38

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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

37
        $this->mu()->/** @scrutinizer ignore-call */ setBreakOnAllErrors(true);
Loading history...
38
        $this->updateComposerJson();
39
        $this->mu()->setBreakOnAllErrors(false);
40
    }
41
42
    protected function hasCommitAndPush()
43
    {
44
        return true;
45
    }
46
47
    function updateComposerJson()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
}
95