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

TokensAnalyzer - fix isConstantInvocation detection for mulitple exce… #6264

Merged
merged 1 commit into from Feb 5, 2022
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
5 changes: 5 additions & 0 deletions src/Tokenizer/TokensAnalyzer.php
Expand Up @@ -381,6 +381,11 @@ public function isConstantInvocation(int $index): bool
}

// check for non-capturing catches

while ($this->tokens[$prevIndex]->isGivenKind([CT::T_NAMESPACE_OPERATOR, T_NS_SEPARATOR, T_STRING, CT::T_TYPE_ALTERNATION])) {
$prevIndex = $this->tokens->getPrevMeaningfulToken($prevIndex);
}

if ($this->tokens[$prevIndex]->equals('(')) {
$prevPrevIndex = $this->tokens->getPrevMeaningfulToken($prevIndex);

Expand Down
4 changes: 2 additions & 2 deletions tests/AutoReview/FixerFactoryTest.php
Expand Up @@ -62,7 +62,7 @@ public function testFixersPriority(): void
foreach ($edges as $edge) {
$second = $fixers[$edge];

static::assertLessThan($first->getPriority(), $second->getPriority(), sprintf('"%s" should have less priority than "%s"', $fixerName, $edge));
static::assertLessThan($first->getPriority(), $second->getPriority(), sprintf('"%s" should have less priority than "%s"', $edge, $fixerName));
}
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ public function testFixersPriorityComment(): void
ksort($fixersPhpDocIssues);

foreach ($fixersPhpDocIssues as $fixerName => $issue) {
$message .= sprintf("\n--------------------------------------------------\n%s\n%s", $fixerName, $issue);
$message .= sprintf("\n--------------------------------------------------\n%s\n%s", $fixers[$fixerName]['short_classname'], $issue);
}

static::fail($message);
Expand Down
18 changes: 14 additions & 4 deletions tests/Fixer/ConstantNotation/NativeConstantInvocationFixerTest.php
Expand Up @@ -489,16 +489,26 @@ public function testFixPrePHP80(): void
}

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

public function provideFix80Cases(): iterable
{
yield [
'<?php
try {
} catch (\Exception) {
}'
);
}',
];

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

yield ['<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}'];
}
}
20 changes: 20 additions & 0 deletions tests/Tokenizer/TokensAnalyzerTest.php
Expand Up @@ -1082,6 +1082,26 @@ abstract public function test(): Foo|Bar;
[3 => false, 5 => false, 12 => false],
'<?php #[\A\Foo()] function foo() {}',
];

yield 'multiple type catch with variable' => [
[5 => false, 15 => false, 18 => false],
'<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}',
];

yield 'multiple type catch without variable 1' => [
[5 => false, 15 => false, 18 => false],
'<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}',
];

yield 'multiple type catch without variable 2' => [
[5 => false, 15 => false, 17 => false, 19 => false, 21 => false, 24 => false, 27 => false],
'<?php try { foo(); } catch(\D|Z|A\B|\InvalidArgumentException|\LogicException) {}',
];

yield 'multiple type catch without variable 3' => [
[5 => false, 14 => false, 16 => false, 19 => false, 22 => false],
'<?php try { foo(); } catch(A\B|\InvalidArgumentException|\LogicException) {}',
];
}

/**
Expand Down