Skip to content

Commit 5faf967

Browse files
committed
NoWhitespaceBeforeCommaInArrayFixer - fix comma after heredoc-end (php 7.3)
1 parent e3f3404 commit 5faf967

File tree

4 files changed

+108
-5
lines changed

4 files changed

+108
-5
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,11 @@ Choose from the list of available rules:
11731173

11741174
In array declaration, there MUST NOT be a whitespace before each comma.
11751175

1176+
Configuration options:
1177+
1178+
- ``after_heredoc`` (``bool``): whether the whitespace between heredoc end and
1179+
comma should be removed; defaults to ``false``
1180+
11761181
* **no_whitespace_in_blank_line** [@Symfony, @PhpCsFixer]
11771182

11781183
Remove trailing whitespace at the end of blank lines.

src/Fixer/ArrayNotation/NoWhitespaceBeforeCommaInArrayFixer.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
namespace PhpCsFixer\Fixer\ArrayNotation;
1414

1515
use PhpCsFixer\AbstractFixer;
16+
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
17+
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
18+
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
19+
use PhpCsFixer\FixerConfiguration\InvalidOptionsForEnvException;
1620
use PhpCsFixer\FixerDefinition\CodeSample;
1721
use PhpCsFixer\FixerDefinition\FixerDefinition;
22+
use PhpCsFixer\FixerDefinition\VersionSpecification;
23+
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
1824
use PhpCsFixer\Tokenizer\CT;
1925
use PhpCsFixer\Tokenizer\Tokens;
26+
use Symfony\Component\OptionsResolver\Options;
2027

2128
/**
2229
* @author Adam Marczuk <adam@marczuk.info>
2330
*/
24-
final class NoWhitespaceBeforeCommaInArrayFixer extends AbstractFixer
31+
final class NoWhitespaceBeforeCommaInArrayFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
2532
{
2633
/**
2734
* {@inheritdoc}
@@ -30,7 +37,23 @@ public function getDefinition()
3037
{
3138
return new FixerDefinition(
3239
'In array declaration, there MUST NOT be a whitespace before each comma.',
33-
[new CodeSample("<?php \$x = array(1 , \"2\");\n")]
40+
[
41+
new CodeSample("<?php \$x = array(1 , \"2\");\n"),
42+
new VersionSpecificCodeSample(
43+
<<<'SAMPLE'
44+
<?php
45+
$x = [<<<EOD
46+
foo
47+
EOD
48+
, 'bar'
49+
];
50+
51+
SAMPLE
52+
,
53+
new VersionSpecification(70300),
54+
['after_heredoc' => true]
55+
),
56+
]
3457
);
3558
}
3659

@@ -54,6 +77,26 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
5477
}
5578
}
5679

80+
/**
81+
* {@inheritdoc}
82+
*/
83+
protected function createConfigurationDefinition()
84+
{
85+
return new FixerConfigurationResolver([
86+
(new FixerOptionBuilder('after_heredoc', 'Whether the whitespace between heredoc end and comma should be removed.'))
87+
->setAllowedTypes(['bool'])
88+
->setDefault(false)
89+
->setNormalizer(static function (Options $options, $value) {
90+
if (\PHP_VERSION_ID < 70300 && $value) {
91+
throw new InvalidOptionsForEnvException('"after_heredoc" option can only be enabled with PHP 7.3+.');
92+
}
93+
94+
return $value;
95+
})
96+
->getOption(),
97+
]);
98+
}
99+
57100
/**
58101
* Method to fix spacing in array declaration.
59102
*
@@ -74,7 +117,11 @@ private function fixSpacing($index, Tokens $tokens)
74117
$i = $this->skipNonArrayElements($i, $tokens);
75118
$currentToken = $tokens[$i];
76119
$prevIndex = $tokens->getPrevNonWhitespace($i - 1);
77-
if ($currentToken->equals(',') && !$tokens[$prevIndex]->equals([T_END_HEREDOC]) && !$tokens[$prevIndex]->isComment()) {
120+
121+
if (
122+
$currentToken->equals(',') && !$tokens[$prevIndex]->isComment() &&
123+
($this->configuration['after_heredoc'] || !$tokens[$prevIndex]->equals([T_END_HEREDOC]))
124+
) {
78125
$tokens->removeLeadingWhitespace($i);
79126
}
80127
}

tests/Fixer/ArrayNotation/NoWhitespaceBeforeCommaInArrayFixerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,39 @@ public function provideFixCases()
140140
],
141141
];
142142
}
143+
144+
/**
145+
* @param string $expected
146+
* @param null|string $input
147+
*
148+
* @dataProvider provideFix73Cases
149+
* @requires PHP 7.3
150+
*/
151+
public function testFix73($expected, $input = null, array $config = [])
152+
{
153+
$this->fixer->configure($config);
154+
$this->doTest($expected, $input);
155+
}
156+
157+
public function provideFix73Cases()
158+
{
159+
return [
160+
[
161+
"<?php \$x = array(<<<'EOF'
162+
<?php \$a = '\\foo\\bar\\\\';
163+
EOF, <<<'EOF'
164+
<?php \$a = \"\\foo\\bar\\\\\";
165+
EOF
166+
);",
167+
"<?php \$x = array(<<<'EOF'
168+
<?php \$a = '\\foo\\bar\\\\';
169+
EOF
170+
, <<<'EOF'
171+
<?php \$a = \"\\foo\\bar\\\\\";
172+
EOF
173+
);",
174+
['after_heredoc' => true],
175+
],
176+
];
177+
}
143178
}

tests/Fixtures/Integration/misc/PHP7_3.test

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PHP 7.3 test.
1414
"native_function_invocation": {"include": ["get_class"]},
1515
"no_unset_cast": true,
1616
"no_unset_on_property": true,
17+
"no_whitespace_before_comma_in_array": {"after_heredoc": true},
1718
"php_unit_dedicate_assert": true,
1819
"php_unit_expectation": true,
1920
"php_unit_mock": true,
@@ -86,12 +87,19 @@ in_array($b, $c, true, ); // `strict_param` rule
8687
foo(null === $a, ); // `yoda_style` rule
8788
$a = null; // `no_unset_cast` rule
8889
$foo->bar = null; // `no_unset_on_property` rule
89-
// `heredoc_indentation` rule
90+
91+
// https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes
9092
$a = <<<'EOD'
9193
abc
9294
def
9395
ghi
9496
EOD;
97+
$a = [<<<'EOD'
98+
foo
99+
EOD, <<<'EOD'
100+
bar
101+
EOD
102+
];
95103

96104
--INPUT--
97105
<?php
@@ -153,9 +161,17 @@ trigger_error('Warning.', E_USER_DEPRECATED, ); // `error_suppression` rule
153161
foo($a === null, ); // `yoda_style` rule
154162
$a =(unset)$z; // `no_unset_cast` rule
155163
unset($foo->bar,); // `no_unset_on_property` rule
156-
// `heredoc_indentation` rule
164+
165+
// https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes
157166
$a = <<<'EOD'
158167
abc
159168
def
160169
ghi
161170
EOD;
171+
$a = [<<<'EOD'
172+
foo
173+
EOD
174+
, <<<'EOD'
175+
bar
176+
EOD
177+
];

0 commit comments

Comments
 (0)