Skip to content

Commit cd5ba0f

Browse files
author
Given Ncube
committed
refactor: extracted duplicated method to a trait
1 parent 81fde00 commit cd5ba0f

File tree

4 files changed

+75
-90
lines changed

4 files changed

+75
-90
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 4
5+
level: 5
66
paths:
77
- src
88
- config

src/Commands/GeneratePermissionsCommand.php

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
namespace FlixtechsLabs\LaravelAuthorizer\Commands;
44

5+
use FlixtechsLabs\LaravelAuthorizer\Commands\Traits\LocatesModels;
56
use Illuminate\Console\Command;
6-
use Illuminate\Database\Eloquent\Model;
7-
use Illuminate\Support\Collection;
8-
use Illuminate\Support\Facades\File;
97
use Illuminate\Support\Str;
10-
use ReflectionClass;
11-
use ReflectionException;
128
use Spatie\Permission\Models\Permission;
13-
use SplFileInfo;
149

1510
class GeneratePermissionsCommand extends Command
1611
{
12+
use LocatesModels;
13+
1714
/**
1815
* The name and signature of the console command.
1916
*
@@ -58,7 +55,7 @@ public function handle(): int
5855
protected function generatePermissionsForAllModels(): void
5956
{
6057
$this->getModels()->each(
61-
fn(string $model) => $this->generatePermissions($model)
58+
fn (string $model) => $this->generatePermissions($model)
6259
);
6360
}
6461

@@ -73,7 +70,7 @@ public function generatePermissions(string $model): void
7370
$permissions = config('authorizer.permissions');
7471

7572
collect($permissions)->each(
76-
fn(string $permission) => $this->generatePermission(
73+
fn (string $permission) => $this->generatePermission(
7774
$model,
7875
$permission
7976
)
@@ -83,8 +80,8 @@ public function generatePermissions(string $model): void
8380
/**
8481
* Generate a permission for a given model.
8582
*
86-
* @param string $model
87-
* @param string $permission
83+
* @param string $model
84+
* @param string $permission
8885
* @return mixed
8986
*/
9087
public function generatePermission(string $model, string $permission): mixed
@@ -94,45 +91,14 @@ public function generatePermission(string $model, string $permission): mixed
9491
Str::contains($permission, 'all')
9592
) {
9693
return Permission::updateOrCreate([
97-
'name' =>
98-
$permission .
99-
' ' .
94+
'name' => $permission.
95+
' '.
10096
Str::snake(Str::plural(Str::lower($model))),
10197
]);
10298
}
10399

104100
return Permission::updateOrCreate([
105-
'name' => $permission . ' ' . Str::snake(Str::lower($model)),
101+
'name' => $permission.' '.Str::snake(Str::lower($model)),
106102
]);
107103
}
108-
109-
/**
110-
* Get all models.
111-
*
112-
* @return array|Collection
113-
*/
114-
public function getModels(): array|Collection
115-
{
116-
return collect(File::allFiles(app_path()))
117-
->map(function (SplFileInfo $info) {
118-
$path = $info->getRelativePathname();
119-
120-
return sprintf(
121-
'\%s%s',
122-
app()->getNamespace(),
123-
Str::replace('/', '\\', Str::beforeLast($path, '.'))
124-
);
125-
})
126-
->filter(function (string $class) {
127-
try {
128-
$reflection = new ReflectionClass($class);
129-
} catch (ReflectionException $throwable) {
130-
return false;
131-
}
132-
133-
return $reflection->isSubclassOf(Model::class) &&
134-
!$reflection->isAbstract();
135-
})
136-
->map(fn($model) => Str::afterLast($model, '\\'));
137-
}
138104
}

src/Commands/LaravelAuthorizerCommand.php

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
namespace FlixtechsLabs\LaravelAuthorizer\Commands;
44

5+
use FlixtechsLabs\LaravelAuthorizer\Commands\Traits\LocatesModels;
56
use Illuminate\Console\Command;
6-
use Illuminate\Database\Eloquent\Model;
7-
use Illuminate\Support\Facades\File;
87
use Illuminate\Support\Str;
9-
use ReflectionClass;
10-
use ReflectionException;
11-
use SplFileInfo;
128
use Symfony\Component\Filesystem\Filesystem;
139

