AddPSR4Autoloading::runActualTask()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 70
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 39
c 3
b 0
f 1
dl 0
loc 70
rs 9.296
cc 4
nc 6
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\Helpers\Composer;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes;
7
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
8
9
/**
10
 * Fixes the folder name cases in to make them PSR4 compatible
11
 * e.g.
12
 * yourmodule/src/model becomes yourmodule/src/Model
13
 */
14
class AddPSR4Autoloading extends Task
15
{
16
    protected $taskStep = 's50';
17
18
    public function getTitle()
19
    {
20
        return 'Add PSR-4 Autoloading to the composer file.';
21
    }
22
23
    public function getDescription()
24
    {
25
        return '
26
            Goes through all the folders in the code or src dir and adds them to the composer.json file as autoloader.
27
            This must run after the folder names have been changed to CamelCase (see: UpperCaseFolderNamesForPSR4).
28
        ';
29
    }
30
31
    public function runActualTask($params = []): ?string
32
    {
33
        //project composer.json
34
        // - app/src/...
35
        // - app2/src/...
36
        // DO FOR BOTH
37
        //module composor.json
38
        //  ONLY FOR module
39
        $baseCommands = '
40
            if(! isset($data["autoload"])) {
41
                $data["autoload"] = [];
42
            }
43
            if(! isset($data["autoload"]["psr-4"])) {
44
                $data["autoload"]["psr-4"] = [];
45
            }
46
        ';
47
        $addPage = '';
48
        if ($this->mu()->getIsModuleUpgrade()) {
0 ignored issues
show
Bug introduced by
It seems like getIsModuleUpgrade() 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
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
Bug introduced by
The method getIsModuleUpgrade() 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

48
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
49
            $addPage = '
50
            if(! isset($data["autoload"]["files"])) {
51
                $data["autoload"]["files"] = [
52
                    "app/src/Page.php",
53
                    "app/src/PageController.php"
54
                ];
55
            }';
56
        }
57
58
        $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

58
        $codeDirs = $this->mu()->/** @scrutinizer ignore-call */ findNameSpaceAndCodeDirs();
Loading history...
59
        $webRootLocation = $this->mu()->getWebRootDirLocation();
0 ignored issues
show
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

59
        $webRootLocation = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
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

59
        $webRootLocation = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
Loading history...
60
        $command = $baseCommands . $addPage;
61
        $comment = 'Adding autoload Page and Page controller details in ' . $webRootLocation . '/composer.json';
62
        ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine(
63
            $webRootLocation,
64
            $command,
65
            $comment
66
        );
67
        foreach ($codeDirs as $baseNameSpace => $codeDir) {
68
            $location = trim(str_replace($webRootLocation, '', $codeDir), '/') . '/';
0 ignored issues
show
Bug introduced by
It seems like $webRootLocation can also be of type Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader and Sunnysideup\UpgradeToSilverstripe4\Traits\Creator; however, parameter $search of str_replace() does only seem to accept string|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

68
            $location = trim(str_replace(/** @scrutinizer ignore-type */ $webRootLocation, '', $codeDir), '/') . '/';
Loading history...
69
            //update webroot composer file
70
            //location:
71
            $command = $baseCommands . '
72
            $data["autoload"]["psr-4"]["' . $this->doubleSlash($baseNameSpace) . '"] = "' . $location . '";';
73
            $comment = 'Adding autoload psr-4 details in ' .
74
                $webRootLocation . '/composer.json: ' .
75
                $baseNameSpace . ' => ' . $location;
76
            ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine(
77
                $webRootLocation,
78
                $command,
79
                $comment
80
            );
81
            if ($this->mu()->getIsModuleUpgrade()) {
82
                $moduleLocation = dirname($codeDir);
83
                $location = trim(basename($codeDir), '/') . '/';
84
                $command = $baseCommands . '
85
                $data["autoload"]["psr-4"]["' .
86
                $this->doubleSlash($baseNameSpace) . '"] = "' .
87
                ltrim($location, '/') . '";';
88
                $comment = 'Adding autoload psr-4 details in ' .
89
                    $moduleLocation . '/composer.json: ' .
90
                    $baseNameSpace . ' => ' . $location;
91
                ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine(
92
                    $moduleLocation,
93
                    $command,
94
                    $comment
95
                );
96
            }
97
        }
98
        Composer::inst($this->mu())
99
            ->DumpAutoload();
100
        return null;
101
    }
102
103
    protected function hasCommitAndPush()
104
    {
105
        return true;
106
    }
107
108
    protected function doubleSlash($str)
109
    {
110
        return str_replace('\\', '\\\\', $str);
111
    }
112
}
113