Skip to content

Commit

Permalink
feature #4012 BracesFixer - add "allow_single_line_anonymous_class_wi…
Browse files Browse the repository at this point in the history
…th_empty_body" option (kubawerlos)

This PR was merged into the 2.17-dev branch.

Discussion
----------

BracesFixer - add "allow_single_line_anonymous_class_with_empty_body" option

Resolves #3989.

Regarding #2782 - I cannot see how `ClassDefinitionFixer` is affecting braces.

Commits
-------

40ee90a BracesFixer - add "allow_single_line_anonymous_class_with_empty_body" option
  • Loading branch information
SpacePossum committed Jan 29, 2020
2 parents 3c8f12a + 40ee90a commit d77d5ac
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ Choose from the list of available rules:

Configuration options:

- ``allow_single_line_anonymous_class_with_empty_body`` (``bool``): whether single
line anonymous class with empty body notation should be allowed;
defaults to ``false``
- ``allow_single_line_closure`` (``bool``): whether single line lambda notation
should be allowed; defaults to ``false``
- ``position_after_anonymous_constructs`` (``'next'``, ``'same'``): whether the
Expand Down
21 changes: 21 additions & 0 deletions src/Fixer/Basic/BracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
protected function createConfigurationDefinition()
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('allow_single_line_anonymous_class_with_empty_body', 'Whether single line anonymous class with empty body notation should be allowed.'))
->setAllowedTypes(['bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('allow_single_line_closure', 'Whether single line lambda notation should be allowed.'))
->setAllowedTypes(['bool'])
->setDefault(false)
Expand Down Expand Up @@ -313,6 +317,23 @@ static function ($item) {
continue;
}

if (
$this->configuration['allow_single_line_anonymous_class_with_empty_body']
&& $token->isGivenKind(T_CLASS)
) {
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind(T_NEW)) {
$braceStartIndex = $tokens->getNextTokenOfKind($index, ['{']);
$braceEndIndex = $tokens->getNextMeaningfulToken($braceStartIndex);

if ('}' === $tokens[$braceEndIndex]->getContent() && !$this->isMultilined($tokens, $index, $braceEndIndex)) {
$index = $braceEndIndex;

continue;
}
}
}

if (
$this->configuration['allow_single_line_closure']
&& $token->isGivenKind(T_FUNCTION)
Expand Down
1 change: 1 addition & 0 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ final class RuleSet implements RuleSetInterface
'statements' => ['return'],
],
'braces' => [
'allow_single_line_anonymous_class_with_empty_body' => true,
'allow_single_line_closure' => true,
],
'cast_spaces' => true,
Expand Down
41 changes: 41 additions & 0 deletions tests/Fixer/Basic/BracesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4718,6 +4718,47 @@ public function sth(): string
null,
self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
],
[
'<?php
$foo = new class () extends \Exception {
};
',
'<?php
$foo = new class () extends \Exception {};
',
],
[
'<?php
$foo = new class () extends \Exception {};
',
null,
['allow_single_line_anonymous_class_with_empty_body' => true],
],
[
'<?php
$foo = new class() {}; // comment
',
null,
['allow_single_line_anonymous_class_with_empty_body' => true],
],
[
'<?php
$foo = new class() { /* comment */ }; // another comment
',
null,
['allow_single_line_anonymous_class_with_empty_body' => true],
],
[
'<?php
$foo = new class () extends \Exception {
protected $message = "Surprise";
};
',
'<?php
$foo = new class () extends \Exception { protected $message = "Surprise"; };
',
['allow_single_line_anonymous_class_with_empty_body' => true],
],
];
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Fixtures/Integration/misc/PHP7_0.test
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ class Foo implements FooInterface
}

// anonymous class
$message = (new class() {
});
$message = (new class() implements FooInterface {
});
$message = (new class() {});
$message = (new class() implements FooInterface {});
if (1) {
$message = (new class() extends Foo {
public function bar()
Expand Down

0 comments on commit d77d5ac

Please sign in to comment.