Code Review in PHP: 8 Key Things Every Developer Should Check

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Code Review Is Not About "Finding Bugs"

\n\n

Imagine: you've written perfect code. It works, passes tests, even the client is happy. But then a colleague comes to the code review and... it begins. "Why is there == here and not ===?", "This method does 5 things at once", "Where is the exception handling?". Sound familiar?

\n\n

In reality, code review in PHP is not about hunting for bugs (although that too). It's a way to make code readable, secure, and maintainable. Especially in PHP, where over 25 years of evolution, many "pitfalls" have accumulated.

\n\n

In this article, we'll break down what to check during a PHP code review so that the code not only works but is a benchmark of quality. Ready? Let's go!

\n\n

1. Typing and Strict Modes

\n\n

PHP 7+ and 8 gave us powerful tools for type control. But many projects still live in "chaos mode".

\n\n

What to check:

\n\n
    \n
  • Are argument and return types declared? If a method expects int, but you pass string — that's a talking point.
  • \n
  • Is there declare(strict_types=1); at the beginning of the file? Without this, PHP will implicitly cast types, which often leads to hidden bugs.
  • \n
\n\n
<?php\ndeclare(strict_types=1);\n\n// Bad: no types\nfunction calculate($a, $b) {\n    return $a + $b;\n}\n\n// Good: strict typing\nfunction calculate(int $a, int $b): int {\n    return $a + $b;\n}\n?>\n
\n\n

Code review tip: if you see mixed or array without specification — ask the question "What exactly is expected here?".

\n\n

2. Security: SQL Injections and XSS

\n\n

PHP is like a Swiss Army knife: many functions, and many of them are dangerous. Especially in legacy code.

\n\n

Top 3 red flags:

\n\n
    \n
  • Concatenation in SQL queries. If you see "SELECT * FROM users WHERE id = ".$_GET['id'] — it's a cry for help.
  • \n
  • Outputting data without escaping. echo $userInput; — hello XSS.
  • \n
  • Storing passwords in plain text. Seriously, this still happens.
  • \n
\n\n
<?php\n// Dangerous!\n$sql = "SELECT * FROM users WHERE email = '" . $email . "'";\n\n// Safe (PDO)\n$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");\n$stmt->execute(['email' => $email]);\n?>\n
\n\n

During a PHP code review, always check the handling of external data. Any $_GET, $_POST, $_COOKIE must go through filtering.

\n\n

3. Following PSR Standards

\n\n

The PHP community created PSR (PHP Standards Recommendations) not just for show. This is the language all PHP developers speak.

\n\n

What should be in every PR:

\n\n
    \n
  • PSR-12 (coding style): 4-space indentation, braces on a new line for classes, spaces around operators.
  • \n
  • PSR-4 (autoloading): namespaces must match the file path.
  • \n
  • Class names — StudlyCaps, methods — camelCase, constants — UPPER_CASE.
  • \n
\n\n
<?php\nnamespace App\\Service;\n\nclass UserService  // PSR-4: file should be in src/Service/UserService.php\n{\n    public function getUserById(int $id): ?User  // camelCase\n    {\n        // ...\n    }\n}\n?>\n
\n\n

Life hack: if the project doesn't have automatic formatting (PHP CS Fixer, PHP_CodeSniffer) — that's the first item for improvement.

\n\n

4. Complexity and Architecture

\n\n

Good code is code that can be read like a book. Bad code is like deciphering an ancient manuscript.

\n\n

What to look for:

\n\n
    \n
  • Method length. If a method is longer than 20-30 lines, it's mo

Blogs

Book Recommendations