Skip to content

Commit 230789a

Browse files
Merge pull request #146 from VincentLanglet/newSniff
Add ImportInternalFunctionSniff and OperatorPlacementSniff
2 parents 40eb821 + 454d59a commit 230789a

26 files changed

+442
-65
lines changed

SymfonyCustom/Helpers/FixerHelper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PHP_CodeSniffer\Files\File;
88

9+
use function str_repeat;
10+
911
/**
1012
* Class FixerHelper
1113
*/

SymfonyCustom/Helpers/SniffHelper.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Util\Tokens;
99

10+
use function mb_strtolower;
11+
use function preg_match;
12+
use function trim;
13+
1014
/**
1115
* Class SniffHelper
1216
*/
@@ -187,6 +191,67 @@ public static function isGlobalUse(File $phpcsFile, int $stackPtr): bool
187191
return true;
188192
}
189193

194+
/**
195+
* @param File $phpcsFile
196+
* @param int $scopePtr
197+
*
198+
* @return array
199+
*/
200+
public static function getUseStatements(File $phpcsFile, int $scopePtr): array
201+
{
202+
$tokens = $phpcsFile->getTokens();
203+
204+
$uses = [];
205+
206+
if (isset($tokens[$scopePtr]['scope_opener'])) {
207+
$start = $tokens[$scopePtr]['scope_opener'];
208+
$end = $tokens[$scopePtr]['scope_closer'];
209+
} else {
210+
$start = $scopePtr;
211+
$end = null;
212+
}
213+
214+
$use = $phpcsFile->findNext(T_USE, $start + 1, $end);
215+
while (false !== $use && T_USE === $tokens[$use]['code']) {
216+
if (
217+
!self::isGlobalUse($phpcsFile, $use)
218+
|| (null !== $end
219+
&& (!isset($tokens[$use]['conditions'][$scopePtr])
220+
|| $tokens[$use]['level'] !== $tokens[$scopePtr]['level'] + 1))
221+
) {
222+
$use = $phpcsFile->findNext(Tokens::$emptyTokens, $use + 1, $end, true);
223+
continue;
224+
}
225+
226+
// find semicolon as the end of the global use scope
227+
$endOfScope = $phpcsFile->findNext(T_SEMICOLON, $use + 1);
228+
229+
$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $use + 1, $endOfScope);
230+
231+
$type = 'class';
232+
if (T_STRING === $tokens[$startOfName]['code']) {
233+
$lowerContent = mb_strtolower($tokens[$startOfName]['content']);
234+
if ('function' === $lowerContent || 'const' === $lowerContent) {
235+
$type = $lowerContent;
236+
237+
$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $startOfName + 1, $endOfScope);
238+
}
239+
}
240+
241+
$uses[] = [
242+
'ptrUse' => $use,
243+
'name' => trim($phpcsFile->getTokensAsString($startOfName, $endOfScope - $startOfName)),
244+
'ptrEnd' => $endOfScope,
245+
'string' => trim($phpcsFile->getTokensAsString($use, $endOfScope - $use + 1)),
246+
'type' => $type,
247+
];
248+
249+
$use = $phpcsFile->findNext(Tokens::$emptyTokens, $endOfScope + 1, $end, true);
250+
}
251+
252+
return $uses;
253+
}
254+
190255
/**
191256
* @param string $content
192257
*

SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use PHP_CodeSniffer\Util\Tokens;
1010
use SymfonyCustom\Helpers\FixerHelper;
1111

12+
use function count;
13+
use function in_array;
14+
use function mb_strlen;
15+
1216
/**
1317
* A test to ensure that arrays conform to the array coding standard.
1418
*/

SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99

10+
use function in_array;
11+
1012
/**
1113
* Throws errors if forbidden tags are met.
1214
*/

SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use SymfonyCustom\Helpers\FixerHelper;
1111
use SymfonyCustom\Helpers\SniffHelper;
1212

13+
use function array_merge;
14+
use function in_array;
15+
1316
/**
1417
* Throws errors if comments are not grouped by type with one blank line between them.
1518
*/

SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use SymfonyCustom\Helpers\FixerHelper;
1010

11+
use function ltrim;
12+
use function rtrim;
13+
use function str_repeat;
14+
1115
/**
1216
* Ensures doc blocks follow basic formatting.
1317
*/

SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use PHP_CodeSniffer\Util\Tokens;
1010
use SymfonyCustom\Helpers\SniffHelper;
1111

12+
use function count;
13+
use function preg_match;
14+
use function preg_replace;
15+
1216
/**
1317
* SymfonyCustom standard customization to PEARs FunctionCommentSniff.
1418
*/

SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
99
use SymfonyCustom\Helpers\SniffHelper;
1010

11+
use function preg_match;
12+
use function preg_replace;
13+
1114
/**
1215
* Parses and verifies the variable doc comment.
1316
*/

SymfonyCustom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
1010

11+
use function in_array;
12+
1113
/**
1214
* Throws errors if there's no blank line before return statements.
1315
*/

SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
1010

11+
use function array_merge;
12+
use function in_array;
13+
1114
/**
1215
* Enforces Yoda conditional statements.
1316
*/

0 commit comments

Comments
 (0)