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

AddNamespace::runActualTask()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 58
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 58
rs 8.5066
cc 7
nc 7
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
6
7
/**
8
 * Places all your code into namespaces (provided by silvertripe/runActualTask),
9
 * using the PSR-4 approach (matching folders and namespaces)
10
 */
11
class AddNamespace extends Task
12
{
13
    protected $taskStep = 's40';
14
15
    public function getTitle()
16
    {
17
        return 'Name Spaces';
18
    }
19
20
    public function getDescription()
21
    {
22
        return '
23
            Places all your code into namespaces (provided by silvertripe/runActualTask),
24
            using the PSR-4 approach (matching folders and namespaces).';
25
    }
26
27
    public function runActualTask($params = [])
28
    {
29
        $codeDirs = $this->mu()->findNameSpaceAndCodeDirs();
30
        $dirsDone = [];
31
        foreach ($codeDirs as $baseNameSpace => $codeDir) {
32
            $directories = new \RecursiveDirectoryIterator($codeDir);
33
            foreach (new \RecursiveIteratorIterator($directories) as $file => $fileObject) {
34
                if ($fileObject->getExtension() === 'php') {
35
                    $dirName = realpath(dirname($file));
36
                    if (! isset($dirsDone[$dirName])) {
37
                        $dirsDone[$dirName] = true;
38
                        $nameSpaceAppendix = str_replace($codeDir, '', $dirName);
39
                        $nameSpaceAppendix = trim($nameSpaceAppendix, '/');
40
                        $nameSpaceAppendix = str_replace('/', '\\', $nameSpaceAppendix);
41
                        //prepend $baseNameSpace
42
                        $nameSpace = $baseNameSpace . '\\' . $nameSpaceAppendix;
43
                        //turn into array
44
                        $nameSpaceArray = explode('\\', $nameSpace);
45
                        $nameSpaceArrayNew = [];
46
                        foreach ($nameSpaceArray as $nameSpaceSnippet) {
47
                            if ($nameSpaceSnippet) {
48
                                $nameSpaceArrayNew[] = $this->mu()->camelCase($nameSpaceSnippet);
49
                            }
50
                        }
51
                        $nameSpace = implode('\\', $nameSpaceArrayNew);
52
                        $this->mu()->execMe(
53
                            $codeDir,
54
                            'php ' . $this->mu()->getLocationOfSSUpgradeModule() . ' add-namespace "' . $nameSpace . '" ' . $dirName . ' --root-dir=' . $this->mu()->getWebRootDirLocation() . ' --write --psr4 -vvv',
0 ignored issues
show
Bug introduced by
The method getLocationOfSSUpgradeModule() 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

54
                            'php ' . $this->mu()->/** @scrutinizer ignore-call */ getLocationOfSSUpgradeModule() . ' add-namespace "' . $nameSpace . '" ' . $dirName . ' --root-dir=' . $this->mu()->getWebRootDirLocation() . ' --write --psr4 -vvv',
Loading history...
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

54
                            'php ' . $this->mu()->getLocationOfSSUpgradeModule() . ' add-namespace "' . $nameSpace . '" ' . $dirName . ' --root-dir=' . $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation() . ' --write --psr4 -vvv',
Loading history...
55
                            'adding namespace: ' . $nameSpace . ' to ' . $dirName,
56
                            false
57
                        );
58
                    }
59
                }
60
            }
61
            // } else {
62
            //     //@todo: we assume 'code' for now ...
63
            //     $codeDirs = $this->mu()->findNameSpaceAndCodeDirs();
64
            //     foreach ($codeDirs as $codeDir) {
65
            //         $this->mu()->execMe(
66
            //             $this->mu()->getLocationOfSSUpgradeModule(),
67
            //             'find '.$codeDir.' -mindepth 1 -maxdepth 2 -type d -exec '.
68
            //                 'sh -c '.
69
            //                     '\'dir=${1##*/}; '.
70
            //                     'php '.$this->mu()->getLocationOfSSUpgradeModule().' add-namespace "'.$this->mu()->getVendorNamespace().'\\'.$this->mu()->getPackageNamespace().'\\$dir" "$dir" --write --psr4 -vvv'.
71
            //                 '\' _ {} '.
72
            //             '\;',
73
            //             'adding name spaces',
74
            //             false
75
            //         );
76
            //     }
77
            // }
78
            $this->mu()->execMe(
79
                $codeDir,
80
                'php ' . $this->mu()->getLocationOfSSUpgradeModule() . ' add-namespace "' . $baseNameSpace . '\" ' . $codeDir . ' --root-dir=' . $this->mu()->getWebRootDirLocation() . ' --write --psr4 -vvv',
81
                'adding namespace: ' . $baseNameSpace . ' to ' . $codeDir,
82
                false
83
            );
84
            $this->setCommitMessage('MAJOR: adding namespaces');
85
        }
86
    }
87
88
    protected function hasCommitAndPush()
89
    {
90
        return true;
91
    }
92
}
93