Completed
Pull Request — master (#1088)
by
unknown
02:55
created

Command::after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Generators;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Stub;
7
use Nwidart\Modules\Commands\GeneratorCommand;
8
use Nwidart\Modules\Console\Traits\Definitions;
9
use Nwidart\Modules\Support\Config\GenerateConfigReader;
10
11
abstract class Command extends GeneratorCommand
12
{
13
    use Definitions;
14
15
    /**
16
     * The name to be appended to the generated resources.
17
     *
18
     * @var null|string
19
     */
20
    protected $appendable;
21
22
    /**
23
     * Stub file of the resource
24
     *
25
     * @var null|string
26
     */
27
    protected $stubFile;
28
29
    /**
30
     * Getter for appendable
31
     *
32
     * @return void
33
     */
34
    public function appendable()
35
    {
36
        return $this->appendable;
37
    }
38
39
    /**
40
     * Getter for the stub file
41
     *
42
     * @return void
43
     */
44
    public function stubFile()
45
    {
46
        return $this->stubFile;
47
    }
48
49
    /**
50
     * Replacements for the stub file
51
     *
52
     * @return array
53
     */
54
    public function replaces()
55
    {
56
        return [
57
            //...
58
        ];
59
    }
60
61
    /**
62
     * Get stub file contents
63
     *
64
     * @return mixed
65
     */
66
    protected function getTemplateContents()
67
    {
68
        return (new Stub($this->stubFile(), $this->replaces()))->render();
69
    }
70
71
    /**
72
     * Get destination file path.
73
     *
74
     * @return mixed
75
     */
76
    public function getDestinationFilePath()
77
    {
78
        return  $this->getModulePath() . "/" .
0 ignored issues
show
Documentation Bug introduced by
The method getModulePath does not exist on object<Nwidart\Modules\Generators\Command>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
            GenerateConfigReader::read($this->getGeneratorConfigKey())->getPath() . "/" .
0 ignored issues
show
Documentation Bug introduced by
The method getGeneratorConfigKey does not exist on object<Nwidart\Modules\Generators\Command>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
            $this->resolveFilename() . '.' . $this->outputExtension;
0 ignored issues
show
Bug introduced by
The property outputExtension does not seem to exist. Did you mean output?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation Bug introduced by
The method resolveFilename does not exist on object<Nwidart\Modules\Generators\Command>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
    }
82
83
    /**
84
     * Method to apply necessary functionality
85
     * before console command gets executed
86
     *
87
     * @return void
88
     */
89
    public function before()
90
    {
91
        // ...
92
    }
93
94
    /**
95
     * Method to apply necessary functionality
96
     * after console command has executed.
97
     *
98
     * @return void
99
     */
100
    public function after()
101
    {
102
        // ...
103
    }
104
105
    /**
106
     * Execute the console command
107
     *
108
     * @return int
109
     */
110
    public function handle(): int
111
    {
112
        $this->before();
113
114
        if (parent::handle() != E_ERROR) {
115
            $this->after();
116
        }
117
        return 0;
118
    }
119
120
    /**
121
     * Get and resolve the filename.
122
     *
123
     * @return string
124
     */
125
    protected function getFileName(): string
126
    {
127
128
        $name = Str::studly($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; 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...
129
        if ($this->appendable() && !Str::contains(strtolower($name), strtolower($this->appendable()))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->appendable() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
130
            $name .= Str::studly($this->appendable());
131
        }
132
133
        return Str::singular(Str::studly($name));
134
    }
135
}
136