Conditions | 47 |
Paths | > 20000 |
Total Lines | 329 |
Code Lines | 212 |
Lines | 64 |
Ratio | 19.45 % |
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 |
||
51 | public function execute(InputInterface $input, OutputInterface $output) |
||
52 | { |
||
53 | |||
54 | View Code Duplication | if ($input->hasOption('settings') && $input->getOption('settings') != null) { |
|
|
|||
55 | $settingsFile = $input->getOption('settings'); |
||
56 | } else { |
||
57 | $settingsFile = '.' . DIRECTORY_SEPARATOR . '.settings.yml'; |
||
58 | } |
||
59 | |||
60 | if (!file_exists($settingsFile)) { |
||
61 | throw new InvalidArgumentException(sprintf('Cannot find settings file "%s"', $settingsFile)); |
||
62 | } |
||
63 | |||
64 | $helper = $this->getHelper('question'); |
||
65 | |||
66 | $settings = Yaml::parse(file_get_contents($settingsFile)); |
||
67 | |||
68 | if (!is_array($settings)) { |
||
69 | throw new InvalidArgumentException(sprintf('Error parsing settings file "%s". Return value unexpected. Expected array, got %s', $settingsFile, gettype($settings))); |
||
70 | } |
||
71 | |||
72 | if (!isset($settings['project']['prefix'])) { |
||
73 | throw new InvalidArgumentException(sprintf('No project prefix found in settings file "%s"', $settingsFile)); |
||
74 | } |
||
75 | $projectLocation = (is_array($settings) && isset($settings['project']['location']) ? $settings['project']['location'] : '.'); |
||
76 | |||
77 | $output->writeln(sprintf("Project location set to \"%s\"", $projectLocation), Output::VERBOSITY_VERY_VERBOSE); |
||
78 | |||
79 | if ($input->hasOption('module') && $input->getOption('module') != null) { |
||
80 | $module = $input->getOption('module'); |
||
81 | } else { |
||
82 | $question = new Question("Module name: "); |
||
83 | $module = $helper->ask($input, $output, $question); |
||
84 | } |
||
85 | |||
86 | $module = ucfirst(TransformIdentifier::transform($module)); |
||
87 | |||
88 | $output->writeln(sprintf("Module name transformed to to \"%s\"", $module), Output::VERBOSITY_VERY_VERBOSE); |
||
89 | |||
90 | |||
91 | View Code Duplication | if (!file_exists($projectLocation . '/app/modules/' . $module . '/config/module.xml')) { |
|
92 | throw new InvalidArgumentException(sprintf('Module "%s" does not seem to exist in "%s"', $module, implode(DIRECTORY_SEPARATOR, [$projectLocation, 'app', 'modules', $module]))); |
||
93 | } |
||
94 | |||
95 | if ($input->hasArgument('name') && $input->getArgument('name') != null) { |
||
96 | $controllerName = $input->getArgument('name'); |
||
97 | } else { |
||
98 | $question = new Question('Controller name: '); |
||
99 | $controllerName = $helper->ask($input, $output, $question); |
||
100 | } |
||
101 | |||
102 | if (strlen($controllerName) == 0) { |
||
103 | throw new InvalidArgumentException("Controller name cannot be empty."); |
||
104 | } |
||
105 | |||
106 | |||
107 | View Code Duplication | if (file_exists($projectLocation . '/app/modules/' . $module . '/controllers/' . str_replace('.', '/', $controllerName))) { |
|
108 | throw new InvalidArgumentException(sprintf('Controller "%s" seems to already exist in "%s"', $controllerName, $projectLocation . |
||
109 | implode(DIRECTORY_SEPARATOR, ['app', 'modules', $module, 'controllers', explode('.', $controllerName)]))); |
||
110 | } |
||
111 | |||
112 | $controllerName = TransformIdentifier::transform($controllerName); |
||
113 | $output->writeln(sprintf("Controller name transformed to \"%s\"", $controllerName), Output::VERBOSITY_VERY_VERBOSE); |
||
114 | |||
115 | if ($input->getOption('simple') == false && (!$input->hasOption('methods') || $input->getOption('methods') == null)) { |
||
116 | $question = new Question("Create a simple controller? Simple controllers cannot handle request methods. (y/n) ", 'n'); |
||
117 | $simple = $helper->ask($input, $output, $question); |
||
118 | |||
119 | if (!in_array($simple, ['y', 'Y', 'n', 'N'])) { |
||
120 | throw new InvalidArgumentException(sprintf('Invalid answer "%s", expected one of y, Y, n or N.'), $simple); |
||
121 | } |
||
122 | } else { |
||
123 | $simple = 'n'; |
||
124 | } |
||
125 | |||
126 | $methodDeclarations = ''; |
||
127 | |||
128 | |||
129 | if (strtolower($simple) == 'n') { |
||
130 | View Code Duplication | if ($input->hasOption('methods') && $input->getOption('methods') != null) { |
|
131 | $requestMethods = $input->getOption('methods'); |
||
132 | } else { |
||
133 | $question = new Question(sprintf("Space-separated list of request methods that should be handled by controller [%s] (empty for none): ", $controllerName)); |
||
134 | $requestMethods = $helper->ask($input, $output, $question); |
||
135 | } |
||
136 | |||
137 | $requestMethods = str_replace('"', '', $requestMethods); |
||
138 | $output->writeln(sprintf("Request methods set to \"%s\"", $requestMethods), Output::VERBOSITY_VERY_VERBOSE); |
||
139 | |||
140 | if (strlen($requestMethods) > 0) { |
||
141 | $methodDeclarations = $this->generateExecuteMethods(explode(' ', $requestMethods)); |
||
142 | } |
||
143 | } else { |
||
144 | $methodDeclarations = file_get_contents($this->getSourceDir() . '/build/templates/code/controllers/Simple.tmpl'); |
||
145 | } |
||
146 | |||
147 | // If the controller name contains a dot, it's a subcontroller, so we create the directory |
||
148 | if (strpos($controllerName, '.') !== false) { |
||
149 | $output->writeln(sprintf("Controller \"%s\" seems to be a subcontroller, creating directories", $controllerName), Output::VERBOSITY_VERY_VERBOSE); |
||
150 | @mkdir($projectLocation . '/app/modules/' . $module . '/controllers/' . str_replace('.', '/', $controllerName), 0755, true); |
||
151 | @mkdir($projectLocation . '/app/modules/' . $module . '/views/' . str_replace('.', '/', $controllerName), 0755, true); |
||
152 | @mkdir($projectLocation . '/app/modules/' . $module . '/templates/' . str_replace('.', '/', $controllerName), 0755, true); |
||
153 | @mkdir($projectLocation . '/app/modules/' . $module . '/cache/' . str_replace('.', '/', $controllerName), 0755, true); |
||
154 | @mkdir($projectLocation . '/app/modules/' . $module . '/validate/' . str_replace('.', '/', $controllerName), 0755, true); |
||
155 | } |
||
156 | |||
157 | |||
158 | $fc = new FileCopyHelper(); |
||
159 | |||
160 | $output->writeln("Copying files for controller", Output::VERBOSITY_VERY_VERBOSE); |
||
161 | |||
162 | // Copy controller |
||
163 | $controllerFile = $projectLocation . '/app/modules/' . $module . '/controllers/' . str_replace('.', '/', $controllerName) . 'Controller.class.php'; |
||
164 | |||
165 | $output->writeln(sprintf("[%s -> %s]", |
||
166 | $this->getSourceDir() . '/build/templates/app/modules/controllers/Controller.class.php.tmpl', |
||
167 | $controllerFile |
||
168 | ), Output::VERBOSITY_DEBUG); |
||
169 | |||
170 | $fc->copy($this->getSourceDir() . '/build/templates/app/modules/controllers/Controller.class.php.tmpl', |
||
171 | $controllerFile, |
||
172 | View Code Duplication | function ($data, $params) { |
|
173 | return str_replace([ |
||
174 | '%%PROJECT_PREFIX%%', |
||
175 | '%%MODULE_NAME%%', |
||
176 | '%%CONTROLLER_CLASS%%', |
||
177 | '%%METHOD_DECLARATIONS%%', |
||
178 | '%%PROJECT_NAMESPACE%%', |
||
179 | '%%FQNS%%' |
||
180 | ], [ |
||
181 | $params['projectPrefix'], |
||
182 | $params['moduleName'], |
||
183 | $params['controllerClass'], |
||
184 | $params['methodDeclarations'], |
||
185 | $params['NS'], |
||
186 | $params['FQNS'] |
||
187 | ], $data); |
||
188 | }, [ |
||
189 | 'projectPrefix' => $settings['project']['prefix'], |
||
190 | 'moduleName' => $module, |
||
191 | 'controllerClass' => $controllerName . 'Controller', |
||
192 | 'methodDeclarations' => $methodDeclarations, |
||
193 | 'FQNS' => $settings['project']['namespace'], |
||
194 | 'NS' => substr($settings['project']['namespace'], 1, strlen($settings['project']['namespace'])) |
||
195 | ] |
||
196 | ); |
||
197 | |||
198 | // Copy validator |
||
199 | $validatorFile = $projectLocation . '/app/modules/' . $module . '/validate/' . str_replace('.', '/', $controllerName) . '.xml'; |
||
200 | |||
201 | $output->writeln(sprintf("[%s -> %s]", |
||
202 | $this->getSourceDir() . '/build/templates/app/modules/validate/controller.xml.tmpl', |
||
203 | $validatorFile |
||
204 | ), Output::VERBOSITY_DEBUG); |
||
205 | |||
206 | |||
207 | $fc->copy($this->getSourceDir() . '/build/templates/app/modules/validate/controller.xml.tmpl', |
||
208 | $validatorFile, |
||
209 | function ($data, $params) { |
||
210 | return str_replace([ |
||
211 | '%%MODULE_NAME%%', |
||
212 | ], [ |
||
213 | $params['moduleName'], |
||
214 | ], $data); |
||
215 | }, [ |
||
216 | 'moduleName' => $module, |
||
217 | ] |
||
218 | ); |
||
219 | |||
220 | // Copy cache file |
||
221 | $cacheFile = $projectLocation . '/app/modules/' . $module . '/cache/' . str_replace('.', '/', $controllerName) . '.xml'; |
||
222 | $output->writeln(sprintf("[%s -> %s]", |
||
223 | $this->getSourceDir() . '/build/templates/app/modules/cache/controller.xml.tmpl', |
||
224 | $cacheFile |
||
225 | ), Output::VERBOSITY_DEBUG); |
||
226 | |||
227 | $fc->copy($this->getSourceDir() . '/build/templates/app/modules/cache/controller.xml.tmpl', |
||
228 | $cacheFile, |
||
229 | function ($data, $params) { |
||
230 | return str_replace([ |
||
231 | '%%MODULE_NAME%%', |
||
232 | ], [ |
||
233 | $params['moduleName'], |
||
234 | ], $data); |
||
235 | }, [ |
||
236 | 'moduleName' => $module, |
||
237 | ] |
||
238 | ); |
||
239 | |||
240 | View Code Duplication | if ($input->hasOption('views') && $input->getOption('views') != null) { |
|
241 | $views = str_replace('"', '', $input->getOption('views')); |
||
242 | } else { |
||
243 | $question = new Question(sprintf("Space-separated list of views for controller [%s] (empty for none): ", $controllerName)); |
||
244 | $views = $helper->ask($input, $output, $question); |
||
245 | } |
||
246 | |||
247 | $output->writeln(sprintf("Setting views to \"%s\"", $views), Output::VERBOSITY_VERY_VERBOSE); |
||
248 | |||
249 | |||
250 | $views = explode(' ', $views); |
||
251 | $viewdefs = []; |
||
252 | if ($input->hasOption('output-types') && $input->getOption('output-types') != null) { |
||
253 | // Remove quotes and turn input into an array |
||
254 | $viewoutputtypes = explode(' ', str_replace('"', '', $input->getOption('output-types'))); |
||
255 | foreach ($viewoutputtypes as $viewoutputtype) { |
||
256 | if (strpos($viewoutputtype, ':') !== false) { |
||
257 | list($view, $output_type) = explode(':', $viewoutputtype); |
||
258 | if (!in_array($view, $views)) { |
||
259 | throw new InvalidArgumentException(sprintf('Cannot find view "%s" in the list of views [%s]', ucfirst($view), implode(', ', ucfirst($views)))); |
||
260 | } |
||
261 | $viewdefs[$view][] = $output_type; |
||
262 | } |
||
263 | } |
||
264 | } else { |
||
265 | // Ask the input types for every view |
||
266 | foreach ($views as $viewname) { |
||
267 | $question = new Question(sprintf("Space-separated list of output-types handled by the view [%s] (empty for none): ", ucfirst($viewname))); |
||
268 | $vopt = $helper->ask($input, $output, $question); |
||
269 | $vopts = explode(' ', $vopt); |
||
270 | foreach ($vopts as $vo) { |
||
271 | $viewdefs[ucfirst($viewname)][] = $vo; |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | $output->writeln('Copying views', Output::VERBOSITY_VERY_VERBOSE); |
||
277 | |||
278 | // Copy views |
||
279 | foreach ($viewdefs as $viewname => $output_types) { |
||
280 | $viewFile = $projectLocation . '/app/modules/' . $module . '/views/' . str_replace('.', '/', $controllerName) . ucfirst($viewname) . 'View.class.php'; |
||
281 | |||
282 | $output->writeln(sprintf("[%s -> %s]", |
||
283 | $this->getSourceDir() . '/build/templates/app/modules/views/View.class.php.tmpl', |
||
284 | $viewFile |
||
285 | ), Output::VERBOSITY_DEBUG); |
||
286 | |||
287 | $srcview = $this->getSourceDir() . '/build/templates/app/modules/views/View.class.php.tmpl'; |
||
288 | |||
289 | // If this is a system controller, copy a specific default view |
||
290 | if ($input->hasOption('system') && $input->getOption('system') != null) { |
||
291 | switch ($input->getOption('system')) { |
||
292 | case 'error_404': |
||
293 | $srcview = $this->getSourceDir() . '/build/templates/defaults/app/modules/views/Error404SuccessView.class.php.tmpl'; |
||
294 | break; |
||
295 | case 'module_disabled': |
||
296 | $srcview = $this->getSourceDir() . '/build/templates/defaults/app/modules/views/ModuleDisabledSuccessView.class.php.tmpl'; |
||
297 | break; |
||
298 | case 'secure': |
||
299 | $srcview = $this->getSourceDir() . '/build/templates/defaults/app/modules/views/SecureSuccessView.class.php.tmpl'; |
||
300 | break; |
||
301 | case 'unavailable': |
||
302 | $srcview = $this->getSourceDir() . '/build/templates/defaults/app/modules/views/UnavailableSuccessView.class.php.tmpl'; |
||
303 | break; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | $fc->copy($srcview, $viewFile, |
||
308 | View Code Duplication | function ($data, $params) { |
|
309 | return str_replace([ |
||
310 | '%%PROJECT_PREFIX%%', |
||
311 | '%%MODULE_NAME%%', |
||
312 | '%%VIEW_CLASS%%', |
||
313 | '%%METHOD_DECLARATIONS%%', |
||
314 | '%%PROJECT_NAMESPACE%%', |
||
315 | '%%FQNS%%' |
||
316 | ], [ |
||
317 | $params['projectPrefix'], |
||
318 | $params['moduleName'], |
||
319 | $params['viewClass'], |
||
320 | $params['methodDeclarations'], |
||
321 | $params['NS'], |
||
322 | $params['FQNS'] |
||
323 | ], $data); |
||
324 | }, [ |
||
325 | 'projectPrefix' => $settings['project']['prefix'], |
||
326 | 'moduleName' => $module, |
||
327 | 'viewClass' => $controllerName . ucfirst($viewname) . 'View', |
||
328 | 'methodDeclarations' => $this->generateHandleOutputTypeMethods($output_types, $controllerName), |
||
329 | 'FQNS' => $settings['project']['namespace'], |
||
330 | 'NS' => substr($settings['project']['namespace'], 1, strlen($settings['project']['namespace'])) |
||
331 | ] |
||
332 | ); |
||
333 | |||
334 | $templateFile = $projectLocation . '/app/modules/' . $module . '/templates/' . str_replace('.', '/', $controllerName) . ucfirst($viewname) . '.php'; |
||
335 | |||
336 | $output->writeln(sprintf('Creating empty template file "%s"', $templateFile), Output::VERBOSITY_DEBUG); |
||
337 | @touch($templateFile, 0755); |
||
338 | } |
||
339 | |||
340 | /* |
||
341 | * For system controllers we need to modify the settings.xml -file too |
||
342 | */ |
||
343 | $system_controllers = ['default', 'error_404', 'login', 'module_disabled', 'secure', 'unavailable']; |
||
344 | if ($input->hasOption('system') && $input->getOption('system') != null) { |
||
345 | $system = $input->getOption('system'); |
||
346 | |||
347 | if (!in_array($system, $system_controllers)) { |
||
348 | throw new InvalidArgumentException(sprintf('"%s" is not an allowed system controller (one of %s)', $system, implode(', ', $system_controllers))); |
||
349 | } |
||
350 | |||
351 | $settingsFile = $projectLocation . '/app/config/settings.xml'; |
||
352 | |||
353 | // Set the module |
||
354 | |||
355 | $moduleXPath = "//*[local-name() = 'configuration' and (namespace-uri() = 'http://agavi.org/agavi/config/global/envelope/1.0' or namespace-uri() = 'http://agavi.org/agavi/config/global/envelope/1.1')]//*[local-name() = 'system_controller' and (namespace-uri() = 'http://agavi.org/agavi/config/parts/settings/1.0' or namespace-uri() = 'http://agavi.org/agavi/config/parts/settings/1.1') and @name='" . $system . "']/*[local-name() = 'module']"; |
||
356 | |||
357 | $this->writeSettings($settingsFile, $moduleXPath, $module, $output); |
||
358 | |||
359 | // Set the controller |
||
360 | $controllerXPath = "//*[local-name() = 'configuration' and (namespace-uri() = 'http://agavi.org/agavi/config/global/envelope/1.0' or namespace-uri() = 'http://agavi.org/agavi/config/global/envelope/1.1')]//*[local-name() = 'system_controller' and (namespace-uri() = 'http://agavi.org/agavi/config/parts/settings/1.0' or namespace-uri() = 'http://agavi.org/agavi/config/parts/settings/1.1') and @name='" . $system . "']/*[local-name() = 'controller']"; |
||
361 | $this->writeSettings($settingsFile, $controllerXPath, $controllerName, $output); |
||
362 | |||
363 | // Copy the default templates |
||
364 | switch ($system) { |
||
365 | case "error_404": |
||
366 | @copy($this->getSourceDir() . '/build/templates/defaults/app/modules/templates/Error404Success.php.tmpl', $projectLocation . '/app/modules/' . $module . '/templates/Error404Success.php'); |
||
367 | break; |
||
368 | case 'module_disabled': |
||
369 | @copy($this->getSourceDir() . '/build/templates/defaults/app/modules/templates/ModuleDisabledSuccess.php.tmpl', $projectLocation . '/app/modules/' . $module . '/templates/ModuleDisabledSuccess.php'); |
||
370 | break; |
||
371 | View Code Duplication | case 'success': |
|
372 | @copy($this->getSourceDir() . '/build/templates/defaults/app/modules/templates/SecureSuccess.php.tmpl', $projectLocation . '/app/modules/' . $module . '/templates/SecureSuccess.php'); |
||
373 | break; |
||
374 | View Code Duplication | case 'unavailable': |
|
375 | @copy($this->getSourceDir() . '/build/templates/defaults/app/modules/templates/UnavailableSuccess.php.tmpl', $projectLocation . '/app/modules/' . $module . '/templates/UnavailableSuccess.php'); |
||
376 | break; |
||
377 | } |
||
378 | } |
||
379 | } |
||
380 | |||
433 |
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.