Passed
Pull Request — 2.x (#729)
by Antonio Carlos
05:57
created

HasCapsules::makeCapsule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 47
nc 1
nop 2
dl 0
loc 77
rs 9.1563
c 1
b 0
f 0

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
2
3
namespace A17\Twill\Services\Capsules;
4
5
use Illuminate\Support\Str;
6
7
trait HasCapsules
8
{
9
    protected function getAutoloader()
10
    {
11
        return app()->bound('autoloader')
12
            ? app('autoloader')
13
            : require base_path('vendor/autoload.php');
14
    }
15
16
    public function getCapsuleList()
17
    {
18
        $path = config('twill.capsules.path');
19
20
        $list = collect(config('twill.capsules.list'));
21
22
        if (config('twill.capsules.loaded')) {
23
            return $list;
24
        }
25
26
        return $list
27
            ->where('enabled', true)
28
            ->map(function ($capsule) use ($path) {
29
                return $this->makeCapsule($capsule, $path);
30
            });
31
    }
32
33
    public function getCapsuleByModel($model)
34
    {
35
        return $this->getCapsuleList()
36
            ->where('singular', $model)
37
            ->first();
38
    }
39
40
    public function getCapsuleByModule($module)
41
    {
42
        return $this->getCapsuleList()
43
            ->filter(function ($capsule) use ($module) {
44
                return Str::lower($capsule['plural']) == Str::lower($module);
45
            })
46
            ->first();
47
    }
48
49
    public function makeCapsule($capsule, $basePath)
50
    {
51
        $capsule['name'] = Str::studly($capsule['name']);
52
53
        $capsule['module'] = Str::camel($capsule['name']);
54
55
        $capsule['plural'] = $name = $capsule['name'];
56
57
        $capsule['singular'] = $singular =
58
            $capsule['singular'] ?? Str::singular($name);
59
60
        $twillNamespace = config('twill.namespace');
0 ignored issues
show
Unused Code introduced by
The assignment to $twillNamespace is dead and can be removed.
Loading history...
61
62
        $capsule[
63
            'namespace'
64
        ] = $capsuleNamespace = $this->getManager()->capsuleNamespace(
0 ignored issues
show
Bug introduced by
The method capsuleNamespace() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        ] = $capsuleNamespace = $this->getManager()->/** @scrutinizer ignore-call */ capsuleNamespace(

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...
65
            $capsule['name']
66
        );
67
68
        $capsule['model'] = $capsule['models'] = $models =
69
            "{$capsuleNamespace}\\" .
70
            config('twill.capsules.namespaces.models');
71
        $capsule['repositories'] = $repositories =
72
            "{$capsuleNamespace}\\" .
73
            config('twill.capsules.namespaces.repositories');
74
        $capsule['controllers'] = $controllers =
75
            "{$capsuleNamespace}\\" .
76
            config('twill.capsules.namespaces.controllers');
77
        $capsule['requests'] = $requests =
78
            "{$capsuleNamespace}\\" .
79
            config('twill.capsules.namespaces.requests');
80
81
        $capsule['psr4_path'] = "$basePath/{$name}/app";
82
83
        $capsule['root_path'] = $root = $this->capsuleRootPath($capsule);
0 ignored issues
show
Unused Code introduced by
The assignment to $root is dead and can be removed.
Loading history...
84
85
        $capsule[
86
            'migrations_dir'
87
        ] = "{$capsule['root_path']}/database/migrations";
88
89
        $capsule['views_dir'] = "{$capsule['root_path']}/resources/views";
90
91
        $capsule['view_prefix'] = "{$name}.resources.views.admin";
92
93
        $capsule['routes_file'] = "{$capsule['root_path']}/routes/admin.php";
94
95
        $capsule['model'] = "{$models}\\{$singular}";
96
97
        $capsule['models_dir'] = $this->namespaceToPath($capsule, $models);
98
99
        $capsule['translation'] = "{$models}\\{$singular}Translation";
100
101
        $capsule['slug'] = "{$models}\\{$singular}Slug";
102
103
        $capsule['revision'] = "{$models}\\{$singular}Revision";
104
105
        $capsule['repository'] = "{$repositories}\\{$singular}Repository";
106
107
        $capsule['repositories_dir'] = $this->namespaceToPath(
108
            $capsule,
109
            $repositories
110
        );
111
112
        $capsule['controller'] = "{$controllers}\\{$singular}Controller";
113
114
        $capsule['controllers_dir'] = $this->namespaceToPath(
115
            $capsule,
116
            $controllers
117
        );
118
119
        $capsule['formRequest'] = "{$requests}\\{$singular}Request";
120
121
        $capsule['requests_dir'] = $this->namespaceToPath($capsule, $requests);
122
123
        $this->registerPsr4Autoloader($capsule);
124
125
        return $capsule;
126
    }
127
128
    public function registerPsr4Autoloader($capsule)
129
    {
130
        $this->getAutoloader()->setPsr4(
0 ignored issues
show
Bug introduced by
The method setPsr4() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        $this->getAutoloader()->/** @scrutinizer ignore-call */ setPsr4(

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...
131
            $capsule['namespace'] . '\\',
132
            $capsule['psr4_path']
133
        );
134
    }
135
136
    public function capsuleRootPath($capsule)
137
    {
138
        return config('twill.capsules.path') . '/' . $capsule['name'] ?? null;
139
    }
140
141
    public function getCapsuleRepositoryClass($model)
142
    {
143
        return $this->getCapsuleByModel($model)['repository'] ?? null;
144
    }
145
146
    public function getCapsuleTranslationClass($model)
147
    {
148
        return $this->getCapsuleByModel($model)['translation'] ?? null;
149
    }
150
151
    public function getCapsuleSlugClass($model)
152
    {
153
        return $this->getCapsuleByModel($model)['slug'] ?? null;
154
    }
155
156
    public function getCapsuleRevisionClass($model)
157
    {
158
        return $this->getCapsuleByModel($model)['revision'] ?? null;
159
    }
160
161
    public function getCapsuleFormRequestClass($model)
162
    {
163
        return $this->getCapsuleByModel($model)['formRequest'] ?? null;
164
    }
165
166
    public function getCapsuleViewPrefix($capsule)
167
    {
168
        return $this->getCapsuleByModule(Str::studly($capsule))[
169
            'view_prefix'
170
        ] ?? null;
171
    }
172
173
    public function namespaceToPath($capsule, $namespace)
174
    {
175
        return $this->capsuleNamespaceToPath(
176
            $namespace,
177
            $capsule['namespace'],
178
            $capsule['root_path']
179
        );
180
    }
181
182
    public function capsuleNamespaceToPath(
183
        $namespace,
184
        $capsuleNamespace,
185
        $rootPath
186
    ) {
187
        $namespace = Str::after($namespace, $capsuleNamespace . '\\');
188
189
        $subdir = config('twill.capsules.namespaces.subdir');
190
191
        $subdir = filled($subdir) ? "{$subdir}/" : '';
192
193
        return "{$rootPath}/{$subdir}" . str_replace('\\', '/', $namespace);
194
    }
195
196
    public function getManager()
197
    {
198
        return $this->manager = $this->manager ?? app('twill.capsules.manager');
0 ignored issues
show
Bug Best Practice introduced by
The property manager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
199
    }
200
}
201