CreateViewCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 3

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 35
cts 35
cp 1
crap 3
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 9
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 9
        $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 9
        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 9
        $validator = new ViewValidator($name);
44
45 9
        if ($validator->fails()) {
46 3
            $message = $validator->getMessage();
47
48 3
            return $output->writeln('<error>' . $message . '</error>');
49
        }
50
51
        $data = [
52 9
            'isBootstrap' => $input->getOption('bootstrap'),
53 9
            'isCamel' => $input->getOption('camel'),
54 9
            'name' => $input->getArgument('name')
55 6
        ];
56
57 9
        $generator = new ViewGenerator($this->describe, $data);
58
59 9
        $result = $generator->generate();
60
61
        $results = [
62 9
            'create' => $this->renderer->render('Views/create.tpl', $result),
63 9
            'edit' => $this->renderer->render('Views/edit.tpl', $result),
64 9
            'index' => $this->renderer->render('Views/index.tpl', $result),
65 9
            'show' => $this->renderer->render('Views/show.tpl', $result)
66 6
        ];
67
68 9
        $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 9
        $create = new File($filePath . '/create.php');
71 9
        $edit = new File($filePath . '/edit.php');
72 9
        $index = new File($filePath . '/index.php');
73 9
        $show = new File($filePath . '/show.php');
74
75 9
        $create->putContents($results['create']);
76 9
        $edit->putContents($results['edit']);
77 9
        $index->putContents($results['index']);
78 9
        $show->putContents($results['show']);
79
80 9
        $create->close();
81 9
        $edit->close();
82 9
        $index->close();
83 9
        $show->close();
84
85 9
        $message = 'The views folder "' . $name . '" has been created successfully!';
86
87 9
        return $output->writeln('<info>' . $message . '</info>');
88
    }
89
}
90