Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

ModuleController::actionCreate()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 8.4032
c 0
b 0
f 0
cc 6
nc 11
nop 0

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 luya\console\commands;
4
5
use Yii;
6
use yii\helpers\Console;
7
use luya\helpers\FileHelper;
8
use yii\helpers\Inflector;
9
10
/**
11
 * Command to create a new LUYA Module.
12
 *
13
 * @author Basil Suter <[email protected]>
14
 * @since 1.0.0
15
 */
16
class ModuleController extends \luya\console\Command
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public $defaultAction = 'create';
22
    
23
    /**
24
     * Humanize the class name
25
     *
26
     * @return string The humanized name.
27
     */
28
    public function humanizeName($name)
29
    {
30
        return $name = Inflector::humanize(Inflector::camel2words($name));
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
    }
32
    
33
    /**
34
     * Render the readme template.
35
     *
36
     * @param array $folders
37
     * @param string $name
38
     * @param string $ns
39
     * @return string
40
     */
41 View Code Duplication
    public function renderReadme($folders, $name, $ns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        return $this->view->render('@luya/console/commands/views/module/readme.php', [
44
            'folders' => $folders,
45
            'name' => $name,
46
            'humanName' => $this->humanizeName($name),
47
            'ns' => $ns,
48
            'luyaText' => $this->getGeneratorText('module/create'),
49
        ]);
50
    }
51
    
52
    /**
53
     * Render the admin template.
54
     *
55
     * @param array $folders
56
     * @param string $name
57
     * @param string $ns
58
     * @return string
59
     */
60 View Code Duplication
    public function renderAdmin($folders, $name, $ns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        return $this->view->render('@luya/console/commands/views/module/adminmodule.php', [
63
            'folders' => $folders,
64
            'name' => $this->humanizeName($name),
65
            'ns' => $ns,
66
            'luyaText' => $this->getGeneratorText('module/create'),
67
        ]);
68
    }
69
    
70
    /**
71
     * Render the frontend template.
72
     *
73
     * @param array $folders
74
     * @param string $name
75
     * @param string $ns
76
     * @return string
77
     */
78 View Code Duplication
    public function renderFrontend($folders, $name, $ns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        return $this->view->render('@luya/console/commands/views/module/frontendmodule.php', [
81
            'folders' => $folders,
82
            'name' => $this->humanizeName($name),
83
            'ns' => $ns,
84
            'luyaText' => $this->getGeneratorText('module/create'),
85
        ]);
86
    }
87
    
88
    /**
89
     * Create a new frontend/admin module.
90
     *
91
     * @return number
92
     */
93
    public function actionCreate()
94
    {
95
        Console::clearScreenBeforeCursor();
96
        
97
        $moduleName = $this->prompt("Enter the name of the module you like to generate:");
98
99
        $newName = preg_replace("/[^a-z]/", "", strtolower($moduleName));
100
        
101
        if ($newName !== $moduleName) {
102
            if (!$this->confirm("We have changed the name to '{$newName}'. Do you want to proceed with this name?")) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->confirm("We have ...oceed with this name?") of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
103
                return $this->outputError('Abort by user.');
104
            } else {
105
                $moduleName = $newName;
106
            }
107
        }
108
        
109
        $appModulesFolder = Yii::$app->basePath . DIRECTORY_SEPARATOR . 'modules';
110
        $moduleFolder = $appModulesFolder . DIRECTORY_SEPARATOR . $moduleName;
111
        
112
        if (file_exists($moduleFolder)) {
113
            return $this->outputError("The folder " . $moduleFolder . " exists already.");
114
        }
115
        
116
        $folders = [
117
            'basePath' => $moduleFolder,
118
            'adminPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'admin',
119
            'adminAwsPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'aws',
120
            'adminMigrationPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'migrations',
121
            'frontendPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend',
122
            'frontendBlocksPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'blocks',
123
            'frontendControllersPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'controllers',
124
            'frontendViewsPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'views',
125
            'modelsPath' => $moduleFolder . DIRECTORY_SEPARATOR . 'models',
126
        ];
127
128
        $ns = 'app\\modules\\'.$moduleName;
129
        
130
        foreach ($folders as $folder) {
131
            FileHelper::createDirectory($folder);
132
        }
133
        
134
        $contents = [
135
            $moduleFolder. DIRECTORY_SEPARATOR . 'README.md' => $this->renderReadme($folders, $moduleName, $ns),
136
            $moduleFolder. DIRECTORY_SEPARATOR . 'admin/Module.php' => $this->renderAdmin($folders, $moduleName, $ns),
137
            $moduleFolder. DIRECTORY_SEPARATOR . 'frontend/Module.php' => $this->renderFrontend($folders, $moduleName, $ns),
138
        ];
139
        
140
        foreach ($contents as $fileName => $content) {
141
            FileHelper::writeFile($fileName, $content);
142
        }
143
        
144
        return $this->outputSuccess("Module files has been created successfully. Check the README file to understand how to add the module to your config.");
145
    }
146
}
147