php-guidelines-from-spatie
Applies Spatie’s coding standards for PHP and Laravel to improve code readability and maintainability.
Install
mkdir -p .claude/skills/php-guidelines-from-spatie && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/1610" && unzip -o skill.zip -d .claude/skills/php-guidelines-from-spatie && rm skill.zipInstalls to .claude/skills/php-guidelines-from-spatie
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Describes PHP and Laravel guidelines provided by Spatie. These rules result in more maintainable, and readable code.Key capabilities
- →Enforce PSR-1, PSR-2, PSR-12 coding standards
- →Implement short nullable type syntax
- →Standardize docblock generics for collections
- →Enforce early return patterns in control flow
How it works
Applies a predefined checklist of PSR standards and Laravel-centric coding conventions for PHP class structures and type safety.
Inputs & outputs
When to use php-guidelines-from-spatie
- →Standardizing PHP coding style
- →Applying Laravel best practices
- →Refactoring legacy PHP code
- →Enforcing type declaration standards
About this skill
Core Laravel Principle
Follow Laravel conventions first. If Laravel has a documented way to do something, use it. Only deviate when you have a clear justification.
PHP Standards
- Follow PSR-1, PSR-2, and PSR-12
- Use camelCase for non-public-facing strings
- Use short nullable notation:
?stringnotstring|null - Always specify
voidreturn types when methods return nothing
Class Structure
- Use typed properties, not docblocks:
- Constructor property promotion when all properties can be promoted:
- One trait per line:
Type Declarations & Docblocks
- Use typed properties over docblocks
- Specify return types including
void - Use short nullable syntax:
?TypenotType|null - Document iterables with generics:
/** @return Collection<int, User> */ public function getUsers(): Collection
Docblock Rules
- Don't use docblocks for fully type-hinted methods (unless description needed)
- Always import classnames in docblocks - never use fully qualified names:
use \Spatie\Url\Url; /** @return Url */ - Use one-line docblocks when possible:
/** @var string */ - Most common type should be first in multi-type docblocks:
/** @var Collection|SomeWeirdVendor\Collection */ - If one parameter needs docblock, add docblocks for all parameters
- For iterables, always specify key and value types:
/** * @param array<int, MyObject> $myArray * @param int $typedArgument */ function someFunction(array $myArray, int $typedArgument) {} - Use array shape notation for fixed keys, put each key on it's own line:
/** @return array{ first: SomeClass, second: SomeClass } */
Control Flow
- Happy path last: Handle error conditions first, success case last
- Avoid else: Use early returns instead of nested conditions
- Separate conditions: Split compound
ifstatements that use&&into nestedifstatements for better readability - Always use curly brackets even for single statements
- Ternary operators: Each part on own line unless very short
// Happy path last
if (! $user) {
return null;
}
if (! $user->isActive()) {
return null;
}
// Process active user...
// Short ternary
$name = $isFoo ? 'foo' : 'bar';
// Multi-line ternary
$result = $object instanceof Model ?
$object->name :
'A default value';
// Ternary instead of else
$condition
? $this->doSomething()
: $this->doSomethingElse();
// Bad: compound condition with &&
if ($user->isActive() && $user->hasPermission('edit')) {
$user->edit();
}
// Good: nested ifs
if ($user->isActive()) {
if ($user->hasPermission('edit')) {
$user->edit();
}
}
Laravel Conventions
Routes
- URLs: kebab-case (
/open-source) - Route names: camelCase (
->name('openSource')) - Parameters: camelCase (
{userId}) - Use tuple notation:
[Controller::class, 'method']
Controllers
- Plural resource names (
PostsController) - Stick to CRUD methods (
index,create,store,show,edit,update,destroy) - Extract new controllers for non-CRUD actions
Configuration
- Files: kebab-case (
pdf-generator.php) - Keys: snake_case (
chrome_path) - Add service configs to
config/services.php, don't create new files - Use
config()helper, avoidenv()outside config files
Artisan Commands
- Names: kebab-case (
delete-old-records) - Always provide feedback (
$this->comment('All ok!')) - Show progress for loops, summary at end
- Put output BEFORE processing item (easier debugging):
$items->each(function(Item $item) { $this->info("Processing item id `{$item->id}`..."); $this->processItem($item); }); $this->comment("Processed {$items->count()} items.");
Strings & Formatting
- String interpolation over concatenation:
Enums
- Use PascalCase for enum values:
Comments
Be very critical about adding comments as they often become outdated and can mislead over time. Code should be self-documenting through descriptive variable and function names.
Adding comments should never be the first tactic to make code readable.
Instead of this:
// Get the failed checks for this site
$checks = $site->checks()->where('status', 'failed')->get();
Do this:
$failedChecks = $site->checks()->where('status', 'failed')->get();
Guidelines:
- Don't add comments that describe what the code does - make the code describe itself
- Short, readable code doesn't need comments explaining it
- Use descriptive variable names instead of generic names + comments
- Only add comments when explaining why something non-obvious is done, not what is being done
- Never add comments to tests - test names should be descriptive enough
Whitespace
- Add blank lines between statements for readability
- Exception: sequences of equivalent single-line operations
- No extra empty lines between
{}brackets - Let code "breathe" - avoid cramped formatting
Validation
- Use array notation for multiple rules (easier for custom rule classes):
public function rules() { return [ 'email' => ['required', 'email'], ]; } - Custom validation rules use snake_case:
Validator::extend('organisation_type', function ($attribute, $value) { return OrganisationType::isValid($value); });
Blade Templates
- Indent with 4 spaces
- No spaces after control structures:
@if($condition) Something @endif
Authorization
- Policies use camelCase:
Gate::define('editPost', ...) - Use CRUD words, but
viewinstead ofshow
Translations
- Use
__()function over@lang:
API Routing
- Use plural resource names:
/errors - Use kebab-case:
/error-occurrences - Limit deep nesting for simplicity:
/error-occurrences/1 /errors/1/occurrences
Testing
- Keep test classes in same file when possible
- Use descriptive test method names
- Follow the arrange-act-assert pattern
- Put helper functions last in test files, below the
it/testblocks (the tests should read first; helpers are support detail)
Quick Reference
Naming Conventions
- Classes: PascalCase (
UserController,OrderStatus) - Methods/Variables: camelCase (
getUserName,$firstName) - Routes: kebab-case (
/open-source,/user-profile) - Config files: kebab-case (
pdf-generator.php) - Config keys: snake_case (
chrome_path) - Artisan commands: kebab-case (
php artisan delete-old-records)
File Structure
- Controllers: plural resource name +
Controller(PostsController) - Views: camelCase (
openSource.blade.php) - Jobs: action-based (
CreateUser,SendEmailNotification) - Events: tense-based (
UserRegistering,UserRegistered) - Listeners: action +
Listenersuffix (SendInvitationMailListener) - Commands: action +
Commandsuffix (PublishScheduledPostsCommand) - Mailables: purpose +
Mailsuffix (AccountActivatedMail) - Resources/Transformers: plural +
Resource/Transformer(UsersResource) - Enums: descriptive name, no prefix (
OrderStatus,BookingType)
Migrations
- do not write down methods in migrations, only up methods
Code Quality Reminders
PHP
- Use typed properties over docblocks
- Prefer early returns over nested if/else
- Use constructor property promotion when all properties can be promoted
- Avoid
elsestatements when possible - Split compound
ifconditions using&&into nestedifstatements - Use string interpolation over concatenation
- Always use curly braces for control structures
- Always import namespaces with
usestatements — never use inline fully qualified class names (e.g.\Exception,\Illuminate\Support\Facades\Http) - Never use single-letter variable names — use descriptive names (e.g.
$exceptionnot$e,$requestnot$r)
When not to use it
- →Non-PHP codebases
- →Projects using conflicting legacy frameworks
Limitations
- →Rules are subjective to Spatie guidelines
- →Does not automatically fix deep logical bugs
How it compares
Standardizes the stylistic and structural implementation details of PHP code according to widely recognized community patterns.
Compared to similar skills
php-guidelines-from-spatie side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| php-guidelines-from-spatie (this skill) | 6 | 6mo | No flags | Beginner |
| php-best-practices | 15 | 1mo | No flags | Beginner |
| laravel-architecture | 7 | 1mo | No flags | Intermediate |
| fluent-validation-migrate-messages | 0 | 1mo | No flags | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by freekmurze
View all by freekmurze →You might also like
php-best-practices
HoangNguyen0403
PHP best practices, PSR standards, and code quality guidelines.
laravel-architecture
HoangNguyen0403
Core architectural standards for scalable Laravel applications.
fluent-validation-migrate-messages
angelgarciasolorzano
Migrate FormRequest `messages(): array` to inline `message:` on fluent chains. Dry-run, then apply. Activates when: user mentions migrate messages, messages array, inline message, remove messages().
php-pro
ngxtm
Use when building PHP applications with modern PHP 8.3+ features, Laravel, or Symfony frameworks. Invoke for strict typing, PHPStan level 9, async patterns with Swoole, PSR standards.
php-pro
sickn33
Write idiomatic PHP code with generators, iterators, SPL data structures, and modern OOP features. Use PROACTIVELY for high-performance PHP applications.
code-refactor-php-mediawiki
SemanticMediaWiki
>