-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #13. Implemented Ely\multiline_if_statement_braces fixer
- Loading branch information
1 parent
e9ad4a9
commit 7d36ce9
Showing
7 changed files
with
195 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Ely\CS\Fixer\Whitespace; | ||
|
||
use Ely\CS\Fixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\ConfigurableFixerInterface; | ||
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface; | ||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Tokenizer\Analyzer\WhitespacesAnalyzer; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
use PhpCsFixer\Tokenizer\TokensAnalyzer; | ||
use SplFileInfo; | ||
|
||
final class MultilineIfStatementBracesFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface { | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public const C_KEEP_ON_OWN_LINE = 'keep_on_own_line'; | ||
|
||
public function getDefinition(): FixerDefinitionInterface { | ||
return new FixerDefinition( | ||
'Ensures that multiline if statement body curly brace placed on the right line.', | ||
[ | ||
new CodeSample( | ||
'<?php | ||
if ($condition1 == true | ||
&& $condition2 === false) {} | ||
', | ||
), | ||
new CodeSample( | ||
'<?php | ||
if ($condition1 == true | ||
&& $condition2 === false | ||
) {} | ||
', | ||
[self::C_KEEP_ON_OWN_LINE => false], | ||
), | ||
], | ||
); | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool { | ||
return $tokens->isTokenKindFound(T_IF); | ||
} | ||
|
||
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface { | ||
return new FixerConfigurationResolver([ | ||
(new FixerOptionBuilder(self::C_KEEP_ON_OWN_LINE, 'adjusts the position of condition closing brace.')) | ||
->setAllowedTypes(['bool']) | ||
->setDefault(true) | ||
->getOption(), | ||
]); | ||
} | ||
|
||
protected function applyFix(SplFileInfo $file, Tokens $tokens): void { | ||
$keepOnOwnLine = $this->configuration[self::C_KEEP_ON_OWN_LINE]; | ||
$tokensAnalyzer = new TokensAnalyzer($tokens); | ||
$eol = $this->whitespacesConfig->getLineEnding(); | ||
foreach ($tokens as $i => $token) { | ||
if (!$token->isGivenKind(T_IF)) { | ||
continue; | ||
} | ||
|
||
$openBraceIndex = $tokens->getNextTokenOfKind($i, ['(']); | ||
if (!$tokensAnalyzer->isBlockMultiline($tokens, $openBraceIndex)) { | ||
continue; | ||
} | ||
|
||
$closingBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openBraceIndex); | ||
/** @var \PhpCsFixer\Tokenizer\Token $statementBeforeClosingBrace */ | ||
$statementBeforeClosingBrace = $tokens[$closingBraceIndex - 1]; | ||
if ($keepOnOwnLine) { | ||
if (!$statementBeforeClosingBrace->isWhitespace() | ||
|| !str_contains($statementBeforeClosingBrace->getContent(), $eol) | ||
) { | ||
$indent = WhitespacesAnalyzer::detectIndent($tokens, $i); | ||
$tokens->ensureWhitespaceAtIndex($closingBraceIndex, 0, $eol . $indent); | ||
} | ||
} else { | ||
$tokens->removeLeadingWhitespace($closingBraceIndex); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
tests/Fixer/Whitespace/MultilineIfStatementBracesFixerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Ely\CS\Test\Fixer\Whitespace; | ||
|
||
use Ely\CS\Fixer\Whitespace\MultilineIfStatementBracesFixer; | ||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Tests\Test\AbstractFixerTestCase; | ||
|
||
/** | ||
* @covers \Ely\CS\Fixer\Whitespace\MultilineIfStatementBracesFixer | ||
*/ | ||
class MultilineIfStatementBracesFixerTest extends AbstractFixerTestCase { | ||
|
||
/** | ||
* @dataProvider provideFixCases | ||
*/ | ||
public function testFixOnNewLine(string $expected, ?string $input = null): void { | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
public function provideFixCases(): iterable { | ||
yield 'simple' => [ | ||
'<?php | ||
if ($condition1 | ||
&& $condition2 | ||
) {}', | ||
'<?php | ||
if ($condition1 | ||
&& $condition2) {}', | ||
]; | ||
|
||
yield 'nested' => [ | ||
'<?php | ||
function foo() { | ||
if ($condition1 | ||
&& $condition2 | ||
) {} | ||
}', | ||
'<?php | ||
function foo() { | ||
if ($condition1 | ||
&& $condition2) {} | ||
}', | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideInvertedFixCases | ||
*/ | ||
public function testFixOnSameLine(string $expected, ?string $input = null): void { | ||
$this->fixer->configure([ | ||
MultilineIfStatementBracesFixer::C_KEEP_ON_OWN_LINE => false, | ||
]); | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
public function provideInvertedFixCases(): iterable { | ||
foreach ($this->provideFixCases() as $name => $case) { | ||
yield $name => [$case[1], $case[0]]; | ||
} | ||
} | ||
|
||
protected function createFixer(): AbstractFixer { | ||
return new MultilineIfStatementBracesFixer(); | ||
} | ||
|
||
} |