make tests pass

This commit is contained in:
TomasVotruba 2017-11-09 03:33:17 +01:00
parent 7998094641
commit 28c672312b
8 changed files with 45 additions and 112 deletions

View File

@ -5,6 +5,9 @@ checkers:
# order use statements A-Z # order use statements A-Z
- PhpCsFixer\Fixer\Import\OrderedImportsFixer - PhpCsFixer\Fixer\Import\OrderedImportsFixer
# remove leading slash ("\") at imports
- PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer
# remove extra spaces from DocBlocks # remove extra spaces from DocBlocks
- PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer - PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer

View File

@ -6,7 +6,6 @@ use PhpParser\Node;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Use_; use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use Rector\Node\Attribute; use Rector\Node\Attribute;
use Rector\NodeVisitor\Collector\NodeCollector; use Rector\NodeVisitor\Collector\NodeCollector;
use Rector\Rector\AbstractRector; use Rector\Rector\AbstractRector;
@ -19,32 +18,17 @@ final class ClassReplacerRector extends AbstractRector
*/ */
private $oldToNewClasses = []; private $oldToNewClasses = [];
/**
* @var NamespaceAnalyzer
*/
private $namespaceAnalyzer;
/**
* @var NodeCollector
*/
private $nodeCollector;
/** /**
* @param string[] $oldToNewClasses * @param string[] $oldToNewClasses
*/ */
public function __construct( public function __construct(array $oldToNewClasses)
array $oldToNewClasses, {
NamespaceAnalyzer $namespaceAnalyzer,
NodeCollector $nodeCollector
) {
$this->oldToNewClasses = $oldToNewClasses; $this->oldToNewClasses = $oldToNewClasses;
$this->namespaceAnalyzer = $namespaceAnalyzer;
$this->nodeCollector = $nodeCollector;
} }
public function isCandidate(Node $node): bool public function isCandidate(Node $node): bool
{ {
if (! $node instanceof Name && ! $node instanceof Use_) { if (! $node instanceof Name) {
return false; return false;
} }
@ -57,32 +41,16 @@ final class ClassReplacerRector extends AbstractRector
} }
/** /**
* @param Name|UseUse $node * @param Name $nameNode
*/ */
public function refactor(Node $node): ?Node public function refactor(Node $nameNode): ?Node
{ {
if ($node instanceof Name) { if ($nameNode instanceof Name) {
$newName = $this->resolveNewNameFromNode($node); $newName = $this->resolveNewNameFromNode($nameNode);
return new FullyQualified($newName); return new FullyQualified($newName);
} }
if ($node instanceof Use_) {
$newName = $this->resolveNewNameFromNode($node);
if ($this->namespaceAnalyzer->isUseStatementAlreadyPresent($node, $newName)) {
$this->nodeCollector->addNodeToRemove($node);
return null;
}
$node->uses[0]->name = new Name($newName);
$node->setAttribute(Attribute::ORIGINAL_NODE, null);
return $node;
}
return null; return null;
} }

View File

@ -23,11 +23,6 @@ final class Test extends AbstractConfigurableRectorTestCase
__DIR__ . '/wrong/wrong3.php.inc', __DIR__ . '/wrong/wrong3.php.inc',
__DIR__ . '/correct/correct3.php.inc' __DIR__ . '/correct/correct3.php.inc'
); );
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/wrong/wrong4.php.inc',
__DIR__ . '/correct/correct4.php.inc'
);
} }
protected function provideConfig(): string protected function provideConfig(): string

View File

@ -1,16 +1,20 @@
<?php declare(strict_types=1); <?php
/*
namespace SomeNamespace; * This file is part of the Sonata Project package.
*
use Bagr; * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
use NewClass; * For the full copyright and license information, please view the LICENSE
class SomeClass * file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Twig\Extension;
use Sonata\CoreBundle\FlashMessage\FlashManager;
/**
* This is the Sonata core flash message Twig extension.
*
* @author Vincent Composieux <composieux@ekino.com>
*/
class FlashMessageExtension extends \Twig\Extension\AbstractExtension
{ {
public function create()
{
$newClass = new NewClass;
return new \NewClass;
}
} }

View File

@ -10,7 +10,7 @@ use PhpParser\Builder\Method;
use PhpParser\Builder\Param; use PhpParser\Builder\Param;
use PhpParser\Builder\Property; use PhpParser\Builder\Property;
use PhpParser\Builder\Trait_; use PhpParser\Builder\Trait_;
use PhpParser\Builder; use \PhpParser\Builder;
use PhpParser\BuilderFactory; use PhpParser\BuilderFactory;
use PhpParser\Comment\Doc; use PhpParser\Comment\Doc;
use PhpParser\Node\Const_; use PhpParser\Node\Const_;

View File

@ -1,20 +0,0 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Twig\Extension;
use Sonata\CoreBundle\FlashMessage\FlashManager;
/**
* This is the Sonata core flash message Twig extension.
*
* @author Vincent Composieux <composieux@ekino.com>
*/
class FlashMessageExtension extends \Twig\Extension\AbstractExtension
{
}

View File

@ -1,17 +1,20 @@
<?php declare(strict_types=1); <?php
/*
namespace SomeNamespace; * This file is part of the Sonata Project package.
*
use Bagr; * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
use OldClass; *
use NewClass; * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
class SomeClass */
namespace Sonata\CoreBundle\Twig\Extension;
use Sonata\CoreBundle\FlashMessage\FlashManager;
/**
* This is the Sonata core flash message Twig extension.
*
* @author Vincent Composieux <composieux@ekino.com>
*/
class FlashMessageExtension extends \Twig_Extension
{ {
public function create()
{
$newClass = new NewClass;
return new OldClass;
}
} }

View File

@ -1,20 +0,0 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CoreBundle\Twig\Extension;
use Sonata\CoreBundle\FlashMessage\FlashManager;
/**
* This is the Sonata core flash message Twig extension.
*
* @author Vincent Composieux <composieux@ekino.com>
*/
class FlashMessageExtension extends \Twig_Extension
{
}