Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeFunctionInvocationFixer - PHP 8 attributes #5464

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')) {
keradus marked this conversation as resolved.
Show resolved Hide resolved
$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