What is Coding standard?
Coding isn't all about writing code to fulfill the purpose or provide expected output instead, it is about making sure that the code is easy to maintain and debug.
The “Maintainable Code” is one of the key constraints considering the coding standards. Maintaining unfamiliar code that is well indented and commented is understandable but on the contrary the code which is not indented can be headache for every developer.
Coding standards are nothing but the rules or the set of instructions designed for defining the standard way of writing code in any language.
What are NetBeans?
NetBeans is nothing but a software development platform developed in java. It is created for writing applications using java but also supports other languages as well such as PHP.
What is Code Sniffer Tool?
Code sniffer is a tool that enables a set of rules or standard that needs to be applied to the source code. These rules are used to detect common programming errors, anti-patterns and to define a set of coding standards for your project.
In this article you will get to know practical implementation for setting PSR-2 coding standards for NetBeans using code sniffer tool.
Some standards followed by the PSR-2 Coding Standard:
- Code must follow “Coding style guide” for PSR-1.
- Code must use 4 spaces for indenting, not tabs.
- There must not be a hard limit on line length and the soft limit must be 120 characters, lines should be 80 characters or less.
- There must be one blank line after the namespace declaration, and there must be one blank line after the block of use declarations.
- Opening braces for classes must go on the next line, and closing braces must go on the next line after the body. It applies methods as well.
- Visibility must be declared on all properties and methods; abstract and final must be declared before the visibility; static must be declared after the visibility.
- Control structure keywords must have one space after them, method and function calls must not.
- Opening braces for control structures must go on the same line, and closing braces must go on the next line after the body.
- Opening parentheses for control structures must not. have a space after them, and closing parentheses for control structures must not. have a space before.
Installing Code Sniffer PSR2 Standard for Net Beans:
- Check if pear is installed on your machine or not by executing pear command.
- If it is not installed get go-pear.phar file download from google and copy it into (xampp/php) or (wamp/bin/php/php(version)).
- Goto command prompt at above mentioned php path and run php go-pear.phar command to install pear on your machine.
- After successful installation of pear it will create PEAR_ENV.reg file just double click on it. This creates environment variables for pear.
- Now to install code sniffer for Netbeans just follow the steps below:
- Run command - pear install -- alldeps PHP_CodeSniffer
- Run phpcs -i command to check if the code sniffer installed successfully or not. If it is installed successfully it will show the list of standards available like: The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend.
- Install netbeans8.1 and go to tools->plugins and search for phpCS-MD plugin. If it is available install it. if not then download it from google and install it.
- After installing the plugin go to tools->Options->PHP->select code sniffer tab and PSR2 in standards section.
- You will see an eye icon in the toolbar of editor. Just click on that to scan the currently opened file. And that’s it.