creating-helpers

moonpixels's avatarfrom moonpixels

Create global PHP helper functions for common utility operations. Use when creating helper functions, utility functions, or when user mentions helpers, global functions, or utility methods.

⭐1stars🔀0forks📁View on GitHub🕐Updated Jan 10, 2026⬇️1downloads

When & Why to Use This Skill

This Claude skill automates the creation of global PHP helper functions, specifically optimized for Laravel environments. It ensures high-quality code generation by enforcing strict typing, snake_case naming conventions, and safety checks to prevent function collisions. By providing a standardized framework for utility methods, it helps developers maintain a clean codebase while accelerating the implementation of common formatting, string manipulation, and domain-specific logic.

Use Cases

  • Case 1: Generating standardized data formatting helpers for currency, dates, and percentages to ensure UI consistency.
  • Case 2: Creating custom Laravel route and request helpers to simplify conditional logic within views and controllers.
  • Case 3: Implementing domain-specific utility functions, such as retrieving user avatar URLs or calculating order totals, following pure function principles.
  • Case 4: Refactoring repetitive inline code into globally accessible, type-hinted utility methods to improve project maintainability and reduce technical debt.
namecreating-helpers
descriptionCreate global PHP helper functions for common utility operations. Use when creating helper functions, utility functions, or when user mentions helpers, global functions, or utility methods.

Create Laravel Helpers

Create global PHP helper functions for common utility operations. Helpers are simple, pure functions for formatting, string manipulation, and convenience wrappers. For complex logic with dependencies or side effects, use Action classes instead.

File Structure

Helpers are defined in:

app/Helpers/Helpers.php

This file is autoloaded via composer.json:

"autoload": {
    "files": [
        "app/Helpers/Helpers.php"
    ]
}

Core Conventions

1. Helper Function Structure

<?php

declare(strict_types=1);

if (! function_exists('helper_name')) {
    function helper_name(string $param): string
    {
        // Implementation
        return $result;
    }
}

Key Requirements:

  • Always include declare(strict_types=1);
  • Wrap in function_exists() check
  • Use snake_case for function names
  • Full type hints on parameters and return type
  • Keep functions simple and focused

2. Naming Conventions

  • Use descriptive snake_case names
  • Prefix with domain if specific: user_avatar_url(), order_total()
  • Keep generic helpers unprefixed: money(), percentage()

Examples

Example 1: Route Helper

if (! function_exists('route_is')) {
    /**
     * Determine if the current route matches the given patterns.
     */
    function route_is(string ...$patterns): bool
    {
        return request()->routeIs(...$patterns);
    }
}

Example 2: Formatting Helper

if (! function_exists('money')) {
    /**
     * Format a value as money.
     */
    function money(int|float $amount, string $currency = 'USD'): string
    {
        return number_format($amount, 2) . ' ' . $currency;
    }
}

Example 3: Domain Helper

if (! function_exists('user_avatar_url')) {
    /**
     * Get the avatar URL for a user.
     */
    function user_avatar_url(User $user): ?string
    {
        return $user->avatar_path
            ? Storage::url($user->avatar_path)
            : null;
    }
}

Anti-Patterns

❌ Don't Do This

// Don't skip function_exists check
function helper_name() {} // ❌ Wrap in check

// Don't omit type hints
function format_date($date) {} // ❌ Type hint

// Don't create complex helpers
function process_entire_order($order) {
    // 50 lines of logic ❌ Use Action class
}

// Don't use camelCase
function userName() {} // ❌ Use snake_case

✅ Do This Instead

// Always use function_exists
if (! function_exists('helper_name')) {
    function helper_name(string $param): string {}
}

// Type hint everything
function format_date(Carbon $date): string {}

// Keep simple
function user_initials(User $user): string {
    return strtoupper(substr($user->name, 0, 1));
}

// Use snake_case
function user_name(User $user): string {}

Helpers vs Actions

Use Helper Use Action
Pure formatting/transformation Requires dependency injection
No side effects Has side effects (DB, API, email)
Under 10 lines Complex business logic
No mocking needed Testing requires mocks

Quality Standards

  • All helpers must pass PHPStan level 8
  • 100% type coverage required
  • Code formatted with Pint
  • Add PHPDoc describing purpose
  • Keep functions pure (no side effects) when possible