Skip to content

Commit 891ecca

Browse files
committed
bug #4217 Psr0Fixer - class with anonymous class (kubawerlos)
This PR was merged into the 2.12 branch. Discussion ---------- Psr0Fixer - class with anonymous class Similar to #3974 Commits ------- e28c9e4 Psr0Fixer - class with anonymous class
2 parents 4537bfd + e28c9e4 commit 891ecca

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Fixer/Basic/Psr0Fixer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
8181

8282
$namespace = trim($tokens->generatePartialCode($namespaceIndex, $namespaceEndIndex - 1));
8383
} elseif ($token->isClassy()) {
84-
if (null !== $classyName) {
85-
return;
86-
}
87-
8884
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
8985
if ($prevToken->isGivenKind(T_NEW)) {
86+
break;
87+
}
88+
89+
if (null !== $classyName) {
9090
return;
9191
}
9292

tests/Fixer/Basic/Psr0FixerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,35 @@ class Psr0Fixer {}
191191
$this->doTest($expected, null, $file);
192192
}
193193

194+
/**
195+
* @requires PHP 7.0
196+
*/
197+
public function testClassWithAnonymousClass()
198+
{
199+
$file = $this->getTestFile(__FILE__);
200+
201+
$expected = <<<'EOF'
202+
<?php
203+
namespace PhpCsFixer\Tests\Fixer\Basic;
204+
class Psr0FixerTest {
205+
public function foo() {
206+
return new class() implements FooInterface {};
207+
}
208+
}
209+
EOF;
210+
$input = <<<'EOF'
211+
<?php
212+
namespace PhpCsFixer\Tests\Fixer\Basic;
213+
class stdClass {
214+
public function foo() {
215+
return new class() implements FooInterface {};
216+
}
217+
}
218+
EOF;
219+
220+
$this->doTest($expected, $input, $file);
221+
}
222+
194223
/**
195224
* @requires PHP 7.0
196225
*/

0 commit comments

Comments
 (0)