code-sniper
A clone of code sniffer (PHPCS) written in nodejs.
CLI Usage
Installation :
npm install php-sniper -g
Command line :
php-sniper -o txt ./**/*.php
Note : If the working directory contains a ̀
.php-sniper.json
file, the cli will try to load it and use it as a ruleset configuration file
Integrate the library into your project
The API is open, so you can use this library in your own project, without needing to run it from a CLI process for example.
npm install php-sniper --save
And from your code :
var fs = ;var Sniper = ;var scan = ruleset: 'phpcs' // defines a default ruleset (see src/rulesets/*.json);var file = __dirname + '/foo.php';var ast = scan;console;
Writing your own Rule
Writing a rule is quite simple, this library is specially designed for that :
- Register a rule from the instance
- CSS syntax based node/token filtering
- Session for storing data/criterias between files
- Namespace resolution helper
- Automatic multiple passes optimisation (with parsing cache)
- PHP-Parser decorator (a drop-in replacement)
Here a sample code :
var Sniper = ;var scan = ; scan; scan; // loads a customized rulesetscan; // ... write here the parsing of your files ...var filename = __dirname + '/foo.php'; // php-parser drop-in replacementvar ast = scan; // important call in order to say to the system the first pass is finished// so other passes will automatically run after the first onescan;
Configure a customized ruleset
Rulesets are similar to PHPCS, but json based instead of xml based. A ruleset is just a way to define a list of rules, with options if needed.
A ruleset can inherit configuration from a list of parent rulesets, and the rules property will overwrite their configuration.
An example of ruleset configuration my-custom-phpcs.json
Read further from the API documentation.
Roadmap
- 0.1.x : First alpha prototype (soon)
- run some rules
- implement the selector
- pass some tests
- benchmarks & early optimisations
- 1.0.x : Release a stable version of API
- framework of automated tests
- clean documentation
- all API classes implemented
- 1.1.0 : handle all PHPCS options
- 1.2.0 : Integrate all generic rules
- 1.3.0 : Integrate all PHPCS rules
- 1.4.0 : Integrate all PEAR rules
- 1.5.0 : Integrate all PSR1/2 rules
- 1.6.0 : Integrate all Squiz rules
- 1.7.0 : Integrate all Zend rules
- 1.8.0 : Integrate all Squiz rules
Contributions
Any contribution is welcomed; after the alpha release, I'll be able to merge any PR about a rule definition.
Misc
This library is released under BSD-3 license clause.
This Library is Under Development - no BETA release available