Passed
Push — master ( 53bb62...283d0c )
by Rougin
08:28
created

CreateViewCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.944

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 33
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 53
ccs 14
cts 35
cp 0.4
crap 4.944
rs 9.392

How to fix   Long Method   

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 Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Rougin\Combustor\Common\File;
9
use Rougin\Combustor\Common\Tools;
10
use Rougin\Combustor\Generator\ViewGenerator;
11
use Rougin\Combustor\Validator\ViewValidator;
12
13
/**
14
 * Create View Command
15
 *
16
 * Creates a list of views for CodeIgniter
17
 *
18
 * @package Combustor
19
 * @author  Rougin Gutib <[email protected]>
20
 */
21
class CreateViewCommand extends AbstractCommand
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $command = 'view';
27
28
    /**
29
     * Executes the command.
30
     *
31
     * @param \Symfony\Component\Console\Input\InputInterface   $input
32
     * @param \Symfony\Component\Console\Output\OutputInterface $output
33
     * @return object|\Symfony\Component\Console\Output\OutputInterface
34
     */
35 6
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 6
        $name = Tools::stripTableSchema(plural($input->getArgument('name')));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('name') can also be of type string[]; however, parameter $str of plural() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $name = Tools::stripTableSchema(plural(/** @scrutinizer ignore-type */ $input->getArgument('name')));
Loading history...
38
39 6
        if ($input->getOption('keep')) {
40 3
            $name = Tools::stripTableSchema($input->getArgument('name'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('name') can also be of type string[]; however, parameter $table of Rougin\Combustor\Common\Tools::stripTableSchema() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            $name = Tools::stripTableSchema(/** @scrutinizer ignore-type */ $input->getArgument('name'));
Loading history...
41 2
        }
42
43 6
        $validator = new ViewValidator($name);
44
45 6
        if ($validator->fails()) {
46
            $message = $validator->getMessage();
47
48
            return $output->writeln('<error>' . $message . '</error>');
49
        }
50
51
        $data = [
52 6
            'isBootstrap' => $input->getOption('bootstrap'),
53 6
            'isCamel' => $input->getOption('camel'),
54 6
            'name' => $input->getArgument('name')
55 4
        ];
56
57 6
        $generator = new ViewGenerator($this->describe, $data);
58
59 6
        $result = $generator->generate();
60
61
        $results = [
62 6
            'create' => $this->renderer->render('Views/create.tpl', $result),
63
            'edit' => $this->renderer->render('Views/edit.tpl', $result),
64
            'index' => $this->renderer->render('Views/index.tpl', $result),
65
            'show' => $this->renderer->render('Views/show.tpl', $result)
66
        ];
67
68
        $filePath = APPPATH . 'views/' . $name;
0 ignored issues
show
Bug introduced by
The constant Rougin\Combustor\Commands\APPPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
70
        $create = new File($filePath . '/create.php');
71
        $edit = new File($filePath . '/edit.php');
72
        $index = new File($filePath . '/index.php');
73
        $show = new File($filePath . '/show.php');
74
75
        $create->putContents($results['create']);
76
        $edit->putContents($results['edit']);
77
        $index->putContents($results['index']);
78
        $show->putContents($results['show']);
79
80
        $create->close();
81
        $edit->close();
82
        $index->close();
83
        $show->close();
84
85
        $message = 'The views folder "' . $name . '" has been created successfully!';
86
87
        return $output->writeln('<info>' . $message . '</info>');
88
    }
89
}
90