Skip to content

Commit faa8da3

Browse files
committed
UseStatementHelper speedup
1 parent c2cfca3 commit faa8da3

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

SlevomatCodingStandard/Helpers/UseStatementHelper.php

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use function array_key_exists;
77
use function array_merge;
88
use function array_reverse;
9+
use function count;
10+
use function current;
911
use function in_array;
10-
use function sprintf;
1112
use const T_ANON_CLASS;
1213
use const T_AS;
1314
use const T_COMMA;
@@ -22,7 +23,7 @@ class UseStatementHelper
2223
{
2324

2425
/** @var array<string, array<int, array<string, \SlevomatCodingStandard\Helpers\UseStatement>>> */
25-
private static $allUseStatements = [];
26+
private static $fileUseStatements = [];
2627

2728
public static function isAnonymousFunctionUse(File $phpcsFile, int $usePointer): bool
2829
{
@@ -89,28 +90,71 @@ public static function getFullyQualifiedTypeNameFromUse(File $phpcsFile, int $us
8990
}
9091

9192
/**
93+
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
9294
* @param \PHP_CodeSniffer\Files\File $phpcsFile
9395
* @param int $openTagPointer
9496
* @return array<int, array<string, \SlevomatCodingStandard\Helpers\UseStatement>>
9597
*/
9698
public static function getUseStatements(File $phpcsFile, int $openTagPointer): array
9799
{
98-
$cacheKey = sprintf('%s-%s', $phpcsFile->getFilename(), $openTagPointer);
100+
return self::getFileUseStatements($phpcsFile);
101+
}
102+
103+
/**
104+
* @param \PHP_CodeSniffer\Files\File $phpcsFile
105+
* @param int $pointer
106+
* @return array<string, \SlevomatCodingStandard\Helpers\UseStatement>
107+
*/
108+
public static function getUseStatementsForPointer(File $phpcsFile, int $pointer): array
109+
{
110+
$allUseStatements = self::getFileUseStatements($phpcsFile);
111+
112+
if (count($allUseStatements) === 1) {
113+
return current($allUseStatements);
114+
}
115+
116+
foreach (array_reverse($allUseStatements, true) as $pointerBeforeUseStatements => $useStatements) {
117+
if ($pointerBeforeUseStatements < $pointer) {
118+
return $useStatements;
119+
}
120+
}
121+
122+
return [];
123+
}
124+
125+
/**
126+
* @param \PHP_CodeSniffer\Files\File $phpcsFile
127+
* @return array<int, array<string, \SlevomatCodingStandard\Helpers\UseStatement>>
128+
*/
129+
public static function getFileUseStatements(File $phpcsFile): array
130+
{
131+
$cacheKey = $phpcsFile->getFilename();
99132

100133
$fixerLoops = $phpcsFile->fixer !== null ? $phpcsFile->fixer->loops : null;
101134
if ($fixerLoops !== null) {
102135
$cacheKey .= '-loop' . $fixerLoops;
103136
if ($fixerLoops > 0) {
104-
unset(self::$allUseStatements[$cacheKey . '-loop' . ($fixerLoops - 1)]);
137+
unset(self::$fileUseStatements[$cacheKey . '-loop' . ($fixerLoops - 1)]);
105138
}
106139
}
107140

108-
if (!array_key_exists($cacheKey, self::$allUseStatements)) {
141+
if (!array_key_exists($cacheKey, self::$fileUseStatements)) {
109142
$useStatements = [];
110143
$tokens = $phpcsFile->getTokens();
144+
145+
$namespaceAndOpenTagPointers = TokenHelper::findNextAll($phpcsFile, [T_OPEN_TAG, T_NAMESPACE], 0);
146+
$openTagPointer = $namespaceAndOpenTagPointers[0];
147+
111148
foreach (self::getUseStatementPointers($phpcsFile, $openTagPointer) as $usePointer) {
112-
/** @var int $pointerBeforeUseStatements */
113-
$pointerBeforeUseStatements = TokenHelper::findPrevious($phpcsFile, [T_OPEN_TAG, T_NAMESPACE], $usePointer - 1);
149+
$pointerBeforeUseStatements = $openTagPointer;
150+
if (count($namespaceAndOpenTagPointers) > 1) {
151+
foreach (array_reverse($namespaceAndOpenTagPointers) as $namespaceAndOpenTagPointer) {
152+
if ($namespaceAndOpenTagPointer < $usePointer) {
153+
$pointerBeforeUseStatements = $namespaceAndOpenTagPointer;
154+
break;
155+
}
156+
}
157+
}
114158

115159
$nextTokenFromUsePointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
116160
$type = UseStatement::TYPE_DEFAULT;
@@ -132,28 +176,10 @@ public static function getUseStatements(File $phpcsFile, int $openTagPointer): a
132176
$useStatements[$pointerBeforeUseStatements][UseStatement::getUniqueId($type, $name)] = $useStatement;
133177
}
134178

135-
self::$allUseStatements[$cacheKey] = $useStatements;
179+
self::$fileUseStatements[$cacheKey] = $useStatements;
136180
}
137181

138-
return self::$allUseStatements[$cacheKey];
139-
}
140-
141-
/**
142-
* @param \PHP_CodeSniffer\Files\File $phpcsFile
143-
* @param int $pointer
144-
* @return array<string, \SlevomatCodingStandard\Helpers\UseStatement>
145-
*/
146-
public static function getUseStatementsForPointer(File $phpcsFile, int $pointer): array
147-
{
148-
$allUseStatements = self::getUseStatements($phpcsFile, TokenHelper::findPrevious($phpcsFile, T_OPEN_TAG, $pointer - 1));
149-
150-
foreach (array_reverse($allUseStatements, true) as $pointerBeforeUseStatements => $useStatements) {
151-
if ($pointerBeforeUseStatements < $pointer) {
152-
return $useStatements;
153-
}
154-
}
155-
156-
return [];
182+
return self::$fileUseStatements[$cacheKey];
157183
}
158184

159185
/**

0 commit comments

Comments
 (0)