Skip to content

Commit 16ce122

Browse files
committed
Fixed bug #2624 : PSR12.Traits.UseDeclaration doesnt apply the correct indent during auto fixing
1 parent 8febaac commit 16ce122

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3030
- Fixed bug #2619 : PSR12.Files.FileHeader locks up when inline comment is the last content in a file
3131
- Fixed bug #2621 : PSR12.Classes.AnonClassDeclaration.CloseBraceSameLine false positive for anon class passed as function argument
3232
-- Thanks to Martins Sipenko for the patch
33+
- Fixed bug #2624 : PSR12.Traits.UseDeclaration doesnt apply the correct indent during auto fixing
3334
</notes>
3435
<contents>
3536
<dir name="/">

src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,18 @@ public function process(File $phpcsFile, $stackPtr)
120120
$phpcsFile->fixer->replaceToken($x, '');
121121
}
122122

123-
$padding = str_repeat(' ', ($tokens[$firstUse]['column'] - 1));
124123
$phpcsFile->fixer->addNewline($prev);
125-
$phpcsFile->fixer->addContent($prev, $padding);
124+
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
125+
if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
126+
$phpcsFile->fixer->replaceToken(($stackPtr - 1), '');
127+
}
128+
129+
$padding = str_repeat(' ', ($tokens[$firstUse]['column'] - 1));
130+
$phpcsFile->fixer->addContent($prev, $padding);
131+
}
132+
126133
$phpcsFile->fixer->endChangeset();
127-
}
134+
}//end if
128135
}//end if
129136
}//end if
130137
}//end if

src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ class ClassName9
7676
as FooBar;
7777
}
7878
}
79+
80+
class ClassName10
81+
{
82+
use TransactionTrait;
83+
84+
use PermissionAwareTrait;
85+
86+
use FirstTrait; use SecondTrait; use ThirdTrait;
87+
}

src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@ class ClassName9
7373
as FooBar;
7474
}
7575
}
76+
77+
class ClassName10
78+
{
79+
use TransactionTrait;
80+
use PermissionAwareTrait;
81+
use FirstTrait;
82+
use SecondTrait;
83+
use ThirdTrait;
84+
}

src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function getErrorList()
3838
71 => 1,
3939
73 => 2,
4040
76 => 1,
41+
84 => 1,
42+
86 => 3,
4143
];
4244

4345
}//end getErrorList()

0 commit comments

Comments
 (0)