Symfony framework provides static code analysis commands (also known as linters) that enables to check some parts of application such TWIG syntax, service definitions in container, etc.
This tutorial shows how to check errors in YAML files using console command in Symfony 6 application.
Let's say we have the following YAML file:
controllers: resource: ../src/Controller/ resource: ../src/Kernel.php type: annotation
lint:yaml command can be used for checking if YAML files don't have any syntax errors. YAML filename can be provided as argument:
php bin/console lint:yaml config/routes.yaml
Command prints success message if YAML file is valid, otherwise error is displayed:
ERROR in config/routes.yaml >> Duplicate key "resource" detected at line 3 (near "resource: ../src/Kernel.php").
A whole directory can be checked as well by providing directory name as argument:
php bin/console lint:yaml config
Problem can be fixed as follows:
controllers: resource: ../src/Controller/ type: annotation kernel: resource: ../src/Kernel.php type: annotation
If you are using a continuous integration, recommended to add
lint:yaml command to the list of commands which executed on each build.