Today’s projects are built up from dozens of different components, configuration files, third-party libraries, tests, build scripts, etc. And even if you have the greatest test suite, bad things can happen sometimes. It’s important to catch bugs as early as possible, and syntax validators can be a great (and easy) addition to your continuous integration system. You would be surprised at how many problems are caused by syntax errors. At ServerGrove, we see these kind of problems with our clients almost every day.


PHP code

The PHP binary provides a way to check the syntax of a given PHP file, by using the “-l” option:

$ php -l script.php

Wildcards can also be used to check more than one file. And to check all PHP files in a project, the following command can be used:

$ find . -type f -name "*.php" -exec php -l {} \;

If there are too many files and the process is slow, you may want it to execute it in parallel. For that, create a simple shell script that does the actual lint and then modify the “find” command:

php -l $1
$ find . -type f -name "*.php" | parallel -j4 ./ {}

Twig templates

When used with Symfony, the framework has a command to check one or more Twig templates:

$ php app/console twig:lint template.html.twig

All Twig extensions are taken into account, so custom functions, filters or tags are taken into account to validate the input template.

When using Twig without Symfony, there is no a direct way to validate a template. You can build your own linter or use asm89/twig-lint.


Composer provides a command to validate “composer.json” files in order to find both syntax errors and common issues.

$ composer validate composer.json

XML files

It is common to use XML files for configuration, translations or service definitions. xmllint is a command-line tool that checks that XML are well-formed and valid for a given DTD or XML Schema.

For example, you can check that an XML file is well formed with the following command:

$ xmllint --schema src/ServerGrove/DemoBundle/Resources/config/services.xml

Normally, tags and attributes that you can use in XML files are defined in a DTD or XML Schema. For example, when using XLIFF files for translations, you can both check that are well formed and valid:

$ xmllint --schema --noout src/ServerGrove/DemoBundle/Resources/translations/

There are schemas for many file types:

  • Doctrine mapping:
  • Symfony DIC services:
  • Symfony routing:
  • Symfony validator constraint mapping:
  • PHPUnit configuration:

Bash scripts

If you use bash scripts to automatize tasks, the “bash” command contains an option to check its syntax without executing the script:

$ bash -n

JSON files

JSON files are sometimes used for configuration and dependency management. While there are several tools that you can use, we created a simple Melody script that makes use of Seldaek/jsonlint to check JSON files:

$ melody run bower.json

YAML files

JSON files are widely used for configuration purposes as they are easier to write for a human being. When using Symfony, there is a “yaml:lint” command since 2.5. Without Symfony, you can build your own linter or use the Melody script we did for you:

$ melody run parameters.yml

Are you missing any other file type? Let us know!

Photo: “Syntax Error – Folded Up Beyond All Recognition”, by “Simon Pow”