How to Apply Nette Coding Standard in Your Project

Pull-requests are more fun thanks to automated coding standard. You don't have explain where to put space or bracket. You can talk about architecture or meaning of the code instead. Moreover in open-source. I wanted to make this possible in Nette, but Coding Standards could be found only in documentation.

This year I started to work on a Nette Coding Standard (NCS) that you can put to CLI. And you'll be able set it up in in your project yourself today.

Nette\CodingStandard version 0.5 with important bug-fixes was released a week ago. This version is ready to use, includes all important checkers and is used on all Nette\* packages in Travis.

NCS checks every pull-request you make to Nette:

All that need is to define stage in travis.yml:

That's it! Just 2 commands and it checks any project you have.

But first...

PHP 7.1+

This packages requires PHP 7.1 to run as the rest of the Nette (mostly current master or 3.0).

If you still don't know why should you join Symfony, Nette, Doctrine, Zend or Sylius, you can read this post or wait a bit longer. It's up to you.

Setup Your Project

You have 2 options how to use NCS in your project.

1. As a Composer Project

Nette packages require this approach, because NCS depends on many Nette packages. NCS should be installed to standalone directory, so changing the Nette code by NCS doesn't break NCS.

The easiest way is to setup .travis.yml:

    - composer create-project nette/coding-standard temp/nette-coding-standard

    - temp/nette-coding-standard/ecs check src tests --config temp/nette-coding-standard/coding-standard-php71.neon

And you are ready to go!

2. As a Composer Dev Dependency

I prefer this in projects, where I want to check coding standards locally and have dependencies up-to date. It's easier than composer project, where you need to remember to update dependencies manually in the directory.

composer require nette/coding-standard --dev

Check the code:

vendor/bin/ecs check src tests --config vendor/nette/coding-standard/coding-standard-php71.neon

Fix the code:

vendor/bin/ecs check src tests --config vendor/nette/coding-standard/coding-standard-php71.neon --fix

3 Configs based on PHP version

Do you need to check code that is not PHP 7.0 ready? You can.

At the moment there are 3 configs with set of checkers (click to see their content):

Config with higher PHP version includes all lower versions, so with coding-standard-php71.neon you cover the other 2 configs as well.

For PHP 5.6 it would like this:

vendor/bin/ecs check src tests --config vendor/nette/coding-standard/coding-standard-php56.neon

Happy coding!

  Continue Learning

Typo? Fix it, please  and join 49 people who build this website