Conditions | 1 |
Paths | 1 |
Total Lines | 278 |
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 |
||
330 | protected function createConfigController($pluginDir, $code) |
||
331 | { |
||
332 | $snakecased = Container::underscore($code); |
||
333 | |||
334 | $source = <<<EOL |
||
335 | <?php |
||
336 | |||
337 | namespace Plugin\\${code}\\Controller\\Admin; |
||
338 | |||
339 | use Eccube\\Controller\\AbstractController; |
||
340 | use Plugin\\${code}\\Form\\Type\\Admin\\ConfigType; |
||
341 | use Plugin\\${code}\\Repository\\ConfigRepository; |
||
342 | use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template; |
||
343 | use Symfony\\Component\\HttpFoundation\\Request; |
||
344 | use Symfony\\Component\\Routing\\Annotation\\Route; |
||
345 | |||
346 | class ConfigController extends AbstractController |
||
347 | { |
||
348 | /** |
||
349 | * @var ConfigRepository |
||
350 | */ |
||
351 | protected \$configRepository; |
||
352 | |||
353 | /** |
||
354 | * ConfigController constructor. |
||
355 | * |
||
356 | * @param ConfigRepository \$configRepository |
||
357 | */ |
||
358 | public function __construct(ConfigRepository \$configRepository) |
||
359 | { |
||
360 | \$this->configRepository = \$configRepository; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @Route("/%eccube_admin_route%/${snakecased}/config", name="${snakecased}_admin_config") |
||
365 | * @Template("@${code}/admin/config.twig") |
||
366 | */ |
||
367 | public function index(Request \$request) |
||
368 | { |
||
369 | \$Config = \$this->configRepository->get(); |
||
370 | \$form = \$this->createForm(ConfigType::class, \$Config); |
||
371 | \$form->handleRequest(\$request); |
||
372 | |||
373 | if (\$form->isSubmitted() && \$form->isValid()) { |
||
374 | \$Config = \$form->getData(); |
||
375 | \$this->entityManager->persist(\$Config); |
||
376 | \$this->entityManager->flush(\$Config); |
||
377 | \$this->addSuccess('登録しました。', 'admin'); |
||
378 | |||
379 | return \$this->redirectToRoute('${snakecased}_admin_config'); |
||
380 | } |
||
381 | |||
382 | return [ |
||
383 | 'form' => \$form->createView(), |
||
384 | ]; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | EOL; |
||
389 | |||
390 | $this->fs->dumpFile($pluginDir.'/Controller/Admin/ConfigController.php', $source); |
||
391 | |||
392 | $source = <<<EOL |
||
393 | <?php |
||
394 | |||
395 | namespace Plugin\\${code}\\Entity; |
||
396 | |||
397 | use Doctrine\\ORM\\Mapping as ORM; |
||
398 | |||
399 | /** |
||
400 | * Config |
||
401 | * |
||
402 | * @ORM\Table(name="plg_${snakecased}_config") |
||
403 | * @ORM\Entity(repositoryClass="Plugin\\${code}\\Repository\\ConfigRepository") |
||
404 | */ |
||
405 | class Config |
||
406 | { |
||
407 | /** |
||
408 | * @var int |
||
409 | * |
||
410 | * @ORM\Column(name="id", type="integer", options={"unsigned":true}) |
||
411 | * @ORM\Id |
||
412 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
413 | */ |
||
414 | private \$id; |
||
415 | |||
416 | /** |
||
417 | * @var string |
||
418 | * |
||
419 | * @ORM\Column(name="name", type="string", length=255) |
||
420 | */ |
||
421 | private \$name; |
||
422 | |||
423 | /** |
||
424 | * @return int |
||
425 | */ |
||
426 | public function getId() |
||
427 | { |
||
428 | return \$this->id; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @return string |
||
433 | */ |
||
434 | public function getName() |
||
435 | { |
||
436 | return \$this->name; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @param string \$name |
||
441 | * |
||
442 | * @return \$this; |
||
443 | */ |
||
444 | public function setName(\$name) |
||
445 | { |
||
446 | \$this->name = \$name; |
||
447 | |||
448 | return \$this; |
||
449 | } |
||
450 | } |
||
451 | |||
452 | EOL; |
||
453 | |||
454 | $this->fs->dumpFile($pluginDir.'/Entity/Config.php', $source); |
||
455 | |||
456 | $source = <<<EOL |
||
457 | <?php |
||
458 | |||
459 | namespace Plugin\\${code}\\Repository; |
||
460 | |||
461 | use Eccube\\Repository\\AbstractRepository; |
||
462 | use Plugin\\${code}\\Entity\\Config; |
||
463 | use Symfony\\Bridge\\Doctrine\\RegistryInterface; |
||
464 | |||
465 | /** |
||
466 | * ConfigRepository |
||
467 | * |
||
468 | * This class was generated by the Doctrine ORM. Add your own custom |
||
469 | * repository methods below. |
||
470 | */ |
||
471 | class ConfigRepository extends AbstractRepository |
||
472 | { |
||
473 | /** |
||
474 | * ConfigRepository constructor. |
||
475 | * |
||
476 | * @param RegistryInterface \$registry |
||
477 | */ |
||
478 | public function __construct(RegistryInterface \$registry) |
||
479 | { |
||
480 | parent::__construct(\$registry, Config::class); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @param int \$id |
||
485 | * |
||
486 | * @return null|Config |
||
487 | */ |
||
488 | public function get(\$id = 1) |
||
489 | { |
||
490 | return \$this->find(\$id); |
||
491 | } |
||
492 | } |
||
493 | |||
494 | EOL; |
||
495 | |||
496 | $this->fs->dumpFile($pluginDir.'/Repository/ConfigRepository.php', $source); |
||
497 | |||
498 | $source = <<<EOL |
||
499 | <?php |
||
500 | |||
501 | namespace Plugin\\${code}\\Form\\Type\\Admin; |
||
502 | |||
503 | use Plugin\\${code}\\Entity\\Config; |
||
504 | use Symfony\\Component\\Form\\AbstractType; |
||
505 | use Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType; |
||
506 | use Symfony\\Component\\Form\\FormBuilderInterface; |
||
507 | use Symfony\\Component\\OptionsResolver\\OptionsResolver; |
||
508 | use Symfony\Component\Validator\Constraints\Length; |
||
509 | use Symfony\Component\Validator\Constraints\NotBlank; |
||
510 | |||
511 | class ConfigType extends AbstractType |
||
512 | { |
||
513 | /** |
||
514 | * {@inheritdoc} |
||
515 | */ |
||
516 | public function buildForm(FormBuilderInterface \$builder, array \$options) |
||
517 | { |
||
518 | \$builder->add('name', TextType::class, [ |
||
519 | 'constraints' => [ |
||
520 | new NotBlank(), |
||
521 | new Length(['max' => 255]), |
||
522 | ], |
||
523 | ]); |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * {@inheritdoc} |
||
528 | */ |
||
529 | public function configureOptions(OptionsResolver \$resolver) |
||
530 | { |
||
531 | \$resolver->setDefaults([ |
||
532 | 'data_class' => Config::class, |
||
533 | ]); |
||
534 | } |
||
535 | } |
||
536 | |||
537 | EOL; |
||
538 | |||
539 | $this->fs->dumpFile($pluginDir.'/Form/Type/Admin/ConfigType.php', $source); |
||
540 | |||
541 | $source = <<<EOL |
||
542 | {% extends '@admin/default_frame.twig' %} |
||
543 | |||
544 | {% set menus = ['store', 'plugin', 'plugin_list'] %} |
||
545 | |||
546 | {% block title %}${code}{% endblock %} |
||
547 | {% block sub_title %}プラグイン一覧{% endblock %} |
||
548 | |||
549 | {% form_theme form '@admin/Form/bootstrap_4_horizontal_layout.html.twig' %} |
||
550 | |||
551 | {% block stylesheet %}{% endblock stylesheet %} |
||
552 | |||
553 | {% block javascript %}{% endblock javascript %} |
||
554 | |||
555 | {% block main %} |
||
556 | <form role="form" method="post"> |
||
557 | |||
558 | {{ form_widget(form._token) }} |
||
559 | |||
560 | <div class="c-contentsArea__cols"> |
||
561 | <div class="c-contentsArea__primaryCol"> |
||
562 | <div class="c-primaryCol"> |
||
563 | <div class="card rounded border-0 mb-4"> |
||
564 | <div class="card-header"><span>設定</span></div> |
||
565 | <div class="card-body"> |
||
566 | <div class="row"> |
||
567 | <div class="col-3"><span>名前</span><span |
||
568 | class="badge badge-primary ml-1">必須</span></div> |
||
569 | <div class="col mb-2"> |
||
570 | {{ form_widget(form.name) }} |
||
571 | {{ form_errors(form.name) }} |
||
572 | </div> |
||
573 | </div> |
||
574 | </div> |
||
575 | </div> |
||
576 | </div> |
||
577 | </div> |
||
578 | </div> |
||
579 | <div class="c-conversionArea"> |
||
580 | <div class="c-conversionArea__container"> |
||
581 | <div class="row justify-content-between align-items-center"> |
||
582 | <div class="col-6"> |
||
583 | <div class="c-conversionArea__leftBlockItem"> |
||
584 | <a class="c-baseLink" |
||
585 | href="{{ url('admin_store_plugin') }}"> |
||
586 | <i class="fa fa-backward" aria-hidden="true"></i> |
||
587 | <span>プラグイン一覧</span> |
||
588 | </a> |
||
589 | </div> |
||
590 | </div> |
||
591 | <div class="col-6"> |
||
592 | <div class="row align-items-center justify-content-end"> |
||
593 | <div class="col-auto"> |
||
594 | <button class="btn btn-ec-conversion px-5" |
||
595 | type="submit">登録</button> |
||
596 | </div> |
||
597 | </div> |
||
598 | </div> |
||
599 | </div> |
||
600 | </div> |
||
601 | </div> |
||
602 | </form> |
||
603 | {% endblock %} |
||
604 | |||
605 | EOL; |
||
606 | $this->fs->dumpFile($pluginDir.'/Resource/template/admin/config.twig', $source); |
||
607 | } |
||
608 | } |
||
609 |
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.