Skip to content

Commit

Permalink
Add support for modules (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
smortexa committed Jan 19, 2023
1 parent 529322c commit 09a816b
Show file tree
Hide file tree
Showing 35 changed files with 292 additions and 186 deletions.
19 changes: 19 additions & 0 deletions src/Rules/BaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}
}
11 changes: 5 additions & 6 deletions src/Rules/Extending/CommandsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
11 changes: 5 additions & 6 deletions src/Rules/Extending/ControllersExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
11 changes: 5 additions & 6 deletions src/Rules/Extending/ExceptionsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
29 changes: 27 additions & 2 deletions src/Rules/Extending/FactoriesExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
11 changes: 5 additions & 6 deletions src/Rules/Extending/MailsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
13 changes: 6 additions & 7 deletions src/Rules/Extending/ModelsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
11 changes: 5 additions & 6 deletions src/Rules/Extending/NotificationsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
18 changes: 11 additions & 7 deletions src/Rules/Extending/ProvidersExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
11 changes: 5 additions & 6 deletions src/Rules/Extending/RequestsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
9 changes: 4 additions & 5 deletions src/Rules/Extending/ResourceCollectionsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

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()
->that(new HaveNameMatching('*Collection'))
->should(new Extend('Illuminate\Http\Resources\Json\ResourceCollection'))
->because('we use Laravel framework!');
}

public static function path(): string
{
return 'app/Http/Resources';
}
}
9 changes: 4 additions & 5 deletions src/Rules/Extending/ResourcesExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

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()
->that(new HaveNameMatching('*Resource'))
->should(new Extend('Illuminate\Http\Resources\Json\JsonResource'))
->because('we use Laravel framework!');
}

public static function path(): string
{
return 'app/Http/Resources';
}
}
29 changes: 27 additions & 2 deletions src/Rules/Extending/SeedersExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
11 changes: 5 additions & 6 deletions src/Rules/Extending/ViewsExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
11 changes: 5 additions & 6 deletions src/Rules/Implementing/CastsImplementing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
Loading

0 comments on commit 09a816b

Please sign in to comment.