Passed
Push — master ( f01d00...2f7647 )
by Nicolaas
03:27
created

FixRequirements::setIgnoreFolderArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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 $checkReplacementIssues = false;
20
21
    private $ignoreFolderArray = [
22
        '.git',
23
    ];
24
25
    public function getTitle()
26
    {
27
        return 'Finds requirements (Requirements::) and fixes them to be exposed properly';
28
    }
29
30
    public function getDescription()
31
    {
32
        return '
33
            Finds Requirements:: instances and fixes them to be used properly for modules - e.g. [vendorname] / [modulename] : location/for/my/script.js';
34
    }
35
36
    public function setCheckReplacementIssues($b)
37
    {
38
        $this->checkReplacementIssues = $b;
39
40
        return $this;
41
    }
42
43
    public function setIgnoreFolderArray($a)
44
    {
45
        $this->ignoreFolderArray = $a;
46
47
        return $this;
48
    }
49
50
    public function runActualTask($params = [])
51
    {
52
        //replacement data patterns that will be searched for
53
        $replacementArray = [
54
            'src' => [
55
                'php' => [
56
                    'Requirements::javascript(' => [
57
                        'R' => '',
58
                    ],
59
                    'Requirements::css(' => [
60
                        'R' => '',
61
                    ],
62
                    'Requirements::themedCSS(' => [
63
                        'R' => '',
64
                    ],
65
                ],
66
            ],
67
        ];
68
69
        if ($this->debug) {
70
            $this->mu()->colourPrint(print_r($replacementArray, 1));
71
        }
72
        foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) {
73
            //Start search machine from the module location. replace API
74
            $textSearchMachine = new SearchAndReplaceAPI($moduleDir);
75
            $textSearchMachine->setIsReplacingEnabled(true);
76
            $textSearchMachine->addToIgnoreFolderArray($this->ignoreFolderArray);
77
78
            /*For all the different patterns listed in the replacement array
79
            * iterate over them such that the $path would be 'src' and $patharray would be 'php'
80
            * together making it ['src']['php']
81
            */
82
            foreach ($replacementArray as $path => $pathArray) {
83
                $path = $moduleDir . '/' . $path ?: '';
84
                $path = $this->mu()->checkIfPathExistsAndCleanItUp($path);
85
                if (! file_exists($path)) {
86
                    $this->mu()->colourPrint("SKIPPING ${path} as it does not exist.");
87
                } else {
88
                    $textSearchMachine->setSearchPath($path);
89
                    foreach ($pathArray as $extension => $extensionArray) {
90
                        $textSearchMachine->setExtensions(explode('|', $extension)); //setting extensions to search files within
91
                        $this->mu()->colourPrint(
92
                            "++++++++++++++++++++++++++++++++++++\n" .
93
                            "CHECKING\n" .
94
                            "IN ${path}\n" .
95
                            "FOR ${extension} FILES\n" .
96
                            'BASE ' . $moduleDir . "\n" .
97
                            "++++++++++++++++++++++++++++++++++++\n"
98
                        );
99
                        foreach ($extensionArray as $find => $findDetails) {
100
                            $replace = isset($findDetails['R']) ? $findDetails['R'] : $find;
101
                            $comment = isset($findDetails['C']) ? $findDetails['C'] : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $comment is dead and can be removed.
Loading history...
102
                            $ignoreCase = true;
103
                            $caseSensitive = ! $ignoreCase;
0 ignored issues
show
introduced by
The condition $ignoreCase is always true.
Loading history...
104
105
                            $isStraightReplace = true;
106
107
                            // REPLACMENT PATTERN!
108
                            //Requirements::javascript(moduledirfolder/bla);
109
                            //Requirements::javascript(vpl: bla);
110
                            $findWithPackageName = $find . strtolower($this->mu()->getPackageName());
0 ignored issues
show
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

110
                            $findWithPackageName = $find . strtolower($this->mu()->/** @scrutinizer ignore-call */ getPackageName());
Loading history...
Unused Code introduced by
The assignment to $findWithPackageName is dead and can be removed.
Loading history...
111
                            $vendorAndPackageFolderNameForInstall = $this->mu()->getVendorAndPackageFolderNameForInstall();
0 ignored issues
show
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

111
                            $vendorAndPackageFolderNameForInstall = $this->mu()->/** @scrutinizer ignore-call */ getVendorAndPackageFolderNameForInstall();
Loading history...
112
                            if (! $find) {
113
                                user_error("no find is specified, replace is: ${replace}");
114
                            }
115
                            $replacementType = $isStraightReplace ? 'BASIC' : 'COMPLEX';
116
117
                            foreach (['\'', '"'] as $quoteMark) {
118
                                $finalReplace = $find . $quoteMark . $vendorAndPackageFolderNameForInstall . ': ';
119
                                if (! $finalReplace && $finalReplace !== ' ') {
120
                                    user_error("no replace is specified, find is: ${find}. We suggest setting your final replace to a single space if you would like to replace with NOTHING.");
121
                                }
122
                                $finalFind = $find . $quoteMark;
123
                                $this->mu()->colourPrint(
124
                                    '    --- FIND: ' . $finalFind . "\n" .
125
                                    '    --- REPLACE: ' . $finalReplace . "\n"
126
                                );
127
128
                                $textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType);
129
                                $textSearchMachine->setReplacementKey($finalReplace);
130
                                $textSearchMachine->startSearchAndReplace();
131
                            }
132
                        }
133
134
                        //fix double-ups
135
                        //fixes things like
136
                        //vendor/packagename: silverstripe/admin
137
                        //to
138
                        //silverstripe/admin: only
139
                        foreach (['cms', 'framework', 'siteconfig', 'reports'] as $ssModule) {
140
                            $isStraightReplace = true;
141
                            $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...
142
                            $finalReplace = 'silverstripe/' . $ssModule . ': ';
143
                            $this->mu()->colourPrint(
144
                                '    --- FIND: ' . $finalFind . "\n" .
145
                                '    --- REPLACE: ' . $finalReplace . "\n"
146
                            );
147
                            $textSearchMachine->setSearchKey($finalFind, $isStraightReplace, 'silverstripe/' . $ssModule . '/@@@@double-up@@@@');
148
                            $textSearchMachine->setReplacementKey($finalReplace);
149
                            $textSearchMachine->startSearchAndReplace();
150
                        }
151
152
                        //SHOW TOTALS
153
                        $replacements = $textSearchMachine->showFormattedSearchTotals();
154
                        if (! $replacements) {
155
                            //flush output anyway!
156
                            $this->mu()->colourPrint("No replacements for  ${extension}");
157
                        }
158
                        $this->mu()->colourPrint($textSearchMachine->getOutput());
159
                    }
160
                }
161
            }
162
        }
163
    }
164
165
    protected function hasCommitAndPush()
166
    {
167
        return true;
168
    }
169
}
170