| Conditions | 21 |
| Paths | 342 |
| Total Lines | 137 |
| Code Lines | 87 |
| Lines | 29 |
| Ratio | 21.17 % |
| 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 |
||
| 48 | public function execute(InputInterface $input, OutputInterface $output) |
||
| 49 | { |
||
| 50 | |||
| 51 | View Code Duplication | if ($input->hasOption('settings') && $input->getOption('settings') != null) { |
|
|
|
|||
| 52 | $settingsFile = $input->getOption('settings'); |
||
| 53 | } else { |
||
| 54 | $settingsFile = '.' . DIRECTORY_SEPARATOR . '.settings.yml'; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!file_exists($settingsFile)) { |
||
| 58 | throw new InvalidArgumentException(sprintf('Cannot find settings file "%s"', $settingsFile)); |
||
| 59 | } |
||
| 60 | |||
| 61 | $helper = $this->getHelper('question'); |
||
| 62 | |||
| 63 | $settings = Yaml::parse(file_get_contents($settingsFile)); |
||
| 64 | |||
| 65 | if (!is_array($settings)) { |
||
| 66 | throw new InvalidArgumentException(sprintf('Error parsing settings file "%s". Return value unexpected. Expected array, got %s', $settingsFile, gettype($settings))); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!isset($settings['project']['prefix'])) { |
||
| 70 | throw new InvalidArgumentException(sprintf('No project prefix found in settings file "%s"', $settingsFile)); |
||
| 71 | } |
||
| 72 | $projectLocation = (is_array($settings) && isset($settings['project']['location']) ? $settings['project']['location'] : '.'); |
||
| 73 | |||
| 74 | $output->writeln(sprintf("Project location set to \"%s\"", $projectLocation), Output::VERBOSITY_VERY_VERBOSE); |
||
| 75 | |||
| 76 | if ($input->hasOption('module') && $input->getOption('module') != null) { |
||
| 77 | $module = $input->getOption('module'); |
||
| 78 | } else { |
||
| 79 | $question = new Question("Module name: "); |
||
| 80 | $module = $helper->ask($input, $output, $question); |
||
| 81 | } |
||
| 82 | |||
| 83 | $module = ucfirst(TransformIdentifier::transform($module)); |
||
| 84 | |||
| 85 | $output->writeln(sprintf("Module name transformed to to \"%s\"", $module), Output::VERBOSITY_VERY_VERBOSE); |
||
| 86 | |||
| 87 | |||
| 88 | View Code Duplication | if (!file_exists($projectLocation . '/app/modules/' . $module . '/config/module.xml')) { |
|
| 89 | throw new InvalidArgumentException(sprintf('Module "%s" does not seem to exist in "%s"', $module, implode(DIRECTORY_SEPARATOR, [$projectLocation, 'app', 'modules', $module]))); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($input->hasOption('controller') && $input->getOption('controller') != null) { |
||
| 93 | $controllerName = $input->getOption('controller'); |
||
| 94 | } else { |
||
| 95 | $question = new Question('Controller name: '); |
||
| 96 | $controllerName = $helper->ask($input, $output, $question); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (strlen($controllerName) == 0) { |
||
| 100 | throw new InvalidArgumentException("Controller name cannot be empty."); |
||
| 101 | } |
||
| 102 | |||
| 103 | View Code Duplication | if (!file_exists($projectLocation . '/app/modules/' . $module . '/controllers/' . str_replace('.', '/', $controllerName) . 'Controller.class.php')) { |
|
| 104 | throw new InvalidArgumentException(sprintf('Controller "%s" does not seem to exist in "%s"', $controllerName, $projectLocation . |
||
| 105 | implode(DIRECTORY_SEPARATOR, ['app', 'modules', $module, 'controllers', explode('.', $controllerName)]) . 'Controller.class.php')); |
||
| 106 | } |
||
| 107 | |||
| 108 | $controllerName = TransformIdentifier::transform($controllerName); |
||
| 109 | $output->writeln(sprintf("Controller name transformed to \"%s\"", $controllerName), Output::VERBOSITY_VERY_VERBOSE); |
||
| 110 | |||
| 111 | if ($input->hasArgument('view') && $input->getArgument('view') != null) { |
||
| 112 | $viewName = $input->getArgument('view'); |
||
| 113 | } else { |
||
| 114 | $question = new Question('View name: '); |
||
| 115 | $viewName = $helper->ask($input, $output, $question); |
||
| 116 | } |
||
| 117 | |||
| 118 | if (strlen($viewName) == 0) { |
||
| 119 | throw new InvalidArgumentException("Controller name cannot be empty."); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (file_exists($projectLocation . '/app/modules/' . $module . '/views/' . str_replace('.', '/', $controllerName) . $viewName . 'View.class.php')) { |
||
| 123 | throw new InvalidArgumentException(sprintf('View "%s" seems to already exist in "%s"', $controllerName . $viewName, $projectLocation . |
||
| 124 | implode(DIRECTORY_SEPARATOR, ['app', 'modules', $module, 'views', explode('.', $controllerName)]) . $viewName . 'View.class.php')); |
||
| 125 | } |
||
| 126 | |||
| 127 | $viewName = TransformIdentifier::transform($viewName); |
||
| 128 | $output->writeln(sprintf("View name transformed to \"%s\"", $viewName), Output::VERBOSITY_VERY_VERBOSE); |
||
| 129 | |||
| 130 | $fc = new FileCopyHelper(); |
||
| 131 | |||
| 132 | if ($input->hasOption('output-types') && $input->getOption('output-types') != null) { |
||
| 133 | // Remove quotes and turn input into an array |
||
| 134 | $output_types = explode(' ', str_replace('"', '', $input->getOption('output-types'))); |
||
| 135 | } else { |
||
| 136 | // Ask for the input types |
||
| 137 | $question = new Question(sprintf("Space-separated list of output-types handled by the view [%s] (empty for none): ", $controllerName . ucfirst($viewName))); |
||
| 138 | $output_types = $helper->ask($input, $output, $question); |
||
| 139 | $output_types = explode(' ', $output_types); |
||
| 140 | } |
||
| 141 | |||
| 142 | $output->writeln('Copying view', Output::VERBOSITY_VERY_VERBOSE); |
||
| 143 | |||
| 144 | $viewFile = $projectLocation . '/app/modules/' . $module . '/views/' . str_replace('.', '/', $controllerName) . ucfirst($viewName) . 'View.class.php'; |
||
| 145 | |||
| 146 | $output->writeln(sprintf("[%s -> %s]", |
||
| 147 | $this->getSourceDir() . '/build/templates/app/modules/views/View.class.php.tmpl', |
||
| 148 | $viewFile |
||
| 149 | ), Output::VERBOSITY_DEBUG); |
||
| 150 | |||
| 151 | $srcview = $this->getSourceDir() . '/build/templates/app/modules/views/View.class.php.tmpl'; |
||
| 152 | |||
| 153 | $fc->copy($srcview, $viewFile, |
||
| 154 | View Code Duplication | function ($data, $params) { |
|
| 155 | return str_replace([ |
||
| 156 | '%%PROJECT_PREFIX%%', |
||
| 157 | '%%MODULE_NAME%%', |
||
| 158 | '%%VIEW_CLASS%%', |
||
| 159 | '%%METHOD_DECLARATIONS%%', |
||
| 160 | '%%PROJECT_NAMESPACE%%', |
||
| 161 | '%%FQNS%%' |
||
| 162 | ], [ |
||
| 163 | $params['projectPrefix'], |
||
| 164 | $params['moduleName'], |
||
| 165 | $params['viewClass'], |
||
| 166 | $params['methodDeclarations'], |
||
| 167 | $params['NS'], |
||
| 168 | $params['FQNS'] |
||
| 169 | ], $data); |
||
| 170 | }, [ |
||
| 171 | 'projectPrefix' => $settings['project']['prefix'], |
||
| 172 | 'moduleName' => $module, |
||
| 173 | 'viewClass' => $module . '_' . $controllerName . ucfirst($viewName) . 'View', |
||
| 174 | 'methodDeclarations' => $this->generateHandleOutputTypeMethods($output_types, $controllerName), |
||
| 175 | 'FQNS' => $settings['project']['namespace'], |
||
| 176 | 'NS' => substr($settings['project']['namespace'], 1, strlen($settings['project']['namespace'])) |
||
| 177 | ] |
||
| 178 | ); |
||
| 179 | |||
| 180 | $templateFile = $projectLocation . '/app/modules/' . $module . '/templates/' . str_replace('.', '/', $controllerName) . ucfirst($viewName) . '.php'; |
||
| 181 | |||
| 182 | $output->writeln(sprintf('Creating empty template file "%s"', $templateFile), Output::VERBOSITY_DEBUG); |
||
| 183 | @touch($templateFile, 0755); |
||
| 184 | } |
||
| 185 | |||
| 204 |
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.