Bug fix knowledge

From dykyi-roman/awesome-claude-code · View interactive version

Bug fix knowledge is an AI agent skill from the GitHub repository dykyi-roman/awesome-claude-code. Bug fix knowledge base. Provides bug categories, symptoms, fix patterns, and minimal intervention principles for PHP 8.4 projects.

How to use this skill

Skill file

Bug Fix Knowledge Base

Comprehensive knowledge for diagnosing and fixing bugs in PHP applications following DDD, CQRS, and Clean Architecture patterns.

Bug Categories and Symptoms

1. Logic Errors

Symptoms:

Common Causes:

Fix Pattern:

// Before: Logic error
if ($amount > $limit) { // Should be >=
    throw new LimitExceededException();
}

// After: Fixed
if ($amount >= $limit) {
    throw new LimitExceededException();
}

2. Null Pointer Issues

Symptoms:

Common Causes:

Fix Pattern:

// Before: Null pointer risk
$user = $this->userRepository->find($id);
$email = $user->getEmail(); // Crashes if user is null

// After: Safe with null check
$user = $this->userRepository->find($id);
if ($user === null) {
    throw new UserNotFoundException($id);
}
$email = $user->getEmail();

// Alternative: Null coalescing
$email = $user?->getEmail() ?? throw new UserNotFoundException($id);

3. Boundary Issues

Symptoms:

Common Causes:

Fix Pattern:

// Before: Boundary issue
$firstItem = $items[0]; // Crashes if empty

// After: Safe boundary check
if ($items === []) {
    throw new EmptyCollectionException('items');
}
$firstItem = $items[0];

// Alternative: Using first() with default
$firstItem = $items[0] ?? throw new EmptyCollectionException('items');

4. Race Conditions

Symptoms:

Common Causes:

Fix Pattern:

// Before: Race condition
if (!$this->repository->exists($id)) {
    $this->repository->save($entity); // Another process might insert between check and save
}

// After: Atomic operation with locking
$this->lockManager->acquire("entity:$id");
try {
    if (!$this->repository->exists($id)) {
        $this->repository->save($entity);
    }
} finally {
    $this->lockManager->release("entity:$id");
}

// Alternative: Database-level uniqueness
// Use UNIQUE constraint + INSERT ... ON DUPLICATE KEY

5. Resource Leaks

Symptoms:

Common Causes:

Fix Pattern:

// Before: Resource leak
$handle = fopen($path, 'r');
$content = fread($handle, filesize($path));
// Missing fclose()

// After: Proper resource management
$handle = fopen($path, 'r');
try {
    $content = fread($handle, filesize($path));
} finally {
    fclose($handle);
}

// Better: Use high-level functions
$content = file_get_contents($path);

6. Exception Handling Issues

Symptoms:

Common Causes:

Fix Pattern:

// Before: Swallowed exception
try {
    $this->service->process($data);
} catch (Exception $e) {
    // Silent failure - bug hidden
}

// After: Proper exception handling
try {
    $this->service->process($data);
} catch (ValidationException $e) {
    throw new ProcessingFailedException(
        "Failed to process data: {$e->getMessage()}",
        previous: $e
    );
}

7. Type Issues

Symptoms:

Common Causes:

Fix Pattern:

// Before: Type issue
function calculate($amount) { // No type hint
    return $amount * 1.1; // Fails if string passed
}

// After: Strict typing
declare(strict_types=1);

function calculate(float $amount): float {
    return $amount * 1.1;
}

8. SQL Injection

Symptoms:

Common Causes:

Fix Pattern:

// Before: SQL injection vulnerability
$query = "SELECT * FROM users WHERE email = '$email'";

// After: Parameterized query
$query = "SELECT * FROM users WHERE email = :email";
$stmt = $pdo->prepare($query);
$stmt->execute(['email' => $email]);

9. Infinite Loops

Symptoms:

Common Causes:

Fix Pattern:

// Before: Potential infinite loop
while ($item = $queue->pop()) {
    $this->process($item);
    // If process() adds items back to queue, infinite loop
}

// After: Safe with limit
$maxIterations = 10000;
$iterations = 0;
while ($item = $queue->pop()) {
    if (++$iterations > $maxIterations) {
        throw new MaxIterationsExceededException($maxIterations);
    }
    $this->process($item);
}

Minimal Intervention Principles

1. Single Responsibility Fix

2. Preserve Behavior

3. Backward Compatibility

4. Test First

Fix Validation Checklist

Before applying a fix, verify:

  1. Reproduction Test Exists
  1. Minimal Change
  1. No Regressions
  1. Code Quality
  1. Documentation

DDD-Specific Bug Patterns

Domain Layer Bugs

Application Layer Bugs

Infrastructure Layer Bugs

Quick Reference: Fix by Error Message

| Error Message | Likely Bug | Quick Fix |

|--------------|------------|-----------|

| "Call to member function on null" | Null pointer | Add null check |

| "Undefined array key" | Boundary issue | Check array_key_exists |

| "Type error: Argument X" | Type issue | Add type validation |

| "Maximum execution time" | Infinite loop | Add iteration limit |

| "Allowed memory exhausted" | Resource leak | Close resources in finally |

| "Integrity constraint violation" | Race condition | Add locking/transaction |

| "Cannot modify readonly property" | Immutability violation | Create new instance |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.