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

RestCommand::verify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 14
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\InputException;
10
use Bluzman\Generator;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * ModelCommand
16
 *
17
 * @package  Bluzman\Command\Generate
18
 */
19
class RestCommand extends AbstractGenerateCommand
20
{
21
    /**
22
     * Command configuration
23
     */
24 14
    protected function configure()
25
    {
26
        $this
27
            // the name of the command (the part after "bin/bluzman")
28 14
            ->setName('generate:rest')
29
            // the short description shown while running "php bin/bluzman list"
30 14
            ->setDescription('Generate a REST controller')
31
            // the full command description shown when running the command with
32
            // the "--help" option
33 14
            ->setHelp('This command allows you to generate REST controller')
34
        ;
35
36 14
        $this->addModelArgument();
37 14
        $this->addModuleArgument();
38 14
    }
39
40
    /**
41
     * @param InputInterface $input
42
     * @param OutputInterface $output
43
     * @return void
44
     */
45 1
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        try {
48 1
            $this->write('Running <info>generate:rest</info> command');
49
50 1
            $model = $input->getArgument('model');
51 1
            $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...
52
53 1
            if (!$this->getApplication()->isModelExists($model)) {
54
                throw new InputException(
55
                    "Model $model is not exist, " .
56
                    "run command <question>bluzman generate:model $model</question> before"
57
                );
58
            }
59
60 1
            $crudPath = $this->getApplication()->getModelPath($model) . DS . 'Crud.php';
61 1
            if (!is_file($crudPath)) {
62
                throw new InputException(
63
                    "CRUD for $model is not exist, " .
64
                    "run command <question>bluzman generate:crud $model</question> before"
65
                );
66
            }
67
68 1
            $module = $input->getArgument('module');
69 1
            $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...
70
71 1
            if (!$this->getApplication()->isModuleExists($module)) {
72
                throw new InputException(
73
                    "Module $module is not exist, " .
74
                    "run command <question>bluzman generate:module $module</question> before"
75
                );
76
            }
77
78
            // generate directories and files
79 1
            $this->generate($input, $output);
80
81
            // verify it
82 1
            $this->verify($input, $output);
83
84 1
            $this->write("REST for <info>{$model}</info> has been successfully created.");
85 1
            $this->write("Open page <info>/acl</info> in your browser and set permissions for <info>{$module}</info>");
86
        } catch (InputException $e) {
87
            $this->error("ERROR: {$e->getMessage()}");
88
        }
89 1
    }
90
91
    /**
92
     * @param InputInterface $input
93
     * @param OutputInterface $output
94
     * @return void
95
     * @throws InputException
96
     */
97 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...
98
    {
99 1
        $model = ucfirst($input->getArgument('model'));
100 1
        $module = $input->getArgument('module');
101
102 1
        $this->write("Generate <info>$module/controllers/rest.php</info>");
103
104 1
        $controllerFile = $this->getControllerPath($module, 'rest');
105 1
        if (file_exists($controllerFile)) {
106
            $this->comment("Controller file <info>$module/crud</info> already exists");
107
        } else {
108 1
            $template = $this->getTemplate('RestTemplate');
109 1
            $template->setFilePath($controllerFile);
110 1
            $template->setTemplateData(['model' => $model]);
111
112 1
            $generator = new Generator\Generator($template);
113 1
            $generator->make();
114
        }
115 1
    }
116
117
    /**
118
     * @param InputInterface $input
119
     * @param OutputInterface $output
120
     * @return void
121
     * @throws \Bluzman\Generator\GeneratorException
122
     */
123 1 View Code Duplication
    public function verify(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
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...
124
    {
125 1
        $modulePath = $this->getApplication()->getModulePath($input->getArgument('module'));
126
127
        $paths = [
128 1
            $modulePath . DS . 'controllers' . DS . 'rest.php',
129
        ];
130
131 1
        foreach ($paths as $path) {
132 1
            if (!$this->getFs()->exists($path)) {
133
                throw new Generator\GeneratorException("File `$path` is not exists");
134
            }
135
        }
136 1
    }
137
}
138