UpperCaseFolderNamesForPSR4::getDescription()   A
last analyzed

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\Api\FileSystemFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
/**
9
 * Fixes the folder name cases in to make them PSR4 compatible
10
 * e.g.
11
 * yourmodule/src/model becomes yourmodule/src/Model
12
 */
13
class UpperCaseFolderNamesForPSR4 extends Task
14
{
15
    protected $taskStep = 's30';
16
17
    protected $nameReplacements = [
18
        'interface' => 'Interfaces',
19
    ];
20
21
    public function getTitle()
22
    {
23
        return 'Fix Folder Case';
24
    }
25
26
    public function getDescription()
27
    {
28
        return '
29
            Change your src/code folders from lowercase to TitleCase - e.g.
30
            yourmodule/src/model becomes yourmodule/src/Model in accordance with PSR-4 autoloading';
31
    }
32
33
    public function setNameReplacements($a)
34
    {
35
        $this->nameReplacements = $a;
36
    }
37
38
    public function runActualTask($params = []): ?string
39
    {
40
        foreach ($this->mu()->findNameSpaceAndCodeDirs() as $codeDir) {
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

40
        foreach ($this->mu()->/** @scrutinizer ignore-call */ findNameSpaceAndCodeDirs() as $codeDir) {
Loading history...
41
            $di = new \RecursiveIteratorIterator(
42
                new \RecursiveDirectoryIterator($codeDir, \FilesystemIterator::SKIP_DOTS),
43
                \RecursiveIteratorIterator::CHILD_FIRST
44
            );
45
46
            //For all directories
47
            foreach ($di as $name => $fio) {
48
                if ($fio->isDir()) {
49
                    //If its a directory then
50
                    $newName = $fio->getPath() . DIRECTORY_SEPARATOR . $this->mu()->cleanCamelCase($fio->getFilename());
0 ignored issues
show
Bug introduced by
It seems like cleanCamelCase() 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

50
                    $newName = $fio->getPath() . DIRECTORY_SEPARATOR . $this->mu()->/** @scrutinizer ignore-call */ cleanCamelCase($fio->getFilename());
Loading history...
51
                    foreach ($this->nameReplacements as $from => $to) {
52
                        if ($from === $name) {
53
                            $newName = $to;
54
                        }
55
                    }
56
                    if ($name === $newName) {
57
                        $this->mu()->colourPrint('No need to move ' . str_replace($codeDir, '', $name) . ' as it is already in CamelCase', 'dark_gray');
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('No need to move ' . str_replace($codeDir, '', $name) . ' as it is already in CamelCase', 'dark_gray');
Loading history...
58
                    } else {
59
                        $this->mu()->colourPrint('New name for directory: ' . $newName, 'green');
60
                        $fixer = FileSystemFixes::inst($this->mu());
61
                        $fixer->moveFolderOrFile($name, $newName);
62
                    }
63
                    //rename($name, $newname); - first check the output, then remove the comment...
64
                }
65
            }
66
        }
67
        return null;
68
    }
69
70
    protected function hasCommitAndPush()
71
    {
72
        return true;
73
    }
74
}
75