FormGenCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 44.05 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
c 3
b 1
f 0
lcom 1
cbo 9
dl 37
loc 84
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
A execute() 18 18 1
A createGenerator() 0 4 1
A getGenerator() 0 7 1
A getSkeletonDirs() 19 19 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kpicaza\GenBundle\Command;
4
5
use Kpicaza\GenBundle\Generator\GenFormGenerator;
6
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineFormCommand;
7
use Sensio\Bundle\GeneratorBundle\Command\Validators;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
12
13
/**
14
 * Class FormGenCommand.
15
 */
16
class FormGenCommand extends GenerateDoctrineFormCommand
17
{
18
    /**
19
     * @see Command
20
     */
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('gen:generate:form')
25
            ->setDescription('Generates a form type class based on a Doctrine entity')
26
            ->setDefinition(array(
27
                new InputArgument('entity', InputArgument::REQUIRED, 'The entity class name to initialize (shortcut notation)'),
28
            ))
29
            ->setHelp(<<<EOT
30
The <info>%command.name%</info> command generates a form class based on a Doctrine entity.
31
32
<info>php %command.full_name% AcmeBlogBundle:Post</info>
33
34
Every generated file is based on a template. There are default templates but they can be overriden by placing custom templates in one of the following locations, by order of priority:
35
36
<info>BUNDLE_PATH/Resources/SensioGeneratorBundle/skeleton/form
37
APP_PATH/Resources/SensioGeneratorBundle/skeleton/form</info>
38
39
You can check https://github.com/sensio/SensioGeneratorBundle/tree/master/Resources/skeleton
40
in order to know the file structure of the skeleton
41
EOT
42
            );
43
    }
44
45
    /**
46
     * @see Command
47
     */
48 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
49
    {
50
        $entity = Validators::validateEntityName($input->getArgument('entity'));
51
        list($bundle, $entity) = $this->parseShortcutNotation($entity);
52
53
        $entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle).'\\'.$entity;
54
        $metadata = $this->getEntityMetadata($entityClass);
55
        $bundle = $this->getApplication()->getKernel()->getBundle($bundle);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56
        $generator = $this->getGenerator($bundle);
57
58
        $generator->generate($bundle, $entity, $metadata[0]);
59
60
        $output->writeln(sprintf(
61
            'The new %s.php class file has been created under %s.',
62
            $generator->getClassName(),
63
            $generator->getClassPath()
64
        ));
65
    }
66
67
    protected function createGenerator()
68
    {
69
        return new GenFormGenerator($this->getContainer()->get('filesystem'));
70
    }
71
72
    protected function getGenerator(BundleInterface $bundle = null)
73
    {
74
        $formGenerator = $this->createGenerator();
75
        $formGenerator->setSkeletonDirs($this->getSkeletonDirs($bundle));
76
77
        return $formGenerator;
78
    }
79
80 View Code Duplication
    protected function getSkeletonDirs(BundleInterface $bundle = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
81
    {
82
        $skeletonDirs = parent::getSkeletonDirs($bundle);
83
84
        if (isset($bundle) && is_dir($dir = $bundle->getPath().'/Resources/skeleton')) {
85
            $skeletonDirs[] = $dir;
86
        }
87
88
        if (is_dir($dir = $this->getContainer()->get('kernel')->getRootdir().'/Resources/skeleton')) {
89
            $skeletonDirs[] = $dir;
90
        }
91
92
        $kernel = $this->getApplication()->getKernel();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
93
94
        $skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources/skeleton');
95
        $skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources');
96
97
        return array_reverse($skeletonDirs);
98
    }
99
}
100