Skip to content

Instantly share code, notes, and snippets.

@gsherwood
Created November 4, 2014 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gsherwood/1bf55026748fd38000bd to your computer and use it in GitHub Desktop.
Save gsherwood/1bf55026748fd38000bd to your computer and use it in GitHub Desktop.
PHPCS/PHPCBF PSR-2 auto-formatting example
<?php
namespace Vendor\Package;
use FooInterface; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass;
class Foo extends Bar implements FooInterface {
public function sampleFunction($a,$b=null) {
if ($a === $b) { bar();
} else if ($a>$b) {
$foo->bar( $arg1 ); }
else {
BazClass::bar($arg2,$arg3 );
}
}
final public static function bar() {
// method body
}
}
$ phpcs temp.php --standard=PSR2
FILE: temp.php
--------------------------------------------------------------------------------
FOUND 24 ERRORS AND 1 WARNING AFFECTING 11 LINES
--------------------------------------------------------------------------------
2 | ERROR | [x] There must be one blank line after the namespace
| | declaration
3 | ERROR | [x] Each PHP statement must be on a line by itself
3 | ERROR | [x] There must be one blank line after the last USE statement;
| | 0 found;
3 | ERROR | [x] Each PHP statement must be on a line by itself
4 | ERROR | [x] Opening brace of a class must be on the line after the
| | definition
5 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces,
| | found 0
5 | ERROR | [x] Incorrect spacing between argument "$b" and equals sign;
| | expected 1 but found 0
5 | ERROR | [x] Incorrect spacing between default value and equals sign for
| | argument "$b"; expected 1 but found 0
5 | ERROR | [x] Expected 1 space between comma and argument "$b"; 0 found
5 | ERROR | [x] Opening brace should be on a new line
6 | ERROR | [x] Line indented incorrectly; expected 8 spaces, found 0
6 | ERROR | [x] Expected 1 newline after opening brace; 0 found
7 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead
8 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces,
| | found 0
8 | ERROR | [x] Space after opening parenthesis of function call prohibited
8 | ERROR | [x] Expected 0 spaces before closing bracket; 1 found
8 | ERROR | [x] Closing brace must be on a line by itself
8 | ERROR | [x] Expected 1 space after closing brace; newline found
10 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces,
| | found 0
10 | ERROR | [x] Expected 0 spaces before closing bracket; 1 found
10 | ERROR | [x] No space found after comma in function call
11 | ERROR | [x] Line indented incorrectly; expected 0 spaces, found 4
11 | ERROR | [x] Closing brace indented incorrectly; expected 0 spaces,
| | found 4
14 | ERROR | [x] Opening brace should be on a new line
17 | ERROR | [x] Expected 1 newline at end of file; 0 found
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 25 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------
Time: 19ms; Memory: 3.25Mb
$ phpcbf temp.php --standard=PSR2
Processing temp.php [PHP => 142 tokens in 17 lines]... DONE in 4ms (25 fixable violations)
=> Fixing file: 0/25 violations remaining [made 4 passes]... DONE in 14ms
Patched 1 file
Time: 42ms; Memory: 3.5Mb
<?php
namespace Vendor\Package;
use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
class Foo extends Bar implements FooInterface
{
public function sampleFunction($a, $b = null)
{
if ($a === $b) {
bar();
} elseif ($a>$b) {
$foo->bar($arg1);
} else {
BazClass::bar($arg2, $arg3);
}
}
final public static function bar()
{
// method body
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment