Skip to content

Commit

Permalink
fix: Constant invocation detected in DNF types (#7869)
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkyKZ committed Mar 11, 2024
1 parent b52108e commit 18d9e42
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Tokenizer/TokensAnalyzer.php
Expand Up @@ -383,7 +383,7 @@ public function isConstantInvocation(int $index): bool
$prevIndex = $this->tokens->getPrevMeaningfulToken($prevIndex);
}

if ($this->tokens[$prevIndex]->isGivenKind([CT::T_CONST_IMPORT, T_EXTENDS, CT::T_FUNCTION_IMPORT, T_IMPLEMENTS, T_INSTANCEOF, T_INSTEADOF, T_NAMESPACE, T_NEW, CT::T_NULLABLE_TYPE, CT::T_TYPE_COLON, T_USE, CT::T_USE_TRAIT])) {
if ($this->tokens[$prevIndex]->isGivenKind([CT::T_CONST_IMPORT, T_EXTENDS, CT::T_FUNCTION_IMPORT, T_IMPLEMENTS, T_INSTANCEOF, T_INSTEADOF, T_NAMESPACE, T_NEW, CT::T_NULLABLE_TYPE, CT::T_TYPE_COLON, T_USE, CT::T_USE_TRAIT, CT::T_TYPE_INTERSECTION])) {
return false;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Fixer/ConstantNotation/NativeConstantInvocationFixerTest.php
Expand Up @@ -542,4 +542,25 @@ public static function provideFixPhp80Cases(): iterable

yield ['<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}'];
}

/**
* @dataProvider provideFixPhp82Cases
*
* @requires PHP 8.2
*/
public function testFixPhp82(string $expected): void
{
$this->fixer->configure(['strict' => true]);
$this->doTest($expected);
}

/**
* @return iterable<array{0: string}>
*/
public static function provideFixPhp82Cases(): iterable
{
yield ['<?php class Foo { public (\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var; }'];

yield ['<?php function foo ((\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var) {}'];
}
}
28 changes: 28 additions & 0 deletions tests/Tokenizer/TokensAnalyzerTest.php
Expand Up @@ -1481,6 +1481,34 @@ abstract public function foo(): Foo&Bar1&Bar2&Bar3&Bar4;
];
}

/**
* @param array<int, bool> $expected
*
* @dataProvider provideIsConstantInvocationPhp82Cases
*
* @requires PHP 8.2
*/
public function testIsConstantInvocationPhp82(array $expected, string $source): void
{
$this->doIsConstantInvocationTest($expected, $source);
}

/**
* @return iterable<array{array<int, bool>, string}>
*/
public static function provideIsConstantInvocationPhp82Cases(): iterable
{
yield [
[3 => false, 11 => false, 13 => false, 17 => false, 20 => false, 23 => false, 25 => false, 28 => false, 31 => false, 33 => false, 35 => false, 39 => false, 42 => false, 44 => false],
'<?php class Foo { public (\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var; }',
];

yield [
[3 => false, 8 => false, 10 => false, 14 => false, 17 => false, 20 => false, 22 => false, 25 => false, 28 => false, 30 => false, 32 => false, 36 => false, 39 => false, 41 => false],
'<?php function foo ((\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var) {}',
];
}

public function testIsConstantInvocationInvalid(): void
{
$this->expectException(\LogicException::class);
Expand Down

0 comments on commit 18d9e42

Please sign in to comment.