1410
class LaravelAuthorizerCommand extends Command
1511
{
12+
use LocatesModels;
13+
1614
/**
1715
* The name and signature of the console command.
1816
*
@@ -67,31 +65,9 @@ public function handle(): int
6765
*/
6866
public function generateAllPolicies(): void
6967
{
70-
$models = collect(File::allFiles(app_path()))
71-
->map(function (SplFileInfo $info) {
72-
$path = $info->getRelativePathname();
73-
74-
return sprintf(
75-
'\%s%s',
76-
app()->getNamespace(),
77-
Str::replace('/', '\\', Str::beforeLast($path, '.'))
78-
);
79-
})
80-
->filter(function (string $class) {
81-
try {
82-
$reflection = new ReflectionClass($class);
83-
} catch (ReflectionException $throwable) {
84-
return false;
85-
}
86-
87-
return $reflection->isSubclassOf(Model::class) &&
88-
!$reflection->isAbstract();
89-
})
90-
->map(fn($model) => Str::afterLast($model, '\\'));
91-
92-
$models->each(function (string $model) {
93-
$this->generatePolicy($model, $model);
94-
});
68+
$this->getModels()->each(
69+
fn (string $model) => $this->generatePolicy($model, $model)
70+
);
9571
}
9672

9773
/**
@@ -104,7 +80,7 @@ public function generatePlainPolicy(string $name): void
10480
{
10581
if (
10682
file_exists($this->getPolicyPath($name)) &&
107-
!$this->option('force')
83+
! $this->option('force')
10884
) {
10985
$this->error(sprintf('Policy "%s" already exists!', $name));
11086

@@ -116,8 +92,8 @@ public function generatePlainPolicy(string $name): void
11692
'namespace' => $this->getNamespace(),
11793
'class' => $this->getClassName($name),
11894
])->reduce(
119-
fn($carry, $value, $key) => Str::replace(
120-
'{{ ' . $key . ' }}',
95+
fn ($carry, $value, $key) => Str::replace(
96+
'{{ '.$key.' }}',
12197
$value,
12298
$carry
12399
)
@@ -137,7 +113,7 @@ private function generatePolicy(string $name, string $model): void
137113
{
138114
if (
139115
file_exists($this->getPolicyPath($name)) &&
140-
!$this->option('force')
116+
! $this->option('force')
141117
) {
142118
$this->error(sprintf('Policy "%s" already exists!', $name));
143119

@@ -147,8 +123,7 @@ private function generatePolicy(string $name, string $model): void
147123
$compiled = collect([
148124
'name' => $name,
149125
'model' => $model,
150-
'modelVariable' =>
151-
strtolower($model) ===
126+
'modelVariable' => strtolower($model) ===
152127
strtolower(
153128
Str::afterLast($this->getNamespacedUserModel(), '\\')
154129
)
@@ -162,8 +137,8 @@ private function generatePolicy(string $name, string $model): void
162137
'namespacedUserModel' => $this->getNamespacedUserModel(),
163138
'user' => Str::afterLast($this->getNamespacedUserModel(), '\\'),
164139
])->reduce(
165-
static fn($old, $value, $key) => Str::replace(
166-
'{{ ' . $key . ' }}',
140+
static fn ($old, $value, $key) => Str::replace(
141+
'{{ '.$key.' }}',
167142
$value,
168143
$old
169144
),
@@ -176,22 +151,22 @@ private function generatePolicy(string $name, string $model): void
176151
/**
177152
* Get the path to the policy.
178153
*
179-
* @param string $name
154+
* @param string $name
180155
* @return string
181156
*/
182157
public function getPolicyPath(string $name): string
183158
{
184-
return app_path('Policies/' . $this->getClassName($name) . '.php');
159+
return app_path('Policies/'.$this->getClassName($name).'.php');
185160
}
186161

187162
/**
188-
* Get the policies namespace.
163+
* Get the policies' namespace.
189164
*
190165
* @return string
191166
*/
192167
public function getNamespace(): string
193168
{
194-
return app()->getNamespace() . 'Policies';
169+
return app()->getNamespace().'Policies';
195170
}
196171

197172
/**
@@ -206,7 +181,7 @@ public function getClassName(string $name): string
206181
return Str::studly($name);
207182
}
208183

209-
return Str::studly($name) . 'Policy';
184+
return Str::studly($name).'Policy';
210185
}
211186

212187
/**
@@ -217,7 +192,7 @@ public function getClassName(string $name): string
217192
*/
218193
public function getNamespacedModel(string $model): string
219194
{
220-
return app()->getNamespace() . 'Models\\' . Str::studly($model);
195+
return app()->getNamespace().'Models\\'.Str::studly($model);
221196
}
222197

223198
/**
@@ -237,6 +212,6 @@ public function getNamespacedUserModel(): string
237212
*/
238213
public function getStub(): string
239214
{
240-
return __DIR__ . '/stubs/policy.stub';
215+
return __DIR__.'/stubs/policy.stub';
241216
}
242217
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace FlixtechsLabs\LaravelAuthorizer\Commands\Traits;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Str;
9+
use ReflectionClass;
10+
use ReflectionException;
11+
use SplFileInfo;
12+
13+
trait LocatesModels
14+
{
15+
/**
16+
* Get all models.
17+
*
18+
* @return Collection
19+
*/
20+
protected function getModels(): Collection
21+
{
22+
return collect(File::allFiles(app_path()))
23+
->map(function (SplFileInfo $info) {
24+
$path = $info->getRelativePathname();
25+
26+
return sprintf(
27+
'\%s%s',
28+
app()->getNamespace(),
29+
Str::replace('/', '\\', Str::beforeLast($path, '.'))
30+
);
31+
})
32+
->filter(function (string $class) {
33+
try {
34+
$reflection = new ReflectionClass($class);
35+
} catch (ReflectionException $throwable) {
36+
return false;
37+
}
38+
39+
return $reflection->isSubclassOf(Model::class) &&
40+
! $reflection->isAbstract();
41+
})
42+
->map(fn ($model) => Str::afterLast($model, '\\'));
43+
}
44+
}

0 commit comments

Comments
 (0)