Skip to content

Commit

Permalink
Braces - (re)indenting comment issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Feb 18, 2020
1 parent a4df718 commit 02fd913
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Fixer/Basic/BracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,8 @@ private function ensureWhitespaceAtIndexAndIndentMultilineComment(Tokens $tokens
$tokens[$nextTokenIndex] = new Token([
$nextToken->getId(),
Preg::replace(
'/(\R)'.$this->detectIndent($tokens, $nextTokenIndex).'/',
'$1'.Preg::replace('/^.*\R([ \t]*)$/s', '$1', $whitespace),
'/(\R)'.$this->detectIndent($tokens, $nextTokenIndex).'(\h*\S+.*)/',
'$1'.Preg::replace('/^.*\R(\h*)$/s', '$1', $whitespace).'$2',
$nextToken->getContent()
),
]);
Expand Down
93 changes: 93 additions & 0 deletions tests/Fixer/Basic/BracesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5263,4 +5263,97 @@ function example()
}'
);
}

/**
* @param string $expected
* @param null|string $input
*
* @dataProvider provideIndentCommentCases
*/
public function testIndentComment($expected, $input, WhitespacesFixerConfig $config = null)
{
if (null !== $config) {
$this->fixer->setWhitespacesConfig($config);
}

$this->doTest($expected, $input);
}

public function provideIndentCommentCases()
{
yield [
"<?php
if (true) {
\t\$i += 2;
\treturn foo(\$i);
\t/*
\t \$i += 3;
\t // 1
"."
\t return foo(\$i);
\t */
}",
'<?php
if (true) {
$i += 2;
return foo($i);
/*
$i += 3;
// 1
'.'
return foo($i);
*/
}',
new WhitespacesFixerConfig("\t", "\n"),
];

yield [
'<?php
class MyClass extends SomeClass
{
/* public function myFunction() {
$MyItems = [];
return $MyItems;
}
*/
}',
'<?php
class MyClass extends SomeClass {
/* public function myFunction() {
$MyItems = [];
return $MyItems;
}
*/
}',
];

yield [
'<?php
if (true) {
$i += 2;
return foo($i);
/*
$i += 3;
return foo($i);
*/
}',
'<?php
if (true) {
$i += 2;
return foo($i);
/*
$i += 3;
return foo($i);
*/
}',
];
}
}

0 comments on commit 02fd913

Please sign in to comment.