Passed
Push — master ( f01d00...2f7647 )
by Nicolaas
03:27
created

UpperCaseFolderNamesForPSR4::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
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\Tasks\Task;
6
7
/**
8
 * Fixes the folder name cases in to make them PSR4 compatible
9
 * e.g.
10
 * yourmodule/src/model becomes yourmodule/src/Model
11
 */
12
class UpperCaseFolderNamesForPSR4 extends Task
13
{
14
    protected $taskStep = 's30';
15
16
    protected $nameReplacements = [
17
        'interface' => 'Interfaces',
18
    ];
19
20
    public function getTitle()
21
    {
22
        return 'Fix Folder Case';
23
    }
24
25
    public function getDescription()
26
    {
27
        return '
28
            Change your src/code folders from lowercase to TitleCase - e.g.
29
            yourmodule/src/model becomes yourmodule/src/Model in accordance with PSR-4 autoloading';
30
    }
31
32
    public function setNameReplacements($a)
33
    {
34
        $this->nameReplacements = $a;
35
    }
36
37
    public function runActualTask($params = [])
38
    {
39
        foreach ($this->mu()->findNameSpaceAndCodeDirs() as $baseNameSpace => $codeDir) {
40
            $di = new \RecursiveIteratorIterator(
41
                new \RecursiveDirectoryIterator($codeDir, \FilesystemIterator::SKIP_DOTS),
42
                \RecursiveIteratorIterator::CHILD_FIRST
43
            );
44
45
            //For all directories
46
            foreach ($di as $name => $fio) {
47
                if ($fio->isDir()) {
48
                    //If its a directory then
49
                    $newName = $fio->getPath() . DIRECTORY_SEPARATOR . $this->mu()->camelCase($fio->getFilename());
50
                    foreach ($this->nameReplacements as $from => $to) {
51
                        if ($from === $name) {
52
                            $newName = $to;
53
                        }
54
                    }
55
                    if ($name === $newName) {
56
                        $this->mu()->colourPrint('No need to move ' . str_replace($codeDir, '', $name) . ' as it is already in CamelCase', 'dark_gray');
57
                    } else {
58
                        $this->mu()->colourPrint('New name for directory: ' . $newName, 'green');
59
                        $this->mu()->execMe(
60
                            $this->mu()->getWebRootDirLocation(),
0 ignored issues
show
Bug introduced by
The method getWebRootDirLocation() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                            $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation(),
Loading history...
61
                            'mv ' . $name . ' ' . $newName,
62
                            'renaming code dir (' . $baseNameSpace . ') form ' . str_replace($codeDir, '', $name) . ' to ' . str_replace($codeDir, '', $newName),
63
                            false
64
                        );
65
                    }
66
                    //rename($name, $newname); - first check the output, then remove the comment...
67
                }
68
            }
69
        }
70
    }
71
72
    protected function hasCommitAndPush()
73
    {
74
        return true;
75
    }
76
}
77