Sometimes, we really commit stupid mistakes in programing. Because, we all are fallible and whether it be in a moment of rage, or just because we are in a rush, we will make mistakes.

That’s why I have PHP Lint for PHP web development projects added as a validator tool in my project editor.

It only takes a minute to set it up.

Benefits of using a linter

  • Get instantly notified of your mistakes
  • Save time
  • Safer code
  • You can stay focused on the hard part of your coding
  • Better coding flow
  • Obvious highlighting in your code
  • Your team will follow the same coding style

 

PHPLint is a parser and semantic validator for PHP programs written in PHP itself.
It is useful for complexes one where much code and included libraries and
difficult to track down formal and conceptual errors such as

  1.  General syntax check.
  2.  Strict type checking in expressions, assignments and function arguments.
  3.  Structured array declarations.
  4.  Checks functions signature usage.
  5.  Checks methods signature and method inheritance.
  6.  Recursive inclusion of the required packages.
  7.  Meta-code that implement C-style type declarations.
  8.  Check item’s declared but never used, or used but never declared.
  9.  Documentation generation.

It also includes strict checking of types handling,checking of functions and methods signatures,
tracking of errors exceptions detection and propagation, and much more.

Linter supports PHP 5 and php 7. Linting your code is a great thing to do no matter what language you code with. it’s probably exist form for all languages.you need to install a specific linter for your language such as PHP, JavaScript, CSS, Bash and so on.

You will find a full list on atomlinter.github.io.

First, you need to make sure you have PHP Lint installed simply run one command on your system.

apm install linter

Secondly, Linter Specific Packages

Linter also allows more specific packages for different languages,
I’ve provided a quick install command for the most popular packages.

Note that you may need other applications installed to run each one, reference the Linter Atom page for more info.

apm install linter-jshint linter-php linter-phpcs linter-phpmd linter-scss-lint linter-csslint linter-js-yaml linter-tidy linter-htmlhint

Later, install the packages that you need. You can find total number of error message and warning into your editor.

Here, I give you example of syntax error:

 

 

If you would like PHP Lint to be added as a pre-commit hook on your PHP Project repos
or .git directory. you have to add following code to
the file .git/hooks/pre-commit and will have you a ton of time, and headache in the future.

#!/usr/local/bin/php
<?php

# PHP LINTING
$output = array();
$return = 0;
exec(‘git rev-parse –verify HEAD 2> /dev/null’, $output, $return);
$against = $return == 0 ? ‘HEAD’ : ‘4b825dc642cb6eb9a060e54bf8d69288fbee4904’;

exec(“git diff-index –diff-filter=ACMRTUXB –cached –name-only {$against}”, $output);

$filename_pattern = ‘/\.php$/’;
$exit_status = 0;

foreach ($output as $file) {
if (!preg_match($filename_pattern, $file)) {
// don’t check files that aren’t PHP
continue;
}

$lint_output = array();
exec(“php -l ” . escapeshellarg($file), $lint_output, $return);
if ($return == 0) {
continue;
}
echo implode(“\n”, $lint_output), “\n”;
$exit_status = 1;
}

exit($exit_status);

?>

And that should be it. Now every time you do a commit on some PHP web development code, your git workflow will send each file through the PHP Linter to check for errors. In Case any error exits, Git will refuse to commit the files


One thing to note, this git hook will only parse the files you are committing and not the entire project. Meaning, if you have a co-working was not doing linting, and you pull in their changes but don’t touch the file,your commit will not catch it.

Classifying Linters

  • Linters that check for syntax errors, and,
  • Linters that check for coding standards violations.

I consider linters that check for syntax errors mandatory, since I can’t really think of a time when I would want code which contains syntax errors (in PHP and JavaScript, syntax errors cause unrecoverable fatal errors if the file containing the code is ever parsed (i.e.: white screen of death, JavaScript engine halting)). I will not commit code unless these types of linters give the okay.

I consider linters that check for coding standards violations strongly recommended (as they affect the maintainability of your code), but technically optional (because code that doesn’t conform to standards still works). I try to avoid committing code that doesn’t pass these linters; but there are some circumstances when that is unavoidable (e.g.: if I have to commit a contributed module, or if it’s a hotfix and there’s a plan to make the code more-maintainable later).

A linter is a static analysis tool that you can run to ensure that your code is free from syntax and/or style errors.It’s a great way to save time, maintain quality and write safer code.You can plug linters into your code editor or copy-paste your code to a linting tool.

 

It’s just more fun. It’s like magic. Hope this article added value to your knowledge.

Share your thoughts with us. If you have any query, you can write to us at info@letsnurture.com.

Want to work with us? We're hiring!