| Conditions | 6 |
| Paths | 11 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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?")) { |
||
| 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 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.