php -l and lint-php.sh

Install

Nothing to install. php -l ships with every PHP interpreter. The wrapper scripts/lint-php.sh needs only the project Docker image, exactly like the other linters:

php -l src/dev/php/array.php ./scripts/lint-php.sh --help

Simple example

Lint one file after editing it:

php -l src/ru/qa/.php/Rblock.php

No syntax errors detected in src/ru/qa/.php/Rblock.php

Whole-tree syntax check from container/:

./scripts/lint-php.sh

Complex usage

Same selection flags as lint-static.sh: explicit paths, staged changes, or the whole tree. Read-only runs never modify code:

./scripts/lint-php.sh --files src/ru/qa/.php/Rblock.php src/dev/php/linters/ ./scripts/lint-php.sh --staged ./scripts/lint-php.sh --all

php -l is also the safety gate of wrap-long-lines.sh --fix: every file the wrapper touches must pass php -l afterwards, otherwise the file is restored byte-for-byte and reported as failed.

Unexpected token "." after commenting out HTML

A page prints its HTML as one string, and a part of it has to be hidden. Closing the string, opening a comment and joining the string again looks symmetrical, but breaks the file:

vi page.php

<?php echo ' <p>before</p> ' . /* <figure>hidden</figure> */ . ' <p>after</p> ';

php -l page.php

PHP Parse error: syntax error, unexpected token "." in page.php on line 6 Errors parsing page.php

PHP drops the comment before it parses the code. What is left of lines 4 to 6 is . .: two concatenation operators with no value between them. The error points to line 6, where the second dot is.

Keep exactly one dot. Remove the one after */, so that line 4 joins the string before the comment to the string after it:

<?php echo ' <p>before</p> ' . /* <figure>hidden</figure> */ ' <p>after</p> ';

php -l page.php

No syntax errors detected in page.php

The hidden part is not sent to the browser:

php page.php

<p>before</p> <p>after</p>

The string has to be closed first. A /* typed inside the quotes is ordinary text: php -l passes, and the browser shows /*, the HTML between and */. An HTML comment <!-- --> hides the part on screen, but it stays in the page source.

Unexpected single-quoted string after commenting out HTML

The opposite mistake is a dot typed before the closing quote instead of after it:

vi page.php

<?php echo ' <p>before</p> . ' /* <figure>hidden</figure> */ ' <p>after</p> <p>end</p> ';

php -l page.php

PHP Parse error: syntax error, unexpected single-quoted string "", expecting "," or ";" in page.php on line 10 Errors parsing page.php

The dot on line 4 is inside the string, so it is ordinary text. The quote after it closes the string, and line 7 opens the next one. Without the comment PHP sees two strings with no operator between them.

The mistake is on line 4, but the message names line 10: a string that spans several lines is reported at the line where it ends. Look above that line, at the place where the string starts. Close the string first, then put the dot:

<?php echo ' <p>before</p> ' . /* <figure>hidden</figure> */ ' <p>after</p> <p>end</p> ';

php -l page.php

No syntax errors detected in page.php

Both visible parts are printed:

php page.php

<p>before</p> <p>after</p> <p>end</p>

What it fixed in src/

Strictly speaking php -l fixes nothing — it only reports. But it certified the two mass cleanups below:

  1. After the PHPCBF safe-sniff pass (1050 errors in 113 files) and the FileHeader pass (1735 errors in 1734 files): linted: 5105, failures: 0.
  2. After renaming 7 snake_case methods in src/ru/qa/.php/Rblock.php (show_rblock to showRblock and friends) plus 12 callers: no syntax errors in any touched file.

php -l prints Deprecated notices it does not fail on, for example optional parameters declared before required ones in Rblock.php and Page.php. Those are left alone: fixing them is a refactor, not a syntax fix.

Article author: Andrei Olegovich