Skip to content

Commit 56efba7

Browse files
committed
feat: throw exception when folder has incorrect case
refs: #100
1 parent 69604d3 commit 56efba7

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Exceptions;
4+
5+
use Exception;
6+
7+
class IncorrectClassPathException extends Exception
8+
{
9+
}

src/Generators/EntityGenerator.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Str;
1010
use RonasIT\Support\Events\WarningEvent;
1111
use RonasIT\Support\Exceptions\ClassNotExistsException;
12+
use RonasIT\Support\Exceptions\IncorrectClassPathException;
1213
use Throwable;
1314
use ReflectionMethod;
1415
use ReflectionClass;
@@ -93,9 +94,15 @@ protected function getOrCreateNamespace(string $configPath): string
9394
array_pop($pathParts);
9495
}
9596

96-
$namespace = Arr::map($pathParts, fn (string $part, int $key) => (
97-
$configPath !== 'models' || $key === array_key_first($pathParts)
98-
) ? ucfirst($part) : $part);
97+
foreach ($pathParts as $part) {
98+
if (!$this->isFolderHasCorrectCase($part, $configPath)) {
99+
throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR.");
100+
}
101+
}
102+
103+
$namespace = array_map(function (string $part) {
104+
return ucfirst($part);
105+
}, $pathParts);
99106

100107
$fullPath = base_path($path);
101108

@@ -106,6 +113,22 @@ protected function getOrCreateNamespace(string $configPath): string
106113
return implode('\\', $namespace);
107114
}
108115

116+
protected function isFolderHasCorrectCase(string $folder, string $configPath): bool
117+
{
118+
$lowerCaseDirectoriesMap = [
119+
'migrations' => 'database/migrations',
120+
'factories' => 'database/factories',
121+
'seeders' => 'database/seeders',
122+
'database_seeder' => 'database/seeders',
123+
'tests' => 'tests',
124+
'routes' => 'routes',
125+
];
126+
127+
$directory = Arr::get($lowerCaseDirectoriesMap, $configPath);
128+
129+
return $folder === 'app' || preg_match('/^[A-Z]/', $folder) || Str::contains($directory, $folder);
130+
}
131+
109132
abstract public function generate(): void;
110133

111134
protected function classExists($path, $name): bool

0 commit comments

Comments
 (0)