Skip to content

Commit

Permalink
chore: cs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
k0d3r1s committed Aug 27, 2024
1 parent ef6f6b0 commit 055869e
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 38 deletions.
13 changes: 10 additions & 3 deletions Fixer/DeclareAfterOpeningTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function applyFix(
assert(is_int($semicolonIndex));

$tokensToInsert = [];

for ($index = $declareIndex; $index <= $semicolonIndex; $index++) {
$tokensToInsert[] = $tokens[$index];
}
Expand All @@ -75,10 +76,16 @@ public function applyFix(
$tokensToInsert[] = new Token([T_WHITESPACE, substr($openingTagTokenContent, 5)]);
}

if ($tokens[$semicolonIndex + 1]->isGivenKind(T_WHITESPACE)) {
$content = (new class {
static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _Replace;
})::replace('/^(\\R?)(?=\\R)/', '', $tokens[$semicolonIndex + 1]->getContent());
};
}

if ($tokens[$semicolonIndex + 1]->isGivenKind(T_WHITESPACE)) {
$content = $_helper->replace('/^(\\R?)(?=\\R)/', '', $tokens[$semicolonIndex + 1]->getContent());

$tokens->ensureWhitespaceAtIndex($semicolonIndex + 1, 0, $content);
}
Expand Down
2 changes: 2 additions & 0 deletions Fixer/IssetToArrayKeyExistsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function applyFix(

$closeBrackets = $tokens->getPrevMeaningfulToken($closeParenthesis);
assert(is_int($closeBrackets));

if (!$tokens[$closeBrackets]->equals(']')) {
continue;
}
Expand All @@ -61,6 +62,7 @@ public function applyFix(
$keyEndIndex = $tokens->getPrevMeaningfulToken($closeBrackets);

$keyTokens = [];

for ($i = $keyStartIndex; $i <= $keyEndIndex; $i++) {
if ($tokens[$i]->equals('')) {
continue;
Expand Down
9 changes: 9 additions & 0 deletions Fixer/NoUselessDirnameCallFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ public function applyFix(
}

$prevInserts = $this->getPrevTokensUpdates($tokens, $index);

if (null === $prevInserts) {
continue;
}

$nextInserts = $this->getNextTokensUpdates($tokens, $index);

if (null === $nextInserts) {
continue;
}
Expand Down Expand Up @@ -89,10 +91,12 @@ private function getNextTokensUpdates(

$commaOrClosingParenthesisIndex = $tokens->getNextMeaningfulToken($index);
assert(is_int($commaOrClosingParenthesisIndex));

if ($tokens[$commaOrClosingParenthesisIndex]->equals(',')) {
$updates[$commaOrClosingParenthesisIndex] = '';
$afterCommaIndex = $tokens->getNextMeaningfulToken($commaOrClosingParenthesisIndex);
assert(is_int($afterCommaIndex));

if ($tokens[$afterCommaIndex]->isGivenKind(T_LNUMBER)) {
$depthLevel = (int) $tokens[$afterCommaIndex]->getContent();
$updates[$afterCommaIndex] = '';
Expand All @@ -115,12 +119,14 @@ private function getNextTokensUpdates(

$concatenationIndex = $tokens->getNextMeaningfulToken($closingParenthesisIndex);
assert(is_int($concatenationIndex));

if (!$tokens[$concatenationIndex]->equals('.')) {
return null;
}

$stringIndex = $tokens->getNextMeaningfulToken($concatenationIndex);
assert(is_int($stringIndex));

if (!$tokens[$stringIndex]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
return null;
}
Expand All @@ -139,13 +145,15 @@ private function getPrevTokensUpdates(

$openParenthesisIndex = $tokens->getPrevMeaningfulToken($index);
assert(is_int($openParenthesisIndex));

if (!$tokens[$openParenthesisIndex]->equals('(')) {
return null;
}
$updates[$openParenthesisIndex] = '';

$dirnameCallIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex);
assert(is_int($dirnameCallIndex));

if (!$tokens[$dirnameCallIndex]->equals([T_STRING, 'dirname'], false)) {
return null;
}
Expand All @@ -157,6 +165,7 @@ private function getPrevTokensUpdates(

$namespaceSeparatorIndex = $tokens->getPrevMeaningfulToken($dirnameCallIndex);
assert(is_int($namespaceSeparatorIndex));

if ($tokens[$namespaceSeparatorIndex]->isGivenKind(T_NS_SEPARATOR)) {
$updates[$namespaceSeparatorIndex] = '';
}
Expand Down
3 changes: 3 additions & 0 deletions Fixer/NoUselessStrlenFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function applyFix(
assert(is_int($prevIndex));

$startIndex = $index;

if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
$startIndex = $prevIndex;
$tokensToRemove[$prevIndex] = 1;
Expand Down Expand Up @@ -140,6 +141,7 @@ private function transformConditionLeft(
assert(is_int($prevIndex));

$changeCondition = false;

if ($tokens[$prevIndex]->equals('<')) {
$changeCondition = true;
} elseif (!$tokens[$prevIndex]->isGivenKind([T_IS_IDENTICAL, T_IS_NOT_IDENTICAL, T_IS_EQUAL, T_IS_NOT_EQUAL])) {
Expand Down Expand Up @@ -170,6 +172,7 @@ private function transformConditionRight(
assert(is_int($nextIndex));

$changeCondition = false;

if ($tokens[$nextIndex]->equals('>')) {
$changeCondition = true;
} elseif (!$tokens[$nextIndex]->isGivenKind([T_IS_IDENTICAL, T_IS_NOT_IDENTICAL, T_IS_EQUAL, T_IS_NOT_EQUAL])) {
Expand Down
19 changes: 16 additions & 3 deletions Fixer/PromotedConstructorPropertyFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function applyFix(
}

$constructorAnalysis = $constructorAnalyzer->findNonAbstractConstructor($index);

if (null === $constructorAnalysis) {
continue;
}
Expand Down Expand Up @@ -101,6 +102,7 @@ private function getClassProperties(
int $classIndex,
): array {
$properties = [];

foreach ((new TokensAnalyzer($tokens))->getClassyElements() as $index => $element) {
if ($element['classIndex'] !== $classIndex) {
continue;
Expand Down Expand Up @@ -177,6 +179,7 @@ private function getType(
assert(is_int($index));

$type = '';

while ($index < $variableIndex) {
$type .= $tokens[$index]->getContent();

Expand All @@ -198,10 +201,16 @@ private function isDoctrineEntity(
return false;
}

foreach ((new DocBlock($tokens[$phpDocIndex]->getContent()))->getAnnotations() as $annotation) {
if ((new class {
static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _Match;
})::match('/\\*\\h+(@Document|@Entity|@Mapping\\\\Entity|@ODM\\\\Document|@ORM\\\\Entity|@ORM\\\\Mapping\\\\Entity)/', $annotation->getContent())) {
};
}

foreach ((new DocBlock($tokens[$phpDocIndex]->getContent()))->getAnnotations() as $annotation) {
if ($_helper->match('/\\*\\h+(@Document|@Entity|@Mapping\\\\Entity|@ODM\\\\Document|@ORM\\\\Entity|@ORM\\\\Mapping\\\\Entity)/', $annotation->getContent())) {
return true;
}
}
Expand Down Expand Up @@ -269,6 +278,7 @@ private function promoteProperties(
$assignedPropertyIndex = $tokens->getPrevTokenOfKind($constructorPromotableAssignments[$constructorParameterName], [[T_STRING]]);
$oldParameterName = $tokens[$constructorParameterIndex]->getContent();
$newParameterName = '$' . $tokens[$assignedPropertyIndex]->getContent();

if ($oldParameterName !== $newParameterName && in_array($newParameterName, $constructorParameterNames, true)) {
continue;
}
Expand Down Expand Up @@ -318,6 +328,7 @@ private function removePropertyAndReturnTokensToInsert(
$removeFrom = $tokens->getTokenNotOfKindSibling($prevPropertyIndex, 1, [[T_WHITESPACE], [T_COMMENT]]);
assert(is_int($removeFrom));
$removeTo = $nextPropertyIndex;

if ($tokens[$prevPropertyIndex]->equals(',')) {
$removeFrom = $prevPropertyIndex;
$removeTo = $propertyIndex;
Expand All @@ -328,11 +339,13 @@ private function removePropertyAndReturnTokensToInsert(
}

$tokensToInsert = [];

for ($index = $removeFrom; $index <= $visibilityIndex - 1; $index++) {
$tokensToInsert[] = $tokens[$index];
}

$visibilityToken = $tokens[$visibilityIndex];

if ($tokens[$visibilityIndex]->isGivenKind(T_VAR)) {
$visibilityToken = new Token([T_PUBLIC, 'public']);
}
Expand Down
19 changes: 14 additions & 5 deletions Fixers.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ final class Fixers implements IteratorAggregate
public function getIterator(): Generator
{
$classNames = [];

foreach (new DirectoryIterator(__DIR__ . '/Fixer') as $fileInfo) {
$fileName = $fileInfo->getBasename('.php');

if (in_array($fileName, ['.', '..', ], true)) {
continue;
}
Expand All @@ -54,15 +56,21 @@ public static function getFixers(): array
$finder->files()->in(__DIR__ . '/Fixer/')->name('*.php');
$files = [];

$match = new class {
use _Match;
};
static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _Match;
};
}

$classMatches = $namespaceMatches = ['', ''];

foreach ($finder as $file) {
$fileContents = file_get_contents($file->getRealPath());

if ($match::match('/namespace\s+(.+?);/', $fileContents, $namespaceMatches)
&& $match::match('/class\s+(\w+)/', $fileContents, $classMatches)) {
if ($_helper->match('/namespace\s+(.+?);/', $fileContents, $namespaceMatches)
&& $_helper->match('/class\s+(\w+)/', $fileContents, $classMatches)) {
$className = $classMatches[1];
$fullClassName = $namespaceMatches[1] . '\\' . $className;

Expand All @@ -73,6 +81,7 @@ public static function getFixers(): array
}

$fixers = [];

foreach ($files as $fixer) {
$fixers[AbstractFixer::getNameForClass($fixer)] = true;
}
Expand Down
72 changes: 50 additions & 22 deletions PhpCsFixer/AbstractFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,15 @@ final public static function getNameForClass(
$parts = explode('\\', $class);
$name = substr($parts[count($parts) - 1], 0, -strlen('Fixer'));

return sprintf('%s/%s', self::PREFIX, (new class {
use _SnakeCaseFromCamelCase;
})->snakeCaseFromCamelCase($name));
static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _SnakeCaseFromCamelCase;
};
}

return sprintf('%s/%s', self::PREFIX, $_helper->snakeCaseFromCamelCase($name));
}

public static function removeWithLinesIfPossible(
Expand All @@ -148,6 +154,7 @@ public static function removeWithLinesIfPossible(
$wasNewlineRemoved = self::handleWhitespaceBefore($tokens, $prev);

$next = $tokens->getNonEmptySibling($index, 1);

if (null !== $next) {
self::handleWhitespaceAfter($tokens, $next, $wasNewlineRemoved);
}
Expand Down Expand Up @@ -186,6 +193,7 @@ protected function getComments(
Tokens $tokens,
): array {
$comments = [];

foreach ($tokens as $index => $token) {
if ($token->isComment()) {
$comments[$index] = $token;
Expand All @@ -201,6 +209,7 @@ protected function getUseStatements(
): ?array {
$fqcnArray = is_array($fqcn) ? $fqcn : explode('\\', $fqcn);
$sequence = [[T_USE]];

foreach ($fqcnArray as $component) {
$sequence[] = [T_STRING, $component];
$sequence[] = [T_NS_SEPARATOR];
Expand Down Expand Up @@ -238,9 +247,16 @@ protected static function handleWhitespaceAfter(
bool $wasNewlineRemoved,
): void {
$pattern = $wasNewlineRemoved ? '/^\\h+/' : '/^\\h*\\R/';
$newContent = (new class {
use _Replace;
})::replace($pattern, '', $tokens[$index]->getContent());

static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _Replace;
};
}

$newContent = $_helper->replace($pattern, '', $tokens[$index]->getContent());
$tokens->ensureWhitespaceAtIndex($index, 0, $newContent);
}

Expand All @@ -252,12 +268,16 @@ protected static function handleWhitespaceBefore(
return false;
}

$replace = new class {
use _Replace;
};
static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _Replace;
};
}

$withoutTrailingSpaces = $replace::replace('/\\h+$/', '', $tokens[$index]->getContent());
$withoutNewline = $replace::replace('/\\R$/', '', $withoutTrailingSpaces, 1);
$withoutTrailingSpaces = $_helper->replace('/\\h+$/', '', $tokens[$index]->getContent());
$withoutNewline = $_helper->replace('/\\R$/', '', $withoutTrailingSpaces, 1);
$tokens->ensureWhitespaceAtIndex($index, 0, $withoutNewline);

return $withoutTrailingSpaces !== $withoutNewline;
Expand All @@ -269,11 +289,15 @@ protected static function hasMeaningTokenInLineAfter(
): bool {
$next = $tokens->getNonEmptySibling($index, 1);

return null !== $next
&& (!$tokens[$next]->isGivenKind(T_WHITESPACE)
|| !(new class {
use _Match;
})::match('/\\R/', $tokens[$next]->getContent()));
static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _Match;
};
}

return null !== $next && (!$tokens[$next]->isGivenKind(T_WHITESPACE) || !$_helper->match('/\\R/', $tokens[$next]->getContent()));
}

protected static function hasMeaningTokenInLineBefore(
Expand All @@ -287,19 +311,23 @@ protected static function hasMeaningTokenInLineBefore(
return true;
}

$match = new class {
use _Match;
};
static $_helper = null;

if (null === $_helper) {
$_helper = new class {
use _Match;
};
}

if ($tokens[$prev]->isGivenKind(T_OPEN_TAG) && !$match::match('/\\R$/', $tokens[$prev]->getContent())) {
if ($tokens[$prev]->isGivenKind(T_OPEN_TAG) && !$_helper->match('/\\R$/', $tokens[$prev]->getContent())) {
return true;
}

if (!$match::match('/\\R/', $tokens[$prev]->getContent())) {
if (!$_helper->match('/\\R/', $tokens[$prev]->getContent())) {
$prevPrev = $tokens->getNonEmptySibling($prev, -1);
assert(is_int($prevPrev));

if (!$tokens[$prevPrev]->isGivenKind(T_OPEN_TAG) || !$match::match('/\\R$/', $tokens[$prevPrev]->getContent())) {
if (!$tokens[$prevPrev]->isGivenKind(T_OPEN_TAG) || !$_helper->match('/\\R$/', $tokens[$prevPrev]->getContent())) {
return true;
}
}
Expand Down
Loading

0 comments on commit 055869e

Please sign in to comment.