Completed
Pull Request — master (#354)
by Martin
02:25
created

PolicyMakeCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 85
loc 85
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 4 4 1
A getArguments() 7 7 1
A getTemplateContents() 9 9 1
A getDestinationFilePath() 8 8 1
A getFileName() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Stub;
7
use Nwidart\Modules\Traits\ModuleCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
10 View Code Duplication
class PolicyMakeCommand extends GeneratorCommand
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
13
    use ModuleCommandTrait;
14
15
    /**
16
     * The name of argument name.
17
     *
18
     * @var string
19
     */
20
    protected $argumentName = 'name';
21
22
    /**
23
     * The console command name.
24
     *
25
     * @var string
26
     */
27
    protected $name = 'module:make-policy';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new policy class for the specified module.';
35
36
37
    /**
38
     * @return string
39
     */
40 1
    public function getDefaultNamespace()
41
    {
42 1
        return 'Policies';
43
    }
44
45
46
    /**
47
     * Get the console command arguments.
48
     *
49
     * @return array
50
     */
51 60
    protected function getArguments()
52
    {
53
        return [
54 60
            ['name', InputArgument::REQUIRED, 'The name of the policy class.'],
55
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
56
        ];
57
    }
58
59
60
    /**
61
     * @return mixed
62
     */
63 1
    protected function getTemplateContents()
64
    {
65 1
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
66
67 1
        return (new Stub('/policy.plain.stub', [
68 1
            'NAMESPACE' => $this->getClassNamespace($module),
69 1
            'CLASS'     => $this->getClass(),
70 1
        ]))->render();
71
    }
72
73
74
    /**
75
     * @return mixed
76
     */
77 1
    protected function getDestinationFilePath()
78
    {
79 1
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
80
81 1
        $policyPath = $this->laravel['modules']->config('paths.generator.policies');
82
83 1
        return $path.$policyPath.'/'.$this->getFileName().'.php';
84
    }
85
86
87
    /**
88
     * @return string
89
     */
90 1
    private function getFileName()
91
    {
92 1
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
93
    }
94
}
95