laravel-stubs maintained by concept-image
Laravel Stubs
Artisan generators for the PHP classes Laravel does not scaffold natively: actions, services, helpers, facades and value objects. Every generated file uses declare(strict_types=1) and a final (or final readonly) class.
Sister package of WP Acorn Stubs for Laravel applications.
Requirements
- PHP 8.3 or higher
- Laravel 12 or 13
Installation
Install it as a development dependency:
composer require --dev concept-image/laravel-stubs
The service provider is registered through Laravel package discovery.
Available commands
| Command | Namespace | Suffix | Options |
|---|---|---|---|
make:action |
App\Actions |
Action |
--model, --force |
make:service |
App\Services |
Service |
--force |
make:helper |
App\Helpers |
Helper |
--force |
make:facade |
App\Support\Facades |
none | --accessor, --force |
make:value-object |
App\ValueObjects |
none | --force |
Use the native commands for the rest: make:enum (--string / --int), make:trait, make:interface, make:class.
Naming
- The suffix is appended once:
make:action CreateUserandmake:action CreateUserActionboth createCreateUserAction. - A trailing
.phpis ignored and each segment is StudlyCased (make:helper string→StringHelper). /and.create sub-namespaces:make:action Billing.ChargeCard→App\Actions\Billing\ChargeCardAction.- An existing file is never overwritten (the command exits with code 1) unless
--forceis given.
Action
php artisan make:action PlaceOrder --model=Order
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Models\Order;
final readonly class PlaceOrderAction
{
/**
* Execute the action.
*/
public function handle(Order $order): void
{
//
}
}
Without --model, handle() takes no parameter.
Service
php artisan make:service Payment # App\Services\PaymentService
A final readonly class with an empty constructor, ready for constructor-injected dependencies.
Helper
php artisan make:helper String # App\Helpers\StringHelper
A final class with a private constructor: helpers only expose static methods.
Facade
php artisan make:facade PaymentGateway # accessor 'payment_gateway'
php artisan make:facade Payment --accessor=payments.gateway # accessor 'payments.gateway'
php artisan make:facade Payment --accessor="App\Services\PaymentService" # accessor PaymentService::class
By default the accessor is the snake_cased class name. A class given to --accessor is imported, returned as ::class and linked with an @see tag for IDEs.
Value object
php artisan make:value-object EmailAddress # App\ValueObjects\EmailAddress
final readonly class EmailAddress
{
public function __construct(
public string $value,
) {
//
}
public function equals(self $other): bool
{
return $this->value === $other->value;
}
}
Customising the stubs
php artisan vendor:publish --tag=laravel-stubs
Stubs are copied to stubs/ at the root of your application. A stub there takes precedence over the package's, as with Laravel's own stub:publish. Available placeholders:
| Stub | Placeholders |
|---|---|
| all | {{ namespace }}, {{ class }} |
action.stub |
{{ imports }}, {{ parameters }} |
facade.stub |
{{ imports }}, {{ docblock }}, {{ accessor }} |
Empty placeholders leave no extra blank lines; use statements are sorted.
Development
composer install
composer test # Pest
composer analyse # PHPStan / Larastan, level max
composer lint # Pint
composer ci # Pint --test, PHPStan and Pest
vendor/bin/testbench make:action Demo # try a command against the Testbench skeleton
License
This package is open-sourced software licensed under the MIT license.
Credits
- Author: Aurélien Cellier
- Inspired by: nunomaduro/essentials
make:action