Skip to content

Commit

Permalink
HeredocIndentationFixer - config option for indentation level
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Mar 20, 2020
1 parent 002d3a4 commit b03554d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 35 additions & 2 deletions src/Fixer/Whitespace/HeredocIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +27,7 @@
/**
* @author Gregor Harlan
*/
final class HeredocIndentationFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
final class HeredocIndentationFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -58,6 +61,19 @@ public function getDefinition()
,
new VersionSpecification(70300)
),
new VersionSpecificCodeSample(
<<<'SAMPLE'
<?php
$a = <<<'EOD'
abc
def
EOD;
SAMPLE
,
new VersionSpecification(70300),
['indentation' => 'same_as_start']
),
]
);
}
Expand All @@ -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) {
Expand All @@ -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];
Expand Down
49 changes: 48 additions & 1 deletion tests/Fixer/Whitespace/HeredocIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -258,6 +259,52 @@ public function provideFixCases()
INPUT
,
],
[
<<<'EXPECTED'
<?php
foo(<<<EOD
abc
def
EOD
);
EXPECTED
,
<<<'INPUT'
<?php
foo(<<<EOD
abc
def
EOD
);
INPUT
,
['indentation' => 'same_as_start'],
],
[
<<<'EXPECTED'
<?php
foo(<<<EOD
abc
def
EOD
);
EXPECTED
,
<<<'INPUT'
<?php
foo(<<<EOD
abc
def
EOD
);
INPUT
,
['indentation' => 'same_as_start'],
],
];
}

Expand Down

0 comments on commit b03554d

Please sign in to comment.