Skip to content

Commit 49a82bc

Browse files
Add ImportInternalFunctionSniff
1 parent d68d298 commit 49a82bc

20 files changed

+259
-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
*/

SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php

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

10+
use function array_keys;
11+
use function in_array;
12+
1013
/**
1114
* Throws warnings if properties are declared after methods
1215
*/

SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use PHP_CodeSniffer\Util\Tokens;
109
use SymfonyCustom\Helpers\SniffHelper;
1110

11+
use function explode;
12+
use function str_replace;
13+
use function strcasecmp;
14+
1215
/**
1316
* Class AlphabeticallySortedUseSniff
1417
*/
@@ -40,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr): int
4043
}
4144
}
4245

43-
$uses = $this->getUseStatements($phpcsFile, $stackPtr);
46+
$uses = SniffHelper::getUseStatements($phpcsFile, $stackPtr);
4447

4548
$lastUse = null;
4649
foreach ($uses as $use) {
@@ -68,67 +71,6 @@ public function process(File $phpcsFile, $stackPtr): int
6871
return T_OPEN_TAG === $tokens[$stackPtr]['code'] ? $phpcsFile->numTokens + 1 : $stackPtr + 1;
6972
}
7073

71-
/**
72-
* @param File $phpcsFile
73-
* @param int $scopePtr
74-
*
75-
* @return array
76-
*/
77-
private function getUseStatements(File $phpcsFile, int $scopePtr): array
78-
{
79-
$tokens = $phpcsFile->getTokens();
80-
81-
$uses = [];
82-
83-
if (isset($tokens[$scopePtr]['scope_opener'])) {
84-
$start = $tokens[$scopePtr]['scope_opener'];
85-
$end = $tokens[$scopePtr]['scope_closer'];
86-
} else {
87-
$start = $scopePtr;
88-
$end = null;
89-
}
90-
91-
$use = $phpcsFile->findNext(T_USE, $start + 1, $end);
92-
while (false !== $use && T_USE === $tokens[$use]['code']) {
93-
if (
94-
!SniffHelper::isGlobalUse($phpcsFile, $use)
95-
|| (null !== $end
96-
&& (!isset($tokens[$use]['conditions'][$scopePtr])
97-
|| $tokens[$use]['level'] !== $tokens[$scopePtr]['level'] + 1))
98-
) {
99-
$use = $phpcsFile->findNext(Tokens::$emptyTokens, $use + 1, $end, true);
100-
continue;
101-
}
102-
103-
// find semicolon as the end of the global use scope
104-
$endOfScope = $phpcsFile->findNext(T_SEMICOLON, $use + 1);
105-
106-
$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $use + 1, $endOfScope);
107-
108-
$type = 'class';
109-
if (T_STRING === $tokens[$startOfName]['code']) {
110-
$lowerContent = mb_strtolower($tokens[$startOfName]['content']);
111-
if ('function' === $lowerContent || 'const' === $lowerContent) {
112-
$type = $lowerContent;
113-
114-
$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $startOfName + 1, $endOfScope);
115-
}
116-
}
117-
118-
$uses[] = [
119-
'ptrUse' => $use,
120-
'name' => trim($phpcsFile->getTokensAsString($startOfName, $endOfScope - $startOfName)),
121-
'ptrEnd' => $endOfScope,
122-
'string' => trim($phpcsFile->getTokensAsString($use, $endOfScope - $use + 1)),
123-
'type' => $type,
124-
];
125-
126-
$use = $phpcsFile->findNext(Tokens::$emptyTokens, $endOfScope + 1, $end, true);
127-
}
128-
129-
return $uses;
130-
}
131-
13274
/**
13375
* @param array $a
13476
* @param array $b

SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php

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

12+
use function in_array;
13+
use function mb_strrpos;
14+
use function mb_strtolower;
15+
use function mb_substr;
16+
use function preg_match;
17+
use function preg_quote;
18+
use function strcasecmp;
19+
use function trim;
20+
1221
/**
1322
* Checks for "use" statements that are not needed in a file.
1423
*/
@@ -300,7 +309,7 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
300309
if (strcasecmp($namespace, $useNamespace) === 0) {
301310
$classUsed = false;
302311
}
303-
} elseif (false === $namespacePtr && '' === $useNamespace) {
312+
} elseif ('' === $useNamespace) {
304313
$classUsed = false;
305314
}
306315
}

SymfonyCustom/Sniffs/NamingConventions/ValidClassNameSniff.php

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

10+
use function mb_strlen;
11+
use function mb_substr;
12+
1013
/**
1114
* Throws errors if symfony's naming conventions are not met.
1215
*/

SymfonyCustom/Sniffs/NamingConventions/ValidFileNameSniff.php

Lines changed: 4 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\Sniffs\Sniff;
99

10+
use function basename;
11+
use function ctype_alnum;
12+
use function mb_strlen;
13+
1014
/**
1115
* Checks whether filename contains any other character than alphanumeric and underscores.
1216
*/

SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
use PHP_CodeSniffer\Sniffs\Sniff;
1010
use SymfonyCustom\Helpers\SniffHelper;
1111

12+
use function array_pop;
13+
use function array_unique;
14+
use function count;
15+
use function implode;
16+
use function in_array;
17+
use function mb_strlen;
18+
use function mb_strpos;
19+
use function mb_strtolower;
20+
use function mb_substr;
21+
use function preg_match;
22+
use function preg_replace;
23+
use function preg_split;
24+
use function usort;
25+
1226
/**
1327
* Throws errors if PHPDocs type hint are not valid.
1428
*/
@@ -233,7 +247,7 @@ private function getValidType(string $typeName): string
233247
return self::TYPES[$lowerType] ? $lowerType : $typeName;
234248
}
235249

236-
// This can't be case insensitive since this is not reserved keyword
250+
// This can't be case-insensitive since this is not reserved keyword
237251
if (isset(self::ALIAS_TYPES[$typeName])) {
238252
return self::ALIAS_TYPES[$typeName];
239253
}

0 commit comments

Comments
 (0)