Skip to content

Commit

Permalink
Merge 1376135 into 4a32d5b
Browse files Browse the repository at this point in the history
  • Loading branch information
nightlinus committed Mar 25, 2020
2 parents 4a32d5b + 1376135 commit 03924d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
38 changes: 22 additions & 16 deletions SlevomatCodingStandard/Helpers/ClassHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace SlevomatCodingStandard\Helpers;

use Generator;
use PHP_CodeSniffer\Files\File;
use function iterator_to_array;
use function array_reverse;
use function sprintf;
use const T_ANON_CLASS;
use const T_FINAL;
Expand All @@ -14,6 +13,18 @@
class ClassHelper
{

public static function getClassPointer(File $phpcsFile, int $pointer): ?int
{
$classPointers = array_reverse(self::getAllClassPointers($phpcsFile));
foreach ($classPointers as $classPointer) {
if ($classPointer < $pointer) {
return $classPointer;
}
}

return null;
}

public static function isFinal(File $phpcsFile, int $classPointer): bool
{
return $phpcsFile->getTokens()[TokenHelper::findPreviousEffective($phpcsFile, $classPointer - 1)]['code'] === T_FINAL;
Expand Down Expand Up @@ -50,11 +61,9 @@ public static function getName(File $phpcsFile, int $classPointer): string
*/
public static function getAllNames(File $phpcsFile): array
{
$previousClassPointer = 0;

$names = [];
/** @var int $classPointer */
foreach (iterator_to_array(self::getAllClassPointers($phpcsFile, $previousClassPointer)) as $classPointer) {
foreach (self::getAllClassPointers($phpcsFile) as $classPointer) {
$names[$classPointer] = self::getName($phpcsFile, $classPointer);
}

Expand Down Expand Up @@ -90,21 +99,18 @@ public static function getTraitUsePointers(File $phpcsFile, int $classPointer):

/**
* @param File $phpcsFile
* @param int $previousClassPointer
* @return Generator<int>
* @return array<int>
*/
private static function getAllClassPointers(File $phpcsFile, int &$previousClassPointer): Generator
private static function getAllClassPointers(File $phpcsFile): array
{
do {
$nextClassPointer = TokenHelper::findNext($phpcsFile, TokenHelper::$typeKeywordTokenCodes, $previousClassPointer + 1);
if ($nextClassPointer === null) {
break;
}
static $cache;
$cache = $cache ?? new SniffLocalCache();

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

yield $nextClassPointer;
} while (true);
return $cache->getAndSetIfNotCached($phpcsFile, $lazyValue);
}

}
3 changes: 2 additions & 1 deletion SlevomatCodingStandard/Sniffs/Classes/MethodSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\ClassHelper;
use SlevomatCodingStandard\Helpers\DocCommentHelper;
use SlevomatCodingStandard\Helpers\FunctionHelper;
use SlevomatCodingStandard\Helpers\IndentationHelper;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function process(File $phpcsFile, $methodPointer): void
? $tokens[$methodPointer]['scope_closer']
: TokenHelper::findNext($phpcsFile, T_SEMICOLON, $methodPointer + 1);

$classPointer = TokenHelper::findPrevious($phpcsFile, TokenHelper::$typeKeywordTokenCodes, $methodPointer - 1);
$classPointer = ClassHelper::getClassPointer($phpcsFile, $methodPointer - 1);

$actualPointer = $methodEndPointer;
do {
Expand Down

0 comments on commit 03924d0

Please sign in to comment.