Skip to content

Commit b0bb443

Browse files
Merge pull request #2 from VincentLanglet/staging
Staging
2 parents ce8aa8c + 4fb8386 commit b0bb443

26 files changed

+538
-104
lines changed

README.md

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# Symfony3 Custom PHP CodeSniffer Coding Standard
22

3-
This is a fork of [endouble/Symfony3-coding-standard](https://github.com/endouble/Symfony3-coding-standard).
4-
These are the Symfony standards, but tweaked to meet some needs I have in my project, for example to comply with
5-
[PSR-12](https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md) for PHP 7.
6-
7-
## Installation
8-
9-
### Composer
10-
113
This standard can be installed with the [Composer](https://getcomposer.org/) dependency manager.
124

135
1. Add the repository to your composer.json:
@@ -53,24 +45,3 @@ bin/phpcs --standard=Symfony3Custom /path/to/code
5345

5446
- configure code sniffer under Languages & Frameworks -> PHP -> Code Sniffer
5547
- Go to Editor -> Inspections -> PHP Code sniffer, refresh the standards and select Symfony3Custom
56-
57-
## Customizations
58-
59-
The following adjustments have been made to the original standard:
60-
61-
In Sniff/WhiteSpace/AssignmentSpacingSniff:
62-
- Added an exception for ```declare(strict_types=1);``` to comply with [PSR-12](https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md#3-declare-statements-namespace-and-use-declarations)
63-
64-
In Sniff/WhiteSpace/FunctionalClosingBraceSniff:
65-
- copied from Squiz and adapted to have no blank line at the end of a function
66-
67-
In Sniff/Commenting/FunctionCommentSniff:
68-
- check for 1 blank line above a docblock
69-
- don't check docblocks for test, setUp and tearDown methods (PHPunit, would be blank)
70-
- do check protected and private methods for docblocks
71-
72-
In ruleset.xml
73-
- Disabled the class comment rule
74-
- Changed the concatenation spacing rule, for readability, to require 1 space around concatenation dot, instead of no spaces as the [Symfony](https://symfony.com/doc/current/contributing/code/standards.html#structure) standard requires.
75-
- Re-enabled the blank line check from superfluousWhitespace (disabled in PSR-2)
76-

Symfony3Custom/Sniffs/Arrays/MultiLineArrayCommaSniff.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,42 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5050
}
5151

5252
if ($open['line'] <> $tokens[$closePtr]['line']) {
53-
$lastComma = $phpcsFile->findPrevious(T_COMMA, $closePtr);
53+
$arrayIsNotEmpty = $phpcsFile->findPrevious(
54+
array(
55+
T_WHITESPACE,
56+
T_COMMENT,
57+
T_ARRAY,
58+
T_OPEN_PARENTHESIS,
59+
T_OPEN_SHORT_ARRAY,
60+
),
61+
$closePtr - 1,
62+
$stackPtr,
63+
true
64+
);
5465

55-
while ($lastComma < $closePtr -1) {
56-
$lastComma++;
66+
if ($arrayIsNotEmpty !== false) {
67+
$lastComma = $phpcsFile->findPrevious(T_COMMA, $closePtr);
5768

58-
if ($tokens[$lastComma]['code'] !== T_WHITESPACE
59-
&& $tokens[$lastComma]['code'] !== T_COMMENT
60-
) {
61-
$phpcsFile->addError(
62-
'Add a comma after each item in a multi-line array',
63-
$stackPtr,
64-
'Invalid'
65-
);
66-
break;
69+
while ($lastComma < $closePtr - 1) {
70+
$lastComma++;
71+
72+
if ($tokens[$lastComma]['code'] !== T_WHITESPACE
73+
&& $tokens[$lastComma]['code'] !== T_COMMENT
74+
) {
75+
$fix = $phpcsFile->addFixableError(
76+
'Add a comma after each item in a multi-line array',
77+
$stackPtr,
78+
'Invalid'
79+
);
80+
81+
if ($fix === true) {
82+
$ptr = $phpcsFile->findPrevious(array(T_WHITESPACE, T_COMMENT), $closePtr-1, $stackPtr, true);
83+
$phpcsFile->fixer->addContent($ptr, ',');
84+
$phpcsFile->fixer->endChangeset();
85+
}
86+
87+
break;
88+
}
6789
}
6890
}
6991
}

Symfony3Custom/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
<?php
22

3-
/**
4-
* This file is part of the Symfony3Custom-coding-standard (phpcs standard)
5-
*
6-
* PHP version 5
7-
*
8-
* @category PHP
9-
* @package Symfony3Custom-coding-standard
10-
* @author Authors <Symfony3Custom-coding-standard@escapestudios.github.com>
11-
* @license http://spdx.org/licenses/MIT MIT License
12-
* @link https://github.com/escapestudios/Symfony3Custom-coding-standard
13-
*/
14-
153
/**
164
* Throws warnings if properties are declared after methods
175
*/
186
class Symfony3Custom_Sniffs_Classes_PropertyDeclarationSniff
197
implements PHP_CodeSniffer_Sniff
208
{
21-
229
/**
2310
* A list of tokenizers this sniff supports.
2411
*

Symfony3Custom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,21 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6262
) {
6363
return;
6464
} else if (count($prevLineTokens) > 0) {
65-
$phpcsFile->addError(
65+
$fix = $phpcsFile->addFixableError(
6666
'Missing blank line before return statement',
67-
$stackPtr
67+
$stackPtr,
68+
'MissedBlankLineBeforeRetrun'
6869
);
70+
71+
if ($fix === true) {
72+
$phpcsFile->fixer->beginChangeset();
73+
$i = 1;
74+
while($tokens[$stackPtr-$i]['type'] == "T_WHITESPACE") {
75+
$i++;
76+
}
77+
$phpcsFile->fixer->addNewLine($stackPtr-$i);
78+
$phpcsFile->fixer->endChangeset();
79+
}
6980
}
7081

7182
return;

Symfony3Custom/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
4242
if ($tokens[$stackPtr -1]['code'] !== T_WHITESPACE
4343
|| $tokens[$stackPtr +1]['code'] !== T_WHITESPACE
4444
) {
45-
$phpcsFile->addError(
45+
$fix = $phpcsFile->addFixableError(
4646
'Add a single space around binary operators',
4747
$stackPtr,
4848
'Invalid'
4949
);
50+
51+
if ($fix === true) {
52+
if ($tokens[$stackPtr -1]['code'] !== T_WHITESPACE) {
53+
$phpcsFile->fixer->addContentBefore($stackPtr, ' ');
54+
}
55+
if ($tokens[$stackPtr +1]['code'] !== T_WHITESPACE) {
56+
$phpcsFile->fixer->addContent($stackPtr, ' ');
57+
}
58+
}
5059
}
5160
}
5261
}

Symfony3Custom/Sniffs/WhiteSpace/CommaSpacingSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
4545
if ($tokens[$stackPtr + 1]['line'] === $line
4646
&& $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE
4747
) {
48-
$phpcsFile->addError(
48+
$fix = $phpcsFile->addFixableError(
4949
'Add a single space after each comma delimiter',
5050
$stackPtr,
5151
'Invalid'
5252
);
53+
54+
if ($fix) {
55+
$phpcsFile->fixer->addContent($stackPtr, ' ');
56+
}
5357
}
5458

5559
}

Symfony3Custom/Tests/Arrays/MultiLineArrayCommaUnitTest.inc

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,84 @@
44
$arr = array('foo', 'bar');
55

66
$arr = array(
7-
'foo'
7+
'foo',
8+
'bar'
9+
);
10+
11+
$arr = array(
12+
'foo',
813
'bar',
914
);
1015

1116
$arr = array(
12-
'foo'
13-
'bar'
1417
);
1518

1619
// short array syntax
1720
$arr = ['foo', 'bar'];
1821

1922
$arr = [
20-
'foo'
23+
'foo',
24+
'bar'
25+
];
26+
27+
$arr = [
28+
'foo',
2129
'bar',
2230
];
2331

2432
$arr = [
25-
'foo'
26-
'bar'
2733
];
2834

2935
// classic array syntax with comments
3036
$arr = array('foo', 'bar' /* baz */);
3137

3238
$arr = array(
33-
'foo'
39+
'foo',
40+
'bar' //baz
41+
);
42+
43+
$arr = array(
44+
'foo',
3445
'bar', //baz
3546
);
3647

3748
$arr = array(
38-
'foo'
39-
'bar' //baz
49+
'foo',
50+
'bar' /* baz */
4051
);
4152

4253
$arr = array(
43-
'foo'
54+
'foo',
4455
'bar', /* baz */
4556
);
4657

4758
$arr = array(
48-
'foo'
49-
'bar' /* baz */
59+
// foo => bar
5060
);
5161

5262
// short array syntax with comments
5363
$arr = ['foo', 'bar' /* baz */];
5464

5565
$arr = [
56-
'foo'
66+
'foo',
67+
'bar' //baz
68+
];
69+
70+
$arr = [
71+
'foo',
5772
'bar', //baz
5873
];
5974

6075
$arr = [
61-
'foo'
62-
'bar' //baz
76+
'foo',
77+
'bar' /* baz */
6378
];
6479

6580
$arr = [
66-
'foo'
81+
'foo',
6782
'bar', /* baz */
6883
];
6984

7085
$arr = [
71-
'foo'
72-
'bar' /* baz */
86+
// foo => bar
7387
];
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
// classic array syntax
4+
$arr = array('foo', 'bar');
5+
6+
$arr = array(
7+
'foo',
8+
'bar',
9+
);
10+
11+
$arr = array(
12+
'foo',
13+
'bar',
14+
);
15+
16+
$arr = array(
17+
);
18+
19+
// short array syntax
20+
$arr = ['foo', 'bar'];
21+
22+
$arr = [
23+
'foo',
24+
'bar',
25+
];
26+
27+
$arr = [
28+
'foo',
29+
'bar',
30+
];
31+
32+
$arr = [
33+
];
34+
35+
// classic array syntax with comments
36+
$arr = array('foo', 'bar' /* baz */);
37+
38+
$arr = array(
39+
'foo',
40+
'bar', //baz
41+
);
42+
43+
$arr = array(
44+
'foo',
45+
'bar', //baz
46+
);
47+
48+
$arr = array(
49+
'foo',
50+
'bar', /* baz */
51+
);
52+
53+
$arr = array(
54+
'foo',
55+
'bar', /* baz */
56+
);
57+
58+
$arr = array(
59+
// foo => bar
60+
);
61+
62+
// short array syntax with comments
63+
$arr = ['foo', 'bar' /* baz */];
64+
65+
$arr = [
66+
'foo',
67+
'bar', //baz
68+
];
69+
70+
$arr = [
71+
'foo',
72+
'bar', //baz
73+
];
74+
75+
$arr = [
76+
'foo',
77+
'bar', /* baz */
78+
];
79+
80+
$arr = [
81+
'foo',
82+
'bar', /* baz */
83+
];
84+
85+
$arr = [
86+
// foo => bar
87+
];

Symfony3Custom/Tests/Arrays/MultiLineArrayCommaUnitTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class Symfony3Custom_Tests_Arrays_MultiLineArrayCommaUnitTest
2020
public function getErrorList()
2121
{
2222
return array(
23-
11 => 1,
24-
24 => 1,
25-
37 => 1,
26-
47 => 1,
27-
60 => 1,
28-
70 => 1,
23+
6 => 1,
24+
22 => 1,
25+
38 => 1,
26+
48 => 1,
27+
65 => 1,
28+
75 => 1,
2929
);
3030
}
3131

0 commit comments

Comments
 (0)