Skip to content

Commit d0cc813

Browse files
committed
refactor: remove useless from generator
1 parent f3fa391 commit d0cc813

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

src/Generators/NovaTestGenerator.php

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ public function generate(): void
4141
);
4242
}
4343

44-
$this->isNovaResourceExists();
44+
$resource = $this->resourceName ?? $this->getNovaResource();
45+
46+
$this->shortNovaResourceName = Str::afterLast($resource, '\\');
47+
48+
$this->fullNovaResourcePath = "App\\Nova\\{$resource}";
49+
50+
if (!class_exists($this->fullNovaResourcePath)) {
51+
$this->throwFailureException(
52+
ClassNotExistsException::class,
53+
"Cannot create Nova{$this->shortNovaResourceName}Test cause {$this->resourceName} Nova resource does not exist.",
54+
"Create {$this->resourceName} Nova resource."
55+
);
56+
}
4557

4658
if ($this->classExists('nova', "Nova{$this->shortNovaResourceName}Test")) {
4759
$this->throwFailureException(
@@ -132,78 +144,59 @@ protected function isFixtureNeeded($type): bool
132144
return true;
133145
}
134146

135-
protected function isNovaResourceExists(): void
147+
protected function getNovaResource(): string
136148
{
137-
$resource = $this->getNovaResource();
149+
$commonResources = $this->getCommonNovaResources();
138150

139-
$this->shortNovaResourceName = Str::afterLast($resource, '\\');
140-
$this->fullNovaResourcePath = "App\\Nova\\{$resource}";
151+
if (count($commonResources) > 1) {
152+
$commonResources = implode(', ', $commonResources);
141153

142-
if (!class_exists($this->fullNovaResourcePath)) {
143154
$this->throwFailureException(
144-
ClassNotExistsException::class,
145-
"Cannot create Nova{$this->shortNovaResourceName}Test cause {$this->resourceName} Nova resource does not exist.",
146-
"Create {$this->resourceName} Nova resource."
155+
EntityCreateException::class,
156+
"Cannot create Nova{$this->model}Resource Test cause was found a lot of suitable resources: $commonResources",
157+
"Please, use --resource-name option"
147158
);
148159
}
149-
}
150-
151-
protected function getNovaResource(): ?string
152-
{
153-
if (!empty($this->resourceName)) {
154-
return $this->resourceName;
155-
} else {
156-
$commonResources = $this->getCommonNovaResources();
157-
158-
if (count($commonResources) > 1) {
159-
$commonResources = implode(', ', $commonResources);
160160

161-
$this->throwFailureException(
162-
EntityCreateException::class,
163-
"Cannot create Nova{$this->model}Resource Test cause was found a lot of suitable resources: $commonResources",
164-
"Please, use --resource-name option"
165-
);
166-
}
167-
168-
return $commonResources[0];
169-
}
161+
return array_pop($commonResources);
170162
}
171163

172-
protected function allCommonNovaResources(): Generator
164+
protected function getNovaFiles(): Generator
173165
{
174166
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->novaPath));
175167

176168
foreach ($iterator as $file) {
177169
if ($file->isFile() && $file->getExtension() === 'php') {
178-
$relativePath = Str::after($file->getPathname(), $this->novaPath . DIRECTORY_SEPARATOR);
179-
180-
$class = str_replace(['/', '.php'], ['\\', ''], $relativePath);
181-
182-
if ($this->isValidCommonResourceCheck($class)) {
183-
yield $class;
184-
}
170+
yield $file;
185171
}
186172
}
187173
}
188174

189175
protected function getCommonNovaResources(): array
190176
{
191-
$commonNovaResources = $this->allCommonNovaResources();
192-
193177
$resources = [];
194178

195-
foreach ($commonNovaResources as $resource) {
196-
$resources[] = $resource;
179+
foreach ($this->getNovaFiles() as $file) {
180+
$relativePath = Str::after($file->getPathname(), $this->novaPath . DIRECTORY_SEPARATOR);
181+
182+
$class = str_replace(['/', '.php'], ['\\', ''], $relativePath);
183+
184+
if ($this->isNovaResource($class) && $this->isResourceNameContainModel($class)) {
185+
$resources[] = $class;
186+
}
197187
}
198188

199189
return $resources;
200190
}
201191

202-
protected function isValidCommonResourceCheck(string $resource)
192+
protected function isNovaResource(string $resource): bool
203193
{
204-
$isContainModel = Str::afterLast(str_replace('Resource', '', $resource), '\\') === $this->model;
194+
return is_subclass_of("App\\Nova\\{$resource}", "Laravel\\Nova\\Resource");
195+
}
205196

206-
return is_subclass_of("App\\Nova\\{$resource}", "Laravel\\Nova\\Resource") && $isContainModel;
197+
protected function isResourceNameContainModel(string $resource): bool
198+
{
199+
return Str::afterLast(str_replace('Resource', '', $resource), '\\') === $this->model;
207200
}
208201

209202
protected function collectFilters(): array

0 commit comments

Comments
 (0)