Completed
Branch master (f09254)
by Koldo
03:40 queued 01:22
created

RepositoryGenCommand::createGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kpicaza\GenBundle\Command;
13
14
use Kpicaza\GenBundle\Generator\GenRepositoryGenerator;
15
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCommand;
16
use Sensio\Bundle\GeneratorBundle\Command\Validators;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
23
24
/**
25
 * Generates a form type class for a given Doctrine entity.
26
 *
27
 * @author Fabien Potencier <[email protected]>
28
 * @author Hugo Hamon <[email protected]>
29
 */
30
class RepositoryGenCommand extends GenerateDoctrineCommand
31
{
32
    /**
33
     * @see Command
34
     */
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('gen:generate:repository-pattern')
39
            ->setAliases(array('generate:doctrine:repository-pattern'))
40
            ->setDescription('Generates a repository pattern classes based on a Doctrine entity')
41
            ->setDefinition(array(
42
                new InputArgument('entity', InputArgument::REQUIRED, 'The entity class name to initialize (shortcut notation)'),
43
                new InputOption('overwrite', '', InputOption::VALUE_NONE, 'Overwrite any existing class when generating the Repository pattern contents'),
44
            ))
45
            ->setHelp(<<<EOT
46
The <info>%command.name%</info> command generates a repository pattern classes based on a Doctrine entity.
47
48
<info>php %command.full_name% AcmeBlogBundle:Post</info>
49
50
EOT
51
            );
52
    }
53
54
    /**
55
     * @see Command
56
     */
57 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...
58
    {
59
        $entity = Validators::validateEntityName($input->getArgument('entity'));
60
        list($bundle, $entity) = $this->parseShortcutNotation($entity);
61
62
        $forceOverwrite = $input->getOption('overwrite');
63
64
        $entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle) . '\\' . $entity;
65
        $metadata = $this->getEntityMetadata($entityClass);
66
        $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...
67
        $generator = $this->getGenerator($bundle);
68
69
        $generator->generate($bundle, $entity, $metadata[0], 'yml', $forceOverwrite);
70
71
        $output->writeln(
72
            'The new Repository pattern implementation has been created under Model and Repository folders.'
73
        );
74
    }
75
76
    protected function createGenerator()
77
    {
78
        return new GenRepositoryGenerator(
79
            $this->getContainer()->get('filesystem'),
80
            $this->getContainer()->getParameter('kernel.root_dir')
81
        );
82
    }
83
84
    protected function getGenerator(BundleInterface $bundle = null)
85
    {
86
        $repositoryGenerator = $this->createGenerator();
87
        $repositoryGenerator->setSkeletonDirs($this->getSkeletonDirs($bundle));
88
89
        return $repositoryGenerator;
90
    }
91
92 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...
93
    {
94
        $skeletonDirs = parent::getSkeletonDirs($bundle);
95
96
        if (isset($bundle) && is_dir($dir = $bundle->getPath() . '/Resources/skeleton')) {
97
            $skeletonDirs[] = $dir;
98
        }
99
100
        if (is_dir($dir = $this->getContainer()->get('kernel')->getRootdir() . '/Resources/skeleton')) {
101
            $skeletonDirs[] = $dir;
102
        }
103
104
        $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...
105
106
        $skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources/skeleton');
107
        $skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources');
108
109
        return array_reverse($skeletonDirs);
110
    }
111
}
112