Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to Cordon Modulith are documented here. The project follows

## [Unreleased]

### Added
- `cordon:modules` warns when the detected modules look like the layers of a standard Laravel app (`Http`, `Models`, `Providers`...) rather than modules split by feature, and the installation guide explains which projects Cordon fits.

### Fixed
- Class references are matched to modules and to public namespaces regardless of case, like PHP resolves class names. A reference written as `modules\catalog\Models\Product` was not attributed to `Catalog`, so its use of an internal class was not reported. Thanks to @bfs2021 (#26).

Expand Down
8 changes: 8 additions & 0 deletions docs-site/guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Requirements: PHP 8.2+ and Laravel 12 or 13 (Laravel 13 needs PHP 8.3+).

## Is Cordon for my project?

Cordon checks the boundaries between **modules split by feature**: `Billing`, `Catalog`, `Orders`, each with its own models, services and events. That is the layout of nwidart/laravel-modules, InterNACHI/modular or an `app/Modules` folder.

Most Laravel apps start **split by layer** instead: `app/Http`, `app/Models`, `app/Services`, `app/Providers`. There every layer uses `Models`, so pointing Cordon at `app/` reports almost every reference as `internal_access`, plus one large cycle. That tells you the app is layered, not that it is broken. For rules between layers ("controllers may use services, services may use models"), use [Deptrac](https://github.com/deptrac/deptrac) or Pest `arch()`; see the [comparison](../comparison).

`cordon:modules` warns when the modules it found look like those layers. If your app is layered today but you are moving features into modules, Cordon fits once the first module exists: point it at the modules folder, not at `app/`.

```bash
composer require --dev chrisabner/cordon-modulith
```
Expand Down
11 changes: 11 additions & 0 deletions src/Laravel/Commands/ModulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use Cordon\Contracts\ModuleResolver;
use Cordon\Laravel\ResolverFactory;
use Cordon\Module\LayeredLayout;
use Cordon\Module\Module;
use Cordon\Support\Paths;
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;

final class ModulesCommand extends Command
{
private const DOCS = 'https://chrisabner.github.io/Cordon-Modulith/guide/installation#is-cordon-for-my-project';

protected $signature = 'cordon:modules';

protected $description = 'List the modules Cordon detected and their boundary settings';
Expand Down Expand Up @@ -45,6 +48,14 @@ public function handle(ModuleResolver $resolver, Repository $config): int
], $modules->all()),
);

if (($layers = LayeredLayout::layersIn($modules)) !== []) {
$this->warn(sprintf(
'These modules look like the layers of a standard Laravel app (%s), not modules split by feature. Cordon checks the boundaries between feature modules; for rules between layers, a tool like Deptrac fits better: %s',
implode(', ', $layers),
self::DOCS,
));
}

return self::SUCCESS;
}
}
47 changes: 47 additions & 0 deletions src/Module/LayeredLayout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Cordon\Module;

/**
* Recognises when the detected "modules" are the layers of a standard
* Laravel app (app/Http, app/Models, app/Providers...) rather than modules
* split by feature, which is a setup Cordon is not meant for.
*/
final class LayeredLayout
{
/**
* Directory names of a Laravel app split by layer.
*/
private const LAYERS = [
'Actions', 'Casts', 'Console', 'Events', 'Exceptions', 'Helpers', 'Http', 'Jobs', 'Listeners',
'Mail', 'Models', 'Notifications', 'Observers', 'Policies', 'Providers', 'Rules', 'Services',
'Traits', 'View',
];

/**
* One module may be called like a layer (a Notifications module, say);
* two or more mean the resolver is most likely pointed at app/.
*/
private const MINIMUM = 2;

/**
* The module names that are layers, when there are enough of them to
* suggest a layered app; otherwise none.
*
* @return list<string>
*/
public static function layersIn(ModuleMap $modules): array
{
$layers = [];

foreach ($modules as $module) {
if (in_array($module->name, self::LAYERS, true)) {
$layers[] = $module->name;
}
}

return count($layers) >= self::MINIMUM ? $layers : [];
}
}
15 changes: 15 additions & 0 deletions tests/Feature/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,18 @@ function stale_entry(): array
$this->artisan('cordon:verify', ['--prune-baseline' => true, '--generate-baseline' => true])->assertExitCode(2);
$this->artisan('cordon:verify', ['--prune-baseline' => true, '--no-baseline' => true])->assertExitCode(2);
});

it('hints that a layered app is not split into modules', function () {
config()->set('cordon.resolvers.namespace.path', fixture_path('layered-app/app'));
config()->set('cordon.resolvers.namespace.namespace', 'App');

$this->artisan('cordon:modules')
->expectsOutputToContain('These modules look like the layers of a standard Laravel app (Http, Models, Providers, Services)')
->assertExitCode(0);
});

it('does not hint for modules split by feature', function () {
$this->artisan('cordon:modules')
->doesntExpectOutputToContain('look like the layers')
->assertExitCode(0);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Controllers;

use App\Models\Order;
use App\Services\OrderService;

class OrderController
{
public function store(OrderService $orders): Order
{
return $orders->place();
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/layered-app/app/Models/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace App\Models;

class Order {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace App\Providers;

class AppServiceProvider {}
13 changes: 13 additions & 0 deletions tests/Fixtures/layered-app/app/Services/OrderService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Services;

use App\Models\Order;

class OrderService
{
public function place(): Order
{
return new Order;
}
}
22 changes: 22 additions & 0 deletions tests/Unit/LayeredLayoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Cordon\Module\LayeredLayout;
use Cordon\Module\Module;
use Cordon\Module\ModuleMap;

function modules_named(string ...$names): ModuleMap
{
return new ModuleMap(array_map(fn (string $name) => Module::make($name, 'App\\'.$name, '/app/'.$name), $names));
}

it('recognises the layers of a standard Laravel app', function () {
expect(LayeredLayout::layersIn(modules_named('Http', 'Models', 'Providers', 'Services')))
->toBe(['Http', 'Models', 'Providers', 'Services']);
});

it('does not flag modules split by feature', function (array $names) {
expect(LayeredLayout::layersIn(modules_named(...$names)))->toBe([]);
})->with([
'feature modules' => [['Billing', 'Catalog', 'Shared']],
'one module named like a layer' => [['Billing', 'Catalog', 'Notifications']],
]);
Loading