Skip to content

Enforce app conventions

App rules read the booted app, as runtime links do. They need laravel/framework. Each section below is one goal. Use the ones you need.

use function Nwidart\Larch\larch;
arch('api routes need auth')
->expect(larch()->routes()->prefixed('api')->except('api/webhooks/*', 'api/health'))
->toUseMiddleware('auth:sanctum');

To select routes by name instead of URI, use named('admin.*'). toUseMiddleware() takes a name with its parameters (auth:sanctum), a group (api) or a class.

use Illuminate\Database\Eloquent\Relations\Pivot;
arch('every model has a policy')
->expect(larch()->models()->except(Pivot::class))
->toHavePolicy();

A policy counts when it is registered with Gate::policy() or when Laravel finds it by name. except() takes classes, which also skip the models that extend them, and patterns such as 'App\Legacy\*'.

arch('queued jobs set retry limits')
->expect(larch()->jobs()->queued())
->toDefine('tries', 'timeout');

A job defines a member with a property, a queue attribute such as #[Tries(3)], or, for tries, backoff and retryUntil, a method.

arch('listeners on billing events are queued')
->expect(larch()->listenersOf('Modules\Billing\Events\*'))
->toBeQueued();

Register a queued listener by its class name. Laravel calls a listener registered as an object at once, even when it implements ShouldQueue.

arch('polymorphic models are in the morph map')
->expect(larch()->models()->usedInMorphs())
->toBeInMorphMap();

App rules work with the baseline. See Adopt Larch in an existing app.