Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

GeneratorBundle/Command/GenerateLayoutCommand.php (3 issues)

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()->getProjectDir() . '/';
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
        return 0;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function doInteract()
78
    {
79
        if (!$this->isSubCommand()) {
80
            $this->assistant->writeLine(["This command helps you to generate a basic layout for your website.\n"]);
81
        }
82
83
        /**
84
         * Ask for which bundle we need to create the layout
85
         */
86
        $bundleNamespace = $this->assistant->getOptionOrDefault('namespace', null);
87
        $this->bundle = $this->askForBundleName('layout', $bundleNamespace);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->askForBundleName(...out', $bundleNamespace) of type object<Symfony\Component...Bundle\BundleInterface> is incompatible with the declared type object<Kunstmaan\Generat...ommand\BundleInterface> of property $bundle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
        $this->browserSyncUrl = $this->assistant->getOptionOrDefault('browsersync', null);
89
90
        if (null === $this->browserSyncUrl) {
91
            $this->browserSyncUrl = $this->assistant->ask('Which URL would you like to configure for browserSync?', 'http://myproject.dev');
92
        }
93
    }
94
95
    /**
96
     * Get the generator.
97
     *
98
     * @return LayoutGenerator
99
     */
100 View Code Duplication
    protected function createGenerator()
0 ignored issues
show
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...
101
    {
102
        $filesystem = $this->getContainer()->get('filesystem');
103
        $registry = $this->getContainer()->get('doctrine');
104
105
        return new LayoutGenerator($filesystem, $registry, '/layout', $this->assistant, $this->getContainer());
106
    }
107
108
    /**
109
     * Check that the command is ran as sub command or not.
110
     *
111
     * @return bool
112
     */
113
    private function isSubCommand()
114
    {
115
        return $this->assistant->getOptionOrDefault('subcommand', false);
116
    }
117
}
118