CreateCommand::execute()   F
last analyzed

Complexity

Conditions 14
Paths 8192

Size

Total Lines 84
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 84
rs 2
cc 14
eloc 48
nc 8192
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace N98\Magento\Command\Developer\Module;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
use N98\Magento\Command\SubCommand\ConfigBag;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Create a magento module skeleton
14
 */
15
class CreateCommand extends AbstractMagentoCommand
16
{
17
    protected function configure()
18
    {
19
        $this
20
            ->setName('dev:module:create')
21
            ->addArgument('vendorNamespace', InputArgument::REQUIRED, 'Namespace (your company prefix)')
22
            ->addArgument('moduleName', InputArgument::REQUIRED, 'Name of your module.')
23
            ->addOption('minimal', 'm', InputOption::VALUE_NONE, 'Create only module file')
24
            ->addOption('add-blocks', null, InputOption::VALUE_NONE, 'Adds blocks')
25
            ->addOption('add-helpers', null, InputOption::VALUE_NONE, 'Adds helpers')
26
            ->addOption('add-models', null, InputOption::VALUE_NONE, 'Adds models')
27
            ->addOption('add-setup', null, InputOption::VALUE_NONE, 'Adds SQL setup')
28
            ->addOption('add-all', null, InputOption::VALUE_NONE, 'Adds blocks, helpers and models')
29
            ->addOption('enable', 'e', InputOption::VALUE_NONE, 'Enable module after creation')
30
            ->addOption('modman', null, InputOption::VALUE_NONE, 'Create all files in folder with a modman file.')
31
            ->addOption('add-readme', null, InputOption::VALUE_NONE, 'Adds a readme.md file to generated module')
32
            ->addOption('add-composer', null, InputOption::VALUE_NONE, 'Adds a composer.json file to generated module')
33
            ->addOption('author-name', null, InputOption::VALUE_OPTIONAL, 'Author for readme.md or composer.json')
34
            ->addOption('author-email', null, InputOption::VALUE_OPTIONAL, 'Author for readme.md or composer.json')
35
            ->addOption('description', null, InputOption::VALUE_OPTIONAL, 'Description for readme.md or composer.json')
36
            ->setDescription('Create and register a new magento module.');
37
    }
38
39
    /**
40
     * @param \Symfony\Component\Console\Input\InputInterface $input
41
     * @param \Symfony\Component\Console\Output\OutputInterface $output
42
     * @return int|void
43
     * @throws \InvalidArgumentException
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $subCommandFactory = $this->createSubCommandFactory(
48
            $input,
49
            $output,
50
            'N98\Magento\Command\Developer\Module\Create\SubCommand' // sub-command namespace
51
        );
52
53
        $configBag = $subCommandFactory->getConfig();
54
55
        if (!$input->getOption('modman')) {
56
            $this->detectMagento($output);
57
        }
58
59
        $configBag->setBool('isModmanMode', $input->getOption('modman'));
60
        $configBag->setString('magentoRootFolder', $this->_magentoRootFolder);
0 ignored issues
show
Documentation introduced by
$this->_magentoRootFolder is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
62
        $this->initConfigBagDefaultValues($configBag);
63
64
        if ($input->getOption('add-all')) {
65
            $configBag->setBool('shouldAddBlocks', true);
66
            $configBag->setBool('shouldAddHelpers', true);
67
            $configBag->setBool('shouldAddModels', true);
68
            $configBag->setBool('shouldAddSetup', true);
69
        }
70
71
        if ($input->getOption('add-blocks')) {
72
            $configBag->setBool('shouldAddBlocks', true);
73
        }
74
75
        if ($input->getOption('add-helpers')) {
76
            $configBag->setBool('shouldAddHelpers', true);
77
        }
78
79
        if ($input->getOption('add-models')) {
80
            $configBag->setBool('shouldAddModels', true);
81
        }
82
83
        if ($input->getOption('add-setup')) {
84
            $configBag->setBool('shouldAddSetup', true);
85
        }
86
87
        if ($input->getOption('enable')) {
88
            $configBag->setBool('shouldEnableModule', true);
89
        }
90
91
        $configBag->setString('baseFolder', __DIR__ . '/../../../../../../res/module/create');
0 ignored issues
show
Documentation introduced by
__DIR__ . '/../../../../../../res/module/create' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
        $configBag->setString('vendorNamespace', ucfirst($input->getArgument('vendorNamespace')));
0 ignored issues
show
Documentation introduced by
ucfirst($input->getArgument('vendorNamespace')) is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
        $configBag->setString('moduleName', ucfirst($input->getArgument('moduleName')));
0 ignored issues
show
Documentation introduced by
ucfirst($input->getArgument('moduleName')) is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
95
        $this->initView($input, $configBag);
96
97
        $subCommandFactory->create('CreateModuleFolders')->execute();
98
        $subCommandFactory->create('CreateModuleRegistrationFiles')->execute();
99
100
        if (!$input->getOption('minimal')) {
101
            $subCommandFactory->create('CreateModuleConfigFile')->execute();
102
            $subCommandFactory->create('CreateModuleDiFile')->execute();
103
            $subCommandFactory->create('CreateModuleEventsFile')->execute();
104
            $subCommandFactory->create('CreateModuleCrontabFile')->execute();
105
        }
106
107
        $subCommandFactory->create('EnableModule')->execute();
108
109
        if ($input->getOption('add-readme')) {
110
            $subCommandFactory->create('CreateReadmeFile')->execute();
111
        }
112
113
        if ($input->getOption('modman')) {
114
            $subCommandFactory->create('CreateModmanFile')->execute();
115
        }
116
117
        if ($input->getOption('add-composer')) {
118
            $subCommandFactory->create('CreateComposerFile')->execute();
119
        }
120
121
        if ($input->getOption('add-setup')) {
122
            $subCommandFactory->create('CreateSetupFiles')->execute();
123
        }
124
125
        if (!$input->getOption('minimal')) {
126
            $subCommandFactory->create('CreateAdditionalFiles')->execute();
127
        }
128
    }
129
130
    private function initView(InputInterface $input, ConfigBag $configBag)
131
    {
132
        $configBag->setArray('twigVars', array(
133
            'vendorNamespace' => $configBag->getString('vendorNamespace'),
134
            'moduleName'      => $configBag->getString('moduleName'),
135
            'createBlocks'    => $configBag->getBool('shouldAddBlocks'),
136
            'createModels'    => $configBag->getBool('shouldAddModels'),
137
            'createHelpers'   => $configBag->getBool('shouldAddHelpers'),
138
            'createSetup'     => $configBag->getBool('shouldAddSetup'),
139
            'authorName'      => $input->getOption('author-name'),
140
            'authorEmail'     => $input->getOption('author-email'),
141
            'description'     => $input->getOption('description'),
142
        ));
143
    }
144
145
    /**
146
     * @param ConfigBag $configBag
147
     */
148
    private function initConfigBagDefaultValues($configBag)
149
    {
150
        $configBag->setBool('shouldAddBlocks', false);
151
        $configBag->setBool('shouldAddHelpers', false);
152
        $configBag->setBool('shouldAddModels', false);
153
        $configBag->setBool('shouldAddSetup', false);
154
        $configBag->setBool('shouldEnableModule', false);
155
    }
156
}
157