GuideOpen
PHP - Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only PHP-specific rules and idioms.
Front matter
language: php
extensions: [".php", ".phtml", ".php3", ".php4", ".php5", ".phps"]PHP — Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only PHP-specific rules and idioms.
PR Analyzer — PHP Risk Signals
var_dump/print_r/echodebug statements left in production code@error suppression operator — masks real errors; verify it is justified// phpcs:ignore/// phpstan-ignorecomments — verify they are justified- Hardcoded credentials, database passwords, or API keys in source
eval()anywhere — almost always a security issue$_GET/$_POST/$_REQUEST/$_COOKIEused without sanitization
Code Quality — PHP Checks
- Missing type declarations on function parameters and return types
mixedreturn type used broadly — tighten to specific types- Global variables (
global $var) — pass dependencies explicitly - Long functions (>50 lines) — PHP functions tend to accumulate logic
isset()/empty()used to mask type errors instead of fixing the root cause- Missing
strict_types=1declaration at the top of the file
Security
- Flag
$_GET/$_POST/$_REQUESTused directly in SQL queries — require PDO prepared statements - Flag
mysqli_query($conn, "SELECT ... WHERE id = " . $_GET['id'])— SQL injection - Flag
echo $_GET['name']or any unescaped output — XSS; usehtmlspecialchars()withENT_QUOTES - Flag
include/requirewith user-controlled paths — local/remote file inclusion - Flag
eval()— remote code execution risk; no legitimate use in application code - Flag
shell_exec/exec/system/passthruwith user-controlled input — command injection - Flag
unserialize()on untrusted data — arbitrary object instantiation and code execution - Flag
move_uploaded_filewithout MIME type validation and extension whitelist — file upload attack - Flag
header("Location: " . $_GET['url'])without validation — open redirect - Flag missing CSRF token validation on state-changing form endpoints
Async / Concurrency
- Flag long-running synchronous operations in a request cycle — offload to a queue (Laravel Queue, RabbitMQ)
- Flag
sleep()used inside a request handler — blocks the PHP-FPM worker - Flag shared mutable state in
staticproperties accessed across requests in long-running processes (Swoole, RoadRunner) - Flag missing idempotency in queued jobs — jobs can be retried on failure
Resource Management
- Flag database connections not closed or returned to the pool (
$pdo = nullor$conn->close()) - Flag
fopen/fwritewithout a matchingfcloseon all paths - Flag
curl_initwithoutcurl_close— leaks the curl handle - Flag unbounded file uploads with no size or type restriction
- Flag sessions not explicitly closed (
session_write_close()) before long operations — session locking blocks other requests
Exception Handling
- Flag empty
catchblocks — swallowed exceptions - Flag
catch (Exception $e) {}without logging — silent failure - Flag
die()/exit()used for error handling in library code — use exceptions - Flag
@operator used to suppress errors from functions that can fail — check return values instead - Flag
trigger_errorused in new code — prefer exceptions
Performance
- Flag N+1 Eloquent / Doctrine queries — use eager loading (
with(),load(),join) - Flag
count($array)called repeatedly in a loop condition — cache the result - Flag
array_push($arr, $val)— use$arr[] = $valwhich is faster - Flag
in_arrayon large arrays without the strict third argument — useisseton a flipped array for O(1) lookup - Flag
file_get_contentson remote URLs in a request cycle — use an HTTP client with timeout and async where possible - Flag Eloquent
all()without pagination — loads entire table into memory
Idioms and Best Practices
Type Safety
- Always declare
declare(strict_types=1)at the top of every file - Use union types (
int|string) and nullable types (?string) rather thanmixed - Use typed properties on classes — avoid untyped
public $foo - Use constructor promotion for simple value objects
Modern PHP (8.x)
- Prefer
matchexpressions overswitch— strict comparison, no fall-through - Use named arguments for functions with many optional parameters
- Use
enumfor fixed sets of values instead of class constants - Use
readonlyproperties for immutable data - Use nullsafe operator (
?->) instead of nestedissetchecks - Use
first-class callable syntax(strlen(...)) instead of string references
Laravel / Symfony Specific
- Keep controllers thin — logic belongs in service classes or action classes
- Use form requests for validation — never validate in the controller directly
- Prefer Eloquent relationships over manual joins for readability
- Flag raw queries where the ORM can express the same intent safely
Author: alirezarezvani, Alireza Rezvani skills and agents. License: MIT.
Shown here with formatting for the web. Keep the author and license notice when you reuse it.
Related entries
There are more like this in the members library
The Codex library holds 7,009 entries. Members can search all of them by meaning and read every one in full.