ChangeControllerInitToProtected::runActualTask()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 37
c 2
b 1
f 0
dl 0
loc 50
rs 9.0168
cc 5
nc 6
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
class ChangeControllerInitToProtected extends Task
9
{
10
    protected $taskStep = 's10';
11
12
    protected $debug = false;
13
14
    private $extensionArray = [
15
        'php',
16
    ];
17
18
    private $findArray = [
19
        '    public function init()',
20
        "\t" . 'public function init()',
21
        '    function init()',
22
        "\t" . 'function init()',
23
    ];
24
25
    public function getTitle()
26
    {
27
        return 'Change Controller::init function to protected';
28
    }
29
30
    public function getDescription()
31
    {
32
        return '
33
            Look for all init functions in Controllers (based on file name) and change to protected functions.';
34
    }
35
36
    public function setExtensionArray($a)
37
    {
38
        $this->extensionArray = $a;
39
40
        return $this;
41
    }
42
43
    public function setFindArray($a)
44
    {
45
        $this->findArray = $a;
46
47
        return $this;
48
    }
49
50
    public function runActualTask($params = []): ?string
51
    {
52
        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

52
        foreach ($this->mu()->/** @scrutinizer ignore-call */ getExistingModuleDirLocations() as $moduleDir) {
Loading history...
53
            $moduleDir = $this->mu()->findMyCodeDir($moduleDir);
0 ignored issues
show
Bug introduced by
It seems like findMyCodeDir() 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

53
            $moduleDir = $this->mu()->/** @scrutinizer ignore-call */ findMyCodeDir($moduleDir);
Loading history...
54
            //Start search machine from the module location. replace API
55
            $textSearchMachine = new SearchAndReplaceAPI($moduleDir);
56
            $textSearchMachine->setIsReplacingEnabled(true);
57
            $textSearchMachine->setFileReplacementMaxCount(1);
58
            $textSearchMachine->setFileNameMustContain('Controller');
59
            $this->mu()->colourPrint("Checking ${moduleDir}");
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

59
            $this->mu()->/** @scrutinizer ignore-call */ colourPrint("Checking ${moduleDir}");
Loading history...
60
            $moduleDir = $this->mu()->checkIfPathExistsAndCleanItUp($moduleDir);
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

60
            $moduleDir = $this->mu()->/** @scrutinizer ignore-call */ checkIfPathExistsAndCleanItUp($moduleDir);
Loading history...
61
            if (! file_exists($moduleDir)) {
62
                $this->mu()->colourPrint("SKIPPING ${moduleDir} as it does not exist.");
63
            } else {
64
                $textSearchMachine->setSearchPath($moduleDir);
65
                $textSearchMachine->setExtensions($this->extensionArray); //setting extensions to search files within
66
                $this->mu()->colourPrint(
67
                    "++++++++++++++++++++++++++++++++++++\n" .
68
                    "CHECKING\n" .
69
                    "IN ${moduleDir}\n" .
70
                    'FOR ' . implode(',', $this->extensionArray) . " FILES\n" .
71
                    'BASE ' . $moduleDir . "\n" .
72
                    "++++++++++++++++++++++++++++++++++++\n"
73
                );
74
                foreach ($this->findArray as $finalFind) {
75
                    $caseSensitive = false;
76
                    $replacementType = 'COMPLEX';
77
                    $comment = 'Controller init functions are now protected  please check that is a controller.';
78
                    $finalReplace = '    protected function init()';
79
                    $this->mu()->colourPrint(
80
                        '    --- FIND: ' . $finalFind . "\n" .
81
                        '    --- REPLACE: ' . $finalReplace . "\n"
82
                    );
83
84
                    $textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType);
85
                    $textSearchMachine->setReplacementKey($finalReplace);
86
                    $textSearchMachine->setComment($comment);
87
                    $textSearchMachine->startSearchAndReplace();
88
                }
89
90
                //SHOW TOTALS
91
                $replacements = $textSearchMachine->showFormattedSearchTotals();
92
                if (! $replacements) {
93
                    //flush output anyway!
94
                    $this->mu()->colourPrint('No replacements for  ' . implode(',', $this->extensionArray));
95
                }
96
                $this->mu()->colourPrint($textSearchMachine->getOutput());
97
            }
98
        }
99
        return null;
100
    }
101
102
    protected function hasCommitAndPush()
103
    {
104
        return true;
105
    }
106
}
107