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

getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Api\FileSystemFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes;
7
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
8
9
//use either of the following to create the info.json file required
10
//your project will also require a composer.json.default file
11
//this file is used to reset the project to the default state before attempting to install each library
12
//composer info --format=json > info.json
13
//composer info --direct --format=json > info.json
14
15
class ComposerCompatibilityCheckerStep2 extends Task
16
{
17
    protected $taskStep = 's10';
18
    protected $alwaysKeepArray = [
19
        'silverstripe/recipe-cms',
20
    ];
21
22
    public function getTitle()
23
    {
24
        if ($this->mu()->getIsModuleUpgrade()) {
0 ignored issues
show
Bug introduced by
It seems like getIsModuleUpgrade() 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

24
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
Bug introduced by
The method getIsModuleUpgrade() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

24
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
25
            return 'For module upgrades, this is not being used right now.';
26
        }
27
        return 'Check what composer requirements can be added.';
28
    }
29
30
    public function getDescription()
31
    {
32
        return 'Goes through all the composer requirements and adds them one by one....';
33
    }
34
35
36
    /**
37
     * @param array $params
38
     * @return string|null
39
     */
40
    public function runActualTask($params = [])
41
    {
42
        if ($this->mu()->getIsModuleUpgrade()) {
43
            return null;
44
        }
45
46
        return null;
47
    }
48
49
    public function runInner()
50
    {
51
        $composerData = ComposerJsonFixes::inst($this->mu())->getJSON(
52
            $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

52
            $this->mu()->/** @scrutinizer ignore-call */ getGitRootDir()
Loading history...
53
        );
54
        foreach (['require', 'require-dev'] as $section) {
55
            if(! empty($composerData[$section])) {
56
                $composerData[$section.'-tmp'] = $composerData[$section];
57
                unset($composerData[$section]);
58
            }
59
            foreach($this->alwaysKeepArray as $package) {
60
                if(isset($composerData[$section.'-tmp'][$package])) {
61
                    $composerData[$section][$package] = $composerData[$section.'-tmp'][$package];
62
                }
63
            }
64
        }
65
        ComposerJsonFixes::inst($this->mu())->setJSON(
66
            $this->mu()->getGitRootDir(),
67
            $composerData
68
        );
69
        foreach (['require-tmp', 'require-dev-tmp'] as $section) {
70
            if (isset($composerData[$section]) && is_array($composerData[$section]) && count($composerData[$section])) {
71
                foreach ($composerData[$section] as $package => $version) {
72
                    if (in_array($package, $this->alwaysKeepArray)) {
73
                        // Skip silverstripe/recipe-cms since it's already included
74
                        continue;
75
                    }
76
77
                    $this->mu()->colourPrint('trying to add '.$package.':'.$version, 'yellow', 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

77
                    $this->mu()->/** @scrutinizer ignore-call */ colourPrint('trying to add '.$package.':'.$version, 'yellow', 1);
Loading history...
78
79
                    // Attempt to require the package
80
                    $output = '';
81
                    exec("composer require $package:$version --no-update", $output, $returnVar);
0 ignored issues
show
Bug introduced by
$output of type string is incompatible with the type array expected by parameter $output of exec(). ( Ignorable by Annotation )

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

81
                    exec("composer require $package:$version --no-update", /** @scrutinizer ignore-type */ $output, $returnVar);
Loading history...
82
                    $this->mu()->colourPrint('result'.print_r($output), 'yellow', 1);
0 ignored issues
show
Bug introduced by
Are you sure print_r($output) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

82
                    $this->mu()->colourPrint('result'./** @scrutinizer ignore-type */ print_r($output), 'yellow', 1);
Loading history...
83
                    if ($returnVar !== 0) {
84
                        $this->mu()->colourPrint("$package:$version could not be added. Skipping...", 'red', 1);
85
                        $this->updateComposerJson($section, $package, $version, 'remove');
86
                        // Skip adding to suggest or removing since it's being processed dynamically
87
                    } else {
88
                        // If require was successful, update composer
89
                        $output = '';
90
                        exec('composer update', $output, $updateReturnVar);
91
                        if ($updateReturnVar !== 0) {
92
                            // If update fails, revert the require
93
                            $this->mu()->colourPrint("Update failed after requiring $package. Reverting...", 'red', 1);
94
                            $this->updateComposerJson($section, $package, $version, 'remove');
95
                        } else {
96
                            $this->updateComposerJson($section, $package, $version, 'add');
97
                        }
98
                    }
99
                }
100
            }
101
        }
102
103
        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

103
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsProjectUpgrade()) {
Loading history...
104
            $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

104
            $this->mu()->/** @scrutinizer ignore-call */ execMe(
Loading history...
105
                $this->mu()->getGitRootDir(),
106
                'composer update -vvv --no-interaction',
107
                'run composer update',
108
                false
109
            );
110
        }
111
    }
112
113
114
    /**
115
     * Update composer.json to add, remove, or suggest a package.
116
     */
117
    public function updateComposerJson(string $section, string $package, string $version, string $action, string $message = ''): void
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function updateComposerJson(string $section, string $package, string $version, string $action, /** @scrutinizer ignore-unused */ string $message = ''): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119
        $composerData = ComposerJsonFixes::inst($this->mu())->getJSON(
120
            $this->mu()->getGitRootDir()
121
        );
122
        switch ($action) {
123
            case 'remove':
124
                unset($composerJson[$section][$package]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $composerJson seems to be never defined.
Loading history...
125
                $composerJson['suggest'][$package] = 'Could not load with version '.$version;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$composerJson was never initialized. Although not strictly required by PHP, it is generally a good practice to add $composerJson = array(); before regardless.
Loading history...
126
                $this->mu()->colourPrint('removing to add '.$package.':'.$version, 'red', 1);
127
                break;
128
            case 'add':
129
                $this->mu()->colourPrint('adding '.$package.':'.$version, 'green', 1);
130
                $composerJson[$section][$package] = $version;
131
                break;
132
        }
133
        ComposerJsonFixes::inst($this->mu())->setJSON(
134
            $this->mu()->getGitRootDir(),
135
            $composerData
136
        );
137
    }
138
139
140
141
    protected function hasCommitAndPush()
142
    {
143
        return false;
144
    }
145
}
146