Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Feb 21, 2022
1 parent eccfd7c commit c4fba27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/Fixer/ClassNotation/ClassDefinitionFixer.php
Expand Up @@ -201,6 +201,18 @@ private function fixClassyDefinition(Tokens $tokens, int $classyIndex): void
$end = $tokens->getPrevNonWhitespace($classDefInfo['open']);
}

if ($classDefInfo['anonymousClass'] && !$this->configuration['inline_constructor_arguments']) {
if (!$tokens[$end]->equals(')')) { // anonymous class with `extends` and/or `implements`
$start = $tokens->getPrevMeaningfulToken($end);
$this->makeClassyDefinitionSingleLine($tokens, $start, $end);
$end = $start;
}

if ($tokens[$end]->equals(')')) { // skip constructor arguments of anonymous class
$end = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $end);
}
}

// 4.1 The extends and implements keywords MUST be declared on the same line as the class name.
$this->makeClassyDefinitionSingleLine($tokens, $classDefInfo['start'], $end);
}
Expand Down Expand Up @@ -335,12 +347,6 @@ private function getClassyInheritanceInfo(Tokens $tokens, int $startIndex, strin
private function makeClassyDefinitionSingleLine(Tokens $tokens, int $startIndex, int $endIndex): void
{
for ($i = $endIndex; $i >= $startIndex; --$i) {
if (!$this->configuration['inline_constructor_arguments'] && $tokens[$i]->equals(')')) {
$i = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $i);

continue;
}

if ($tokens[$i]->isWhitespace()) {
if ($tokens[$i - 1]->isComment() || $tokens[$i + 1]->isComment()) {
$content = $tokens[$i - 1]->getContent();
Expand Down
17 changes: 16 additions & 1 deletion tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php
Expand Up @@ -159,6 +159,11 @@ public function provideAnonymousClassesCases(): array
"<?php \$a = new\n class ( ){};",
['inline_constructor_arguments' => false],
],
[
'<?php $a = new class implements Foo {};',
"<?php \$a = new\n class implements Foo {};",
['inline_constructor_arguments' => false],
],
[
'<?php $a = new class( $this->foo() , bar ( $a) ) {};',
"<?php \$a = new\n class ( \$this->foo() , bar ( \$a) ){};",
Expand Down Expand Up @@ -186,6 +191,16 @@ public function provideAnonymousClassesCases(): array
'<?php $a = new class( $this->prop ){};',
['inline_constructor_arguments' => false],
],
[
"<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, B {};",
"<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, \t B{};",
['inline_constructor_arguments' => false],
],
[
"<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, B {};",
"<?php \$a = new class (\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, \t B{};",
['inline_constructor_arguments' => false],
],
[
'<?php $a = new class($this->prop, $v[3], 4) {};',
'<?php $a = new class( $this->prop,$v[3], 4) {};',
Expand All @@ -198,7 +213,7 @@ public function provideAnonymousClassesCases(): array
\Serializable
{};',
'<?php
$instance = new class extends \Foo implements
$instance = new class extends \Foo implements
\ArrayAccess,\Countable,\Serializable{};',
],
'PSR-12 Implements Parenthesis on the next line.' => [
Expand Down

0 comments on commit c4fba27

Please sign in to comment.