Skip to content

Commit

Permalink
bug #6264 TokensAnalyzer - fix isConstantInvocation detection for mul…
Browse files Browse the repository at this point in the history
…itple exce… (SpacePossum)

This PR was squashed before being merged into the master branch (closes #6264).

Discussion
----------

TokensAnalyzer - fix isConstantInvocation detection for mulitple exce…

…ption types catch without variable

fixes #5684 (comment)

Commits
-------

eb1e3d5 TokensAnalyzer - fix isConstantInvocation detection for mulitple exce…
  • Loading branch information
SpacePossum committed Feb 5, 2022
2 parents b61622c + eb1e3d5 commit ed3f63e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
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

0 comments on commit ed3f63e

Please sign in to comment.