FixClassNamesWithUnderscores::getDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Api\FindFiles;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
class FixClassNamesWithUnderscores extends Task
9
{
10
    protected $taskStep = 's60';
11
12
    protected $listOfOKOnes = [];
13
14
    public function getTitle()
15
    {
16
        return 'Finds classes with underscores and removes them';
17
    }
18
19
    public function getDescription()
20
    {
21
        return '
22
            Goes through all the PHP files and
23
            finds classes with underscores the removes them and adds an entry to .upgrade.yml and database legacy yml. ';
24
    }
25
26
    public function runActualTask($params = []): ?string
27
    {
28
        $errors = 0;
29
        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

29
        foreach ($this->mu()->/** @scrutinizer ignore-call */ getExistingModuleDirLocations() as $moduleDir) {
Loading history...
30
            $this->mu()->colourPrint('Searching ' . $moduleDir, 'grey');
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

30
            $this->mu()->/** @scrutinizer ignore-call */ colourPrint('Searching ' . $moduleDir, 'grey');
Loading history...
31
            $fileFinder = new FindFiles();
32
            $searchPath = $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

32
            $searchPath = $this->mu()->/** @scrutinizer ignore-call */ findMyCodeDir($moduleDir);
Loading history...
33
            if (file_exists($searchPath)) {
34
                $flatArray = $fileFinder
35
                    ->setSearchPath($searchPath)
36
                    ->setExtensions(['php'])
37
                    ->getFlatFileArray();
38
                if (is_array($flatArray) && count($flatArray)) {
39
                    foreach ($flatArray as $path) {
40
                        $shortClassName = str_replace('.php', '', basename($path));
41
                        if (strpos($shortClassName, '_')) {
42
                            $errors++;
43
                            $this->mu()->colourPrint('Found an underscore in ... ' . $searchPath, 'red');
44
                        } else {
45
                            $this->mu()->colourPrint('All Good in the Hood for ...  ' . $path, 'grey');
46
                        }
47
                    }
48
                } else {
49
                    $this->mu()->colourPrint('Could not find any files in ' . $searchPath, 'red');
50
                }
51
            } else {
52
                $this->mu()->colourPrint('Could not find ' . $searchPath, 'blue');
53
            }
54
        }
55
        if ($errors) {
56
            $this->mu()->colourPrint(
57
                '
58
                    Found ' . $errors . ' errors.
59
                    You need to do the following things:
60
                    (a) check table name issues,
61
                    (b) update .upgrade.yml,
62
                    (c) update database legacy yml
63
                    (d) run a find and replace to update all files.
64
                ',
65
                'red'
66
            );
67
            return 'There are errors to fix';
68
        } else {
69
            $this->mu()->colourPrint('There are no classes with underscores', 'green');
70
        }
71
        return null;
72
    }
73
74
    protected function hasCommitAndPush()
75
    {
76
        return false;
77
    }
78
}
79