Index of every page on this site: llms.txt
All skills
haythamasalama avatar

/laravel-code-quality

@54e769e
Run it yourself

Use when writing, reviewing or cleaning up Laravel PHP for quality and safety rather than layout — formatting with Pint, one-line method signatures, named arguments, the few-comments rule and when PHPDoc is allowed, naming and types, readable conditionals (guard clauses, named conditions, elseif, match), private methods over single-use classes; security (fail-closed authorization, routes inside auth groups, file-route permissions, row-level visibility, Form Request validation, one-value-or-list fields, mass assignment, bound raw SQL, XSS, secrets and env(), throttle keys, random_int, signed public links); sensitive operations such as refunds, payouts, data exports and access grants (duplicate-submit protection, once-only execution, maker-checker approvals, self-approval, overrides, step-up auth, append-only audit logs, masking, bounded output); tests that guard all of this; the numbered anti-pattern catalogue; the code review report format. Also triggers on "review this diff", "code review", "clean this up", "is this secure", "anti-pattern", "double submit", "approval flow", "audit log", "مراجعة الكود", "جودة الكود", "تنظيف الكود", "التعليقات", "ثغرة", "أمان", "موافقات".

Use this Skill: https://skilld.dev/gh/haythamasalama/skills/laravel-code-quality

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesconditionals.md

≈1.4k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Readable conditionals

Read when: writing or reviewing an if, elseif, match, a ternary, or a condition with more than one part. Owns: guard clauses, naming conditions, elseif for sequential checks, lookup maps instead of branches.

TL;DR

  • Guard clauses first. Handle the special or failing case, return / throw / $fail(), and let the main path run last, unindented. No else after a return.
  • Name the condition. A condition with more than one part goes in a variable named for its meaning, or a private method if it is reused. The name replaces the comment you would otherwise write.
  • One if / elseif chain for an exclusive choice in the middle of a method, never a row of separate ifs.
  • match, a constant map, config or an enum method for value picking. Branches that only map one value to another are a table in disguise.

Pick the tool

The code… Use
stops for a special case (invalid input, nothing to do, a failed check) a guard clause
has a condition you have to read twice a named variable or a private method
picks one of several actions, then continues one if / elseif chain
maps one value to another match, a const array, config, or an enum method

Splitting a method so a chain becomes guard clauses is often the best move of all.

1. Guard clauses

// ❌ every case nested in one chain
private static function recordChange(Invoice $invoice, ?Customer $previous): void
{
    if (! $previous) {
        self::record($invoice, 'invoice.customer_set');
    } elseif ($previous->is($invoice->customer)) {
        self::record($invoice, 'invoice.customer_updated');
    } else {
        self::record($invoice, 'invoice.customer_moved');
    }
}

// ✅ each case leaves; the last line is the main path
private static function recordChange(Invoice $invoice, ?Customer $previous): void
{
    if (! $previous) {
        self::record($invoice, 'invoice.customer_set');

        return;
    }

    if ($previous->is($invoice->customer)) {
        self::record($invoice, 'invoice.customer_updated');

        return;
    }

    self::record($invoice, 'invoice.customer_moved');
}

Fail first rather than wrapping the happy path:

// ❌ the real work is one level deep; the failure waits in the else
if ($invoice->status === InvoiceStatus::DRAFT) {
    // … issue it …
} else {
    throw InvoiceException::alreadyIssued();
}

// ✅
throw_unless($invoice->status === InvoiceStatus::DRAFT, InvoiceException::alreadyIssued());

// … issue it …

Validation rules follow the same shape: empty value, wrong format and out-of-range each return early; the expensive checks run at the bottom.

2. Name the condition

// ❌ a ternary inside the condition
if ($isYearly ? $months > 12 : $months < 1) {
    $fail(__('validation.invalid_period'));
}

// ✅
$periodIsValid = $isYearly ? $months <= 12 : $months >= 1;

if (! $periodIsValid) {
    $fail(__('validation.invalid_period'));
}
// ❌ two checks and a comparison to decode
if ($previousCustomer && ! $previousCustomer->is($invoice->customer)) {
    self::notifyMove($invoice, $previousCustomer);
}

// ✅
$customerChanged = $previousCustomer && ! $previousCustomer->is($invoice->customer);

if ($customerChanged) {
    self::notifyMove($invoice, $previousCustomer);
}
  • Reused, or several statements long: a private method in the same class (isCompleteAddress()), not a new class.
  • Mixed concerns: group by meaning ($meetsTaxRules && $creditIsAvailable) rather than five && parts.

3. elseif for sequential checks

// ❌ separate ifs for one exclusive choice: all run, a later one can fire on state an earlier one changed
if (! $previous) {
    $key = 'invoice.customer_set';
}
if ($previous && $previous->is($invoice->customer)) {
    $key = 'invoice.customer_updated';
}
if ($previous && ! $previous->is($invoice->customer)) {
    $key = 'invoice.customer_moved';
}

// ✅ one chain; its order is the priority
if (! $previous) {
    $key = 'invoice.customer_set';
} elseif ($previous->is($invoice->customer)) {
    $key = 'invoice.customer_updated';
} else {
    $key = 'invoice.customer_moved';
}

Write elseif, not else if (PSR-12; Pint rewrites it). When the chain is the last thing a method does, or you can move it into its own method, prefer guard clauses.

4. Maps instead of branches

// ❌ branches that only pick a value
if ($this === self::PAID) {
    return 'ok';
} elseif ($this === self::CANCELLED) {
    return 'danger';
}

// ✅
return match ($this) {
    self::PAID => 'ok',
    self::CANCELLED => 'danger',
    self::DRAFT, self::ISSUED => 'warn',
};
The mapping… Use
belongs to an enum case a method on the enum (label(), tone()) so callers never branch on the case
is a fixed list inside one class a private const array
is data the business may change a config file
is a one-off pick in a method match

match is for values. When branches do different things, use guard clauses or an elseif chain; match (true) used for side effects reads worse than either. On a status enum, list every case and leave out default, so a new case fails loudly.

Verify

These find existing code too. Treat the output as a review list scoped to what you changed.

# else / elseif right after a return: guard-clause candidates
grep -rnE -B2 "\} ?(elseif|else)\b" src/ app/ --include='*.php' | grep -E "return|throw" | head

# Ternaries inside an if condition: name them
grep -rnE "if \(.*\?.*:.*\) \{" src/ app/ --include='*.php'

# else if instead of elseif
grep -rn "else if" src/ app/ --include='*.php'

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts20d3 checks · Risk SAFE
  • Gen Agent Trust Hub20d

    This skill provides a comprehensive framework for auditing and improving Laravel PHP code. It focuses on security, clean code principles, and sensitive operations, providing the AI agent with specific anti-patterns to detect and standard auditing commands to verify codebase integrity.

  • Socket20d

    No alerts

  • Snyk20d

    Risk: LOW · No issues

Signed by skilld at 54e769e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 3 weeks ago
Other metadata
metadata
{
  "summary": "Laravel code style, security, sensitive operations and code review.",
  "tags": "laravel, php, security, code-review, clean-code"
}

README badge

README badge for haythamasalama/skills/laravel-code-quality