Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and jrmajor committed Feb 16, 2022
1 parent 0cfa4ab commit a2b325f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
18 changes: 7 additions & 11 deletions src/Fixer/Operator/NewWithBracesFixer.php
Expand Up @@ -122,6 +122,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
[CT::T_BRACE_CLASS_INSTANTIATION_OPEN],
[CT::T_BRACE_CLASS_INSTANTIATION_CLOSE],
];

if (\defined('T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG')) { // @TODO: drop condition when PHP 8.1+ is required
$nextTokenKinds[] = [T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG];
$nextTokenKinds[] = [T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG];
Expand All @@ -134,10 +135,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

$nextIndex = $tokens->getNextTokenOfKind($index, $nextTokenKinds);
$nextToken = $tokens[$nextIndex];

// new anonymous class definition
if ($nextToken->isGivenKind(T_CLASS)) {
if ($tokens[$nextIndex]->isGivenKind(T_CLASS)) {
$nextIndex = $tokens->getNextMeaningfulToken($nextIndex);

if ($this->configuration['anonymous_class']) {
Expand All @@ -150,14 +150,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

// entrance into array index syntax - need to look for exit
while ($nextToken->equals('[') || $nextToken->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) {
$nextIndex = $tokens->findBlockEnd(Tokens::detectBlockType($nextToken)['type'], $nextIndex) + 1;
$nextToken = $tokens[$nextIndex];
}

// new statement has a gap in it - advance to the next token
if ($nextToken->isWhitespace()) {
$nextIndex = $tokens->getNextNonWhitespace($nextIndex);
while ($tokens[$nextIndex]->equals('[') || $tokens[$nextIndex]->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) {
$nextIndex = $tokens->findBlockEnd(Tokens::detectBlockType($tokens[$nextIndex])['type'], $nextIndex);
$nextIndex = $tokens->getNextMeaningfulToken($nextIndex);
}

if ($this->configuration['named_class']) {
Expand Down Expand Up @@ -206,11 +202,11 @@ private function ensureNoBracesAt(Tokens $tokens, int $index): void
$closingIndex = $tokens->getNextMeaningfulToken($index);

// constructor has arguments - braces can not be removed
if (null === $closingIndex || !$tokens[$closingIndex]->equals(')')) {
if (!$tokens[$closingIndex]->equals(')')) {
return;
}

$tokens->clearAt($closingIndex);
$tokens->clearTokenAndMergeSurroundingWhitespace($closingIndex);
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
}
42 changes: 40 additions & 2 deletions tests/Fixer/Operator/NewWithBracesFixerTest.php
Expand Up @@ -256,6 +256,24 @@ public function provideNamedWithDefaultConfigurationCases(): \Generator
',
],
];

yield [
"<?php \$a = new \$b['class']/* */()\r\n\t ;",
];

yield [
"<?php \$a = new \$b['class'] /* */()\r\n\t ;",
];

yield [
"<?php \$a = new \$b['class']()/* */;",
"<?php \$a = new \$b['class']/* */;",
];

yield [
"<?php \$a = new \$b['class']() /* */;",
"<?php \$a = new \$b['class'] /* */;",
];
}

/**
Expand Down Expand Up @@ -490,6 +508,26 @@ public function provideNamedWithoutBracesCases(): \Generator
',
],
];

yield [
"<?php \$a = new \$b['class']/* */\r\n\t ;",
"<?php \$a = new \$b['class']/* */()\r\n\t ;",
];

yield [
"<?php \$a = new \$b['class'] /* */\r\n\t ;",
"<?php \$a = new \$b['class'] /* */()\r\n\t ;",
];

yield [
"<?php \$a = new \$b['class']/* */;",
"<?php \$a = new \$b['class']()/* */;",
];

yield [
"<?php \$a = new \$b['class'] /* */;",
"<?php \$a = new \$b['class']() /* */;",
];
}

/**
Expand Down Expand Up @@ -579,15 +617,15 @@ public function provideAnonymousWithoutBracesCases(): \Generator
$a = new class /**/ extends Bar1{};
$a = new class extends Bar2 implements Foo{};
$a = new class extends Bar3 implements Foo, Foo2{};
$a = new class {}?>
$a = new class {}?>
',
'<?php
$a = new class() {use SomeTrait;};
$a = new class() implements Foo{};
$a = new class() /**/ extends Bar1{};
$a = new class() extends Bar2 implements Foo{};
$a = new class() extends Bar3 implements Foo, Foo2{};
$a = new class() {}?>
$a = new class ( ) {}?>
',
],
[
Expand Down

0 comments on commit a2b325f

Please sign in to comment.