diff --git a/src/Rules/BaseRule.php b/src/Rules/BaseRule.php index 8079c91..5ae4627 100644 --- a/src/Rules/BaseRule.php +++ b/src/Rules/BaseRule.php @@ -10,6 +10,10 @@ class BaseRule { + public static string $rootNamespace = 'App\\'; + + public static string $rootPath = 'app/'; + public static function classSet(): ClassSet { return ClassSet::fromDir(static::directory()); @@ -20,8 +24,23 @@ public static function directoryExists(): bool return File::isDirectory(static::directory()); } + public static function isValid(): bool + { + return static::directoryExists() && filled(static::path()); + } + public static function directory(): string { return CreateApplication::app()->basePath(static::path()); } + + protected static function namespace(): string + { + return static::$rootNamespace.static::$namespace; + } + + public static function path(): string + { + return static::$rootPath.static::$path; + } } diff --git a/src/Rules/Extending/CommandsExtending.php b/src/Rules/Extending/CommandsExtending.php index 1efb25c..a4812ce 100644 --- a/src/Rules/Extending/CommandsExtending.php +++ b/src/Rules/Extending/CommandsExtending.php @@ -13,16 +13,15 @@ class CommandsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Console\Commands'; + + public static string $path = 'Console/Commands'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Console\Commands')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Console\Command')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Console/Commands'; - } } diff --git a/src/Rules/Extending/ControllersExtending.php b/src/Rules/Extending/ControllersExtending.php index c9c4b61..c4c5379 100644 --- a/src/Rules/Extending/ControllersExtending.php +++ b/src/Rules/Extending/ControllersExtending.php @@ -13,17 +13,16 @@ class ControllersExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Http\Controllers'; + + public static string $path = 'Http/Controllers'; + public static function rule(): ArchRule { return Rule::allClasses() ->except('App\Http\Controllers\Controller') - ->that(new ResideInOneOfTheseNamespaces('App\Http\Controllers')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('App\Http\Controllers\Controller')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Http/Controllers'; - } } diff --git a/src/Rules/Extending/ExceptionsExtending.php b/src/Rules/Extending/ExceptionsExtending.php index 1a60e39..1808624 100644 --- a/src/Rules/Extending/ExceptionsExtending.php +++ b/src/Rules/Extending/ExceptionsExtending.php @@ -13,17 +13,16 @@ class ExceptionsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Exceptions'; + + public static string $path = 'Exceptions'; + public static function rule(): ArchRule { return Rule::allClasses() ->except('App\Exceptions\Handler') - ->that(new ResideInOneOfTheseNamespaces('App\Exceptions')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Exception')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Exceptions'; - } } diff --git a/src/Rules/Extending/FactoriesExtending.php b/src/Rules/Extending/FactoriesExtending.php index 74e047a..cffbbc7 100644 --- a/src/Rules/Extending/FactoriesExtending.php +++ b/src/Rules/Extending/FactoriesExtending.php @@ -13,16 +13,41 @@ class FactoriesExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Database\Factories'; + + public static string $path = 'database/factories'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('Database\Factories')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Database\Eloquent\Factories\Factory')) ->because('we use Laravel framework!'); } + public static function namespace(): string + { + if (static::$rootNamespace === static::$namespace.'\\') { + return static::$namespace; + } + + return parent::namespace(); + } + public static function path(): string { - return 'database/factories'; + if (in_array(static::$rootPath, ['app/', 'database/seeders/'])) { + return ''; + } + + if ('database/factories/' === static::$rootPath) { + return static::$path; + } + + if (str_contains(static::$rootPath, 'src/')) { + return str_replace('src/', '', static::$rootPath).static::$path; + } + + return parent::path(); } } diff --git a/src/Rules/Extending/MailsExtending.php b/src/Rules/Extending/MailsExtending.php index 1b2cb4d..58a0bed 100644 --- a/src/Rules/Extending/MailsExtending.php +++ b/src/Rules/Extending/MailsExtending.php @@ -13,16 +13,15 @@ class MailsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Mails'; + + public static string $path = 'Mails'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Mails')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Mail\Mailable')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Mails'; - } } diff --git a/src/Rules/Extending/ModelsExtending.php b/src/Rules/Extending/ModelsExtending.php index ec55f22..4ff022f 100644 --- a/src/Rules/Extending/ModelsExtending.php +++ b/src/Rules/Extending/ModelsExtending.php @@ -13,17 +13,16 @@ class ModelsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Models'; + + public static string $path = 'Models'; + public static function rule(): ArchRule { return Rule::allClasses() - ->except('App\Models\User', 'App\Models\Scopes') - ->that(new ResideInOneOfTheseNamespaces('App\Models')) + ->except('*\Models\(User|Admin|Client)', 'App\Models\Scopes') + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Database\Eloquent\Model')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Models'; - } } diff --git a/src/Rules/Extending/NotificationsExtending.php b/src/Rules/Extending/NotificationsExtending.php index 5150472..863579f 100644 --- a/src/Rules/Extending/NotificationsExtending.php +++ b/src/Rules/Extending/NotificationsExtending.php @@ -13,16 +13,15 @@ class NotificationsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Notifications'; + + public static string $path = 'Notifications'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Notifications')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Notifications\Notification')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Notifications'; - } } diff --git a/src/Rules/Extending/ProvidersExtending.php b/src/Rules/Extending/ProvidersExtending.php index a4dc776..bbf7a5a 100644 --- a/src/Rules/Extending/ProvidersExtending.php +++ b/src/Rules/Extending/ProvidersExtending.php @@ -13,17 +13,21 @@ class ProvidersExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Providers'; + + public static string $path = 'Providers'; + public static function rule(): ArchRule { return Rule::allClasses() - ->except('App\Providers\(Auth|Event|Route|Horizon)ServiceProvider') - ->that(new ResideInOneOfTheseNamespaces('App\Providers')) + ->except( + 'App\Providers\AuthServiceProvider', + 'App\Providers\EventServiceProvider', + 'App\Providers\RouteServiceProvider', + 'App\Providers\HorizonServiceProvider', + ) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Support\ServiceProvider')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Providers'; - } } diff --git a/src/Rules/Extending/RequestsExtending.php b/src/Rules/Extending/RequestsExtending.php index 3f05a3a..a13488e 100644 --- a/src/Rules/Extending/RequestsExtending.php +++ b/src/Rules/Extending/RequestsExtending.php @@ -13,16 +13,15 @@ class RequestsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Http\Requests'; + + public static string $path = 'Http/Requests'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Http\Requests')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Foundation\Http\FormRequest')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Http/Requests'; - } } diff --git a/src/Rules/Extending/ResourceCollectionsExtending.php b/src/Rules/Extending/ResourceCollectionsExtending.php index 2e34042..9ec7673 100644 --- a/src/Rules/Extending/ResourceCollectionsExtending.php +++ b/src/Rules/Extending/ResourceCollectionsExtending.php @@ -13,6 +13,10 @@ class ResourceCollectionsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Http\Resources'; + + public static string $path = 'Http/Resources'; + public static function rule(): ArchRule { return Rule::allClasses() @@ -20,9 +24,4 @@ public static function rule(): ArchRule ->should(new Extend('Illuminate\Http\Resources\Json\ResourceCollection')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Http/Resources'; - } } diff --git a/src/Rules/Extending/ResourcesExtending.php b/src/Rules/Extending/ResourcesExtending.php index f09be0a..7ea0886 100644 --- a/src/Rules/Extending/ResourcesExtending.php +++ b/src/Rules/Extending/ResourcesExtending.php @@ -13,6 +13,10 @@ class ResourcesExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Http\Resources'; + + public static string $path = 'Http/Resources'; + public static function rule(): ArchRule { return Rule::allClasses() @@ -20,9 +24,4 @@ public static function rule(): ArchRule ->should(new Extend('Illuminate\Http\Resources\Json\JsonResource')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Http/Resources'; - } } diff --git a/src/Rules/Extending/SeedersExtending.php b/src/Rules/Extending/SeedersExtending.php index 73a2c69..89a7eae 100644 --- a/src/Rules/Extending/SeedersExtending.php +++ b/src/Rules/Extending/SeedersExtending.php @@ -13,16 +13,41 @@ class SeedersExtending extends BaseRule implements RuleContract { + public static string $namespace = 'Database\Seeders'; + + public static string $path = 'database/seeders'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('Database\Seeders')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\Database\Seeder')) ->because('we use Laravel framework!'); } + public static function namespace(): string + { + if (static::$rootNamespace === static::$namespace.'\\') { + return static::$namespace; + } + + return parent::namespace(); + } + public static function path(): string { - return 'database/seeders'; + if (in_array(static::$rootPath, ['app/', 'database/factories/'])) { + return ''; + } + + if ('database/seeders/' === static::$rootPath) { + return static::$path; + } + + if (str_contains(static::$rootPath, 'src/')) { + return str_replace('src/', '', static::$rootPath).static::$path; + } + + return parent::path(); } } diff --git a/src/Rules/Extending/ViewsExtending.php b/src/Rules/Extending/ViewsExtending.php index c09418a..0a782ae 100644 --- a/src/Rules/Extending/ViewsExtending.php +++ b/src/Rules/Extending/ViewsExtending.php @@ -13,16 +13,15 @@ class ViewsExtending extends BaseRule implements RuleContract { + public static string $namespace = 'View\Components'; + + public static string $path = 'View/Components'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\View\Components')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Extend('Illuminate\View\Component')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/View/Components'; - } } diff --git a/src/Rules/Implementing/CastsImplementing.php b/src/Rules/Implementing/CastsImplementing.php index 09dc599..b26405a 100644 --- a/src/Rules/Implementing/CastsImplementing.php +++ b/src/Rules/Implementing/CastsImplementing.php @@ -13,16 +13,15 @@ class CastsImplementing extends BaseRule implements RuleContract { + public static string $namespace = 'Casts'; + + public static string $path = 'Casts'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Casts')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Implement('Illuminate\Contracts\Database\Eloquent\CastsAttributes')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Casts'; - } } diff --git a/src/Rules/Implementing/JobsImplementing.php b/src/Rules/Implementing/JobsImplementing.php index 93d86a0..425aca6 100644 --- a/src/Rules/Implementing/JobsImplementing.php +++ b/src/Rules/Implementing/JobsImplementing.php @@ -13,16 +13,15 @@ class JobsImplementing extends BaseRule implements RuleContract { + public static string $namespace = 'Jobs'; + + public static string $path = 'Jobs'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Jobs')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Implement('Illuminate\Contracts\Queue\ShouldQueue')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Jobs'; - } } diff --git a/src/Rules/Implementing/RulesImplementing.php b/src/Rules/Implementing/RulesImplementing.php index b34ad19..73b92ba 100644 --- a/src/Rules/Implementing/RulesImplementing.php +++ b/src/Rules/Implementing/RulesImplementing.php @@ -13,16 +13,15 @@ class RulesImplementing extends BaseRule implements RuleContract { + public static string $namespace = 'Rules'; + + public static string $path = 'Rules'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Rules')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Implement('Illuminate\Contracts\Validation\Rule')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Rules'; - } } diff --git a/src/Rules/Implementing/ScopesImplementing.php b/src/Rules/Implementing/ScopesImplementing.php index 69c070c..7e1976a 100644 --- a/src/Rules/Implementing/ScopesImplementing.php +++ b/src/Rules/Implementing/ScopesImplementing.php @@ -13,16 +13,15 @@ class ScopesImplementing extends BaseRule implements RuleContract { + public static string $namespace = 'Models\Scopes'; + + public static string $path = 'Models/Scopes'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Models\Scopes')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new Implement('Illuminate\Database\Eloquent\Scope')) ->because('we use Laravel framework!'); } - - public static function path(): string - { - return 'app/Models/Scopes'; - } } diff --git a/src/Rules/Naming/BuildersNaming.php b/src/Rules/Naming/BuildersNaming.php index 2941790..b2a8ce4 100644 --- a/src/Rules/Naming/BuildersNaming.php +++ b/src/Rules/Naming/BuildersNaming.php @@ -13,16 +13,15 @@ class BuildersNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Builders'; + + public static string $path = 'Builders'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Builders')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Builder')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Builders'; - } } diff --git a/src/Rules/Naming/ChannelsNaming.php b/src/Rules/Naming/ChannelsNaming.php index 65cb223..8c0c96a 100644 --- a/src/Rules/Naming/ChannelsNaming.php +++ b/src/Rules/Naming/ChannelsNaming.php @@ -13,16 +13,15 @@ class ChannelsNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Broadcasting'; + + public static string $path = 'Broadcasting'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Broadcasting')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Channel')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Broadcasting'; - } } diff --git a/src/Rules/Naming/ContractsNaming.php b/src/Rules/Naming/ContractsNaming.php index 22427ed..5d2bae0 100644 --- a/src/Rules/Naming/ContractsNaming.php +++ b/src/Rules/Naming/ContractsNaming.php @@ -13,16 +13,15 @@ class ContractsNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Contracts'; + + public static string $path = 'Contracts'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Contracts')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Contract')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Contracts'; - } } diff --git a/src/Rules/Naming/ControllersNaming.php b/src/Rules/Naming/ControllersNaming.php index 3984142..14e6cf7 100644 --- a/src/Rules/Naming/ControllersNaming.php +++ b/src/Rules/Naming/ControllersNaming.php @@ -13,16 +13,15 @@ class ControllersNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Http\Controllers'; + + public static string $path = 'Http/Controllers'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Http\Controllers')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Controller')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Http/Controllers'; - } } diff --git a/src/Rules/Naming/ExceptionsNaming.php b/src/Rules/Naming/ExceptionsNaming.php index 049b67e..b9198d4 100644 --- a/src/Rules/Naming/ExceptionsNaming.php +++ b/src/Rules/Naming/ExceptionsNaming.php @@ -13,16 +13,15 @@ class ExceptionsNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Exceptions'; + + public static string $path = 'Exceptions'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Exceptions')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Exception|Handler')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Exceptions'; - } } diff --git a/src/Rules/Naming/FactoriesNaming.php b/src/Rules/Naming/FactoriesNaming.php index 21516f1..77de3d2 100644 --- a/src/Rules/Naming/FactoriesNaming.php +++ b/src/Rules/Naming/FactoriesNaming.php @@ -13,16 +13,41 @@ class FactoriesNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Database\Factories'; + + public static string $path = 'database/factories'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('Database\Factories')) + ->that(new ResideInOneOfTheseNamespaces(self::namespace())) ->should(new HaveNameMatching('*Factory')) ->because('It\'s a Laravel naming convention'); } + public static function namespace(): string + { + if (static::$rootNamespace === static::$namespace.'\\') { + return static::$namespace; + } + + return parent::namespace(); + } + public static function path(): string { - return 'database/factories'; + if (in_array(static::$rootPath, ['app/', 'database/seeders/'])) { + return ''; + } + + if ('database/factories/' === static::$rootPath) { + return static::$path; + } + + if (str_contains(static::$rootPath, 'src/')) { + return str_replace('src/', '', static::$rootPath).static::$path; + } + + return parent::path(); } } diff --git a/src/Rules/Naming/MailsNaming.php b/src/Rules/Naming/MailsNaming.php index 1e3f317..c01d987 100644 --- a/src/Rules/Naming/MailsNaming.php +++ b/src/Rules/Naming/MailsNaming.php @@ -13,16 +13,15 @@ class MailsNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Mails'; + + public static string $path = 'Mails'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Mails')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Mail')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Mails'; - } } diff --git a/src/Rules/Naming/NotificationsNaming.php b/src/Rules/Naming/NotificationsNaming.php index 443c1d3..09eb6fe 100644 --- a/src/Rules/Naming/NotificationsNaming.php +++ b/src/Rules/Naming/NotificationsNaming.php @@ -13,16 +13,15 @@ class NotificationsNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Notifications'; + + public static string $path = 'Notifications'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Notifications')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Notification')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Notifications'; - } } diff --git a/src/Rules/Naming/ObserversNaming.php b/src/Rules/Naming/ObserversNaming.php index 8148257..7a83469 100644 --- a/src/Rules/Naming/ObserversNaming.php +++ b/src/Rules/Naming/ObserversNaming.php @@ -13,16 +13,15 @@ class ObserversNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Observers'; + + public static string $path = 'Observers'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Observers')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Observer')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Observers'; - } } diff --git a/src/Rules/Naming/PoliciesNaming.php b/src/Rules/Naming/PoliciesNaming.php index 7f7de68..270ec6c 100644 --- a/src/Rules/Naming/PoliciesNaming.php +++ b/src/Rules/Naming/PoliciesNaming.php @@ -13,16 +13,15 @@ class PoliciesNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Observers'; + + public static string $path = 'Observers'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Policies')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Policy')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Policies'; - } } diff --git a/src/Rules/Naming/ProvidersNaming.php b/src/Rules/Naming/ProvidersNaming.php index 4cc340e..ca1433a 100644 --- a/src/Rules/Naming/ProvidersNaming.php +++ b/src/Rules/Naming/ProvidersNaming.php @@ -13,16 +13,15 @@ class ProvidersNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Providers'; + + public static string $path = 'Providers'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Providers')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*ServiceProvider')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Providers'; - } } diff --git a/src/Rules/Naming/RepositoriesNaming.php b/src/Rules/Naming/RepositoriesNaming.php index 4c311ad..c1534ce 100644 --- a/src/Rules/Naming/RepositoriesNaming.php +++ b/src/Rules/Naming/RepositoriesNaming.php @@ -13,16 +13,15 @@ class RepositoriesNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Repositories'; + + public static string $path = 'Repositories'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Repositories')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Repository')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Repositories'; - } } diff --git a/src/Rules/Naming/RequestsNaming.php b/src/Rules/Naming/RequestsNaming.php index c22469b..ef81172 100644 --- a/src/Rules/Naming/RequestsNaming.php +++ b/src/Rules/Naming/RequestsNaming.php @@ -13,16 +13,15 @@ class RequestsNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Http\Requests'; + + public static string $path = 'Http/Requests'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Http\Requests')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Request')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Http/Requests'; - } } diff --git a/src/Rules/Naming/ResourcesNaming.php b/src/Rules/Naming/ResourcesNaming.php index ae440a4..26fef53 100644 --- a/src/Rules/Naming/ResourcesNaming.php +++ b/src/Rules/Naming/ResourcesNaming.php @@ -13,16 +13,15 @@ class ResourcesNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Http\Resources'; + + public static string $path = 'Http/Resources'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Http\Resources')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*(Resource|Collection)')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Http/Resources'; - } } diff --git a/src/Rules/Naming/ScopesNaming.php b/src/Rules/Naming/ScopesNaming.php index 03f33d9..ed89f41 100644 --- a/src/Rules/Naming/ScopesNaming.php +++ b/src/Rules/Naming/ScopesNaming.php @@ -13,16 +13,15 @@ class ScopesNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Models\Scopes'; + + public static string $path = 'Models/Scopes'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('App\Models\Scopes')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Scope')) ->because('It\'s a Laravel naming convention'); } - - public static function path(): string - { - return 'app/Models/Scopes'; - } } diff --git a/src/Rules/Naming/SeedersNaming.php b/src/Rules/Naming/SeedersNaming.php index 5bca1bf..3fd05eb 100644 --- a/src/Rules/Naming/SeedersNaming.php +++ b/src/Rules/Naming/SeedersNaming.php @@ -13,16 +13,41 @@ class SeedersNaming extends BaseRule implements RuleContract { + public static string $namespace = 'Database\Seeders'; + + public static string $path = 'database/seeders'; + public static function rule(): ArchRule { return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces('Database\Seeders')) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) ->should(new HaveNameMatching('*Seeder')) ->because('It\'s a Laravel naming convention'); } + public static function namespace(): string + { + if (static::$rootNamespace === static::$namespace.'\\') { + return static::$namespace; + } + + return parent::namespace(); + } + public static function path(): string { - return 'database/seeders'; + if (in_array(static::$rootPath, ['app/', 'database/factories/'])) { + return ''; + } + + if ('database/seeders/' === static::$rootPath) { + return static::$path; + } + + if (str_contains(static::$rootPath, 'src/')) { + return str_replace('src/', '', static::$rootPath).static::$path; + } + + return parent::path(); } } diff --git a/src/phparkitect.php b/src/phparkitect.php index a31084a..bbff844 100644 --- a/src/phparkitect.php +++ b/src/phparkitect.php @@ -4,13 +4,24 @@ use Arkitect\CLI\Config; use Mortexa\LaravelArkitect\RuleLoader; +use Mortexa\LaravelArkitect\Rules\BaseRule; return static function (Config $config): void { + $addRules = function (array $rules) use (&$config): void { + foreach ($rules as $rule) { + if ($rule::isValid()) { + $config->add($rule::classSet(), $rule::rule()); + } + } + }; + $rules = RuleLoader::all(); - foreach ($rules as $rule) { - if ($rule::directoryExists()) { - $config->add($rule::classSet(), $rule::rule()); - } + $psr4_namespaces = data_get(json_decode(file_get_contents('composer.json'), true), 'autoload.psr-4'); + + foreach ($psr4_namespaces as $namespace => $path) { + BaseRule::$rootNamespace = $namespace; + BaseRule::$rootPath = $path; + $addRules($rules); } };