/spwf:php-code-quality-reviewerfrom spwf
Analyse PHP code and flag bad practices with suggested fixes, grouped by risk category. Use before merging PHP changes, when reviewing a PR touching backend logic, or when auditing a legacy PHP codebase. Covers correctness, security, performance, maintainability, and modern PHP opportunities. Framework-aware (Laravel, Symfony, WordPress). Read-only — flags issues and suggests fixes, never applies them. Complement to security-scan (which covers OWASP/injection in depth) and php-code-simplifier (which applies safe refactors). Called as a step by the php-code-quality-reviewer agent, which is why Claude may invoke it. Do not start it unprompted: run it standalone only when the user asks.
Ships in spwf. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Analyse PHP code for bad practices across five risk categories. Produce a structured report with specific findings, suggested fixes, and confidence ratings. Read-only — never edits files.
| Skill | What it does | When to use |
|---|---|---|
php-code-quality-reviewer (this) | Flags bad practices, design issues, correctness | Pre-merge review of PHP changes |
php-code-simplifier | Applies safe refactors | After implementation, before PR |
security-scan | Deep OWASP + SQL injection review | High-risk surfaces: auth, billing, input handling |
spwf:simplify | Language-agnostic dead code removal | Quick pass on any changed file |
# PHP version
cat composer.json 2>/dev/null | grep '"php"'
# Framework detection
grep -q "laravel/framework" composer.json 2>/dev/null && echo "Laravel"
grep -q "symfony/framework-bundle" composer.json 2>/dev/null && echo "Symfony"
grep -rn "add_action\|add_filter" --include="*.php" . -l 2>/dev/null | head -1 && echo "WordPress"
# Static analysis tools available
ls vendor/bin/phpstan vendor/bin/psalm vendor/bin/phpcs vendor/bin/php-cs-fixer 2>/dev/null
# Changed files (if reviewing a branch)
git diff --name-only main...HEAD -- '*.php' 2>/dev/null
Load the relevant framework reference file before reviewing framework-specific code:
references/laravel-patterns.mdreferences/wordpress-patterns.mdreferences/symfony-patterns.mdWork through each category. Skip categories with no relevant code — note the skip so the reader knows it was considered.
Issues that cause wrong behaviour, silent data corruption, or unexpected failures.
Loose comparison where strict is required
// Flag — 0 == 'foo' is true in PHP
if ($status == 0) { ... }
if ($role == false) { ... }
// Suggest
if ($status === 0) { ... }
Detection: grep -n " == \| != " --include="*.php" -r . — review each result for type-safety risk.
empty() hiding valid falsy values
// Flag — empty('0') is true; valid value silently ignored
if (empty($postCode)) { return; }
if (!empty($quantity)) { process($quantity); }
// Suggest — be explicit about what you're checking
if ($postCode === null || $postCode === '') { return; }
if ($quantity !== null && $quantity !== 0) { process($quantity); }
isset() vs array_key_exists() when null is meaningful
// Flag — isset() returns false if key exists but value is null
if (isset($config['timeout'])) { ... }
// Suggest — when null is a valid configured value
if (array_key_exists('timeout', $config)) { ... }
Catching Throwable or Exception too broadly
// Flag — swallows unexpected errors silently
try {
$this->process();
} catch (\Throwable $e) {
// silent
}
// Suggest — catch specific exceptions; log or rethrow others
try {
$this->process();
} catch (PaymentException $e) {
$this->handlePaymentFailure($e);
} catch (\Throwable $e) {
Log::error('Unexpected error in process', ['exception' => $e]);
throw $e;
}
Error suppression with @
// Flag — hides real errors; makes debugging impossible
$result = @file_get_contents($url);
// Suggest — handle the failure explicitly
$result = file_get_contents($url);
if ($result === false) {
throw new \RuntimeException("Failed to fetch: $url");
}
Flag injection, XSS, and access control issues. For deeper SQL injection analysis, direct the user to /spwf:security-scan.
SQL injection
// Flag — string interpolation in query
$sql = "SELECT * FROM users WHERE id = $id";
$results = $wpdb->get_results($sql);
// Suggest
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM users WHERE id = %d", $id));
// Or Laravel: User::where('id', $id)->get();
Detection: grep -n "\"SELECT\|'SELECT\|\"INSERT\|\"UPDATE\|\"DELETE" --include="*.php" -r .
Unescaped output (XSS)
// Flag
echo $_GET['name'];
echo $user->bio; // user-controlled content
// Suggest — plain PHP
echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
// Suggest — WordPress
echo esc_html($name);
echo esc_attr($attribute);
echo esc_url($url);
WordPress-specific: missing nonce verification
// Flag — form handler without nonce check
if (isset($_POST['submit'])) {
update_option('my_setting', $_POST['value']);
}
// Suggest
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'my_action')) {
wp_die('Security check failed');
}
update_option('my_setting', sanitize_text_field($_POST['value']));
WordPress-specific: missing capability check
// Flag — no capability check before sensitive action
function my_admin_action() {
delete_user(intval($_POST['user_id']));
}
// Suggest
function my_admin_action() {
if (!current_user_can('delete_users')) {
wp_die('Insufficient permissions');
}
delete_user(intval($_POST['user_id']));
}
N+1 queries
// Flag — query inside loop
foreach ($posts as $post) {
$author = User::find($post->author_id); // N queries
echo $author->name;
}
// Suggest — Laravel eager loading
$posts = Post::with('author')->get();
foreach ($posts as $post) {
echo $post->author->name; // 1 query total
}
// WordPress equivalent
$posts = get_posts(['numberposts' => -1]);
$author_ids = array_column($posts, 'post_author');
$authors = get_users(['include' => $author_ids]);
Detection: grep -n "find\|get_user\|get_post\|query" --include="*.php" -r . — look for these inside foreach or for loops.
Unbounded queries — missing pagination
// Flag — returns all records regardless of count
$users = User::all();
$orders = Order::where('status', 'pending')->get();
// Suggest
$users = User::paginate(50);
$orders = Order::where('status', 'pending')->cursor(); // for processing
Repeated expensive calls in loops
// Flag
foreach ($items as $item) {
$config = Config::get('pricing'); // re-fetches each iteration
$tax = TaxService::getRate($item->country); // potential DB call
}
// Suggest — hoist invariants out of the loop
$config = Config::get('pricing');
foreach ($items as $item) {
$tax = $taxByCountry[$item->country] ??= TaxService::getRate($item->country);
}
SELECT * on large tables
// Flag
$users = DB::table('users')->get();
// Suggest — select only needed columns
$users = DB::table('users')->select('id', 'name', 'email')->get();
God methods — doing too much
// Flag — method with multiple responsibilities
public function processOrder($orderId) {
// validate order
// check inventory
// charge payment
// send email
// update analytics
// 200+ lines
}
Flag any method over ~50 lines with multiple distinct phases. Suggest extracting named private methods or dedicated service classes per responsibility.
Boolean parameters hiding intent
// Flag — what do true and false mean?
$this->render($template, true, false);
sendEmail($user, true);
// Suggest — named methods or typed options
$this->renderWithLayout($template);
sendWelcomeEmail($user);
Mixed array shaped as implicit object
// Flag — opaque array with string keys
$user['email']
$order['total_price']
$config['retry_count']
// Suggest — typed DTO or value object (especially for complex shapes)
$user->email
$order->totalPrice()
Flag when: the array is passed across method boundaries or stored in properties. Inline arrays within a single method scope are acceptable.
Business logic in controllers or templates
// Flag — pricing logic in controller
public function checkout(Request $request) {
$subtotal = 0;
foreach ($request->items as $item) {
$subtotal += $item['price'] * $item['qty'];
}
$tax = $subtotal * 0.2;
$total = $subtotal + $tax;
// ...
}
// Suggest — extract to service
public function checkout(Request $request) {
$total = $this->pricingService->calculateTotal($request->items);
// ...
}
Static/global coupling outside integration layers
// Flag — global state in domain logic
function calculateTax($amount) {
global $wpdb;
$rate = $wpdb->get_var("SELECT rate FROM tax_rates WHERE ...");
return $amount * $rate;
}
// Suggest — inject repository
class TaxCalculator {
public function __construct(private TaxRateRepository $rates) {}
public function calculate(Money $amount): Money { ... }
}
Exception: thin WordPress integration adapters that legitimately need $wpdb or $wp_query are acceptable at the boundary.
Check PHP version first. Only flag improvements available in the project's PHP version.
| Feature | PHP version | Flag when |
|---|---|---|
| Constructor property promotion | 8.0+ | DTO/value object with manual $this->x = $x assignment |
Nullsafe operator ?-> | 8.0+ | Nested null-guard chains |
| Named arguments | 8.0+ | Function call with 3+ positional args where intent is unclear |
match expression | 8.0+ | switch with simple value mapping, no side effects |
| Enums | 8.1+ | Magic strings used as status/type values across multiple files |
| Readonly properties | 8.1+ | DTOs or value objects whose properties should be immutable |
readonly class | 8.2+ | Entire class with only readonly properties |
declare(strict_types=1) | All | Missing from any file that handles type-sensitive logic |
Load references/php-version-capabilities.md for the full feature list with caveats.
Report which tools are available and their output. Run only if tools are installed:
./scripts/php-quality-baseline.sh
Or manually:
# PHPStan
vendor/bin/phpstan analyse --level=max --no-progress 2>/dev/null | tail -20
# Psalm
vendor/bin/psalm --no-progress --output-format=compact 2>/dev/null | tail -20
# PHP CodeSniffer
vendor/bin/phpcs --standard=PSR12 --report=summary 2>/dev/null | tail -10
Include tool output as a summary section in the report — do not replicate findings already captured by the tools.
## PHP Code Quality Review
**PHP version**: {version}
**Framework**: {Laravel | Symfony | WordPress | Plain PHP}
**Files reviewed**: {N}
**Static analysis**: {PHPStan level X | Psalm | not installed}
---
### Correctness
| # | File:Line | Issue | Suggestion | Confidence |
|---|---|---|---|---|
| 1 | `app/Services/Order.php:42` | Loose comparison `==` on status | Use `===` | High |
### Security
| # | File:Line | Issue | Suggestion | Confidence |
|---|---|---|---|---|
### Performance
| # | File:Line | Issue | Suggestion | Confidence |
|---|---|---|---|---|
### Maintainability
| # | File:Line | Issue | Suggestion | Confidence |
|---|---|---|---|---|
### Modern PHP opportunities
| # | File:Line | Current pattern | Suggested pattern | PHP version required |
|---|---|---|---|---|
---
### Summary
| Category | Critical | High | Medium | Low |
|---|---|---|---|---|
| Correctness | | | | |
| Security | | | | |
| Performance | | | | |
| Maintainability | | | | |
| Modern PHP | — | — | | |
### Top 3 to fix first
1. {Highest impact finding — file:line}
2.
3.
### Next steps
- Run `/spwf:php-code-simplifier` to apply safe refactors
- For SQL injection deep review: `/spwf:security-scan`
- For automated static analysis: `vendor/bin/phpstan analyse --level=9`
empty() and isset() look similar but catch different things. empty() catches null, '', 0, '0', [], false. isset() only catches null and undefined. Neither is always correct — always read the intent.== behaviour in PHP is notoriously surprising. 0 == 'foo' is true (pre-PHP 8), '' == false is true, '1' == true is true. Flag loose comparisons on any non-boolean values.esc_html, esc_attr, esc_url, esc_js, esc_textarea are not interchangeable. Swapping them is a correctness issue, not a style issue.find() inside a foreach is an N+1 only if the foreach iterates a collection from the database. If it iterates a small in-memory array (e.g. $statuses), it is fine.Load on demand:
references/php-smell-catalog.md — full smell catalogue with detection queriesreferences/laravel-patterns.md — Eloquent N+1, service containers, form requests, policiesreferences/wordpress-patterns.md — escaping contexts, nonces, capabilities, $wpdb, hooksreferences/symfony-patterns.md — DI, voters, event subscribers, console commandsreferences/php-version-capabilities.md — feature availability by PHP version with caveatsWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub simon-potter/spwf --plugin spwf38 skills in this plugin
/address-reviewPhase 6.5 — Address Review. Turn review feedback into action. Sources feedback from either a local report file (e.g. {branch}-review.md from /spwf:pr-review or {branch}-self-review.md from /spwf:simplify) or fetched human comments on an open PR/MR. For each item runs READ → VERIFY → EVALUATE → implement-or-push-back, in priority order (blocking → important → nit). Forbids performative agreement ("you're absolutely right"). Use after pr-review, or whenever a reviewer leaves comments./agent-optimiseCross-cutting — Audit both the project .claude/ and personal ~/.claude/ for quality issues. Covers CLAUDE.md scope and length, agent descriptions, skill frontmatter, and settings.json conflicts. Always audits both scopes. Produces a prioritised P1/P2/P3 fix list./approve-planPhase 2 — Approve plan. Reviews the task list from spec for quality (atomicity, testability, clarity), then applies four adversarial lenses (Skeptic, Architect, Minimalist, Security) as advisory input for the human reviewer. Quality issues are blocking; adversarial findings are advisory. Security lens identifies tasks touching auth, billing, user input, or secrets so the builder has explicit awareness before writing those tasks. Presents everything for explicit human go/no-go before building starts./branch-rescueRecovery skill — moves commits that landed on the base branch onto a proper feature branch and resets local base, without touching origin. Resolves the active OpenSpec change, detects the pre-spec base commit (subject-line grep with manual-confirm fallback), performs three local-only safe git operations, and surfaces the force-push command for the user to run manually. Run standalone; pr-create's rescue offer runs the same procedure inline (_shared/branch-management.md §4), because this skill is user-only./briefPhase 1.5 — Explain a planned change before it is built. Runs between spec and approve-plan, so a mismatch costs a conversation rather than a change. Five sections, expanding then summarising — what will be built, why this way, the choices you didn't make, what it touches, and a short summary. Never blocks: it prints, offers one skippable prompt, and returns. Calibrated via .spwf/learner.md. Companion to recap and understand, which teach after the fact; this teaches before. Runs on changes, not bug fixes./buildPhase 3 orchestrator — Full Red-Green-Refactor cycle. For each pending task: invokes write-tests (Red: write failing tests), then opsx:apply (Green: implement via OpenSpec), then run-tests (Verify: confirm green before moving on). Loops until all tasks complete. After all tasks complete, runs opsx:verify (spec sign-off), then recommends simplify (Refactor).