Skip to content

Commit 39f76e0

Browse files
Merge pull request #9 from VincentLanglet/staging
Staging
2 parents 756fb20 + 2b6e5f1 commit 39f76e0

File tree

7 files changed

+447
-45
lines changed

7 files changed

+447
-45
lines changed

README.md

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,6 @@
11
# Symfony3 Custom PHP CodeSniffer Coding Standard
22

3-
This standard can be installed with the [Composer](https://getcomposer.org/) dependency manager.
4-
5-
1. Add the repository to your composer.json:
6-
7-
```json
8-
"repositories": [
9-
{
10-
"type": "vcs",
11-
"url": "git@github.com:VincentLanglet/Symfony3-custom-coding-standard"
12-
}
13-
]
14-
```
15-
16-
2. Add the coding standard as a dependency of your project
17-
18-
```json
19-
"require-dev": {
20-
"vincentlanglet/symfony3-custom-coding-standard": "^2.18"
21-
},
22-
```
23-
24-
3. Add the coding standard to the PHP_CodeSniffer install path
25-
26-
The path is relative to the php_codesniffer install path. This is important to make it work both in your vagrant, local machine and PHPStorm
27-
28-
```
29-
bin/phpcs --config-set installed_paths ../../vincentlanglet/symfony3-custom-coding-standard
30-
```
31-
32-
4. Check the installed coding standards
33-
34-
```
35-
bin/phpcs -i
36-
```
37-
38-
5. Done!
39-
40-
```
41-
bin/phpcs --standard=Symfony3Custom /path/to/code
42-
```
43-
44-
6. (optional) Set up PHPStorm
45-
46-
- configure code sniffer under Languages & Frameworks -> PHP -> Code Sniffer
47-
- Go to Editor -> Inspections -> PHP Code sniffer, refresh the standards and select Symfony3Custom
3+
Documentation
4+
-------------
5+
* [Coding Standard](docs/coding-standard.md)
6+
* [Installation](docs/installation.md)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* Ensures there are no spaces on increment / decrement statements or on +/- sign
5+
* operators or "!" boolean negators.
6+
*/
7+
class Symfony3Custom_Sniffs_WhiteSpace_UnaryOperatorSpacingSniff implements PHP_CodeSniffer_Sniff
8+
{
9+
/**
10+
* Returns an array of tokens this test wants to listen for.
11+
*
12+
* @return array
13+
*/
14+
public function register()
15+
{
16+
return array(
17+
T_DEC,
18+
T_INC,
19+
T_MINUS,
20+
T_PLUS,
21+
T_BOOLEAN_NOT,
22+
);
23+
24+
}
25+
26+
27+
/**
28+
* Processes this test, when one of its tokens is encountered.
29+
*
30+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
31+
* @param int $stackPtr The position of the current token in
32+
* the stack passed in $tokens.
33+
*
34+
* @return void
35+
*/
36+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
37+
{
38+
$tokens = $phpcsFile->getTokens();
39+
40+
// Check decrement / increment.
41+
if ($tokens[$stackPtr]['code'] === T_DEC || $tokens[$stackPtr]['code'] === T_INC) {
42+
$modifyLeft = substr($tokens[($stackPtr - 1)]['content'], 0, 1) === '$' ||
43+
$tokens[($stackPtr + 1)]['content'] === ';';
44+
45+
if ($modifyLeft === true && $tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
46+
$error = 'There must not be a single space before a unary operator statement';
47+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncDecLeft');
48+
49+
if ($fix === true) {
50+
$phpcsFile->fixer->replaceToken($stackPtr - 1, '');
51+
}
52+
53+
return;
54+
}
55+
56+
if ($modifyLeft === false && substr($tokens[($stackPtr + 1)]['content'], 0, 1) !== '$') {
57+
$error = 'A unary operator statement must not be followed by a single space';
58+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncDecRight');
59+
60+
if ($fix === true) {
61+
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
62+
}
63+
64+
return;
65+
}
66+
}
67+
68+
// Check "!" operator.
69+
if ($tokens[$stackPtr]['code'] === T_BOOLEAN_NOT && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
70+
$error = 'A unary operator statement must not be followed by a space';
71+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BooleanNot');
72+
73+
if ($fix === true) {
74+
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
75+
}
76+
77+
return;
78+
}
79+
80+
// Find the last syntax item to determine if this is an unary operator.
81+
$lastSyntaxItem = $phpcsFile->findPrevious(
82+
array(T_WHITESPACE),
83+
$stackPtr - 1,
84+
($tokens[$stackPtr]['column']) * -1,
85+
true,
86+
null,
87+
true
88+
);
89+
$operatorSuffixAllowed = in_array(
90+
$tokens[$lastSyntaxItem]['code'],
91+
array(
92+
T_LNUMBER,
93+
T_DNUMBER,
94+
T_CLOSE_PARENTHESIS,
95+
T_CLOSE_CURLY_BRACKET,
96+
T_CLOSE_SQUARE_BRACKET,
97+
T_VARIABLE,
98+
T_STRING,
99+
)
100+
);
101+
102+
// Check plus / minus value assignments or comparisons.
103+
if ($tokens[$stackPtr]['code'] === T_MINUS || $tokens[$stackPtr]['code'] === T_PLUS) {
104+
if ($operatorSuffixAllowed === false
105+
&& $tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
106+
) {
107+
$error = 'A unary operator statement must not be followed by a space';
108+
$fix = $phpcsFile->addFixableError($error, $stackPtr);
109+
110+
if ($fix === true) {
111+
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
112+
}
113+
}
114+
}
115+
116+
}
117+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$a !== 1;
4+
$a ! == 1;
5+
6+
$a = !$b;
7+
$a = ! $b;
8+
9+
$a++;
10+
$a ++;
11+
++$a;
12+
++ $a;
13+
14+
1+-2;
15+
1+- 2;
16+
1+ -2;
17+
1+ - 2;
18+
1 +-2;
19+
1 + -2;
20+
1 +- 2;
21+
1 + - 2;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$a !== 1;
4+
$a !== 1;
5+
6+
$a = !$b;
7+
$a = !$b;
8+
9+
$a++;
10+
$a++;
11+
++$a;
12+
++$a;
13+
14+
1+-2;
15+
1+-2;
16+
1+ -2;
17+
1+ -2;
18+
1 +-2;
19+
1 + -2;
20+
1 +-2;
21+
1 + -2;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Unit test class for the UnaryOperatorSpacing sniff.
5+
*
6+
* A sniff unit test checks a .inc file for expected violations of a single
7+
* coding standard. Expected errors and warnings are stored in this class.
8+
*/
9+
class Symfony3Custom_Tests_WhiteSpace_UnaryOperatorSpacingUnitTest extends AbstractSniffUnitTest
10+
{
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @return array<int, int>
18+
*/
19+
public function getErrorList()
20+
{
21+
return array(
22+
4 => 1,
23+
7 => 1,
24+
10 => 1,
25+
12 => 1,
26+
15 => 1,
27+
17 => 1,
28+
20 => 1,
29+
21 => 1,
30+
);
31+
}
32+
33+
/**
34+
* Returns the lines where warnings should occur.
35+
*
36+
* The key of the array should represent the line number and the value
37+
* should represent the number of warnings that should occur on that line.
38+
*
39+
* @return array(int => int)
40+
*/
41+
protected function getWarningList()
42+
{
43+
return array();
44+
}
45+
}

0 commit comments

Comments
 (0)