Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

GeneratorBundle/Command/GenerateLayoutCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Command;
4
5
use Kunstmaan\GeneratorBundle\Generator\LayoutGenerator;
6
use Symfony\Component\Console\Input\InputOption;
7
8
/**
9
 * Generates de default layout
10
 */
11
class GenerateLayoutCommand extends KunstmaanGenerateCommand
12
{
13
    /**
14
     * @var BundleInterface
15
     */
16
    private $bundle;
17
18
    private $browserSyncUrl;
19
20
    /**
21
     * @see Command
22
     */
23
    protected function configure()
24
    {
25
        $this->setDescription('Generates a basic layout')
26
            ->setHelp(<<<EOT
27
The <info>kuma:generate:layout</info> command generates a basic website layout.
28
29
<info>php bin/console kuma:generate:layout</info>
30
31
Use the <info>--namespace</info> option to indicate for which bundle you want to create the layout
32
33
<info>php bin/console kuma:generate:layout --namespace=Namespace/NamedBundle</info>
34
EOT
35
            )
36
            ->addOption('namespace', '', InputOption::VALUE_OPTIONAL, 'The namespace of the bundle where we need to create the layout in')
37
            ->addOption('subcommand', '', InputOption::VALUE_OPTIONAL, 'Whether the command is called from an other command or not')
38
            ->addOption('demosite', '', InputOption::VALUE_NONE, 'Pass this parameter when the demosite styles/javascipt should be generated')
39
            ->addOption('browsersync', '', InputOption::VALUE_OPTIONAL, 'The URI that will be used for browsersync to connect')
40
            ->setName('kuma:generate:layout');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function getWelcomeText()
47
    {
48
        if (!$this->isSubCommand()) {
49
            return 'Welcome to the Kunstmaan layout generator';
50
        } else {
51
            return null;
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function doExecute()
59
    {
60
        if (!$this->isSubCommand()) {
61
            $this->assistant->writeSection('Layout generation');
62
        }
63
64
        $rootDir = $this->getApplication()->getKernel()->getRootDir().'/../';
0 ignored issues
show
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...
65
        $this->createGenerator()->generate($this->bundle, $rootDir, $this->assistant->getOption('demosite'), $this->browserSyncUrl);
66
67
        if (!$this->isSubCommand()) {
68
            $this->assistant->writeSection('Layout successfully created', 'bg=green;fg=black');
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function doInteract()
76
    {
77
        if (!$this->isSubCommand()) {
78
            $this->assistant->writeLine(array("This command helps you to generate a basic layout for your website.\n"));
79
        }
80
81
        /**
82
         * Ask for which bundle we need to create the layout
83
         */
84
        $bundleNamespace = $this->assistant->getOptionOrDefault('namespace', null);
85
        $this->bundle = $this->askForBundleName('layout', $bundleNamespace);
86
        $this->browserSyncUrl = $this->assistant->getOptionOrDefault('browsersync', null);
87
88
        if (null === $this->browserSyncUrl) {
89
            $this->browserSyncUrl = $this->assistant->ask('Which URL would you like to configure for browserSync?', 'http://myproject.dev');
90
        }
91
    }
92
93
    /**
94
     * Get the generator.
95
     *
96
     * @return LayoutGenerator
97
     */
98
    protected function createGenerator()
99
    {
100
        $filesystem = $this->getContainer()->get('filesystem');
101
        $registry = $this->getContainer()->get('doctrine');
102
103
        return new LayoutGenerator($filesystem, $registry, '/layout', $this->assistant);
104
    }
105
106
    /**
107
     * Check that the command is ran as sub command or not.
108
     *
109
     * @return bool
110
     */
111
    private function isSubCommand()
112
    {
113
        return $this->assistant->getOptionOrDefault('subcommand', false);
114
    }
115
}
116