Skip to content

Commit 02fd913

Browse files
committed
Braces - (re)indenting comment issues
1 parent a4df718 commit 02fd913

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

src/Fixer/Basic/BracesFixer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,8 @@ private function ensureWhitespaceAtIndexAndIndentMultilineComment(Tokens $tokens
936936
$tokens[$nextTokenIndex] = new Token([
937937
$nextToken->getId(),
938938
Preg::replace(
939-
'/(\R)'.$this->detectIndent($tokens, $nextTokenIndex).'/',
940-
'$1'.Preg::replace('/^.*\R([ \t]*)$/s', '$1', $whitespace),
939+
'/(\R)'.$this->detectIndent($tokens, $nextTokenIndex).'(\h*\S+.*)/',
940+
'$1'.Preg::replace('/^.*\R(\h*)$/s', '$1', $whitespace).'$2',
941941
$nextToken->getContent()
942942
),
943943
]);

tests/Fixer/Basic/BracesFixerTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5263,4 +5263,97 @@ function example()
52635263
}'
52645264
);
52655265
}
5266+
5267+
/**
5268+
* @param string $expected
5269+
* @param null|string $input
5270+
*
5271+
* @dataProvider provideIndentCommentCases
5272+
*/
5273+
public function testIndentComment($expected, $input, WhitespacesFixerConfig $config = null)
5274+
{
5275+
if (null !== $config) {
5276+
$this->fixer->setWhitespacesConfig($config);
5277+
}
5278+
5279+
$this->doTest($expected, $input);
5280+
}
5281+
5282+
public function provideIndentCommentCases()
5283+
{
5284+
yield [
5285+
"<?php
5286+
if (true) {
5287+
\t\$i += 2;
5288+
\treturn foo(\$i);
5289+
\t/*
5290+
\t \$i += 3;
5291+
5292+
\t // 1
5293+
"."
5294+
\t return foo(\$i);
5295+
\t */
5296+
}",
5297+
'<?php
5298+
if (true) {
5299+
$i += 2;
5300+
return foo($i);
5301+
/*
5302+
$i += 3;
5303+
5304+
// 1
5305+
'.'
5306+
return foo($i);
5307+
*/
5308+
}',
5309+
new WhitespacesFixerConfig("\t", "\n"),
5310+
];
5311+
5312+
yield [
5313+
'<?php
5314+
class MyClass extends SomeClass
5315+
{
5316+
/* public function myFunction() {
5317+
5318+
$MyItems = [];
5319+
5320+
return $MyItems;
5321+
}
5322+
*/
5323+
}',
5324+
'<?php
5325+
class MyClass extends SomeClass {
5326+
/* public function myFunction() {
5327+
5328+
$MyItems = [];
5329+
5330+
return $MyItems;
5331+
}
5332+
*/
5333+
}',
5334+
];
5335+
5336+
yield [
5337+
'<?php
5338+
if (true) {
5339+
$i += 2;
5340+
return foo($i);
5341+
/*
5342+
$i += 3;
5343+
5344+
return foo($i);
5345+
*/
5346+
}',
5347+
'<?php
5348+
if (true) {
5349+
$i += 2;
5350+
return foo($i);
5351+
/*
5352+
$i += 3;
5353+
5354+
return foo($i);
5355+
*/
5356+
}',
5357+
];
5358+
}
52665359
}

0 commit comments

Comments
 (0)