Skip to content

Commit 161401a

Browse files
Merge pull request #34 from VincentLanglet/staging
Staging
2 parents 1cca2ce + 469744c commit 161401a

24 files changed

+87
-149
lines changed

README.md

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

3+
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.png?v=103)](https://opensource.org/licenses/mit-license.php)
34
[![CircleCI](https://circleci.com/gh/VincentLanglet/Symfony3-custom-coding-standard.svg?style=svg&circle-token=04bcfbcceb34f9644561c0a9ef27e935ff467705)](https://circleci.com/gh/VincentLanglet/Symfony3-custom-coding-standard)
45

56
Customized coding standards for Symfony3 projects.

Symfony3Custom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public function register()
4848
* Processes this sniff, when one of its tokens is encountered.
4949
*
5050
* @param File $phpcsFile The current file being checked.
51-
* @param int $stackPtr The position of the current token in
52-
* the stack passed in $tokens.
51+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
5352
*
5453
* @return void
5554
*/
@@ -123,8 +122,8 @@ public function process(File $phpcsFile, $stackPtr)
123122
}
124123
}
125124

126-
// We can return here because there is nothing else to check. All code
127-
// below can assume that the array is not empty.
125+
// We can return here because there is nothing else to check.
126+
// All code below can assume that the array is not empty.
128127
return;
129128
}
130129

@@ -139,8 +138,7 @@ public function process(File $phpcsFile, $stackPtr)
139138
* Processes a single-line array definition.
140139
*
141140
* @param File $phpcsFile The current file being checked.
142-
* @param int $stackPtr The position of the current token
143-
* in the stack passed in $tokens.
141+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
144142
* @param int $arrayStart The token that starts the array definition.
145143
* @param int $arrayEnd The token that ends the array definition.
146144
*
@@ -162,8 +160,7 @@ public function processSingleLineArray(File $phpcsFile, $stackPtr, $arrayStart,
162160
}
163161

