From b03554d96985f6f4e7c20995a961a41c88c2548b Mon Sep 17 00:00:00 2001 From: Gregor Harlan Date: Mon, 9 Mar 2020 22:01:29 +0100 Subject: [PATCH] HeredocIndentationFixer - config option for indentation level --- README.rst | 6 +++ .../Whitespace/HeredocIndentationFixer.php | 37 +++++++++++++- .../HeredocIndentationFixerTest.php | 49 ++++++++++++++++++- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 26ed14a6379..6e8ab86557b 100644 --- a/README.rst +++ b/README.rst @@ -814,6 +814,12 @@ Choose from the list of available rules: Heredoc/nowdoc content must be properly indented. Requires PHP >= 7.3. + Configuration options: + + - ``indentation`` (``'same_as_start'``, ``'start_plus_one'``): whether the indentation + should be the same as in the start token line or one level more; + defaults to ``'start_plus_one'`` + * **heredoc_to_nowdoc** [@PhpCsFixer] Convert ``heredoc`` to ``nowdoc`` where possible. diff --git a/src/Fixer/Whitespace/HeredocIndentationFixer.php b/src/Fixer/Whitespace/HeredocIndentationFixer.php index 22d6bccc7a5..a23280f0450 100644 --- a/src/Fixer/Whitespace/HeredocIndentationFixer.php +++ b/src/Fixer/Whitespace/HeredocIndentationFixer.php @@ -13,7 +13,10 @@ namespace PhpCsFixer\Fixer\Whitespace; use PhpCsFixer\AbstractFixer; +use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; +use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; +use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; use PhpCsFixer\FixerDefinition\FixerDefinition; use PhpCsFixer\FixerDefinition\VersionSpecification; use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample; @@ -24,7 +27,7 @@ /** * @author Gregor Harlan */ -final class HeredocIndentationFixer extends AbstractFixer implements WhitespacesAwareFixerInterface +final class HeredocIndentationFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface { /** * {@inheritdoc} @@ -58,6 +61,19 @@ public function getDefinition() , new VersionSpecification(70300) ), + new VersionSpecificCodeSample( + <<<'SAMPLE' + 'same_as_start'] + ), ] ); } @@ -70,6 +86,19 @@ public function isCandidate(Tokens $tokens) return \PHP_VERSION_ID >= 70300 && $tokens->isTokenKindFound(T_START_HEREDOC); } + /** + * {@inheritdoc} + */ + protected function createConfigurationDefinition() + { + return new FixerConfigurationResolver([ + (new FixerOptionBuilder('indentation', 'Whether the indentation should be the same as in the start token line or one level more.')) + ->setAllowedValues(['start_plus_one', 'same_as_start']) + ->setDefault('start_plus_one') + ->getOption(), + ]); + } + protected function applyFix(\SplFileInfo $file, Tokens $tokens) { for ($index = \count($tokens) - 1; 0 <= $index; --$index) { @@ -90,7 +119,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) */ private function fixIndentation(Tokens $tokens, $start, $end) { - $indent = $this->getIndentAt($tokens, $start).$this->whitespacesConfig->getIndent(); + $indent = $this->getIndentAt($tokens, $start); + + if ('start_plus_one' === $this->configuration['indentation']) { + $indent .= $this->whitespacesConfig->getIndent(); + } Preg::match('/^\h*/', $tokens[$end]->getContent(), $matches); $currentIndent = $matches[0]; diff --git a/tests/Fixer/Whitespace/HeredocIndentationFixerTest.php b/tests/Fixer/Whitespace/HeredocIndentationFixerTest.php index 183d86a284b..be36a7978ed 100644 --- a/tests/Fixer/Whitespace/HeredocIndentationFixerTest.php +++ b/tests/Fixer/Whitespace/HeredocIndentationFixerTest.php @@ -48,8 +48,9 @@ public function testDoNotFix() * @dataProvider provideFixCases * @requires PHP 7.3 */ - public function testFix($expected, $input = null) + public function testFix($expected, $input = null, array $config = []) { + $this->fixer->configure($config); $this->doTest($expected, $input); } @@ -258,6 +259,52 @@ public function provideFixCases() INPUT , ], + [ + <<<'EXPECTED' + 'same_as_start'], + ], + [ + <<<'EXPECTED' + 'same_as_start'], + ], ]; }