ConsoleMakeModuleCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 10 2
A rootNamespace() 0 4 1
A moduleName() 0 4 1
A getOptions() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Traits;
6
7
use Symfony\Component\Console\Input\InputOption;
8
9
trait ConsoleMakeModuleCommand
10
{
11
    /**
12
     * The module name.
13
     *
14
     * @var string
15
     */
16
    protected $moduleName;
17
18
    /**
19
     * The root namespace.
20
     *
21
     * @var string
22
     */
23
    protected $rootNamespace;
24
25
    /**
26
     * Get the destination class path.
27
     *
28
     * @param string $name
29
     *
30
     * @throws \Exception
31
     *
32
     * @return string
33
     */
34
    protected function getPath($name): string
35
    {
36
        $name = str_replace_first($this->rootNamespace(), $this->moduleName().DIRECTORY_SEPARATOR.'src', $name);
0 ignored issues
show
Deprecated Code introduced by
The function str_replace_first() has been deprecated with message: Str::replaceFirst() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
37
38
        if (! $this->files->exists($path = $this->laravel['path'].DIRECTORY_SEPARATOR.$this->moduleName())) {
0 ignored issues
show
Bug introduced by
The property files does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property laravel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
            throw new \Exception("Invalid path: {$path}");
40
        }
41
42
        return $this->laravel['path'].DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $name).'.php';
43
    }
44
45
    /**
46
     * Get the root namespace for the class.
47
     *
48
     * @return string
49
     */
50
    protected function rootNamespace(): string
51
    {
52
        return $this->rootNamespace ?? $this->rootNamespace = implode('\\', array_map('ucfirst', explode('/', trim($this->moduleName()))));
53
    }
54
55
    /**
56
     * Get the module name for the class.
57
     *
58
     * @return string
59
     */
60
    protected function moduleName(): string
61
    {
62
        return $this->moduleName ?? $this->input->getOption('module') ?? $this->moduleName = $this->ask('What is your module?');
0 ignored issues
show
Bug introduced by
The property input does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like ask() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
    }
64
65
    /**
66
     * Get the console command options.
67
     *
68
     * @return array
69
     */
70
    protected function getOptions(): array
71
    {
72
        return array_merge(parent::getOptions(), [
73
            ['module', 'd', InputOption::VALUE_REQUIRED, 'The module name to generate the file within.'],
74
        ]);
75
    }
76
}
77