| Conditions | 9 |
| Paths | 27 |
| Total Lines | 68 |
| 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 |
||
| 33 | public function actionCreate(string $themeName = null) |
||
| 34 | { |
||
| 35 | Console::clearScreenBeforeCursor(); |
||
| 36 | |||
| 37 | $themeName = $this->prompt("Enter the name (lower case) of the theme you like to generate:", ['default' => $themeName]); |
||
| 38 | |||
| 39 | $newName = preg_replace("/[^a-z]/", "", strtolower($themeName)); |
||
| 40 | if ($newName !== $themeName) { |
||
| 41 | if (!$this->confirm("We have changed the name to '{$newName}'. Do you want to proceed with this name?")) { |
||
|
|
|||
| 42 | return $this->outputError('Abort by user.'); |
||
| 43 | } else { |
||
| 44 | $themeName = $newName; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | $availableModules = implode(', ', array_column(Yii::$app->getFrontendModules(), 'id')); |
||
| 49 | $themeLocation = $this->prompt("Enter the theme location where to generate (as path alias e.g. app, $availableModules):", ['default' => 'app']); |
||
| 50 | $themeLocation = '@' . ltrim($themeLocation, '@'); |
||
| 51 | |||
| 52 | preg_match("#^@[A-z]+#", $themeLocation, $newThemeLocation); |
||
| 53 | |||
| 54 | if ($newThemeLocation[0] !== $themeLocation) { |
||
| 55 | if (!$this->confirm("We have changed the name to '{$newThemeLocation[0]}'. Do you want to proceed with this name?")) { |
||
| 56 | return $this->outputError('Abort by user.'); |
||
| 57 | } else { |
||
| 58 | $themeLocation = $newThemeLocation[0]; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $basePath = $themeLocation . '/themes/' . $themeName; |
||
| 63 | $themeFolder = Yii::getAlias($basePath); |
||
| 64 | |||
| 65 | if (file_exists($themeFolder)) { |
||
| 66 | return $this->outputError("The folder " . $themeFolder . " exists already."); |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->outputInfo("Theme path alias: " . $basePath); |
||
| 70 | $this->outputInfo("Theme real path: " . $themeFolder); |
||
| 71 | if (!$this->confirm("Do you want continue?")) { |
||
| 72 | return $this->outputError('Abort by user.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | $folders = [ |
||
| 76 | '', |
||
| 77 | 'resources', |
||
| 78 | 'views', |
||
| 79 | 'views/layouts', |
||
| 80 | 'views/cmslayouts', |
||
| 81 | ]; |
||
| 82 | |||
| 83 | foreach ($folders as $folder) { |
||
| 84 | FileHelper::createDirectory($themeFolder . DIRECTORY_SEPARATOR . $folder); |
||
| 85 | } |
||
| 86 | |||
| 87 | $contents = [ |
||
| 88 | $themeFolder. DIRECTORY_SEPARATOR . 'theme.json' => $this->renderJson($basePath, $themeName), |
||
| 89 | $themeFolder. DIRECTORY_SEPARATOR . ucfirst($themeName) . 'Asset.php' => $this->renderAssetClass($themeName), |
||
| 90 | $themeFolder. DIRECTORY_SEPARATOR . 'resources/'. $themeName .'-asset/style.css' => '', |
||
| 91 | $themeFolder. DIRECTORY_SEPARATOR . 'views/layouts/theme.php' => $this->renderLayout($themeName), |
||
| 92 | $themeFolder. DIRECTORY_SEPARATOR . 'views/cmslayouts/theme.php' => $this->renderCmsLayout($themeName), |
||
| 93 | ]; |
||
| 94 | |||
| 95 | foreach ($contents as $fileName => $content) { |
||
| 96 | FileHelper::writeFile($fileName, $content); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $this->outputSuccess("Theme files has been created successfully. Please run `".$_SERVER['PHP_SELF']." import` to import the theme into the database."); |
||
| 100 | } |
||
| 101 | |||
| 199 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.