Skip to content

Commit

Permalink
bug #5855 GlobalNamespaceImportFixer - fix for attributes imported as…
Browse files Browse the repository at this point in the history
… constants (kubawerlos)

This PR was merged into the 3.0 branch.

Discussion
----------

GlobalNamespaceImportFixer - fix for attributes imported as constants

Fixes #5812

Commits
-------

21d6643 GlobalNamespaceImportFixer - fix for attributes imported as constants
  • Loading branch information
keradus committed Aug 29, 2021
2 parents 6c92093 + 21d6643 commit 12b2a96
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/Tokenizer/Analyzer/ClassyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function isClassyInvocation(Tokens $tokens, int $index): bool
return true;
}

if (AttributeAnalyzer::isAttribute($tokens, $index)) {
return true;
}

// `Foo & $bar` could be:
// - function reference parameter: function baz(Foo & $bar) {}
// - bit operator: $x = Foo & $bar;
Expand Down
6 changes: 2 additions & 4 deletions src/Tokenizer/TokensAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Tokenizer;

use PhpCsFixer\Tokenizer\Analyzer\AttributeAnalyzer;
use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer;

/**
Expand Down Expand Up @@ -357,10 +358,7 @@ public function isConstantInvocation(int $index): bool
}

// check for attribute: `#[Foo]`
if (
\defined('T_ATTRIBUTE') // @TODO: drop condition when PHP 8.0+ is required
&& $this->tokens[$prevIndex]->isGivenKind(T_ATTRIBUTE)
) {
if (AttributeAnalyzer::isAttribute($this->tokens, $index)) {
return false;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Fixer/Import/GlobalNamespaceImportFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,39 @@ public function provideMultipleNamespacesCases()
INPUT
];
}

/**
* @requires PHP 8.0
*/
public function testAttributes(): void
{
$this->fixer->configure([
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
]);
$this->doTest(
'<?php
namespace Foo;
use AnAttribute1;
use AnAttribute2;
use AnAttribute3;
class Bar
{
#[AnAttribute1]
public function f1() {}
#[AnAttribute2, AnAttribute3]
public function f2() {}
}',
'<?php
namespace Foo;
class Bar
{
#[\AnAttribute1]
public function f1() {}
#[\AnAttribute2, \AnAttribute3]
public function f2() {}
}'
);
}
}
5 changes: 5 additions & 0 deletions tests/Tokenizer/Analyzer/ClassyAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public function provideIsClassyInvocation80Cases()
'<?php function foo(): int|A|false {}',
[3 => false, 8 => false, 10 => true, 12 => false],
];

yield [
'<?php #[AnAttribute] class Foo {}',
[2 => true],
];
}

private static function assertClassyInvocation(string $source, array $expected): void
Expand Down
8 changes: 4 additions & 4 deletions tests/Tokenizer/TokensAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,13 +980,13 @@ public function provideIsConstantInvocationPhp80Cases()
];

yield [
'<?php #[Foo] function foo() {}',
[2 => false, 7 => false],
'<?php #[Foo, Bar] function foo() {}',
[2 => false, 5 => false, 10 => false],
];

yield [
'<?php #[Foo()] function foo() {}',
[2 => false, 9 => false],
'<?php #[Foo(), Bar()] function foo() {}',
[2 => false, 7 => false, 14 => false],
];
}

Expand Down

0 comments on commit 12b2a96

Please sign in to comment.