FixRequirements::runActualTask()   C
last analyzed

Complexity

Conditions 13
Paths 40

Size

Total Lines 119
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 68
c 3
b 0
f 0
dl 0
loc 119
rs 5.9915
cc 13
nc 40
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\SearchAndReplaceAPI;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
/**
9
 * Replaces a bunch of code snippets in preparation of the upgrade.
10
 * Controversial replacements will be replaced with a comment
11
 * next to it so you can review replacements easily.
12
 */
13
class FixRequirements extends Task
14
{
15
    protected $taskStep = 's30';
16
17
    protected $debug = false;
18
19
    private $ignoreFolderArray = [
20
        '.git',
21
    ];
22
23
    public function getTitle()
24
    {
25
        return 'Finds requirements (Requirements::) and fixes them to be exposed properly';
26
    }
27
28
    public function getDescription()
29
    {
30
        return '
31
            Finds Requirements:: instances and fixes them to be used properly for modules
32
            - e.g. [vendorname] / [modulename] : location/for/my/script.js';
33
    }
34
35
    public function setIgnoreFolderArray($a)
36
    {
37
        $this->ignoreFolderArray = $a;
38
39
        return $this;
40
    }
41
42
    public function runActualTask($params = []): ?string
43
    {
44
        //replacement data patterns that will be searched for
45
        $replacementArray = [
46
            'src' => [
47
                'php' => [
48
                    'Requirements::javascript(' => [
49
                        'R' => '',
50
                    ],
51
                    'Requirements::css(' => [
52
                        'R' => '',
53
                    ],
54
                    'Requirements::themedCSS(' => [
55
                        'R' => '',
56
                    ],
57
                ],
58
            ],
59
        ];
60
61
        if ($this->debug) {
62
            $this->mu()->colourPrint($replacementArray);
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

62
            $this->mu()->/** @scrutinizer ignore-call */ colourPrint($replacementArray);
Loading history...
63
        }
64
        foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) {
0 ignored issues
show
Bug introduced by
It seems like getExistingModuleDirLocations() 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

64
        foreach ($this->mu()->/** @scrutinizer ignore-call */ getExistingModuleDirLocations() as $moduleDir) {
Loading history...
65
            //Start search machine from the module location. replace API
66
            $textSearchMachine = new SearchAndReplaceAPI($moduleDir);
67
            $textSearchMachine->setIsReplacingEnabled(true);
68
            $textSearchMachine->addToIgnoreFolderArray($this->ignoreFolderArray);
69
70
            /*For all the different patterns listed in the replacement array
71
            * iterate over them such that the $path would be 'src' and $patharray would be 'php'
72
            * together making it ['src']['php']
73
            */
74
            foreach ($replacementArray as $path => $pathArray) {
75
                $path = $moduleDir . '/' . $path ?: '';
76
                $path = $this->mu()->checkIfPathExistsAndCleanItUp($path);
0 ignored issues
show
Bug introduced by
It seems like checkIfPathExistsAndCleanItUp() 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

76
                $path = $this->mu()->/** @scrutinizer ignore-call */ checkIfPathExistsAndCleanItUp($path);
Loading history...
77
                if (! file_exists($path)) {
78
                    $this->mu()->colourPrint("SKIPPING ${path} as it does not exist.");
79
                } else {
80
                    $textSearchMachine->setSearchPath($path);
81
                    foreach ($pathArray as $extension => $extensionArray) {
82
                        //setting extensions to search files within
83
                        $textSearchMachine->setExtensions(explode('|', $extension));
84
                        $this->mu()->colourPrint(
85
                            "++++++++++++++++++++++++++++++++++++\n" .
86
                            "CHECKING\n" .
87
                            "IN ${path}\n" .
88
                            "FOR ${extension} FILES\n" .
89
                            'BASE ' . $moduleDir . "\n" .
90
                            "++++++++++++++++++++++++++++++++++++\n"
91
                        );
92
                        foreach ($extensionArray as $find => $findDetails) {
93
                            $replace = $findDetails['R'] ?? $find;
0 ignored issues
show
Unused Code introduced by
The assignment to $replace is dead and can be removed.
Loading history...
94
                            $caseSensitive = false;
95
96
                            $isStraightReplace = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $isStraightReplace is dead and can be removed.
Loading history...
97
                            $replacementType = 'BASIC';
98
99
                            // REPLACMENT PATTERN!
100
                            //Requirements::javascript(moduledirfolder/bla);
101
                            //Requirements::javascript(vpl: bla);
102
                            // $findWithPackageName = $find . strtolower($this->mu()->getPackageName());
103
                            $vendorAndPackageFolderNameForInstall =
104
                                $this->mu()->getVendorAndPackageFolderNameForInstall();
0 ignored issues
show
Bug introduced by
It seems like getVendorAndPackageFolderNameForInstall() 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 */ getVendorAndPackageFolderNameForInstall();
Loading history...
Bug introduced by
The method getVendorAndPackageFolderNameForInstall() 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 */ getVendorAndPackageFolderNameForInstall();
Loading history...
105
106
                            foreach (['\'', '"'] as $quoteMark) {
107
                                $finalReplace = $find . $quoteMark . $vendorAndPackageFolderNameForInstall . ': ';
108
                                if (! $finalReplace && $finalReplace !== ' ') {
109
                                    user_error("
110
                                        no replace is specified, find is: ${find}.
111
                                        We suggest setting your final replace to a single space
112
                                        if you would like to replace with NOTHING.
113
                                    ");
114
                                }
115
                                $finalFind = $find . $quoteMark;
116
                                $this->mu()->colourPrint(
117
                                    '    --- FIND: ' . $finalFind . "\n" .
118
                                    '    --- REPLACE: ' . $finalReplace . "\n"
119
                                );
120
121
                                $textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType);
122
                                $textSearchMachine->setReplacementKey($finalReplace);
123
                                $textSearchMachine->startSearchAndReplace();
124
                            }
125
                        }
126
127
                        //fix double-ups
128
                        //fixes things like
129
                        //vendor/packagename: silverstripe/admin
130
                        //to
131
                        //silverstripe/admin: only
132
                        $isCaseSensitive = true;
133
                        foreach (['cms', 'framework', 'siteconfig', 'reports'] as $ssModule) {
134
                            $finalFind = $vendorAndPackageFolderNameForInstall . ': silverstripe/' . $ssModule . ': ';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $vendorAndPackageFolderNameForInstall does not seem to be defined for all execution paths leading up to this point.
Loading history...
135
                            $finalReplace = 'silverstripe/' . $ssModule . ': ';
136
                            $this->mu()->colourPrint(
137
                                '    --- FIND: ' . $finalFind . "\n" .
138
                                '    --- REPLACE: ' . $finalReplace . "\n"
139
                            );
140
                            $textSearchMachine->setSearchKey(
141
                                $finalFind,
142
                                $isCaseSensitive,
143
                                'silverstripe/' . $ssModule . '/@@@@double-up@@@@'
144
                            );
145
                            $textSearchMachine->setReplacementKey($finalReplace);
146
                            $textSearchMachine->startSearchAndReplace();
147
                        }
148
149
                        //SHOW TOTALS
150
                        $replacements = $textSearchMachine->showFormattedSearchTotals();
151
                        if (! $replacements) {
152
                            //flush output anyway!
153
                            $this->mu()->colourPrint("No replacements for  ${extension}");
154
                        }
155
                        $this->mu()->colourPrint($textSearchMachine->getOutput());
156
                    }
157
                }
158
            }
159
        }
160
        return null;
161
    }
162
163
    protected function hasCommitAndPush()
164
    {
165
        return true;
166
    }
167
}
168