Conditions | 30 |
Paths | > 20000 |
Total Lines | 282 |
Code Lines | 202 |
Lines | 80 |
Ratio | 28.37 % |
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 |
||
228 | private function createFilesAndFolders($code, $paramList) |
||
229 | { |
||
230 | $fsList = array( |
||
231 | 'dir' => array(), |
||
232 | 'file' => array(), |
||
233 | ); |
||
234 | |||
235 | // config.ymlを作成 |
||
236 | $config = array(); |
||
237 | $config['name'] = $paramList['pluginName']['value']; |
||
238 | $config['code'] = $code; |
||
239 | $config['version'] = $paramList['version']['value']; |
||
240 | if (!empty($paramList['hookPoints']['value']) || !empty($paramList['events']['value'])) { |
||
241 | $config['event'] = $code.'Event'; |
||
242 | } |
||
243 | $config['service'] = array($code.'ServiceProvider'); |
||
244 | if ($this->paramList['useOrmPath']['value']) { |
||
245 | $config['orm.path'] = array('/Resource/doctrine'); |
||
246 | } |
||
247 | |||
248 | $codePath = $this->app['config']['root_dir'].'/app/Plugin/'.$code; |
||
249 | |||
250 | $file = new Filesystem(); |
||
251 | $file->mkdir($codePath); |
||
252 | if (is_dir($codePath)) { |
||
253 | $fsList['dir'][$codePath] = true; |
||
254 | } else { |
||
255 | $fsList['dir'][$codePath] = false; |
||
256 | } |
||
257 | |||
258 | $srcPath = $codePath.'/config.yml'; |
||
259 | file_put_contents($srcPath, Yaml::dump($config)); |
||
260 | View Code Duplication | if (is_file($srcPath)) { |
|
261 | $fsList['file'][$srcPath] = true; |
||
262 | } else { |
||
263 | $fsList['file'][$srcPath] = false; |
||
264 | } |
||
265 | |||
266 | $author = $paramList['author']['value']; |
||
267 | $year = date('Y'); |
||
268 | |||
269 | // PluginManager |
||
270 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/PluginManager.php'); |
||
271 | $from = '/\[code\]/'; |
||
272 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
273 | $from = '/\[author\]/'; |
||
274 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
275 | $from = '/\[year\]/'; |
||
276 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
277 | |||
278 | $srcPath = $codePath.'/PluginManager.php'; |
||
279 | file_put_contents($srcPath, $pluginFileAfter); |
||
280 | View Code Duplication | if (is_file($srcPath)) { |
|
281 | $fsList['file'][$srcPath] = true; |
||
282 | } else { |
||
283 | $fsList['file'][$srcPath] = false; |
||
284 | } |
||
285 | |||
286 | // ServiceProvider |
||
287 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ServiceProvider.php'); |
||
288 | $from = '/\[code\]/'; |
||
289 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
290 | $from = '/\[lower_code\]/'; |
||
291 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
292 | $from = '/\[author\]/'; |
||
293 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
294 | $from = '/\[year\]/'; |
||
295 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
296 | |||
297 | $file->mkdir($codePath.'/ServiceProvider'); |
||
298 | View Code Duplication | if (is_dir($codePath.'/ServiceProvider')) { |
|
299 | $fsList['dir'][$codePath.'/ServiceProvider'] = true; |
||
300 | } else { |
||
301 | $fsList['dir'][$codePath.'/ServiceProvider'] = false; |
||
302 | } |
||
303 | |||
304 | $srcPath = $codePath.'/ServiceProvider/'.$code.'ServiceProvider.php'; |
||
305 | file_put_contents($srcPath, $pluginFileAfter); |
||
306 | View Code Duplication | if (is_file($srcPath)) { |
|
307 | $fsList['file'][$srcPath] = true; |
||
308 | } else { |
||
309 | $fsList['file'][$srcPath] = false; |
||
310 | } |
||
311 | |||
312 | // ConfigController |
||
313 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ConfigController.php'); |
||
314 | $from = '/\[code\]/'; |
||
315 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
316 | $from = '/\[author\]/'; |
||
317 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
318 | $from = '/\[year\]/'; |
||
319 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
320 | $from = '/\[code_name\]/'; |
||
321 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
322 | |||
323 | $file->mkdir($codePath.'/Controller'); |
||
324 | View Code Duplication | if (is_dir($codePath.'/Controller')) { |
|
325 | $fsList['dir'][$codePath.'/Controller'] = true; |
||
326 | } else { |
||
327 | $fsList['dir'][$codePath.'/Controller'] = false; |
||
328 | } |
||
329 | |||
330 | $srcPath = $codePath.'/Controller/ConfigController.php'; |
||
331 | file_put_contents($srcPath, $pluginFileAfter); |
||
332 | View Code Duplication | if (is_file($srcPath)) { |
|
333 | $fsList['file'][$srcPath] = true; |
||
334 | } else { |
||
335 | $fsList['file'][$srcPath] = false; |
||
336 | } |
||
337 | |||
338 | // Controller |
||
339 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/Controller.php'); |
||
340 | $from = '/\[code\]/'; |
||
341 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
342 | $from = '/\[author\]/'; |
||
343 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
344 | $from = '/\[year\]/'; |
||
345 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
346 | $from = '/\[code_name\]/'; |
||
347 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
348 | |||
349 | $srcPath = $codePath.'/Controller/'.$code.'Controller.php'; |
||
350 | file_put_contents($srcPath, $pluginFileAfter); |
||
351 | View Code Duplication | if (is_file($srcPath)) { |
|
352 | $fsList['file'][$srcPath] = true; |
||
353 | } else { |
||
354 | $fsList['file'][$srcPath] = false; |
||
355 | } |
||
356 | |||
357 | // Form |
||
358 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ConfigType.php'); |
||
359 | $from = '/\[code\]/'; |
||
360 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
361 | $from = '/\[author\]/'; |
||
362 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
363 | $from = '/\[year\]/'; |
||
364 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
365 | $from = '/\[code_name\]/'; |
||
366 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter); |
||
367 | |||
368 | $file->mkdir($codePath.'/Form/Type'); |
||
369 | View Code Duplication | if (is_dir($codePath.'/Form/Type')) { |
|
370 | $fsList['dir'][$codePath.'/Form/Type'] = true; |
||
371 | } else { |
||
372 | $fsList['dir'][$codePath.'/Form/Type'] = false; |
||
373 | } |
||
374 | |||
375 | $srcPath = $codePath.'/Form/Type/'.$code.'ConfigType.php'; |
||
376 | file_put_contents($codePath.'/Form/Type/'.$code.'ConfigType.php', $pluginFileAfter); |
||
377 | View Code Duplication | if (is_file($srcPath)) { |
|
378 | $fsList['file'][$srcPath] = true; |
||
379 | } else { |
||
380 | $fsList['file'][$srcPath] = false; |
||
381 | } |
||
382 | |||
383 | // Twig |
||
384 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/config.twig'); |
||
385 | $from = '/\[code\]/'; |
||
386 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
387 | |||
388 | $file->mkdir($codePath.'/Resource/template/admin'); |
||
389 | View Code Duplication | if (is_dir($codePath.'/Resource/template/admin')) { |
|
390 | $fsList['dir'][$codePath.'/Resource/template/admin'] = true; |
||
391 | } else { |
||
392 | $fsList['dir'][$codePath.'/Resource/template/admin'] = false; |
||
393 | } |
||
394 | |||
395 | $srcPath = $codePath.'/Resource/template/admin/config.twig'; |
||
396 | file_put_contents($srcPath, $pluginFileAfter); |
||
397 | View Code Duplication | if (is_file($srcPath)) { |
|
398 | $fsList['file'][$srcPath] = true; |
||
399 | } else { |
||
400 | $fsList['file'][$srcPath] = false; |
||
401 | } |
||
402 | |||
403 | // index.twig |
||
404 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/index.twig'); |
||
405 | $from = '/\[code\]/'; |
||
406 | $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileBefore); |
||
407 | |||
408 | $file->mkdir($codePath.'/Resource/template/admin'); |
||
409 | View Code Duplication | if (is_dir($codePath.'/Resource/template/admin')) { |
|
410 | $fsList['dir'][$codePath.'/Resource/template/admin'] = true; |
||
411 | } else { |
||
412 | $fsList['dir'][$codePath.'/Resource/template/admin'] = false; |
||
413 | } |
||
414 | |||
415 | $srcPath = $codePath.'/Resource/template/index.twig'; |
||
416 | file_put_contents($srcPath, $pluginFileAfter); |
||
417 | View Code Duplication | if (is_file($srcPath)) { |
|
418 | $fsList['file'][$srcPath] = true; |
||
419 | } else { |
||
420 | $fsList['file'][$srcPath] = false; |
||
421 | } |
||
422 | |||
423 | $onFunctions = array(); |
||
424 | $eventKeys = array(); |
||
425 | $onEvents = array(); |
||
426 | |||
427 | // イベント |
||
428 | $events = $paramList['events']['value']; |
||
429 | if (count($events) > 0) { |
||
430 | foreach ($events as $eventKey => $eventConst) { |
||
431 | $onEvents[$eventKey] = array(array('on'.$eventConst.', NORMAL')); |
||
432 | $onFunctions[$eventKey] = 'on'.$eventConst; |
||
433 | $eventKeys[] = $eventKey; |
||
434 | } |
||
435 | } |
||
436 | |||
437 | // フックポイント |
||
438 | $hookPoints = $paramList['hookPoints']['value']; |
||
439 | if (count($hookPoints)) { |
||
440 | foreach ($hookPoints as $hookKey => $hookConst) { |
||
441 | $onName = 'on'.join(array_map('ucfirst', explode('_', strtolower($hookConst)))); |
||
442 | $onEvents[$hookKey] = array(array($onName.', NORMAL')); |
||
443 | $onFunctions[$hookKey] = $onName; |
||
444 | } |
||
445 | } |
||
446 | |||
447 | if (count($onEvents)) { |
||
448 | $srcPath = $codePath.'/event.yml'; |
||
449 | file_put_contents($srcPath, str_replace('\'', '', Yaml::dump($onEvents))); |
||
450 | View Code Duplication | if (is_file($srcPath)) { |
|
451 | $fsList['file'][$srcPath] = true; |
||
452 | } else { |
||
453 | $fsList['file'][$srcPath] = false; |
||
454 | } |
||
455 | |||
456 | $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/Event.php'); |
||
457 | |||
458 | // Event |
||
459 | $from = '/\[code\]/'; |
||
460 | $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore); |
||
461 | $from = '/\[author\]/'; |
||
462 | $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter); |
||
463 | $from = '/\[year\]/'; |
||
464 | $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter); |
||
465 | |||
466 | $functions = ''; |
||
467 | $args = include $this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/eventArguments.php'; |
||
468 | foreach ($onFunctions as $key => $name) { |
||
469 | if (in_array($key, $eventKeys)) { |
||
470 | // 共通イベントは引数の型を利用するイベントにより変更 |
||
471 | $ext = pathinfo($key, PATHINFO_EXTENSION); |
||
472 | if (array_key_exists($ext, $args)) { |
||
473 | $functions .= " /**\n * @param {$args[$ext]} \$event\n */\n public function {$name}({$args[$ext]} \$event)\n {\n }\n\n"; |
||
474 | } else { |
||
475 | // 旧イベントの場合、引数は「eccube.event.render」のみ可能 |
||
476 | if (preg_match("/^eccube.event.render\./", $key)) { |
||
477 | $functions .= " /**\n * @param {$args['eccube.event.render']} \$event\n */\n public function {$name}({$args['eccube.event.render']} \$event)\n {\n }\n\n"; |
||
478 | } else { |
||
479 | $functions .= " /**\n *\n */\n public function {$name}()\n {\n }\n\n"; |
||
480 | } |
||
481 | } |
||
482 | } else { |
||
483 | // HookPointイベントの引数はEventArgs共通 |
||
484 | $functions .= " /**\n * @param EventArgs \$event\n */\n public function {$name}(EventArgs \$event)\n {\n }\n\n"; |
||
485 | } |
||
486 | } |
||
487 | $from = '/\[hookpoint_function\]/'; |
||
488 | $pluginFileAfter = preg_replace($from, $functions, $pluginFileAfter); |
||
489 | $srcPath = $codePath.'/'.$code.'Event.php'; |
||
490 | file_put_contents($srcPath, $pluginFileAfter); |
||
491 | View Code Duplication | if (is_file($srcPath)) { |
|
492 | $fsList['file'][$srcPath] = true; |
||
493 | } else { |
||
494 | $fsList['file'][$srcPath] = false; |
||
495 | } |
||
496 | } |
||
497 | |||
498 | // LICENSE |
||
499 | $srcPath = $codePath.'/LICENSE'; |
||
500 | $file->copy($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/LICENSE', $srcPath); |
||
501 | View Code Duplication | if (is_file($srcPath)) { |
|
502 | $fsList['file'][$srcPath] = true; |
||
503 | } else { |
||
504 | $fsList['file'][$srcPath] = false; |
||
505 | } |
||
506 | |||
507 | $this->completeMessage($fsList); |
||
508 | |||
509 | } |
||
510 | |||
559 |