MagicTestCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 34
c 1
b 0
f 0
dl 0
loc 140
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A alreadyExists() 0 4 2
A getPath() 0 6 1
A buildClass() 0 23 2
A resolveStubPath() 0 5 2
A getStub() 0 3 1
A getDefaultNamespace() 0 3 1
A getOptions() 0 6 1
1
<?php
2
3
namespace Ikechukwukalu\Magicmake\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Input\InputOption;
9
10
#[AsCommand(name: 'magic:test')]
11
class MagicTestCommand extends GeneratorCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'magic:test';
19
20
    /**
21
     * The name of the console command.
22
     *
23
     * This name is used to identify the command during lazy loading.
24
     *
25
     * @var string|null
26
     *
27
     * @deprecated
28
     */
29
    protected static $defaultName = 'magic:test';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create a new magic test class';
37
38
    /**
39
     * The type of class being generated.
40
     *
41
     * @var string
42
     */
43
    protected $type = 'Test';
44
45
    /**
46
     * Build the class with the given name.
47
     *
48
     * @param  string  $name
49
     * @return string
50
     */
51
    protected function buildClass($name)
52
    {
53
        if ($this->alreadyExists(str_replace('.php', '', $this->getPath($name)))) {
54
            $this->components->error("This Test already exists");
55
            return;
56
        }
57
58
        $ary = explode("\\", $name);
59
        $model = $ary[count($ary) - 1];
60
        $modelVariable = $this->option('variable');
61
        $modelUnderScore = $this->option('underscore');
62
        $name = "{$name}Test";
63
64
        $stub = str_replace(
65
            ['DummyModel', '{{ model }}'], class_basename($model), parent::buildClass($name)
66
        );
67
68
        $stub = str_replace(
69
            ['DummyModelVariable', '{{ modelVariable }}'], trim($modelVariable, '\\'), $stub
70
        );
71
72
        return str_replace(
73
            ['DummyModelUnderScore', '{{ modelUnderScore }}'], trim($modelUnderScore, '\\'), $stub
74
        );
75
76
    }
77
78
    /**
79
     * Determine if the class already exists.
80
     *
81
     * @param  string  $rawName
82
     * @return bool
83
     */
84
    protected function alreadyExists($rawName)
85
    {
86
        return class_exists($rawName) ||
87
               $this->files->exists($this->getPath($this->qualifyClass($rawName)));
88
    }
89
90
    /**
91
     * Get the stub file for the generator.
92
     *
93
     * @return string
94
     */
95
    protected function getStub()
96
    {
97
        return __DIR__.'/stubs/test.stub';
98
    }
99
100
    /**
101
     * Resolve the fully-qualified path to the stub.
102
     *
103
     * @param  string  $stub
104
     * @return string
105
     */
106
    protected function resolveStubPath($stub)
107
    {
108
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
109
                        ? $customPath
110
                        : __DIR__.$stub;
111
    }
112
113
    /**
114
     * Get the default namespace for the class.
115
     *
116
     * @param  string  $rootNamespace
117
     * @return string
118
     */
119
    protected function getDefaultNamespace($rootNamespace)
120
    {
121
        return $rootNamespace.'\Tests';
122
    }
123
124
    /**
125
     * Get the console command options.
126
     *
127
     * @return array
128
     */
129
    protected function getOptions()
130
    {
131
        return [
132
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the test already exists'],
133
            ['variable', 'var', InputOption::VALUE_REQUIRED, 'Create a model variable value for this test class'],
134
            ['underscore', 'u', InputOption::VALUE_REQUIRED, 'Create a model underscore value for this test class'],
135
        ];
136
    }
137
138
    /**
139
     * Get the destination class path.
140
     *
141
     * @param  string  $name
142
     * @return string
143
     */
144
    protected function getPath($name)
145
    {
146
        $ary = explode("\\", $name);
147
        $name = $ary[count($ary) - 1];
148
149
        return base_path("tests/Feature/{$name}Test.php");
150
    }
151
}
152