Skip to content

Commit 2b20e3d

Browse files
Merge pull request #10 from VincentLanglet/staging
Staging
2 parents 39f76e0 + fd07be7 commit 2b20e3d

23 files changed

+458
-160
lines changed

Symfony3Custom/Sniffs/Classes/MultipleClassesOneFileSniff.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

Symfony3Custom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5757

5858
if ($commentRequired) {
5959
$phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing');
60+
6061
return;
6162
} else {
6263
// The comment may not be required, we'll see in next checks
@@ -69,7 +70,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6970
$commentStart = null;
7071
if ($hasComment) {
7172
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
72-
$phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle');
73+
$phpcsFile->addError(
74+
'You must use "/**" style comments for a function comment',
75+
$stackPtr,
76+
'WrongStyle'
77+
);
7378

7479
return;
7580
}
@@ -99,11 +104,15 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
99104
// These checks need function comment
100105
$this->processParams($phpcsFile, $stackPtr, $commentStart);
101106
$this->processThrows($phpcsFile, $stackPtr, $commentStart);
102-
} elseif (count($realParams) > 0) {
103-
foreach ($realParams as $neededParam) {
104-
$error = 'Doc comment for parameter "%s" missing';
105-
$data = array($neededParam['name']);
106-
$phpcsFile->addError($error, $stackPtr, 'MissingParamTag', $data);
107+
} else {
108+
$this->processWhitespace($phpcsFile, $stackPtr, $stackPtr);
109+
110+
if (count($realParams) > 0) {
111+
foreach ($realParams as $neededParam) {
112+
$error = 'Doc comment for parameter "%s" missing';
113+
$data = array($neededParam['name']);
114+
$phpcsFile->addError($error, $stackPtr, 'MissingParamTag', $data);
115+
}
107116
}
108117
}
109118
}
@@ -188,14 +197,22 @@ protected function processWhitespace(
188197
$found = $startLine - $prevLine - 1;
189198

190199
// Skip for class opening
191-
if ($found !== 1 && !($found === 0 && $tokens[$before]['type'] === 'T_OPEN_CURLY_BRACKET')) {
200+
if ($found < 1 && !($found === 0 && $tokens[$before]['type'] === 'T_OPEN_CURLY_BRACKET')) {
192201
if ($found < 0) {
193202
$found = 0;
194203
}
195204

196-
$error = 'Expected 1 blank line before docblock; %s found';
205+
if ($stackPtr === $commentStart) {
206+
// There is no docblock
207+
$error = 'Expected 1 blank line before function; %s found';
208+
$rule = 'SpacingBeforeFunction';
209+
} else {
210+
$error = 'Expected 1 blank line before docblock; %s found';
211+
$rule = 'SpacingBeforeDocblock';
212+
}
213+
197214
$data = array($found);
198-
$fix = $phpcsFile->addFixableError($error, $commentStart, 'SpacingBeforeDocblock', $data);
215+
$fix = $phpcsFile->addFixableError($error, $commentStart, $rule, $data);
199216

200217
if ($fix === true) {
201218
if ($found > 1) {
@@ -229,8 +246,6 @@ protected function processWhitespace(
229246
*/
230247
protected function isInheritDoc(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
231248
{
232-
$tokens = $phpcsFile->getTokens();
233-
234249
$start = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1);
235250
$end = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $start);
236251

@@ -255,8 +270,6 @@ protected function processParams(
255270
$stackPtr,
256271
$commentStart
257272
) {
258-
$tokens = $phpcsFile->getTokens();
259-
260273
if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
261274
return;
262275
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* Checks that there is no white space before a closing bracket, for ")" and "}".
5+
* Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
6+
*/
7+
class Symfony3Custom_Sniffs_WhiteSpace_CloseBracketSpacingSniff implements PHP_CodeSniffer_Sniff
8+
{
9+
/**
10+
* A list of tokenizers this sniff supports.
11+
*
12+
* @var array
13+
*/
14+
public $supportedTokenizers = array(
15+
'PHP',
16+
'JS',
17+
);
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return array(
27+
T_CLOSE_CURLY_BRACKET,
28+
T_CLOSE_PARENTHESIS,
29+
);
30+
}
31+
32+
/**
33+
* Processes this test, when one of its tokens is encountered.
34+
*
35+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
36+
* @param int $stackPtr The position of the current token
37+
* in the stack passed in $tokens.
38+
*
39+
* @return void
40+
*/
41+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
45+
// Ignore curly brackets in javascript files.
46+
if ($tokens[$stackPtr]['code'] === T_CLOSE_CURLY_BRACKET
47+
&& $phpcsFile->tokenizerType === 'JS'
48+
) {
49+
return;
50+
}
51+
52+
if (isset($tokens[($stackPtr - 1)]) === true
53+
&& $tokens[($stackPtr - 1)]['code'] === T_WHITESPACE
54+
) {
55+
$before = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
56+
if ($before !== false
57+
&& $tokens[$stackPtr]['line'] === $tokens[$before]['line']
58+
) {
59+
$error = 'There should be no space before a closing "%s"';
60+
$fix = $phpcsFile->addFixableError(
61+
$error,
62+
($stackPtr - 1),
63+
'ClosingWhitespace',
64+
array($tokens[$stackPtr]['content'])
65+
);
66+
67+
if ($fix === true) {
68+
$phpcsFile->fixer->replaceToken($stackPtr - 1, '');
69+
}
70+
}
71+
}
72+
}
73+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Checks that there are not 2 empty lines following each other.
5+
*/
6+
class Symfony3Custom_Sniffs_WhiteSpace_EmptyLinesSniff implements PHP_CodeSniffer_Sniff
7+
{
8+
/**
9+
* A list of tokenizers this sniff supports.
10+
*
11+
* @var array
12+
*/
13+
public $supportedTokenizers = array(
14+
'PHP',
15+
'JS',
16+
'CSS',
17+
);
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return array(T_WHITESPACE);
27+
}
28+
29+
/**
30+
* Processes this test, when one of its tokens is encountered.
31+
*
32+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
33+
* @param int $stackPtr The position of the current token
34+
* in the stack passed in $tokens.
35+
*
36+
* @return void
37+
*/
38+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
39+
{
40+
$tokens = $phpcsFile->getTokens();
41+
if ($tokens[$stackPtr]['content'] === $phpcsFile->eolChar
42+
&& isset($tokens[$stackPtr + 1]) === true
43+
&& $tokens[$stackPtr + 1]['content'] === $phpcsFile->eolChar
44+
&& isset($tokens[$stackPtr + 2]) === true
45+
&& $tokens[$stackPtr + 2]['content'] === $phpcsFile->eolChar
46+
) {
47+
$error = 'More than 1 empty lines are not allowed';
48+
$fix = $phpcsFile->addFixableError($error, $stackPtr + 2, 'EmptyLines');
49+
50+
if ($fix === true) {
51+
$phpcsFile->fixer->replaceToken($stackPtr + 2, '');
52+
}
53+
}
54+
}
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Checks that there is no white space after an opening bracket, for "(" and "{".
5+
* Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
6+
*/
7+
class Symfony3Custom_Sniffs_WhiteSpace_OpenBracketSpacingSniff implements PHP_CodeSniffer_Sniff
8+
{
9+
/**
10+
* A list of tokenizers this sniff supports.
11+
*
12+
* @var array
13+
*/
14+
public $supportedTokenizers = array(
15+
'PHP',
16+
'JS',
17+
);
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return array(
27+
T_OPEN_CURLY_BRACKET,
28+
T_OPEN_PARENTHESIS,
29+
);
30+
31+
}
32+
33+
/**
34+
* Processes this test, when one of its tokens is encountered.
35+
*
36+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
37+
* @param int $stackPtr The position of the current token
38+
* in the stack passed in $tokens.
39+
*
40+
* @return void
41+
*/
42+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
43+
{
44+
$tokens = $phpcsFile->getTokens();
45+
46+
// Ignore curly brackets in javascript files.
47+
if ($tokens[$stackPtr]['code'] === T_OPEN_CURLY_BRACKET
48+
&& $phpcsFile->tokenizerType === 'JS'
49+
) {
50+
return;
51+
}
52+
53+
if (isset($tokens[($stackPtr + 1)]) === true
54+
&& $tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
55+
&& strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) === false
56+
) {
57+
$error = 'There should be no space after an opening "%s"';
58+
$phpcsFile->addError(
59+
$error,
60+
($stackPtr + 1),
61+
'OpeningWhitespace',
62+
array($tokens[$stackPtr]['content'])
63+
);
64+
}
65+
}
66+
}

Symfony3Custom/Tests/Classes/MultipleClassesOneFileUnitTest.inc

Lines changed: 0 additions & 9 deletions
This file was deleted.

Symfony3Custom/Tests/Commenting/FunctionCommentUnitTest.inc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,24 @@ function functionTestInherit($test)
7373
return 42;
7474
}
7575

76-
function testToIgnore($test)
76+
function testToNotIgnore1($test)
7777
{
7878
return 42;
7979
}
8080

8181
/**
8282
* @param string $test should not be ignore
8383
*/
84-
function testToNotIgnore($test)
84+
function testToNotIgnore2($test)
8585
{
8686
return 42;
8787
}
88+
89+
function testToIgnore1()
90+
{
91+
$test = 42;
92+
}
93+
function testToIgnore2()
94+
{
95+
$test = 42;
96+
}

0 commit comments

Comments
 (0)