FixBadUseStatements   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 45
c 1
b 0
f 0
dl 0
loc 108
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setReplacementArray() 0 5 1
A setIgnoreFolderArray() 0 5 1
A getDescription() 0 3 1
A getTitle() 0 3 1
A hasCommitAndPush() 0 3 1
B runActualTask() 0 57 9
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 FixBadUseStatements extends Task
14
{
15
    protected $taskStep = 's50';
16
17
    protected $debug = false;
18
19
    protected $replacementArray = [
20
        'src' => [
21
            'php' => [
22
                'use bool;',
23
                'use string;',
24
                'use int;',
25
                'use array;',
26
            ],
27
        ],
28
    ];
29
30
    private $ignoreFolderArray = [
31
        '.git',
32
    ];
33
34
    public function getTitle()
35
    {
36
        return 'Look for single use statements and comment them out as they are not correct.';
37
    }
38
39
    public function getDescription()
40
    {
41
        return '
42
            Goes through code and removes, for example, "use bool;", lines, as they do not make sense.';
43
    }
44
45
    public function setIgnoreFolderArray($a)
46
    {
47
        $this->ignoreFolderArray = $a;
48
49
        return $this;
50
    }
51
52
    public function setReplacementArray($a)
53
    {
54
        $this->replacementArray = $a;
55
56
        return $this;
57
    }
58
59
    public function runActualTask($params = []): ?string
60
    {
61
        //replacement data patterns that will be searched for
62
63
        if ($this->debug) {
64
            $this->mu()->colourPrint($this->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

64
            $this->mu()->/** @scrutinizer ignore-call */ colourPrint($this->replacementArray);
Loading history...
65
        }
66
        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

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

78
                $path = $this->mu()->/** @scrutinizer ignore-call */ checkIfPathExistsAndCleanItUp($path);
Loading history...
79
                if (! file_exists($path)) {
80
                    $this->mu()->colourPrint("SKIPPING ${path} as it does not exist.");
81
                } else {
82
                    $textSearchMachine->setSearchPath($path);
83
                    foreach ($pathArray as $extension => $extensionArray) {
84
                        //setting extensions to search files within
85
                        $textSearchMachine->setExtensions(explode('|', $extension));
86
                        foreach ($extensionArray as $find) {
87
                            $caseSensitive = false;
88
                            $replacementType = 'BASIC';
89
90
                            // $this->mu()->colourPrint(
91
                            //     "++++++++++++++++++++++++++++++++++++\n".
92
                            //     "CHECKING\n".
93
                            //     "IN $path\n".
94
                            //     "FOR $extension FILES\n".
95
                            //     "BASE ".$moduleDir."\n".
96
                            //     "FIND '".$find."'\n".
97
                            //     "REPLACE ''\n".
98
                            //     "++++++++++++++++++++++++++++++++++++\n"
99
                            // );
100
                            $textSearchMachine->setSearchKey($find, $caseSensitive, $replacementType);
101
                            $textSearchMachine->setReplacementKey('');
102
                            $textSearchMachine->startSearchAndReplace();
103
                        }
104
                        //SHOW TOTALS
105
                        $replacements = $textSearchMachine->showFormattedSearchTotals();
106
                        if (! $replacements) {
107
                            //flush output anyway!
108
                            $this->mu()->colourPrint("No replacements for  ${extension}");
109
                        }
110
                        $this->mu()->colourPrint($textSearchMachine->getOutput());
111
                    }
112
                }
113
            }
114
        }
115
        return null;
116
    }
117
118
    protected function hasCommitAndPush()
119
    {
120
        return true;
121
    }
122
}
123