amlagoda/psr-linter

Code review for compliance with PSR standards

1.13.5 2017-05-06 03:31 UTC

README

Build Status Code Climate Test Coverage Issue Count

Install

$ composer global require amlagoda/psr-linter (If you're having trouble to run psr-linter after global install, make sure Composer's global binary dir is in the $PATH)

or

require: {
  "amlagoda/psr-linter": ">=1.13.5"
}

Usage

$ psr-linter --help

path
     Required. The path to the verifiable file or directory
--config <argument>
     Path to the configuration file(json)
-f/--fix
     Fix errors
--format <argument>
     Output format. Valid formats: table, json, yaml, ini
--help
     Show the help page for this command.

Config

The configuration file is a json file with an array of class names that which be used in the validation

["SideEffect", "StudlyCapsClassName"]

more /src/Config.php

Use of components

$linter = new PsrLinter\Linter();
$errors = $linter->lintToPath('/home/user/project/src', 'json', true);
var_dump($errors);

{
  "/home/user/project/src/file1.php": [
    {
      "line": "5",
      "message": "Rule message",
      "rule": "Rule name",
      "status": "Fixed"
    },
    {
      "line": "3",
      "message": "Rule message",
      "rule": "Rule name",
      "status": "Fixed"
    }
  ],
  "/home/user/project/src/file2.php": [
    {
      "line": "15",
      "message": "Rule message",
      "rule": "Rule name",
      "status": "The program can not fix this error"
    }
  ],
  ...
}


$code = file_get_contents('/home/user/project/file1.php');
$lintResult = $linter->lint($code, 'json', true);
var_dump($lintResult);

[
  'errors' => detected errors in the specified format
  'code' => fixed code(string) or null(if you do not specify the third parameter)
]

lintToPath(<path>, [format], [fix], [configPath])
path - Path to the verifiable file or directory
format - Output format. Valid formats: table, json, yaml, ini
fix - Fix errors? (bool)
configPath - Path to the configuration file(json)

lint(<code>, [format], [fix], [rules])
code - Code in string format
rules - Array of class names

Adding rules

/src/rule/MyRule.php

namespace PsrLinter\Rule;

class MyRule implements Rule // or extends RuleAbstract
{
}

Rules classes must implement the interface PsrLinter\Rule\Rule. For convenience, you can extend the class PsrLinter\Rule\RuleAbstract having all of the required methods for default and override necessary.