AddNamespace   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 36
c 0
b 0
f 0
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A getDescription() 0 3 1
A hasCommitAndPush() 0 3 1
B runActualTask() 0 60 7
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 = []): ?string
28
    {
29
        $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

29
        $codeDirs = $this->mu()->/** @scrutinizer ignore-call */ findNameSpaceAndCodeDirs();
Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type null and true; however, parameter $path of dirname() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

35
                    $dirName = realpath(dirname(/** @scrutinizer ignore-type */ $file));
Loading history...
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()->cleanCamelCase($nameSpaceSnippet);
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

48
                                $nameSpaceArrayNew[] = $this->mu()->/** @scrutinizer ignore-call */ cleanCamelCase($nameSpaceSnippet);
Loading history...
49
                            }
50
                        }
51
                        $nameSpace = implode('\\', $nameSpaceArrayNew);
52
                        $this->mu()->execMe(
0 ignored issues
show
Bug introduced by
It seems like execMe() 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

52
                        $this->mu()->/** @scrutinizer ignore-call */ execMe(
Loading history...
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 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...
Bug introduced by
It seems like getLocationOfSSUpgradeModule() 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

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
It seems like getWebRootDirLocation() 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

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('API:  adding namespaces');
85
        }
86
        return null;
87
    }
88
89
    protected function hasCommitAndPush()
90
    {
91
        return true;
92
    }
93
}
94