PHP Cursor Rules: AI-Powered Development Best Practices

Cursor rules for PHP development enforcing PSR-12 standards, modern PHP 8+ features, and clean code principles with AI assistance for production-ready code.

January 15, 2024by PromptGenius Team
phpcursor-rulesai-codingbest-practicespsr-12

Overview

Professional cursor rules for PHP development that enforce modern standards and best practices. These rules help AI assistants generate clean, secure, and maintainable PHP code with proper context and documentation.

Note:

Enforces PSR-12 standards, PHP 8+ features, and context-aware code generation for production-ready development.

Rules Configuration

---
description: Enforces best practices for PHP development, focusing on context-aware code generation, modern patterns, and maintainable architecture. Provides comprehensive guidelines for writing clean, efficient, and secure PHP code with proper context.
globs: **/*.php
---
# PHP Best Practices

You are an expert in PHP programming, Laravel, Symfony, and related PHP technologies.
You understand modern PHP development practices, architectural patterns, and the importance of providing complete context in code generation.

### Context-Aware Code Generation
- Always provide complete class context including namespaces and use statements
- Include relevant configuration files (composer.json) when generating projects
- Generate complete method signatures with proper parameters, return types, and PHPDoc
- Include comprehensive PHPDoc blocks explaining the purpose, parameters, and return values
- Provide context about the class's role in the larger system architecture

### Code Style and Structure
- Follow PSR-12 style guide and clean code principles
- Structure code in logical namespaces following domain-driven design
- Implement proper separation of concerns (controllers, models, services, repositories)
- Use modern PHP features (typed properties, attributes, enums) appropriately
- Maintain consistent code formatting using PHP-CS-Fixer
- Use proper autoloading and namespace structure

### Framework Best Practices
- Use Laravel/Symfony best practices and patterns
- Implement proper dependency injection and service containers
- Configure proper routing and middleware
- Use proper ORM patterns and database migrations
- Implement proper error handling and logging
- Configure proper testing setup (PHPUnit, Pest)

### Testing and Quality
- Write comprehensive unit tests with proper test context
- Include integration tests for critical paths
- Use proper mocking strategies with PHPUnit
- Implement E2E tests with Laravel Dusk or similar
- Include performance tests for critical components
- Maintain high test coverage for core business logic

### Security and Performance
- Implement proper input validation and sanitization
- Use secure authentication and token management
- Configure proper CORS and CSRF protection
- Implement rate limiting and request validation
- Use proper caching strategies
- Optimize database queries and indexes

### API Design
- Follow RESTful principles with proper HTTP methods
- Use proper status codes and error responses
- Implement proper versioning strategies
- Document APIs using OpenAPI/Swagger
- Include proper request/response validation
- Implement proper pagination and filtering

### Database and Data Access
- Use proper ORM patterns (Eloquent, Doctrine)
- Implement proper transaction management
- Use database migrations
- Optimize queries and use proper indexing
- Implement proper connection pooling
- Use proper database isolation levels

### Build and Deployment
- Use Composer for dependency management
- Implement proper CI/CD pipelines
- Use Docker for containerization
- Configure proper environment variables
- Implement proper logging and monitoring
- Use proper deployment strategies

### Examples

```php
<?php

namespace App\Services;

use App\Models\User;
use App\Exceptions\ApiException;
use Illuminate\Support\Facades\Cache;

/**
 * UserService handles user-related operations.
 * Provides methods for user management and authentication.
 */
class UserService
{
    private $apiClient;
    private $cache;

    public function __construct($apiClient, $cache)
    {
        $this->apiClient = $apiClient;
        $this->cache = $cache;
    }

