Skip to content

Commit

Permalink
Merge 83a0acf into f7bd80b
Browse files Browse the repository at this point in the history
  • Loading branch information
nightlinus committed Mar 25, 2020
2 parents f7bd80b + 83a0acf commit 7661be6
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
24 changes: 22 additions & 2 deletions SlevomatCodingStandard/Helpers/NamespaceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SlevomatCodingStandard\Helpers;

use PHP_CodeSniffer\Files\File;
use function array_reverse;
use function array_slice;
use function count;
use function explode;
Expand All @@ -29,7 +30,14 @@ class NamespaceHelper
*/
public static function getAllNamespacesPointers(File $phpcsFile): array
{
return TokenHelper::findNextAll($phpcsFile, T_NAMESPACE, 0);
static $cache;
$cache = $cache ?? new SniffLocalCache();

$lazyValue = static function () use ($phpcsFile): array {
return TokenHelper::findNextAll($phpcsFile, T_NAMESPACE, 0);
};

return $cache->getAndSetIfNotCached($phpcsFile, $lazyValue);
}

public static function isFullyQualifiedName(string $typeName): bool
Expand Down Expand Up @@ -64,9 +72,21 @@ public static function getName(File $phpcsFile, int $namespacePointer): string
return TokenHelper::getContent($phpcsFile, $namespaceNameStartPointer, $namespaceNameEndPointer);
}

public static function findCurrentNamespacePointer(File $phpcsFile, int $pointer): ?int
{
$allNamespacesPointers = array_reverse(self::getAllNamespacesPointers($phpcsFile));
foreach ($allNamespacesPointers as $namespacesPointer) {
if ($namespacesPointer < $pointer) {
return $namespacesPointer;
}
}

return null;
}

public static function findCurrentNamespaceName(File $phpcsFile, int $anyPointer): ?string
{
$namespacePointer = TokenHelper::findPrevious($phpcsFile, T_NAMESPACE, $anyPointer);
$namespacePointer = self::findCurrentNamespacePointer($phpcsFile, $anyPointer);
if ($namespacePointer === null) {
return null;
}
Expand Down
103 changes: 103 additions & 0 deletions SlevomatCodingStandard/Helpers/SniffLocalCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php declare(strict_types = 1);

namespace SlevomatCodingStandard\Helpers;

use Closure;
use PHP_CodeSniffer\Files\File;
use function array_key_exists;

/**
* Use for caching some value based on phpcsFile version
*
* @internal
*/
final class SniffLocalCache
{

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint
* @var array<string, mixed>
*/
private $cache = [];

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint
* @param File $phpcsFile
* @param Closure $lazyValue
* @return mixed
*/
public function getAndSetIfNotCached(File $phpcsFile, Closure $lazyValue)
{
$this->setIfNotCached($phpcsFile, $lazyValue);

return $this->get($phpcsFile);
}

private function setIfNotCached(File $phpcsFile, Closure $lazyValue): void
{
if ($this->has($phpcsFile)) {
return;
}

$this->set($phpcsFile, $lazyValue());
}

private function has(File $phpcsFile): bool
{
$key = $this->key($phpcsFile);

return array_key_exists($key, $this->cache);
}

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint
* @param File $phpcsFile
* @return mixed
*/
private function get(File $phpcsFile)
{
return $this->cache[$this->key($phpcsFile) ] ?? null;
}

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint
* @param File $phpcsFile
* @param mixed $value
*/
private function set(File $phpcsFile, $value): void
{
$key = $this->key($phpcsFile);
$this->cache[$key] = $value;
$this->removeOldCache($phpcsFile);
}

private function key(File $phpcsFile): string
{
$cacheKey = $phpcsFile->getFilename();
$fixerLoops = $phpcsFile->fixer !== null ? $phpcsFile->fixer->loops : 0;

return $cacheKey . '-loop' . $fixerLoops;
}

private function previousKey(File $phpcsFile): ?string
{
$cacheKey = $phpcsFile->getFilename();
$fixerLoops = $phpcsFile->fixer !== null ? $phpcsFile->fixer->loops : 0;
if ($fixerLoops === 0) {
return null;
}

return $cacheKey . '-loop' . ($fixerLoops - 1);
}

private function removeOldCache(File $phpcsFile): void
{
$key = $this->previousKey($phpcsFile);
if ($key === null) {
return;
}

unset($this->cache[$key]);
}

}

0 comments on commit 7661be6

Please sign in to comment.