From c852a8af835f7dceac321d6110adca6322f67472 Mon Sep 17 00:00:00 2001 From: Rick West Date: Mon, 16 Jun 2025 22:33:42 +0100 Subject: [PATCH] Add abstract base rule --- src/Rules/BaseRule.php | 2 ++ src/Rules/Extending/BaseExtending.php | 32 ++++++++++++++++++++ src/Rules/Extending/CommandsExtending.php | 17 ++--------- src/Rules/Extending/ControllersExtending.php | 20 +++--------- src/Rules/Extending/ExceptionsExtending.php | 20 +++--------- src/Rules/Extending/FactoriesExtending.php | 17 ++--------- src/Rules/Extending/ModelsExtending.php | 30 ++++++------------ 7 files changed, 55 insertions(+), 83 deletions(-) create mode 100644 src/Rules/Extending/BaseExtending.php diff --git a/src/Rules/BaseRule.php b/src/Rules/BaseRule.php index 5ae4627..10ae236 100644 --- a/src/Rules/BaseRule.php +++ b/src/Rules/BaseRule.php @@ -14,6 +14,8 @@ class BaseRule public static string $rootPath = 'app/'; + public static string $reason = 'we use Laravel framework!'; + public static function classSet(): ClassSet { return ClassSet::fromDir(static::directory()); diff --git a/src/Rules/Extending/BaseExtending.php b/src/Rules/Extending/BaseExtending.php new file mode 100644 index 0000000..eec3cb8 --- /dev/null +++ b/src/Rules/Extending/BaseExtending.php @@ -0,0 +1,32 @@ +except(...static::$except) + ->that(new ResideInOneOfTheseNamespaces(static::namespace())) + ->should(new Extend(...static::$classNames)) + ->because(static::$reason); + } +} diff --git a/src/Rules/Extending/CommandsExtending.php b/src/Rules/Extending/CommandsExtending.php index a4812ce..63c4665 100644 --- a/src/Rules/Extending/CommandsExtending.php +++ b/src/Rules/Extending/CommandsExtending.php @@ -4,24 +4,11 @@ namespace Mortexa\LaravelArkitect\Rules\Extending; -use Arkitect\Expression\ForClasses\Extend; -use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; -use Arkitect\Rules\DSL\ArchRule; -use Arkitect\Rules\Rule; -use Mortexa\LaravelArkitect\Contracts\RuleContract; -use Mortexa\LaravelArkitect\Rules\BaseRule; - -class CommandsExtending extends BaseRule implements RuleContract +class CommandsExtending extends BaseExtending { public static string $namespace = 'Console\Commands'; public static string $path = 'Console/Commands'; - public static function rule(): ArchRule - { - return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces(static::namespace())) - ->should(new Extend('Illuminate\Console\Command')) - ->because('we use Laravel framework!'); - } + public static array $classNames = ['Illuminate\Console\Command']; } diff --git a/src/Rules/Extending/ControllersExtending.php b/src/Rules/Extending/ControllersExtending.php index c4c5379..cd26fed 100644 --- a/src/Rules/Extending/ControllersExtending.php +++ b/src/Rules/Extending/ControllersExtending.php @@ -4,25 +4,13 @@ namespace Mortexa\LaravelArkitect\Rules\Extending; -use Arkitect\Expression\ForClasses\Extend; -use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; -use Arkitect\Rules\DSL\ArchRule; -use Arkitect\Rules\Rule; -use Mortexa\LaravelArkitect\Contracts\RuleContract; -use Mortexa\LaravelArkitect\Rules\BaseRule; - -class ControllersExtending extends BaseRule implements RuleContract +class ControllersExtending extends BaseExtending { 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(static::namespace())) - ->should(new Extend('App\Http\Controllers\Controller')) - ->because('we use Laravel framework!'); - } + public static array $except = ['App\Http\Controllers\Controller']; + + public static array $classNames = ['App\Http\Controllers\Controller']; } diff --git a/src/Rules/Extending/ExceptionsExtending.php b/src/Rules/Extending/ExceptionsExtending.php index 1808624..59e59bb 100644 --- a/src/Rules/Extending/ExceptionsExtending.php +++ b/src/Rules/Extending/ExceptionsExtending.php @@ -4,25 +4,13 @@ namespace Mortexa\LaravelArkitect\Rules\Extending; -use Arkitect\Expression\ForClasses\Extend; -use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; -use Arkitect\Rules\DSL\ArchRule; -use Arkitect\Rules\Rule; -use Mortexa\LaravelArkitect\Contracts\RuleContract; -use Mortexa\LaravelArkitect\Rules\BaseRule; - -class ExceptionsExtending extends BaseRule implements RuleContract +class ExceptionsExtending extends BaseExtending { 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(static::namespace())) - ->should(new Extend('Exception')) - ->because('we use Laravel framework!'); - } + public static array $except = ['App\Exceptions\Handler']; + + public static array $classNames = ['Exception']; } diff --git a/src/Rules/Extending/FactoriesExtending.php b/src/Rules/Extending/FactoriesExtending.php index cffbbc7..69de971 100644 --- a/src/Rules/Extending/FactoriesExtending.php +++ b/src/Rules/Extending/FactoriesExtending.php @@ -4,26 +4,13 @@ namespace Mortexa\LaravelArkitect\Rules\Extending; -use Arkitect\Expression\ForClasses\Extend; -use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; -use Arkitect\Rules\DSL\ArchRule; -use Arkitect\Rules\Rule; -use Mortexa\LaravelArkitect\Contracts\RuleContract; -use Mortexa\LaravelArkitect\Rules\BaseRule; - -class FactoriesExtending extends BaseRule implements RuleContract +class FactoriesExtending extends BaseExtending { public static string $namespace = 'Database\Factories'; public static string $path = 'database/factories'; - public static function rule(): ArchRule - { - return Rule::allClasses() - ->that(new ResideInOneOfTheseNamespaces(static::namespace())) - ->should(new Extend('Illuminate\Database\Eloquent\Factories\Factory')) - ->because('we use Laravel framework!'); - } + public static array $classNames = ['Illuminate\Database\Eloquent\Factories\Factory']; public static function namespace(): string { diff --git a/src/Rules/Extending/ModelsExtending.php b/src/Rules/Extending/ModelsExtending.php index 100b063..6ebc330 100644 --- a/src/Rules/Extending/ModelsExtending.php +++ b/src/Rules/Extending/ModelsExtending.php @@ -4,30 +4,18 @@ namespace Mortexa\LaravelArkitect\Rules\Extending; -use Arkitect\Expression\ForClasses\Extend; -use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces; -use Arkitect\Rules\DSL\ArchRule; -use Arkitect\Rules\Rule; -use Mortexa\LaravelArkitect\Contracts\RuleContract; -use Mortexa\LaravelArkitect\Rules\BaseRule; - -class ModelsExtending extends BaseRule implements RuleContract +class ModelsExtending extends BaseExtending { public static string $namespace = 'Models'; public static string $path = 'Models'; - public static function rule(): ArchRule - { - return Rule::allClasses() - ->except( - 'App\Models\User', - 'App\Models\Admin', - 'App\Models\Client', - 'App\Models\Scopes' - ) - ->that(new ResideInOneOfTheseNamespaces(static::namespace())) - ->should(new Extend('Illuminate\Database\Eloquent\Model')) - ->because('we use Laravel framework!'); - } + public static array $except = [ + 'App\Models\User', + 'App\Models\Admin', + 'App\Models\Client', + 'App\Models\Scopes', + ]; + + public static array $classNames = ['Illuminate\Database\Eloquent\Model']; }