Skip to content

Commit

Permalink
bug #5464 NativeFunctionInvocationFixer - PHP 8 attributes (HypeMC, k…
Browse files Browse the repository at this point in the history
…eradus)

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

Discussion
----------

NativeFunctionInvocationFixer - PHP 8 attributes

The `NativeFunctionInvocationFixer` removes the leading `\` from attributes when strict is set to `true` which breaks the code.

Config:
```php
'native_function_invocation' => [
    'strict' => true,
],
```

Result:
```diff
-#[\Attribute(\Attribute::TARGET_CLASS)]
+#[Attribute(\Attribute::TARGET_CLASS)]
class Foo {}
```

This PR should fix the problem.

Commits
-------

54dbfc3 NativeFunctionInvocationFixer - PHP 8 attributes
  • Loading branch information
keradus committed Mar 19, 2021
2 parents c64d3f2 + 54dbfc3 commit d3aaf70
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/Tokenizer/Analyzer/FunctionsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ public function isGlobalFunctionCall(Tokens $tokens, $index)
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
}

if ($tokens[$prevIndex]->isGivenKind([T_DOUBLE_COLON, T_FUNCTION, CT::T_NAMESPACE_OPERATOR, T_NEW, T_OBJECT_OPERATOR, CT::T_RETURN_REF, T_STRING])) {
$possibleKind = [T_DOUBLE_COLON, T_FUNCTION, CT::T_NAMESPACE_OPERATOR, T_NEW, T_OBJECT_OPERATOR, CT::T_RETURN_REF, T_STRING];

// @TODO: drop condition when PHP 8.0+ is required
if (\defined('T_ATTRIBUTE')) {
$possibleKind[] = T_ATTRIBUTE;
}

if ($tokens[$prevIndex]->isGivenKind($possibleKind)) {
return false;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Fixer/FunctionNotation/NativeFunctionInvocationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,19 @@ public function testFixPrePHP80()
'
);
}

/**
* @requires PHP 8.0
*/
public function testFixWithAttributesAndStrict()
{
$this->testFixWithConfiguredInclude(
'<?php
#[\Attribute(\Attribute::TARGET_CLASS)]
class Foo {}
',
null,
['strict' => true]
);
}
}
24 changes: 17 additions & 7 deletions tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,32 +348,42 @@ public function provideIsGlobalFunctionCallPhp74Cases()
}

/**
* @param int[] $globalFunctionIndexes
* @param bool $isFunctionIndex
* @param string $code
* @param int $index
*
* @dataProvider provideIsGlobalFunctionCallPhp80Cases
* @requires PHP 8.0
*/
public function testIsGlobalFunctionCallPhp80(array $globalFunctionIndexes, $code)
public function testIsGlobalFunctionCallPhp80($isFunctionIndex, $code, $index)
{
$tokens = Tokens::fromCode($code);
$analyzer = new FunctionsAnalyzer();

foreach ($globalFunctionIndexes as $index) {
static::assertTrue($analyzer->isGlobalFunctionCall($tokens, $index));
}
static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
}

public function provideIsGlobalFunctionCallPhp80Cases()
{
yield [
[8],
true,
'<?php $a = new (foo());',
8,
];

yield [
[10],
true,
'<?php $b = $foo instanceof (foo());',
10,
];

yield [
false,
'<?php
#[\Attribute(\Attribute::TARGET_CLASS)]
class Foo {}
',
3,
];
}

Expand Down

0 comments on commit d3aaf70

Please sign in to comment.