FixTemplateIncludeStatements   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 48
c 1
b 0
f 0
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A setExtensionArray() 0 5 1
A getTitle() 0 3 1
A setFindArray() 0 5 1
A hasCommitAndPush() 0 3 1
A runActualTask() 0 50 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
class FixTemplateIncludeStatements extends Task
9
{
10
    protected $taskStep = 's30';
11
12
    protected $debug = false;
13
14
    private $extensionArray = [
15
        'ss',
16
    ];
17
18
    private $findArray = [
19
        '<% include ',
20
    ];
21
22
    public function getTitle()
23
    {
24
        return 'Add Namespacing to include statements in templates.';
25
    }
26
27
    public function getDescription()
28
    {
29
        return '
30
            Go through all templates and fix the include statements by adding namespacing e.g. % include Order % becomes % include Sunnysideup\Ecommerce\Includes\Order %.';
31
    }
32
33
    public function setExtensionArray($a)
34
    {
35
        $this->extensionArray = $a;
36
37
        return $this;
38
    }
39
40
    public function setFindArray($a)
41
    {
42
        $this->findArray = $a;
43
44
        return $this;
45
    }
46
47
    public function runActualTask($params = []): ?string
48
    {
49
        $codeDirs = $this->mu()->findNameSpaceAndCodeDirs();
0 ignored issues
show
Bug introduced by
It seems like findNameSpaceAndCodeDirs() 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

49
        $codeDirs = $this->mu()->/** @scrutinizer ignore-call */ findNameSpaceAndCodeDirs();
Loading history...
50
        $caseSensitive = false;
51
        foreach ($codeDirs as $baseNameSpace => $codeDir) {
52
            $baseDir = dirname($codeDir);
53
            //Start search machine from the module location. replace API
54
            $textSearchMachine = new SearchAndReplaceAPI($baseDir);
55
            $textSearchMachine->setIsReplacingEnabled(true);
56
            // $textSearchMachine->setFileNameMustContain('<% include');
57
            $this->mu()->colourPrint("Checking ${baseDir}");
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

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

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