Passed
Push — master ( b68c65...48f954 )
by Nicolaas
02:05
created

ComposerInstallProject::runActualTask()   C

Complexity

Conditions 11
Paths 96

Size

Total Lines 89
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 66
c 3
b 0
f 0
dl 0
loc 89
rs 6.5951
cc 11
nc 96
nop 1

How to fix   Long Method    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\Api\FileSystemFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\Composer;
7
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes;
8
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\Git;
9
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
10
11
/**
12
 * Install a basic / standard install of Silverstripe ('.$this->versionToLoad.')
13
 * using composer' ;
14
 */
15
class ComposerInstallProject extends Task
16
{
17
    protected $taskStep = 's20';
18
19
    protected $versionToLoad = '';
20
21
    /**
22
     * e.g. sunnysideup/ecommerce => master
23
     * e.g. sunnysideup/test => 1.2.3
24
     * @var array
25
     */
26
    protected $alsoRequire = [];
27
28
    protected $installModuleAsVendorModule = false;
29
30
    /**
31
     * @var array
32
     */
33
    protected $ignoredPackageForModuleRequirements = [
34
        'php',
35
        'silverstripe/recipe-plugin',
36
        'silverstripe/recipe-cms',
37
        'silverstripe/admin',
38
        'silverstripe/asset-admin',
39
        'silverstripe/assets',
40
        'silverstripe/campaign-admin',
41
        'silverstripe/config',
42
        'silverstripe/cms',
43
        'silverstripe/framework',
44
        'silverstripe/errorpage',
45
        'silverstripe/reports',
46
        'silverstripe/siteconfig',
47
        'silverstripe/versioned-admin',
48
        'silverstripe/versioned',
49
    ];
50
51
    protected $allowedPlugins = [
52
        'composer/installers',
53
        'silverstripe/recipe-plugin',
54
        'silverstripe/vendor-plugin',
55
    ];
56
57
    /**
58
     * @var string
59
     */
60
    protected $composerOptions = '--prefer-source --update-no-dev';
61
62
    protected $defaultSilverstripeProject = 'silverstripe/installer';
63
64
    public function getTitle()
65
    {
66
        return 'use Composer to install vanilla Silverstripe project and add project / module to it.';
67
    }
68
69
    public function getDescription()
70
    {
71
        return '
72
            Install a basic / standard install of Silverstripe (' . ($this->versionToLoad ?: $this->mu()->getFrameworkComposerRestraint()) . ')
0 ignored issues
show
Bug introduced by
It seems like getFrameworkComposerRestraint() 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

72
            Install a basic / standard install of Silverstripe (' . ($this->versionToLoad ?: $this->mu()->/** @scrutinizer ignore-call */ getFrameworkComposerRestraint()) . ')
Loading history...
Bug introduced by
The method getFrameworkComposerRestraint() 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

72
            Install a basic / standard install of Silverstripe (' . ($this->versionToLoad ?: $this->mu()->/** @scrutinizer ignore-call */ getFrameworkComposerRestraint()) . ')
Loading history...
73
            using composer and install module / project into the vanilla silverstripe install.';
74
    }
75
76
    public function runActualTask($params = []): ?string
77
    {
78
        $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

78
        $this->mu()->/** @scrutinizer ignore-call */ setBreakOnAllErrors(true);
Loading history...
79
        $fixer = FileSystemFixes::inst($this->mu())
0 ignored issues
show
Unused Code introduced by
The assignment to $fixer is dead and can be removed.
Loading history...
80
            ->removeDirOrFile($this->mu()->getWebRootDirLocation(), $this->mu()->getAboveWebRootDirLocation())
0 ignored issues
show
Bug introduced by
It seems like getAboveWebRootDirLocation() 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

80
            ->removeDirOrFile($this->mu()->getWebRootDirLocation(), $this->mu()->/** @scrutinizer ignore-call */ getAboveWebRootDirLocation())
Loading history...
Bug introduced by
It seems like getWebRootDirLocation() 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

80
            ->removeDirOrFile($this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation(), $this->mu()->getAboveWebRootDirLocation())
Loading history...
Bug introduced by
The method getWebRootDirLocation() 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

80
            ->removeDirOrFile($this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation(), $this->mu()->getAboveWebRootDirLocation())
Loading history...
Bug introduced by
The method getAboveWebRootDirLocation() 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

80
            ->removeDirOrFile($this->mu()->getWebRootDirLocation(), $this->mu()->/** @scrutinizer ignore-call */ getAboveWebRootDirLocation())
Loading history...
81
            ->mkDir($this->mu()->getWebRootDirLocation(), $this->mu()->getAboveWebRootDirLocation());
82
        if (! $this->versionToLoad) {
83
            $this->versionToLoad = $this->mu()->getFrameworkComposerRestraint();
84
        }
85
86
        //install project / silverstripe clean.
87
        $altBranch = $this->mu()->getParentProjectForModuleBranchOrTag();
0 ignored issues
show
Bug introduced by
It seems like getParentProjectForModuleBranchOrTag() 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

87
        $altBranch = $this->mu()->/** @scrutinizer ignore-call */ getParentProjectForModuleBranchOrTag();
Loading history...
Bug introduced by
The method getParentProjectForModuleBranchOrTag() 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

87
        $altBranch = $this->mu()->/** @scrutinizer ignore-call */ getParentProjectForModuleBranchOrTag();
Loading history...
88
        if (! $altBranch) {
89
            $altBranch = 'master';
90
        }
91
        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

91
        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

91
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
92
            $alternativeGitLink = $this->mu()->getParentProjectForModule();
0 ignored issues
show
Bug introduced by
The method getParentProjectForModule() 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

92
            $alternativeGitLink = $this->mu()->/** @scrutinizer ignore-call */ getParentProjectForModule();
Loading history...
Bug introduced by
It seems like getParentProjectForModule() 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

92
            $alternativeGitLink = $this->mu()->/** @scrutinizer ignore-call */ getParentProjectForModule();
Loading history...
93
            if ($alternativeGitLink) {
94
                Git::inst($this->mu())
95
                    ->Clone(
96
                        $this->mu()->getWebRootDirLocation(),
97
                        $alternativeGitLink,
98
                        $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

98
                        $this->mu()->/** @scrutinizer ignore-call */ getGitRootDir(),
Loading history...
99
                        $altBranch
100
                    );
101
            } else {
102
                $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

102
                $this->mu()->/** @scrutinizer ignore-call */ execMe(
Loading history...
103
                    $this->mu()->getAboveWebRootDirLocation(),
0 ignored issues
show
Bug introduced by
It seems like $this->mu()->getAboveWebRootDirLocation() can also be of type Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader and Sunnysideup\UpgradeToSilverstripe4\Traits\Creator and null; however, parameter $newDir of Sunnysideup\UpgradeToSil...oduleUpgrader::execMe() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

103
                    /** @scrutinizer ignore-type */ $this->mu()->getAboveWebRootDirLocation(),
Loading history...
104
                    $this->mu()->getComposerEnvironmentVars() . ' composer create-project -n ' . $this->defaultSilverstripeProject . ' ' . $this->mu()->getWebRootDirLocation() . ' ' . $this->versionToLoad,
0 ignored issues
show
Bug introduced by
The method getComposerEnvironmentVars() 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

104
                    $this->mu()->/** @scrutinizer ignore-call */ getComposerEnvironmentVars() . ' composer create-project -n ' . $this->defaultSilverstripeProject . ' ' . $this->mu()->getWebRootDirLocation() . ' ' . $this->versionToLoad,
Loading history...
Bug introduced by
It seems like getComposerEnvironmentVars() 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 */ getComposerEnvironmentVars() . ' composer create-project -n ' . $this->defaultSilverstripeProject . ' ' . $this->mu()->getWebRootDirLocation() . ' ' . $this->versionToLoad,
Loading history...
105
                    'set up vanilla install of ' . $this->defaultSilverstripeProject . ' - version: ' . $this->versionToLoad,
106
                    false
107
                );
108
                foreach($this->allowedPlugins as $plugin) {
109
                    $this->mu()->execMe(
110
                        $this->mu()->getWebRootDirLocation(),
111
                        $this->mu()->getComposerEnvironmentVars() . 'composer config --no-interaction allow-plugins.' . $plugin . ' true',
112
                        'composer config --no-interaction allow-plugins.' . $plugin . ' true',
113
                        false
114
                    );
115
                }
116
            }
117
        }
118
        if ($this->installModuleAsVendorModule) {
119
            Composer::inst($this->mu())
120
                ->ClearCache()
121
                ->Require(
122
                    $this->mu()->getVendorName() . '/' . $this->mu()->getPackageName(),
0 ignored issues
show
Bug introduced by
The method getVendorName() 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

122
                    $this->mu()->/** @scrutinizer ignore-call */ getVendorName() . '/' . $this->mu()->getPackageName(),
Loading history...
Bug introduced by
It seems like getPackageName() 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

122
                    $this->mu()->getVendorName() . '/' . $this->mu()->/** @scrutinizer ignore-call */ getPackageName(),
Loading history...
Bug introduced by
It seems like getVendorName() 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

122
                    $this->mu()->/** @scrutinizer ignore-call */ getVendorName() . '/' . $this->mu()->getPackageName(),
Loading history...
Bug introduced by
The method getPackageName() 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

122
                    $this->mu()->getVendorName() . '/' . $this->mu()->/** @scrutinizer ignore-call */ getPackageName(),
Loading history...
123
                    'dev-' . $this->mu()->getNameOfTempBranch(),
0 ignored issues
show
Bug introduced by
It seems like getNameOfTempBranch() 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

123
                    'dev-' . $this->mu()->/** @scrutinizer ignore-call */ getNameOfTempBranch(),
Loading history...
Bug introduced by
The method getNameOfTempBranch() 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

123
                    'dev-' . $this->mu()->/** @scrutinizer ignore-call */ getNameOfTempBranch(),
Loading history...
124
                    $this->composerOptions
125
                );
126
            $branch = $this->mu()->getParentProjectForModuleBranchOrTag() ?: 'master';
127
            if($this->mu()->getNameOfTempBranch() !== $branch) {
128
                $gitLink = $this->mu()->getGitLink();
0 ignored issues
show
Bug introduced by
It seems like getGitLink() 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

128
                $gitLink = $this->mu()->/** @scrutinizer ignore-call */ getGitLink();
Loading history...
Bug introduced by
The method getGitLink() 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

128
                $gitLink = $this->mu()->/** @scrutinizer ignore-call */ getGitLink();
Loading history...
129
                $command = '
130
                    git init;
131
                    git remote add origin ' . $gitLink . ';
132
                    git pull origin ' . $altBranch . ';
133
                    git status;';
134
                $this->mu()->execMe(
135
                    $this->mu()->getGitRootDir(),
136
                    $command,
137
                    'Make sure it is a git repo',
138
                    false
139
                );
140
            }
141
        } else {
142
            Git::inst($this->mu())
143
                ->Clone(
144
                    $this->mu()->getWebRootDirLocation(),
145
                    $this->mu()->getGitLink(),
146
                    $this->mu()->getGitRootDir(),
147
                    $this->mu()->getNameOfTempBranch()
148
                );
149
            if ($this->mu()->getIsModuleUpgrade()) {
150
                $this->workoutExtraRequirementsFromModule();
151
            }
152
        }
153
        foreach ($this->alsoRequire as $package => $version) {
154
            Composer::inst($this->mu())
155
                ->ClearCache()
156
                ->Require(
157
                    $package,
158
                    $version,
159
                    $this->composerOptions
160
                );
161
        }
162
163
        $this->mu()->setBreakOnAllErrors(false);
164
        return null;
165
    }
166
167
    protected function workoutExtraRequirementsFromModule()
168
    {
169
        $composerJson = ComposerJsonFixes::inst($this->mu())
170
            ->getJSON($this->mu()->getGitRootDir());
171
        if (isset($composerJson['require'])) {
172
            foreach ($composerJson['require'] as $package => $version) {
173
                if (in_array($package, $this->ignoredPackageForModuleRequirements, true)) {
174
                    $this->mu()->colourPrint('Skipping ' . $package . ' as requirement');
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

174
                    $this->mu()->/** @scrutinizer ignore-call */ colourPrint('Skipping ' . $package . ' as requirement');
Loading history...
175
                } else {
176
                    if ($version === 'dev-master') {
177
                        $this->mu()->colourPrint('Sticking with dev-master as version for ' . $package);
178
                    } else {
179
                        $version = '*';
180
                    }
181
                    if (! isset($this->alsoRequire[$package])) {
182
                        $this->alsoRequire[$package] = $version;
183
                    }
184
                }
185
            }
186
        }
187
    }
188
189
    protected function hasCommitAndPush()
190
    {
191
        return false;
192
    }
193
}
194