Lint like a pro

A linter reads code without running it and reports what a person would miss. In PHP the checks form a ladder: the parser first, then style, then a static analyzer that follows the types.

php -l

The fastest check runs before everything else: php -l parses one file and reports syntax errors. Run it on every file you touch - the full guide is php -l, and the family overview is PHP linters we use.

php -l course/041_generators.php

No syntax errors detected in course/041_generators.php

The command runs from the folder of the course, and the message names the file it read. A parse error stops it with the line number instead, before any other tool has a chance to look at the code.

PHPCS rules

Syntax is only step one. PHPCS checks style against a shared ruleset: indentation, line length, naming. See PHPCS for setup and the sniff list.

vendor/bin/phpcs --standard=PSR12 api.php

That command needs PHPCS from the project, which this image does not carry, so it shows no output here. It reports each violation with its file, line and rule, and exits with a non-zero status while anything is left.

PHPStan and Psalm

Static analyzers read code without running it and find bugs tests miss: wrong types, dead branches, unchecked nulls. PHPStan and Psalm both work - start at a low level and raise it file by file. Read PHPStan and Psalm for the comparison.

Typed signatures give analyzers facts to check, so write them everywhere:

<?php function discount(float $price, float $rate): float { return $price - ($price * $rate); } echo discount(100.0, 0.2), PHP_EOL;

php discount.php

80

The two float parameters and the float return tell an analyzer what the function takes and gives back, so a caller with a string is reported before the program runs. The plain run only shows the answer, which is why the types carry the extra value.

One style for the team

One style for the team beats ten perfect personal styles. Agree on line length, brace placement, and naming once, enforce it with tools, and stop debating. Long lines are the first thing to fix: wrap long lines.

Exercises

  1. Break the syntax of discount.php on purpose and run php -l on it.
  2. Write a function with a return type that the body does not satisfy and explain what the error says.
  3. Run phpcs --standard=PSR12 on a file of your project if the tool is installed.
  4. Count the characters of your longest line and shorten it to the agreed limit.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does php -l check?

    Answer

    Only the syntax of one file

    The lint flag parses the file and stops, so a style or type problem is not its business.

  2. What does PHPCS check?

    Answer

    Style against a shared ruleset

    The sniffs compare the code with rules for indentation, line length and naming.

  3. What do PHPStan and Psalm do?

    Answer

    They read the code without running it and report type problems

    A static analyzer follows the values through the code and reports what it can prove is wrong.

  4. How should a team start with a static analyzer?

    Answer

    At a low level, raised file by file

    A low level gives a short list the team can fix, and the level moves up as the list stays empty.

  5. What does this program print? <?php function discount(float $price, float $rate): float { return $price - ($price * $rate); } echo discount(100.0, 0.2);

    Answer

    80

    The rate takes a fifth of the price away, and 100 minus 20 is 80.

  6. What does this program print? <?php function half(int $n): int { return intdiv($n, 2); } echo half(9);

    Answer

    4

    intdiv drops the remainder, so nine halves to four.

  7. What does this program print? <?php function price(float $p): string { return number_format($p, 2); } echo price(3.5);

    Answer

    3.50

    number_format with two decimals pads the value to two places after the dot.

  8. What does this program print? <?php class A { public function b(): int { return 1; } } echo (new A())->b();

    Answer

    1

    The brace placement is style; the method still returns the integer one.

  9. A file passes php -l. What does that prove?

    Answer

    That it parses, and nothing more

    The check is about syntax, so logic, style and types need the other tools.

  10. Why agree on one style instead of ten personal ones?

    Answer

    The team stops debating and the tools can enforce one set of rules

    A single written rule is what a formatter and a linter can check, and reviews stop arguing about taste.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above. PHPCS is not in this image, so its command shows no output.

Article author: Arthur Isaev