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

PhpUnitDedicateAssertFixer - add assertInstanceOf support #6294

Merged
merged 1 commit into from Feb 21, 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
65 changes: 65 additions & 0 deletions src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
Expand Up @@ -24,6 +24,7 @@
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -286,6 +287,10 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments
$testIndex = $tokens->getNextMeaningfulToken($assertCall['openBraceIndex']);

if (!$tokens[$testIndex]->isGivenKind([T_EMPTY, T_STRING])) {
if ($this->fixAssertTrueFalseInstanceof($tokens, $assertCall, $testIndex)) {
return;
}

if (!$tokens[$testIndex]->isGivenKind(T_NS_SEPARATOR)) {
return;
}
Expand Down Expand Up @@ -370,6 +375,66 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments
}
}

private function fixAssertTrueFalseInstanceof(Tokens $tokens, array $assertCall, int $testIndex): bool
{
if ($tokens[$testIndex]->equals('!')) {
$variableIndex = $tokens->getNextMeaningfulToken($testIndex);
$positive = false;
} else {
$variableIndex = $testIndex;
$positive = true;
}

if (!$tokens[$variableIndex]->isGivenKind(T_VARIABLE)) {
return false;
}

$instanceOfIndex = $tokens->getNextMeaningfulToken($variableIndex);

if (!$tokens[$instanceOfIndex]->isGivenKind(T_INSTANCEOF)) {
return false;
}

$classEndIndex = $instanceOfIndex;
$classPartTokens = [];

do {
$classEndIndex = $tokens->getNextMeaningfulToken($classEndIndex);
$classPartTokens[] = $tokens[$classEndIndex];
} while ($tokens[$classEndIndex]->isGivenKind([T_STRING, T_NS_SEPARATOR, T_VARIABLE]));

if ($tokens[$classEndIndex]->equalsAny([',', ')'])) { // do the fixing
array_pop($classPartTokens);
$isInstanceOfVar = (reset($classPartTokens))->isGivenKind(T_VARIABLE);
$insertIndex = $testIndex - 1;
$newTokens = [];

foreach ($classPartTokens as $token) {
$newTokens[++$insertIndex] = clone $token;
}

if (!$isInstanceOfVar) {
$newTokens[++$insertIndex] = new Token([T_DOUBLE_COLON, '::']);
$newTokens[++$insertIndex] = new Token([CT::T_CLASS_CONSTANT, 'class']);
}

$newTokens[++$insertIndex] = new Token(',');
$newTokens[++$insertIndex] = new Token([T_WHITESPACE, ' ']);
$newTokens[++$insertIndex] = clone $tokens[$variableIndex];

for ($i = $classEndIndex - 1; $i >= $testIndex; --$i) {
if (!$tokens[$i]->isComment()) {
$tokens->clearTokenAndMergeSurroundingWhitespace($i);
}
}

$tokens->insertSlices($newTokens);
$tokens[$assertCall['index']] = new Token([T_STRING, $positive ? 'assertInstanceOf' : 'assertNotInstanceOf']);
}

return true;
}

private function fixAssertSameEquals(Tokens $tokens, array $assertCall): void
{
// @ $this->/self::assertEquals/Same([$nextIndex])
Expand Down
49 changes: 49 additions & 0 deletions tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php
Expand Up @@ -267,6 +267,55 @@ public function provideTestFixCases(): \Generator
self::generateTest('self::assertStringEndsNotWith($needle, $haystack);'),
self::generateTest('self::assertFalse(str_ends_with($haystack, $needle));'),
];

yield '$a instanceof class' => [
self::generateTest('
$this->assertInstanceOf(SomeClass::class, $x);
$this->assertInstanceOf(SomeClass::class, $y, $message);
'),
self::generateTest('
$this->assertTrue($x instanceof SomeClass);
$this->assertTrue($y instanceof SomeClass, $message);
'),
];

yield '$a instanceof class\a\b' => [
self::generateTest('
$this->assertInstanceOf(\PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest\SimpleFixer::class, $ii);
'),
self::generateTest('
$this->assertTrue($ii instanceof \PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest\SimpleFixer);
'),
];

yield '$a instanceof $b' => [
self::generateTest('
$this->assertInstanceOf($tty, $abc/* 1 *//* 2 */);
$this->assertInstanceOf($oo, $def, $message);
'),
self::generateTest('
$this->assertTrue($abc instanceof /* 1 */$tty /* 2 */);
$this->assertTrue($def instanceof $oo, $message);
'),
];

yield 'do not fix instance of' => [
self::generateTest('
$this->assertTrue($gg instanceof $ijl . "X", $something);
$this->assertTrue($ff instanceof $klh . $b(1,2,$message), $noMsg);
'),
];

yield '!$a instanceof class' => [
self::generateTest('
$this->assertNotInstanceOf(SomeClass::class, $x);
$this->assertNotInstanceOf(SomeClass::class, $y, $message);
'),
self::generateTest('
$this->assertTrue(!$x instanceof SomeClass);
$this->assertTrue(!$y instanceof SomeClass, $message);
'),
];
}

/**
Expand Down