Skip to content

Commit a15b2d8

Browse files
committed
Rearrange internal helpers; fork SniffLocalCache
1 parent b8393cf commit a15b2d8

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Unleashed\Helpers;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
9+
/**
10+
* @internal
11+
*
12+
* Forked from https://github.com/slevomat/coding-standard/blob/6.3.3/SlevomatCodingStandard/Helpers/SniffLocalCache.php
13+
*/
14+
final class SniffLocalCache
15+
{
16+
/** @var array<int, array<string, mixed>> */
17+
private static $cache = [];
18+
19+
/**
20+
* @return mixed
21+
*/
22+
public static function getAndSetIfNotCached(File $phpcsFile, string $key, \Closure $lazyValue)
23+
{
24+
$fixerLoops = $phpcsFile->fixer !== null ? $phpcsFile->fixer->loops : 0;
25+
$internalKey = \sprintf('%s-%s', $phpcsFile->getFilename(), $key);
26+
27+
self::setIfNotCached($fixerLoops, $internalKey, $lazyValue);
28+
29+
return self::$cache[$fixerLoops][$internalKey] ?? null;
30+
}
31+
32+
private static function setIfNotCached(int $fixerLoops, string $internalKey, \Closure $lazyValue): void
33+
{
34+
if (
35+
\array_key_exists($fixerLoops, self::$cache) &&
36+
\array_key_exists($internalKey, self::$cache[$fixerLoops])
37+
) {
38+
return;
39+
}
40+
41+
self::$cache[$fixerLoops][$internalKey] = $lazyValue();
42+
43+
if ($fixerLoops > 0) {
44+
unset(self::$cache[$fixerLoops - 1]);
45+
}
46+
}
47+
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22

33
declare(strict_types=1);
44

5-
namespace Unleashed;
5+
namespace Unleashed\Helpers;
66

77
use PHP_CodeSniffer\Files\File;
8-
use SlevomatCodingStandard\Helpers\SniffLocalCache;
98
use SlevomatCodingStandard\Helpers\UseStatement;
109
use SlevomatCodingStandard\Helpers\UseStatementHelper;
1110

1211
/**
1312
* @internal
1413
*/
15-
final class SniffHelper
14+
final class UseStatements
1615
{
1716
/**
1817
* @return array<string,bool>
1918
*/
2019
public static function getAliasesAndNonGlobalFunctionsDefinedInUseStatements(File $file): array
2120
{
22-
static $cache;
23-
$cache = $cache ?? new SniffLocalCache();
24-
2521
$lazyValue = static function () use ($file): array {
2622
$result = [];
2723

@@ -44,6 +40,6 @@ public static function getAliasesAndNonGlobalFunctionsDefinedInUseStatements(Fil
4440
return $result;
4541
};
4642

47-
return $cache->getAndSetIfNotCached($file, $lazyValue);
43+
return SniffLocalCache::getAndSetIfNotCached($file, __METHOD__, $lazyValue);
4844
}
4945
}

src/Unleashed/Sniffs/Namespaces/FullyQualifiedGlobalFunctionsSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use SlevomatCodingStandard\Sniffs\Classes\ModernClassNameReferenceSniff;
10-
use Unleashed\SniffHelper;
10+
use Unleashed\Helpers\UseStatements;
1111

1212
final class FullyQualifiedGlobalFunctionsSniff implements Sniff
1313
{
@@ -96,7 +96,7 @@ public function process(File $phpcsFile, $stackPtr)
9696
$globalFunctions = \array_flip(\get_defined_functions()['internal']);
9797
}
9898

99-
$whitelist = SniffHelper::getAliasesAndNonGlobalFunctionsDefinedInUseStatements($phpcsFile);
99+
$whitelist = UseStatements::getAliasesAndNonGlobalFunctionsDefinedInUseStatements($phpcsFile);
100100

101101
$tokens = $phpcsFile->getTokens();
102102
$ignore = [
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

3-
namespace Helpers;
3+
namespace Unleashed\Tests\Helpers;
44

5-
use Unleashed\SniffHelper;
5+
use Unleashed\Helpers\UseStatements;
66
use Unleashed\Tests\PHPCSTestCase;
77

8-
final class SniffHelperTest extends PHPCSTestCase
8+
final class UseStatementsTest extends PHPCSTestCase
99
{
1010
public function testGetAliasesAndNonGlobalFunctionsDefinedInUseStatements()
1111
{
1212
$file = $this->getCodeSnifferFile(__DIR__ . '/data/useStatements.php');
1313

14-
$result = SniffHelper::getAliasesAndNonGlobalFunctionsDefinedInUseStatements($file);
14+
$result = UseStatements::getAliasesAndNonGlobalFunctionsDefinedInUseStatements($file);
1515
$expected = [
1616
'stringlength',
1717
'isbar',

0 commit comments

Comments
 (0)