Skip to content

Commit

Permalink
Change transformers order, fixing untransformed T_USE
Browse files Browse the repository at this point in the history
  • Loading branch information
dmvdbrugge committed Nov 13, 2018
1 parent 5d82d40 commit dd80318
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Tokenizer/Transformer/UseTransformer.php
Expand Up @@ -36,6 +36,15 @@ public function getCustomTokens()
return [CT::T_USE_TRAIT, CT::T_USE_LAMBDA];
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
// Should run after CurlyBraceTransformer
return -5;
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Tokenizer/Transformers.php
Expand Up @@ -64,8 +64,8 @@ public static function create()
*/
public function transform(Tokens $tokens)
{
foreach ($tokens as $index => $token) {
foreach ($this->items as $transformer) {
foreach ($this->items as $transformer) {
foreach ($tokens as $index => $token) {
$transformer->process($tokens, $token, $index);
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/AutoReview/ProjectCodeTest.php
Expand Up @@ -50,7 +50,6 @@ final class ProjectCodeTest extends TestCase
\PhpCsFixer\Runner\FileCachingLintingIterator::class,
\PhpCsFixer\Runner\FileLintingIterator::class,
\PhpCsFixer\Test\AccessibleObject::class,
\PhpCsFixer\Tokenizer\Transformers::class,
];

public function testThatClassesWithoutTestsVarIsProper()
Expand Down
31 changes: 31 additions & 0 deletions tests/AutoReview/TransformerTest.php
Expand Up @@ -18,6 +18,7 @@

/**
* @author SpacePossum
* @author Dave van der Brugge <dmvdbrugge@gmail.com>
*
* @internal
*
Expand Down Expand Up @@ -63,4 +64,34 @@ public function provideTransformerCases()

return $transformersArray;
}

/**
* @dataProvider provideTransformerPriorityCases()
*/
public function testTransformerPriority(TransformerInterface $first, TransformerInterface $second)
{
$this->assertLessThan(
$first->getPriority(),
$second->getPriority(),
sprintf('"%s" should have less priority than "%s"', \get_class($second), \get_class($first))
);
}

public function provideTransformerPriorityCases()
{
$transformers = [];

foreach ($this->provideTransformerCases() as list($transformer)) {
$transformers[$transformer->getName()] = $transformer;
}

return [
[$transformers['curly_brace'], $transformers['brace_class_instantiation']],
[$transformers['curly_brace'], $transformers['use']],
[$transformers['return_ref'], $transformers['type_colon']],
[$transformers['square_brace'], $transformers['brace_class_instantiation']],
[$transformers['type_colon'], $transformers['nullable_type']],
[$transformers['use'], $transformers['type_colon']],
];
}
}
62 changes: 62 additions & 0 deletions tests/Tokenizer/TransformersTest.php
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tests\Tokenizer;

use PhpCsFixer\Tests\TestCase;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;

/**
* @author Dave van der Brugge <dmvdbrugge@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformers
*/
final class TransformersTest extends TestCase
{
public function testTransformLoopOrder()
{
$source = <<<'SOURCE'
<?php
class TransformTest extends TestCase
{
public function testSomething()
{
$a = 1;
$this->assertSame('1', "{$a}");
}
use TestTrait;
public function testUsingTrait()
{
$this->testTraitFunction();
}
}
SOURCE;

$tokens = Tokens::fromCode($source);

/*
* If transform would loop over $tokens first, transformers second, both assertions fail, because when the
* 'class' token is detected in the UseTransformer, the T_CURLY_CLOSE from the string variable will not have
* been transformed by the CurlyBraceTransformer. The UseTransformer thus thinks the class ends at the end of
* the first method instead, and not transform the use.
*/
$this->assertFalse($tokens->isTokenKindFound(T_USE));
$this->assertTrue($tokens->isTokenKindFound(CT::T_USE_TRAIT));
}
}

0 comments on commit dd80318

Please sign in to comment.