Names::initCommonNames()   C
last analyzed

Complexity

Conditions 9
Paths 128

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 6.825
c 0
b 0
f 0
cc 9
nc 128
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators\Traits;
2
3
use Savannabits\JetstreamInertiaGenerator\Generators\Controller;
4
use Savannabits\JetstreamInertiaGenerator\Generators\Model;
5
use Illuminate\Support\Str;
6
7
trait Names
8
{
9
10
    public $tableName;
11
    public $modelBaseName;
12
    public $modelFullName;
13
    public $modelPlural;
14
    public $modelVariableName;
15
    public $modelRouteAndViewName;
16
    public $modelNamespace;
17
    public $modelWithNamespaceFromDefault;
18
    public $modelViewsDirectory;
19
    public $modelDotNotation;
20
    public $modelJSName;
21
    public $modelLangFormat;
22
    public $resource;
23
    public $exportBaseName;
24
    public $titleSingular;
25
    public $titlePlural;
26
27
    public $controllerWithNamespaceFromDefault;
28
29
    protected function initCommonNames(
30
        $tableName,
31
        $modelName = null,
32
        $controllerName = null,
33
        $modelWithFullNamespace = null
34
    ) {
35
        $this->tableName = $tableName;
36
37
        if ($this instanceof Model) {
0 ignored issues
show
Bug introduced by
The class Savannabits\JetstreamIne...erator\Generators\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
38
            $modelGenerator = $this;
39
        } else {
40
            $modelGenerator = app(Model::class);
41
            $modelGenerator->setLaravel($this->laravel);
0 ignored issues
show
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...
42
        }
43
44
        if (is_null($modelName)) {
45
            $modelName = $modelGenerator->generateClassNameFromTable($this->tableName);
46
        }
47
        $this->modelFullName = $modelGenerator->qualifyClass($modelName);
48
49
        $this->modelBaseName = class_basename($modelName);
50
        $this->modelPlural = Str::plural(class_basename($modelName));
51
        $this->modelVariableName = lcfirst(Str::singular(class_basename($this->modelBaseName)));
52
        $this->modelRouteAndViewName = Str::plural(Str::lower(Str::kebab($this->modelBaseName)));
53
        $this->modelNamespace = Str::replaceLast("\\" . $this->modelBaseName, '', $this->modelFullName);
54
        if (!Str::startsWith($this->modelFullName,
55
            $startsWith = trim($modelGenerator->rootNamespace(), '\\') . '\Models\\')) {
56
            $this->modelWithNamespaceFromDefault = $this->modelBaseName;
57
        } else {
58
            $this->modelWithNamespaceFromDefault = Str::replaceFirst($startsWith, '', $this->modelFullName);
59
        }
60
        $this->modelViewsDirectory = Str::plural(Str::lower(Str::kebab(implode('/',
61
            collect(explode('\\', $this->modelWithNamespaceFromDefault))->map(function ($part) {
62
                return lcfirst($part);
63
            })->toArray()))));
64
65
        $parts = collect(explode('\\', $this->modelWithNamespaceFromDefault));
66
        $parts->pop();
67
        $parts->push($this->modelPlural);
68
        $this->resource = Str::lower(Str::kebab(implode('', $parts->toArray())));
69
70
        $this->modelDotNotation = str_replace('/', '.', $this->modelViewsDirectory);
71
        $this->modelJSName = Str::plural(str_replace('/', '-', $this->modelViewsDirectory));
72
        $this->modelLangFormat = str_replace('/', '_', $this->modelViewsDirectory);
73
74
        if ($this instanceof Controller) {
0 ignored issues
show
Bug introduced by
The class Savannabits\JetstreamIne...r\Generators\Controller does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
75
            $controllerGenerator = $this;
76
        } else {
77
            $controllerGenerator = app(Controller::class);
78
            $controllerGenerator->setLaravel($this->laravel);
79
        }
80
81
        if (is_null($controllerName)) {
82
            $controllerName = $controllerGenerator->generateClassNameFromTable($this->tableName);
83
        }
84
85
        $controllerFullName = $controllerGenerator->qualifyClass($controllerName);
86
        if (!Str::startsWith($controllerFullName,
87
            $startsWith = trim($controllerGenerator->rootNamespace(), '\\') . '\Http\\Controllers\\Admin\\')
88
            && !Str::startsWith($controllerFullName,
89
                $startsWith = trim($controllerGenerator->rootNamespace(), '\\') . '\Http\\Controllers\\Api\\' )) {
90
            $this->controllerWithNamespaceFromDefault = $controllerFullName;
91
        } else {
92
            $this->controllerWithNamespaceFromDefault = Str::replaceFirst($startsWith, '', $controllerFullName);
93
        }
94
95
        if (!empty($modelWithFullNamespace)) {
96
            $this->modelFullName = $modelWithFullNamespace;
97
        }
98
        $this->exportBaseName = Str::studly($tableName) . 'Export';
99
100
        $this->titleSingular = Str::singular(str_replace(['_'], ' ', Str::title($this->tableName)));
101
        $this->titlePlural = str_replace(['_'], ' ', Str::title($this->tableName));
102
    }
103
104
    public function valueWithoutId($string)
105
    {
106
        if (Str::endsWith(Str::lower($string), '_id')) {
107
            $string = Str::substr($string, 0, -3);
108
        }
109
110
        return Str::ucfirst(str_replace('_', ' ', $string));
111
    }
112
113
}
114