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

ModuleController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 21.37 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 28
loc 131
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A humanizeName() 0 4 1
A renderReadme() 10 10 1
A renderAdmin() 9 9 1
A renderFrontend() 9 9 1
B actionCreate() 0 53 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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