Completed
Pull Request — master (#30)
by Anton
17:34
created

GridCommand::verify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3.0261
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Generate;
8
9
use Bluzman\Input\InputArgument;
10
use Bluzman\Input\InputException;
11
use Bluzman\Generator;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * ModelCommand
17
 *
18
 * @package  Bluzman\Command\Generate
19
 */
20
class GridCommand extends AbstractGenerateCommand
21
{
22
    /**
23
     * Command configuration
24
     */
25 14
    protected function configure()
26
    {
27
        $this
28
            // the name of the command (the part after "bin/bluzman")
29 14
            ->setName('generate:grid')
30
            // the short description shown while running "php bin/bluzman list"
31 14
            ->setDescription('Generate a GRID for model')
32
            // the full command description shown when running the command with
33
            // the "--help" option
34 14
            ->setHelp('This command allows you to generate GRID files')
35
        ;
36
37
        $this
38 14
            ->addModelArgument()
39 14
            ->addModuleArgument(InputArgument::OPTIONAL)
40
        ;
41 14
    }
42
43
    /**
44
     * @param InputInterface $input
45
     * @param OutputInterface $output
46
     * @return void
47
     */
48 2
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        try {
51 2
            $this->write("Running <info>generate:grid</info> command");
52
53 2
            $model = $input->getArgument('model');
54 2
            $this->getDefinition()->getArgument('model')->validate($model);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Input\InputArgument as the method validate() does only exist in the following sub-classes of Symfony\Component\Console\Input\InputArgument: Bluzman\Input\InputArgument. 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...
55
56 1
            if (!$this->getApplication()->isModelExists($model)) {
57
                throw new InputException(
58
                    "Model $model is not exist, " .
59
                    "run command <question>bluzman generate:model $model</question> before"
60
                );
61
            }
62
63 1 View Code Duplication
            if ($module = $input->getArgument('module')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64
                $this->getDefinition()->getArgument('module')->validate($module);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Input\InputArgument as the method validate() does only exist in the following sub-classes of Symfony\Component\Console\Input\InputArgument: Bluzman\Input\InputArgument. 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
66
                if (!$this->getApplication()->isModuleExists($module)) {
67
                    throw new InputException(
68
                        "Module $module is not exist, " .
69
                        "run command <question>bluzman generate:module $module</question> before"
70
                    );
71
                }
72
            }
73
74
            // generate directories and files
75 1
            $this->generate($input, $output);
76
77
            // verify it
78 1
            $this->verify($input, $output);
79
80 1
            $this->write("GRID for <info>{$model}</info> has been successfully created.");
81
82 1
            if ($module = $input->getArgument('module')) {
83
                $this->write(
84
                    "Open page <info>/acl</info> in your browser and set permissions for <info>{$module}</info>"
85
                );
86
            }
87 1
        } catch (InputException $e) {
88 1
            $this->error("ERROR: {$e->getMessage()}");
89
        }
90 2
    }
91
92
    /**
93
     * @param InputInterface $input
94
     * @param OutputInterface $output
95
     * @return void
96
     * @throws InputException
97
     */
98 1
    protected function generate(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100 1
        $model = ucfirst($input->getArgument('model'));
101
102
        // generate CRUD
103 1
        $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php';
104
105 1
        if (file_exists($crudFile)) {
106
            $this->comment("Crud file <info>$model/Grid.php</info> already exists");
107
        } else {
108 1
            $template = $this->getTemplate('GridTemplate');
109 1
            $template->setFilePath($crudFile);
110 1
            $template->setTemplateData([
111 1
                'model' => $model
112
            ]);
113
114 1
            $generator = new Generator\Generator($template);
115 1
            $generator->make();
116
        }
117
118 1
        if ($module = $input->getArgument('module')) {
119
            $this->write("Generate <info>$module/controllers/grid.php</info>");
120
121
            $controllerFile = $this->getControllerPath($module, 'grid');
122
            if (file_exists($controllerFile)) {
123
                $this->comment("Controller file <info>$module/grid</info> already exists");
124
            } else {
125
                $template = new Generator\Template\GridControllerTemplate();
126
                $template->setFilePath($controllerFile);
127
                $template->setTemplateData(['model' => $model]);
128
129
                $generator = new Generator\Generator($template);
130
                $generator->make();
131
            }
132
133
            $this->write("Generate <info>$module/views/grid.phtml</info>");
134
135
            $viewFile = $this->getViewPath($module, 'grid');
136
            if (file_exists($viewFile)) {
137
                $this->comment("View file <info>$module/grid</info> already exists");
138
            } else {
139
                $template = new Generator\Template\GridViewTemplate();
140
                $template->setFilePath($viewFile);
141
                $template->setTemplateData(['model' => $model]);
142
143
                $generator = new Generator\Generator($template);
144
                $generator->make();
145
            }
146
        }
147 1
    }
148
149
    /**
150
     * @param InputInterface $input
151
     * @param OutputInterface $output
152
     * @return void
153
     * @throws \Bluzman\Generator\GeneratorException
154
     */
155 1
    public function verify(InputInterface $input, OutputInterface $output)
156
    {
157 1
        $modelPath = $this->getApplication()->getModelPath($input->getArgument('model'));
158
159
        $paths = [
160 1
            $modelPath . DS . 'Grid.php',
161
        ];
162
163 1
        foreach ($paths as $path) {
164 1
            if (!$this->getFs()->exists($path)) {
165
                throw new Generator\GeneratorException("File `$path` is not exists");
166
            }
167
        }
168 1
    }
169
}
170