Skip to content
Permalink
Browse files

Merge pull request #150 from kubawerlos/ensure-not-changing-fixed-code

Ensure that fixed code is not changed during fixing
  • Loading branch information...
kubawerlos committed Oct 10, 2019
2 parents 00c5a25 + 347740c commit 7e23e23dd0fc402539451be12d9bb8e62cf4558f
@@ -9,6 +9,7 @@
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
@@ -104,7 +105,7 @@ private function fixNames(Tokens $tokens, int $startIndex, int $endIndex): void
continue;
}
if ($typeAnalysis->getName() !== 'iterable') {
if ($this->getTypeName($tokens, $typeAnalysis) !== 'iterable') {
/** @var int $startIndex */
$startIndex = $tokens->getNextMeaningfulToken($typeAnalysis->getStartIndex() - 1);
$tokens->clearRange($startIndex, $typeAnalysis->getEndIndex());
@@ -154,4 +155,21 @@ private function getDataProviderNames(Tokens $tokens, int $startIndex, int $endI
return $dataProviderNames;
}
/**
* TODO: remove this function after https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4581 is released.
*/
private function getTypeName(Tokens $tokens, TypeAnalysis $typeAnalysis): string
{
$type = '';
for ($index = $typeAnalysis->getStartIndex(); $index <= $typeAnalysis->getEndIndex(); $index++) {
if ($tokens[$index]->isWhitespace() || $tokens[$index]->isComment()) {
continue;
}
$type .= $tokens[$index]->getContent();
}
return $type;
}
}
@@ -134,7 +134,7 @@ public function provideThereIsNoPregFunctionUsedDirectlyCases(): iterable
foreach ($finder as $file) {
$namespace = 'PhpCsFixerCustomFixers';
if ($file->getRelativePath() !== '') {
$namespace .= '\\' . $file->getRelativePath();
$namespace .= '\\' . \str_replace('/', '\\', $file->getRelativePath());
}
yield [$namespace . '\\' . $file->getBasename('.php')];
@@ -54,7 +54,7 @@ public function provideDataProviderCases(): iterable
foreach ($finder as $file) {
$className = 'Tests';
if ($file->getRelativePath() !== '') {
$className .= '\\' . $file->getRelativePath();
$className .= '\\' . \str_replace('/', '\\', $file->getRelativePath());
}
$className .= '\\' . $file->getBasename('.php');
foreach ($this->getDataProviderMethodNames($className) as $dataProviderName) {
@@ -6,6 +6,7 @@
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\DefinedFixerInterface;
use PhpCsFixer\Linter\TokenizerLinter;
use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
use PhpCsFixer\Tokenizer\Tokens;
use PHPUnit\Framework\TestCase;
@@ -84,21 +85,33 @@ abstract class AbstractFixerTestCase extends TestCase
final protected function doTest(string $expected, ?string $input = null): void
{
if ($input === null) {
$input = $expected;
if ($expected === $input) {
throw new \InvalidArgumentException('Expected must be different to input.');
}
$tokens = Tokens::fromCode($input);
$linter = new TokenizerLinter();
static::assertNull($linter->lintSource($expected)->check());
if ($input !== null) {
static::assertNull($linter->lintSource($input)->check());
$tokens = Tokens::fromCode($input);
if ($input !== $expected) {
static::assertTrue($this->fixer->isCandidate($tokens));
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
$tokens->clearEmptyTokens();
static::assertSame($expected, $tokens->generateCode());
static::assertTokens(Tokens::fromCode($expected), $tokens);
}
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
$tokens = Tokens::fromCode($expected);
$tokens->clearEmptyTokens();
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
static::assertSame($expected, $tokens->generateCode());
static::assertTokens(Tokens::fromCode($expected), $tokens);
static::assertFalse($tokens->isChanged());
}
}

0 comments on commit 7e23e23

Please sign in to comment.
You can’t perform that action at this time.