Completed
Push — master ( 7f011d...511c4e )
by recca
04:58
created

RepositoryMakeCommand::getDefaultNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Recca0120\Generator\Console;
4
5
use Illuminate\Support\Str;
6
use InvalidArgumentException;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class RepositoryMakeCommand extends GeneratorCommand
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'g:repository';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Create a new repository class';
24
25
    /**
26
     * The type of class being generated.
27
     *
28
     * @var string
29
     */
30
    protected $type = 'Repository';
31
32
    /**
33
     * Get the stub file for the generator.
34
     *
35
     * @return string
36
     */
37 1
    protected function getStub()
38
    {
39 1
        return __DIR__.'/../../resources/stubs/Repositories/Repository.stub';
40
    }
41
42
    /**
43
     * Get the default namespace for the class.
44
     *
45
     * @param string $rootNamespace
46
     *
47
     * @return string
48
     */
49 1
    protected function getDefaultNamespace($rootNamespace)
50
    {
51 1
        return $rootNamespace.'\Repositories';
52
    }
53
54
    /**
55
     * Build the class with the given name.
56
     *
57
     * @param string $name
58
     *
59
     * @return string
60
     */
61 1
    protected function buildClass($name)
62
    {
63 1
        $modelClass = $this->parseModel(
0 ignored issues
show
Unused Code introduced by
$modelClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64 1
            $this->option('model') ?: $this->rootNamespace().class_basename($name)
1 ignored issue
show
Bug introduced by
It seems like $this->option('model') ?.... class_basename($name) can also be of type array; however, Recca0120\Generator\Cons...keCommand::parseModel() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65 1
        );
66
67 1
        $fullBaseClass = $this->option('extend') ?: 'Recca0120\Repository\EloquentRepository';
68
69 1
        $rootNamespace = trim($this->rootNamespace(), '\\');
70 1
        $namespace = $this->getNamespace($name);
71 1
        $baseClass = ltrim(str_replace($namespace, '', $name), '\\');
72 1
        $repositoryContractInterface = $namespace.'\Contracts\\'.$baseClass.'Repository';
73 1
        $modelClass = $rootNamespace.'\\'.$baseClass;
74
75 1
        if (interface_exists($repositoryContractInterface) === false) {
76 1
            $this->call('g:repository-contract', ['name' => $baseClass]);
77 1
        }
78
79 1
        if (is_null($this->option('without-generator-model')) === true && class_exists($modelClass) === false) {
80 1
            $this->call('g:model', ['name' => $baseClass]);
81 1
        }
82
83 1
        $render = $this->generator->setFullRepositoryClass($name.'Repository')
84 1
            ->setFullBaseClass($fullBaseClass)
1 ignored issue
show
Bug introduced by
It seems like $fullBaseClass defined by $this->option('extend') ...ry\\EloquentRepository' on line 67 can also be of type array; however, Recca0120\Generator\Generator::setFullBaseClass() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85 1
            ->setFullModelClass($modelClass)
86 1
            ->render($this->getStub());
87
88 1
        $this->renderServiceProvider('Providers/AppServiceProvider');
89
90 1
        return $render;
91
    }
92
93
    /**
94
     * renderServiceProvider.
95
     *
96
     * @param string $className
97
     * @return string
98
     */
99 1
    protected function renderServiceProvider($className)
100
    {
101 1
        $file = parent::getPath($className);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getPath() instead of renderServiceProvider()). Are you sure this is correct? If so, you might want to change this to $this->getPath().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
102
103 1
        $this->files->put(
104 1
            $file,
105 1
            $this->generator->renderServiceProvider(
106 1
                $this->files->get($file)
107 1
            )
108 1
        );
109 1
    }
110
111
    /**
112
     * Get the destination class path.
113
     *
114
     * @param string $name
115
     *
116
     * @return string
117
     */
118 1
    protected function getPath($name)
119
    {
120 1
        return str_replace('.php', 'Repository.php', parent::getPath($name));
121
    }
122
123
    /**
124
     * Get the fully-qualified model class name.
125
     *
126
     * @param string $model
127
     *
128
     * @return string
129
     */
130 1
    protected function parseModel($model)
131
    {
132 1
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
133
            throw new InvalidArgumentException('Model name contains invalid characters.');
134
        }
135
136 1
        $model = trim(str_replace('/', '\\', $model), '\\');
137
138 1
        if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) {
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139 1
            $model = $rootNamespace.$model;
140 1
        }
141
142 1
        return $model;
143
    }
144
145
    /**
146
     * Get the console command options.
147
     *
148
     * @return array
149
     */
150 1
    protected function getOptions()
151
    {
152
        return [
153 1
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a repository for the given model.'],
154 1
            ['extend', '', InputOption::VALUE_OPTIONAL, 'repository extend.'],
155 1
            ['without-generator-model', '', InputOption::VALUE_OPTIONAL, 'don\'t generate model.'],
156 1
        ];
157
    }
158
}
159