Skip to content

Commit

Permalink
minor #5827 ClassAttributesSeparationFixer - Add trait_import suppo…
Browse files Browse the repository at this point in the history
…rt (SpacePossum)

This PR was squashed before being merged into the 2.19 branch.

Discussion
----------

ClassAttributesSeparationFixer - Add `trait_import` support

closes #5490

Commits
-------

51f8116 ClassAttributesSeparationFixer - Add `trait_import` support
  • Loading branch information
keradus committed Aug 2, 2021
2 parents 0552e59 + 51f8116 commit 4eb5227
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions doc/rules/class_notation/class_attributes_separation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Configuration
``elements``
~~~~~~~~~~~~

Dictionary of ``const|method|property`` => ``none|one`` values.
Dictionary of ``const|method|property|trait_import`` => ``none|one`` values.

Allowed types: ``array``

Default value: ``['const' => 'one', 'method' => 'one', 'property' => 'one']``
Default value: ``['const' => 'one', 'method' => 'one', 'property' => 'one', 'trait_import' => 'one']``

Examples
--------
Expand Down
18 changes: 10 additions & 8 deletions src/Fixer/ClassNotation/ClassAttributesSeparationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class ClassAttributesSeparationFixer extends AbstractFixer implements Conf
/**
* @internal
*/
const SUPPORTED_TYPES = ['const', 'method', 'property'];
const SUPPORTED_TYPES = ['const', 'method', 'property', 'trait_import'];

/**
* @var array<string, string>
Expand All @@ -70,6 +70,7 @@ public function configure(array $configuration = null)
parent::configure($configuration);

$this->classElementTypes = []; // reset previous configuration

foreach ($this->configuration['elements'] as $elementType => $spacing) {
$this->classElementTypes[$elementType] = $spacing;
}
Expand Down Expand Up @@ -150,7 +151,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
$tokensAnalyzer = new TokensAnalyzer($tokens);
$class = $classStart = $classEnd = false;

foreach (array_reverse($tokensAnalyzer->getClassyElements(), true) as $index => $element) {
foreach (array_reverse($tokensAnalyzer->getClassyElements(true), true) as $index => $element) {
if (!isset($this->classElementTypes[$element['type']])) {
continue; // not configured to be fixed
}
Expand Down Expand Up @@ -190,7 +191,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
protected function createConfigurationDefinition()
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('elements', 'Dictionary of `const|method|property` => `none|one` values.'))
(new FixerOptionBuilder('elements', 'Dictionary of `const|method|property|trait_import` => `none|one` values.'))
->setNormalizer(static function (Options $options, $values) {
$deprecated = array_intersect($values, self::SUPPORTED_TYPES);
if (\count($deprecated) > 0) {
Expand Down Expand Up @@ -239,6 +240,7 @@ protected function createConfigurationDefinition()
'const' => self::SPACING_ONE,
'method' => self::SPACING_ONE,
'property' => self::SPACING_ONE,
'trait_import' => self::SPACING_ONE,
])
->getOption(),
]);
Expand Down Expand Up @@ -316,7 +318,7 @@ private function fixSpaceAboveClassElement(Tokens $tokens, $classStartIndex, $el
$firstElementAttributeIndex = $elementIndex;

for ($i = $elementIndex; $i > $classStartIndex; --$i) {
$nonWhiteAbove = $tokens->getNonWhitespaceSibling($i, -1);
$nonWhiteAbove = $tokens->getPrevNonWhitespace($i);

if (null !== $nonWhiteAbove && $tokens[$nonWhiteAbove]->isGivenKind($methodAttr)) {
$firstElementAttributeIndex = $nonWhiteAbove;
Expand All @@ -325,7 +327,7 @@ private function fixSpaceAboveClassElement(Tokens $tokens, $classStartIndex, $el
}
}

// deal with comments above a element
// deal with comments above an element
if ($tokens[$nonWhiteAbove]->isGivenKind(T_COMMENT)) {
if (1 === $firstElementAttributeIndex - $nonWhiteAbove) {
// no white space found between comment and element start
Expand All @@ -349,7 +351,7 @@ private function fixSpaceAboveClassElement(Tokens $tokens, $classStartIndex, $el
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstElementAttributeIndex, 1);
// ... and make sure there is blank line above the comment (with the exception when it is directly after a class opening)
$nonWhiteAbove = $this->findCommentBlockStart($tokens, $nonWhiteAbove);
$nonWhiteAboveComment = $tokens->getNonWhitespaceSibling($nonWhiteAbove, -1);
$nonWhiteAboveComment = $tokens->getPrevNonWhitespace($nonWhiteAbove);

$this->correctLineBreaks($tokens, $nonWhiteAboveComment, $nonWhiteAbove, $nonWhiteAboveComment === $classStartIndex ? 1 : 2);
} else {
Expand All @@ -367,7 +369,7 @@ private function fixSpaceAboveClassElement(Tokens $tokens, $classStartIndex, $el
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstElementAttributeIndex, 1);

// there should be one blank line between the PHPDoc and whatever is above (with the exception when it is directly after a class opening)
$nonWhiteAbovePHPDoc = $tokens->getNonWhitespaceSibling($nonWhiteAbove, -1);
$nonWhiteAbovePHPDoc = $tokens->getPrevNonWhitespace($nonWhiteAbove);
$this->correctLineBreaks($tokens, $nonWhiteAbovePHPDoc, $nonWhiteAbove, $nonWhiteAbovePHPDoc === $classStartIndex ? 1 : 2);

return;
Expand All @@ -380,7 +382,7 @@ private function fixSpaceAboveClassElement(Tokens $tokens, $classStartIndex, $el

// make sure there is blank line above the comment (with the exception when it is directly after a class opening)
$nonWhiteAbove = $this->findAttributeBlockStart($tokens, $nonWhiteAbove);
$nonWhiteAboveComment = $tokens->getNonWhitespaceSibling($nonWhiteAbove, -1);
$nonWhiteAboveComment = $tokens->getPrevNonWhitespace($nonWhiteAbove);

$this->correctLineBreaks($tokens, $nonWhiteAboveComment, $nonWhiteAbove, $nonWhiteAboveComment === $classStartIndex ? 1 : 2);

Expand Down
35 changes: 35 additions & 0 deletions tests/Fixer/ClassNotation/ClassAttributesSeparationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1530,4 +1530,39 @@ class Foo {
}',
];
}

/**
* @param string $expected
* @param null|string $input
*
* @dataProvider provideFixClassesWithTraitsCases
*/
public function testFixClassesWithTraits($expected, $input)
{
$this->doTest($expected, $input);
}

public function provideFixClassesWithTraitsCases()
{
yield [
'<?php
class Foo
{
use SomeTrait1;
use SomeTrait2;
public function Bar(){}
}
',
'<?php
class Foo
{
use SomeTrait1;
use SomeTrait2;
public function Bar(){}
}
',
];
}
}

0 comments on commit 4eb5227

Please sign in to comment.