Skip to content

Commit

Permalink
NativeFunctionInvocationFixer - PHP 8 attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC authored and keradus committed Mar 19, 2021
1 parent ea98c01 commit 54dbfc3
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
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
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
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 54dbfc3

Please sign in to comment.