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.

252312738_e025b7edfa_z

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:

#!/bin/bash
php -l $1
$ find . -type f -name "*.php" | parallel -j4 ./phplint.sh {}

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

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 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd --noout src/ServerGrove/DemoBundle/Resources/translations/messages.fr.xlf

There are schemas for many file types:

  • Doctrine mapping: http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd
  • Symfony DIC services: http://symfony.com/schema/dic/services/services-1.0.xsd
  • Symfony routing: http://symfony.com/schema/routing/routing-1.0.xsd
  • Symfony validator constraint mapping: http://symfony.com/schema/dic/constraint-mapping-1.0.xsd
  • PHPUnit configuration: http://schema.phpunit.de/4.1/phpunit.xsd

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 script.sh

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 https://gist.github.com/raulfraile/bb3616174c52cd6f077a 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 https://gist.github.com/raulfraile/47d3264fdc86cb3f2cd4 parameters.yml

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

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