    /**
     * Finds a user by their email address.
     *
     * @param string $email The email address to search for
     * @return User|null The user if found, null otherwise
     * @throws ApiException If the request fails
     */
    public function findUserByEmail(string $email): ?User
    {
        try {
            $cachedUser = $this->cache->get("user:{$email}");
            if ($cachedUser) {
                return new User($cachedUser);
            }

            $userData = $this->apiClient->get("/users?email={$email}");
            if ($userData) {
                $user = new User($userData);
                $this->cache->set("user:{$email}", $userData);
                return $user;
            }
            return null;
        } catch (\Exception $e) {
            throw new ApiException("Failed to find user by email: " . $e->getMessage());
        }
    }
}

/**
 * Tests for UserService functionality.
 */
class UserServiceTest extends \PHPUnit\Framework\TestCase
{
    private $service;
    private $apiClient;
    private $cache;

    protected function setUp(): void
    {
        $this->apiClient = $this->createMock(\App\Clients\ApiClient::class);
        $this->cache = $this->createMock(\Illuminate\Cache\Repository::class);
        $this->service = new UserService($this->apiClient, $this->cache);
    }

    public function testFindUserByEmailWhenUserExists()
    {
        // Given
        $email = "[email protected]";
        $userData = ["id" => 1, "email" => $email];
        $this->apiClient->method('get')->willReturn($userData);

        // When
        $result = $this->service->findUserByEmail($email);

        // Then
        $this->assertNotNull($result);
        $this->assertEquals($email, $result->getEmail());
        $this->apiClient->expects($this->once())
            ->method('get')
            ->with("/users?email={$email}");
    }

    public function testFindUserByEmailWhenUserNotFound()
    {
        // Given
        $email = "[email protected]";
        $this->apiClient->method('get')->willReturn(null);

        // When
        $result = $this->service->findUserByEmail($email);

        // Then
        $this->assertNull($result);
        $this->apiClient->expects($this->once())
            ->method('get')
            ->with("/users?email={$email}");
    }
}

Key Features

📦

Complete Code Context

Full namespaces, use statements, and PHPDoc blocks included automatically

🚀

Modern PHP 8+ Features

Typed properties, attributes, enums, and constructor promotion

Testing Built-In

Automatic generation of PHPUnit tests with proper mocking

🔒

Security First

Input validation, CSRF/CORS protection, and secure authentication

📐

PSR-12 Compliant

Consistent code formatting following PHP standards

🏗️

Clean Architecture

Proper separation of concerns and dependency injection

Installation

1

Choose Your IDE

Select the appropriate file path based on your development environment.

2

Create the Rules File

Create the cursor rules file in your project:

Create file: .cursor/rules/php.mdc

3

Add the Rules Configuration

Copy the rules configuration below into your newly created file.

4

Start Coding

Your AI assistant will now follow PHP best practices automatically.

Use Cases

Web Applications

Full-stack PHP applications with modern frameworks and proper architecture

API Development

RESTful APIs with proper versioning, validation, and documentation

Microservices

Containerized PHP services with clean boundaries and proper testing

Legacy Refactoring

Modernize existing PHP codebases with current best practices

Standards Reference

StandardDescriptionEnforcement
PSR-12Extended coding style guideAutomatic formatting
Type SafetyStrict type hints and return typesRequired for all methods
PHPDocComprehensive documentation blocksRequired for classes and methods
TestingPHPUnit/Pest test coverageAuto-generated with code
SecurityInput validation and sanitizationBuilt into patterns
ArchitectureClean separation of concernsEnforced via structure

Note:

Combine these rules with PHP-CS-Fixer and PHPStan for maximum code quality enforcement.

Best Practices

Code Organization

  • Controllers handle HTTP requests only
  • Services contain business logic
  • Repositories manage data access
  • Models represent domain entities

Type Safety

  • Always use type hints for parameters
  • Always declare return types
  • Use nullable types when appropriate
  • Leverage union types in PHP 8+

Testing Strategy

  • Unit tests for business logic
  • Integration tests for database operations
  • Feature tests for user workflows
  • Mocking for external dependencies

Note:

These rules work with any PHP project but are optimized for Laravel and Symfony frameworks. Adjust framework-specific sections as needed.