Passed
Push — master ( 68e144...a80af2 )
by Nicolaas
02:07
created

AddTableName   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 61
c 2
b 0
f 0
dl 0
loc 108
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setFindArray() 0 5 1
A getDescription() 0 3 1
A setExtensionArray() 0 5 1
A getTitle() 0 3 1
A hasCommitAndPush() 0 3 1
B runActualTask() 0 54 5
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 AddTableName extends Task
14
{
15
    protected $taskStep = 's10';
16
17
    protected $debug = false;
18
19
    private $ignoreFolderArray = [
20
        'extensions',
21
        'Extensions',
22
    ];
23
24
    private $extensionArray = [
25
        'php',
26
    ];
27
28
    private $findArray = [
29
        'private static $db',
30
        'private static $has_one' .
31
        'private static $belongs_to =',
32
        'private static $has_many =',
33
        'private static $many_many =',
34
        'private static $belongs_many_many =',
35
    ];
36
37
    public function getTitle()
38
    {
39
        return 'Add the table name ';
40
    }
41
42
    public function getDescription()
43
    {
44
        return '
45
            Finds $db and $has_one and adds the private static $table_name for the class...';
46
    }
47
48
    public function setExtensionArray($a)
49
    {
50
        $this->extensionArray = $a;
51
52
        return $this;
53
    }
54
55
    public function setFindArray($a)
56
    {
57
        $this->findArray = $a;
58
59
        return $this;
60
    }
61
62
    public function runActualTask($params = []): ?string
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
            $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

65
            $moduleDir = $this->mu()->/** @scrutinizer ignore-call */ findMyCodeDir($moduleDir);
Loading history...
66
            //Start search machine from the module location. replace API
67
            $textSearchMachine = new SearchAndReplaceAPI($moduleDir);
68
            $textSearchMachine->setIsReplacingEnabled(true);
69
            $textSearchMachine->setFileReplacementMaxCount(1);
70
            $textSearchMachine->setIgnoreFileIfFound(['private static $table_name']);
71
            $textSearchMachine->addToIgnoreFolderArray($this->ignoreFolderArray);
72
            $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

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

73
            $moduleDir = $this->mu()->/** @scrutinizer ignore-call */ checkIfPathExistsAndCleanItUp($moduleDir);
Loading history...
74
            if (! file_exists($moduleDir)) {
75
                $this->mu()->colourPrint("SKIPPING ${moduleDir} as it does not exist.");
76
            } else {
77
                $textSearchMachine->setSearchPath($moduleDir);
78
                $textSearchMachine->setExtensions($this->extensionArray); //setting extensions to search files within
79
                $this->mu()->colourPrint(
80
                    "++++++++++++++++++++++++++++++++++++\n" .
81
                    "CHECKING\n" .
82
                    "IN ${moduleDir}\n" .
83
                    'FOR ' . implode(',', $this->extensionArray) . " FILES\n" .
84
                    'BASE ' . $moduleDir . "\n" .
85
                    "++++++++++++++++++++++++++++++++++++\n"
86
                );
87
                foreach ($this->findArray as $finalFind) {
88
                    $caseSensitive = true;
89
                    $replacementType = 'COMPLEX';
90
                    $comment = 'Check that is class indeed extends DataObject and that it is not a data-extension!';
91
                    $finalReplace = '
92
    private static $table_name = \'[SEARCH_REPLACE_CLASS_NAME_GOES_HERE]\';
93
94
    ' . $finalFind;
95
                    $this->mu()->colourPrint(
96
                        '    --- FIND: ' . $finalFind . "\n" .
97
                        '    --- REPLACE: ' . $finalReplace . "\n"
98
                    );
99
100
                    $textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType);
101
                    $textSearchMachine->setReplacementKey($finalReplace);
102
                    $textSearchMachine->setComment($comment);
103
                    $textSearchMachine->startSearchAndReplace();
104
                }
105
106
                //SHOW TOTALS
107
                $replacements = $textSearchMachine->showFormattedSearchTotals();
108
                if (! $replacements) {
109
                    //flush output anyway!
110
                    $this->mu()->colourPrint('No replacements for  ' . implode(',', $this->extensionArray));
111
                }
112
                $this->mu()->colourPrint($textSearchMachine->getOutput());
113
            }
114
        }
115
        return null;
116
    }
117
118
    protected function hasCommitAndPush()
119
    {
120
        return true;
121
    }
122
}
123