164162
if (T_COMMA === $tokens[$i]['code']) {
165-
// Before counting this comma, make sure we are not
166-
// at the end of the array.
163+
// Before counting this comma, make sure we are not at the end of the array.
167164
$next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), $arrayEnd, true);
168165
if (false !== $next) {
169166
$valueCount++;
@@ -195,10 +192,7 @@ public function processSingleLineArray(File $phpcsFile, $stackPtr, $arrayStart,
195192
if (1 !== $spaceLength) {
196193
$content = $tokens[($nextArrow - 2)]['content'];
197194
$error = 'Expected 1 space between "%s" and double arrow; %s found';
198-
$data = array(
199-
$content,
200-
$spaceLength,
201-
);
195+
$data = array($content, $spaceLength);
202196

203197
$fix = $phpcsFile->addFixableError($error, $nextArrow, 'SpaceBeforeDoubleArrow', $data);
204198
if (true === $fix) {
@@ -221,10 +215,7 @@ public function processSingleLineArray(File $phpcsFile, $stackPtr, $arrayStart,
221215
if (1 !== $spaceLength) {
222216
$content = $tokens[($nextArrow + 2)]['content'];
223217
$error = 'Expected 1 space between double arrow and "%s"; %s found';
224-
$data = array(
225-
$content,
226-
$spaceLength,
227-
);
218+
$data = array($content, $spaceLength);
228219

229220
$fix = $phpcsFile->addFixableError($error, $nextArrow, 'SpaceAfterDoubleArrow', $data);
230221
if (true === $fix) {
@@ -251,10 +242,7 @@ public function processSingleLineArray(File $phpcsFile, $stackPtr, $arrayStart,
251242
if (1 !== $spaceLength) {
252243
$content = $tokens[($comma + 2)]['content'];
253244
$error = 'Expected 1 space between comma and "%s"; %s found';
254-
$data = [
255-
$content,
256-
$spaceLength,
257-
];
245+
$data = [$content, $spaceLength];
258246

259247
$fix = $phpcsFile->addFixableError($error, $comma, 'SpaceAfterComma', $data);
260248
if (true === $fix) {
@@ -267,10 +255,7 @@ public function processSingleLineArray(File $phpcsFile, $stackPtr, $arrayStart,
267255
$content = $tokens[($comma - 2)]['content'];
268256
$spaceLength = $tokens[($comma - 1)]['length'];
269257
$error = 'Expected 0 spaces between "%s" and comma; %s found';
270-
$data = [
271-
$content,
272-
$spaceLength,
273-
];
258+
$data = [$content, $spaceLength];
274259

275260
$fix = $phpcsFile->addFixableError($error, $comma, 'SpaceBeforeComma', $data);
276261
if (true === $fix) {
@@ -285,16 +270,15 @@ public function processSingleLineArray(File $phpcsFile, $stackPtr, $arrayStart,
285270
* Processes a multi-line array definition.
286271
*
287272
* @param File $phpcsFile The current file being checked.
288-
* @param int $stackPtr The position of the current token
289-
* in the stack passed in $tokens.
273+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
290274
* @param int $arrayStart The token that starts the array definition.
291275
* @param int $arrayEnd The token that ends the array definition.
292276
*
293277
* @return void
294278
*/
295279
public function processMultiLineArray(File $phpcsFile, $stackPtr, $arrayStart, $arrayEnd)
296280
{
297-
$tokens = $phpcsFile->getTokens();
281+
$tokens = $phpcsFile->getTokens();
298282

299283
$indent = $phpcsFile->findFirstOnLine(T_WHITESPACE, $arrayStart);
300284
if (false === $indent) {
@@ -316,10 +300,7 @@ public function processMultiLineArray(File $phpcsFile, $stackPtr, $arrayStart, $
316300
$expected = ($currentIndent);
317301
$found = ($tokens[$arrayEnd]['column'] - 1);
318302
$error = 'Closing parenthesis not aligned correctly; expected %s space(s) but found %s';
319-
$data = array(
320-
$expected,
321-
$found,
322-
);
303+
$data = array($expected, $found);
323304

324305
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNotAligned', $data);
325306
if (true === $fix) {
@@ -559,10 +540,7 @@ public function processMultiLineArray(File $phpcsFile, $stackPtr, $arrayStart, $
559540
$found = ($tokens[$first]['column'] - 1);
560541
if ($found !== $expected) {
561542
$error = 'Array value not aligned correctly; expected %s spaces but found %s';
562-
$data = array(
563-
$expected,
564-
$found,
565-
);
543+
$data = array($expected, $found);
566544

567545
$fix = $phpcsFile->addFixableError($error, $value['value'], 'ValueNotAligned', $data);
568546
if (true === $fix) {
@@ -658,10 +636,7 @@ public function processMultiLineArray(File $phpcsFile, $stackPtr, $arrayStart, $
658636
$expected = ($indicesStart - 1);
659637
$found = ($tokens[$index['index']]['column'] - 1);
660638
$error = 'Array key not aligned correctly; expected %s spaces but found %s';
661-
$data = array(
662-
$expected,
663-
$found,
664-
);
639+
$data = array($expected, $found);
665640

666641
$fix = $phpcsFile->addFixableError($error, $index['index'], 'KeyNotAligned', $data);
667642

@@ -686,10 +661,7 @@ public function processMultiLineArray(File $phpcsFile, $stackPtr, $arrayStart, $
686661
}
687662

688663
$error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s';
689-
$data = array(
690-
$expected,
691-
$found,
692-
);
664+
$data = array($expected, $found);
693665

694666
if ('newline' !== $found || false === $this->ignoreNewLines) {
695667
$fix = $phpcsFile->addFixableError($error, $index['arrow'], 'DoubleArrowNotAligned', $data);
@@ -814,10 +786,7 @@ public function processMultiLineArray(File $phpcsFile, $stackPtr, $arrayStart, $
814786
$content = $tokens[($nextComma - 2)]['content'];
815787
$spaceLength = $tokens[($nextComma - 1)]['length'];
816788
$error = 'Expected 0 spaces between "%s" and comma; %s found';
817-
$data = array(
818-
$content,
819-
$spaceLength,
820-
);
789+
$data = array($content, $spaceLength);
821790

822791
$fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data);
823792
if (true === $fix) {

Symfony3Custom/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public function register()
2525
* Processes this test, when one of its tokens is encountered.
2626
*
2727
* @param File $phpcsFile The file being scanned.
28-
* @param int $stackPtr The position of the current token
29-
* in the stack passed in $tokens.
28+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
3029
*
3130
* @return void
3231
*/

Symfony3Custom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public function register()
6262
* Processes this test, when one of its tokens is encountered.
6363
*
6464
* @param File $phpcsFile All the tokens found in the document.
65-
* @param int $stackPtr The position of the current token in
66-
* the stack passed in $tokens.
65+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
6766
*
6867
* @return void
6968
*/
@@ -103,26 +102,21 @@ public function process(File $phpcsFile, $stackPtr)
103102
}
104103
}
105104

106-
if ($previousLine >= 0) {
105+
if (isset($previousElement) && $previousLine >= 0) {
107106
$currentIsCustom = !in_array($currentType, $this->tags);
108-
$previousIsCustom = ('' !== $previousType)
109-
&& !in_array($previousType, $this->tags);
107+
$previousIsCustom = ('' !== $previousType) && !in_array($previousType, $this->tags);
110108

111-
if (($previousType === $currentType)
112-
|| ($currentIsCustom && $previousIsCustom)
113-
) {
109+
if (($previousType === $currentType) || ($currentIsCustom && $previousIsCustom)) {
114110
if ($previousLine !== $commentTagLine - 1) {
115111
if ($previousType === $currentType) {
116112
$fix = $phpcsFile->addFixableError(
117-
'Expected no empty lines '
118-
.'between annotations of the same type',
113+
'Expected no empty lines between annotations of the same type',
119114
$commentTag,
120115
'SameType'
121116
);
122117
} else {
123118
$fix = $phpcsFile->addFixableError(
124-
'Expected no empty lines '
125-
.'between custom annotations',
119+
'Expected no empty lines between custom annotations',
126120
$commentTag,
127121
'CustomType'
128122
);
@@ -142,8 +136,7 @@ public function process(File $phpcsFile, $stackPtr)
142136
} else {
143137
if ($previousLine !== $commentTagLine - 2) {
144138
$fix = $phpcsFile->addFixableError(
145-
'Expected exactly one empty line '
146-
.'between annotations of different types',
139+
'Expected exactly one empty line between annotations of different types',
147140
$commentTag,
148141
'DifferentType'
149142
);
@@ -199,12 +192,8 @@ public function process(File $phpcsFile, $stackPtr)
199192
*
200193
* @return void
201194
*/
202-
protected function removeLines(
203-
File $phpcsFile,
204-
$fromPtr,
205-
$fromLine,
206-
$toLine
207-
) {
195+
protected function removeLines(File $phpcsFile, $fromPtr, $fromLine, $toLine)
196+
{
208197
$tokens = $phpcsFile->getTokens();
209198

210199
for ($i = $fromPtr;; $i++) {

Symfony3Custom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class FunctionCommentSniff extends PEARFunctionCommentSniff
2222
* Processes this test, when one of its tokens is encountered.
2323
*
2424
* @param File $phpcsFile The file being scanned.
25-
* @param int $stackPtr The position of the current token
26-
* in the stack passed in $tokens.
25+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
2726
*
2827
* @return void
2928
*/
@@ -35,20 +34,19 @@ public function process(File $phpcsFile, $stackPtr)
3534

3635
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
3736
if (T_COMMENT === $tokens[$commentEnd]['code']) {
38-
// Inline comments might just be closing comments for
39-
// control structures or functions instead of function comments
40-
// using the wrong comment type. If there is other code on the line,
41-
// assume they relate to that code.
37+
// Inline comments might just be closing comments for control structures or functions
38+
// instead of function comments using the wrong comment type.
39+
// If there is other code on the line, assume they relate to that code.
4240
$prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true);
4341
if (false !== $prev && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) {
4442
$commentEnd = $prev;
4543
}
4644
}
4745

48-
$name = $phpcsFile->getDeclarationName($stackPtr);
49-
$commentRequired = strpos($name, 'test') !== 0
50-
&& 'setUp' !== $name
51-
&& 'tearDown' !== $name;
46+
$name = $phpcsFile->getDeclarationName($stackPtr);
47+
$isTestFunction = strpos($name, 'test') === 0;
48+
$isAllowedFunction = in_array($name, ['setUp', 'tearDown']);
49+
$commentRequired = !$isTestFunction && !$isAllowedFunction;
5250

5351
if (T_DOC_COMMENT_CLOSE_TAG !== $tokens[$commentEnd]['code']
5452
&& T_COMMENT !== $tokens[$commentEnd]['code']
@@ -131,21 +129,21 @@ public function process(File $phpcsFile, $stackPtr)
131129
/**
132130
* Process the return comment of this function comment.
133131
*
134-
* @param File $phpcsFile The file being scanned.
135-
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
136-
* @param int|null $commentStart The position in the stack where the comment started.
137-
* @param bool $hasRealComment
132+
* @param File $phpcsFile The file being scanned.
133+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
134+
* @param int|null $commentStart The position in the stack where the comment started.
135+
* @param bool $hasComment Use to specify if the function has comments to check
138136
*
139137
* @return void
140138
*/
141139
protected function processReturn(
142140
File $phpcsFile,
143141
$stackPtr,
144142
$commentStart,
145-
$hasRealComment = true
143+
$hasComment = true
146144
) {
147145
// Check for inheritDoc if there is comment
148-
if ($hasRealComment && $this->isInheritDoc($phpcsFile, $stackPtr)) {
146+
if ($hasComment && $this->isInheritDoc($phpcsFile, $stackPtr)) {
149147
return;
150148
}
151149

@@ -172,7 +170,7 @@ protected function processReturn(
172170
if (T_RETURN === $tokens[$i]['code']
173171
&& $this->isMatchingReturn($tokens, $i)
174172
) {
175-
if ($hasRealComment) {
173+
if ($hasComment) {
176174
parent::processReturn($phpcsFile, $stackPtr, $commentStart);
177175
} else {
178176
// There is no doc and we need one with @return
@@ -189,12 +187,12 @@ protected function processReturn(
189187
/**
190188
* @param File $phpcsFile
191189
* @param int $commentStart
192-
* @param bool $hasRealComment
190+
* @param bool $hasComment
193191
*/
194192
protected function processWhitespace(
195193
File $phpcsFile,
196194
$commentStart,
197-
$hasRealComment = true
195+
$hasComment = true
198196
) {
199197
$tokens = $phpcsFile->getTokens();
200198
$before = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);
@@ -210,7 +208,7 @@ protected function processWhitespace(
210208
$found = 0;
211209
}
212210

213-
if ($hasRealComment) {
211+
if ($hasComment) {
214212
$error = 'Expected 1 blank line before docblock; %s found';
215213
$rule = 'SpacingBeforeDocblock';
216214
} else {

Symfony3Custom/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ class VariableCommentSniff extends AbstractVariableSniff
1414
* Called to process class member vars.
1515
*
1616
* @param File $phpcsFile The file being scanned.
17-
* @param int $stackPtr The position of the current token
18-
* in the stack passed in $tokens.
17+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
1918
*
2019
* @return void
2120
*/

Symfony3Custom/Sniffs/Errors/ExceptionMessageSniff.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ public function register()
2626
* Called when one of the token types that this sniff is listening for is found.
2727
*
2828
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
29-
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack
30-
* where the token was found.
29+
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found.
3130
*
32-
* @return void|int Optionally returns a stack pointer. The sniff will not be
33-
* called again on the current file until the returned stack
34-
* pointer is reached. Return (count($tokens) + 1) to skip
35-
* the rest of the file.
31+
* @return void|int Optionally returns a stack pointer. The sniff will not be called again
32+
* on the current file until the returned stack pointer is reached.
33+
* Return (count($tokens) + 1) to skip the rest of the file.
3634
*/
3735
public function process(File $phpcsFile, $stackPtr)
3836
{

Symfony3Custom/Sniffs/Errors/UserDeprecatedSniff.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ public function register()
2626
* Called when one of the token types that this sniff is listening for is found.
2727
*
2828
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
29-
* @param int $stackPtr The position in the PHP_CodeSniffer file's token
30-
* stack where the token was found.
29+
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found.
3130
*
32-
* @return void|int Optionally returns a stack pointer. The sniff will not be
33-
* called again on the current file until the returned stack
34-
* pointer is reached. Return (count($tokens) + 1) to skip
35-
* the rest of the file.
31+
* @return void|int Optionally returns a stack pointer. The sniff will not be called again on
32+
* the current file until the returned stack pointer is reached.
33+
* Return (count($tokens) + 1) to skip the rest of the file.
3634
*/
3735
public function process(File $phpcsFile, $stackPtr)
3836
{

0 commit comments

Comments
 (0)