Merge branch 'sylius-showcase' of https://github.com/EdoBarnas/rector into EdoBarnas-sylius-showcase

This commit is contained in:
Tomas Votruba 2018-04-02 12:26:22 +02:00
commit df954080f2
10 changed files with 264 additions and 0 deletions

View File

@ -4,6 +4,7 @@ namespace Rector\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
@ -42,4 +43,22 @@ final class MethodArgumentAnalyzer
return $node->args[0]->value instanceof String_;
}
public function isMethodSecondArgumentNull(Node $node): bool
{
if (! $this->hasMethodSecondArgument($node)) {
return false;
}
/** @var MethodCall $node */
$value = $node->args[1]->value;
if (! $value instanceof ConstFetch) {
return false;
}
/** @var \PhpParser\Node\Name $nodeName */
$nodeName = $value->name;
return $nodeName->toLowerString() === 'null';
}
}

View File

@ -0,0 +1,73 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\Sylius\Review;
use PhpParser\Node;
use Rector\NodeAnalyzer\MethodArgumentAnalyzer;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
/**
* @author Eduard Barnáš <barnas@neoweb.sk>
*/
final class ReplaceCreateMethodWithoutReviewerRector extends AbstractRector
{
/**
* @var MethodCallAnalyzer
*/
private $methodCallAnalyzer;
/**
* @var MethodArgumentAnalyzer
*/
private $methodArgumentAnalyzer;
/**
* @var IdentifierRenamer
*/
private $identifierRenamer;
/**
* @var string
*/
private $oldMethodName = 'createForSubjectWithReviewer';
/**
* @var string
*/
private $newMethodName = 'createForSubject';
public function __construct(
MethodCallAnalyzer $methodCallAnalyzer,
MethodArgumentAnalyzer $methodArgumentAnalyzer,
IdentifierRenamer $identifierRenamer
) {
$this->methodCallAnalyzer = $methodCallAnalyzer;
$this->methodArgumentAnalyzer = $methodArgumentAnalyzer;
$this->identifierRenamer = $identifierRenamer;
}
public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isMethod($node, $this->oldMethodName)) {
return false;
}
return ! $this->methodArgumentAnalyzer->hasMethodSecondArgument($node)
|| $this->methodArgumentAnalyzer->isMethodSecondArgumentNull($node);
}
public function refactor(Node $node): ?Node
{
$this->identifierRenamer->renameNode($node, $this->newMethodName);
if ($this->methodArgumentAnalyzer->hasMethodSecondArgument($node)) {
$node->args = [
array_shift($node->args),
];
}
return $node;
}
}

View File

@ -0,0 +1,71 @@
# source: https://github.com/Sylius/Sylius/blob/master/UPGRADE-1.0.md#upgrade-from-100-beta3-to-100
services:
Rector\Rector\Contrib\Sylius\Review\ReplaceCreateMethodWithoutReviewerRector: ~
Rector\Rector\Dynamic\MethodNameReplacerRector:
$perClassOldToNewMethods:
'Sylius\Component\Core\Repository\OrderRepositoryInterface':
'count': 'countPlacedOrders'
'countByChannel': 'countFulfilledByChannel'
'Sylius\Component\Product\Repository\ProductVariantRepositoryInterface':
'findByCodeAndProductCode': 'findByCodesAndProductCode'
'Sylius\Component\Core\Model\OrderInterface':
getLastNewPayment: 'getLastPayment'
'Sylius\Component\Taxonomy\Model\TaxonInterface':
'getParents ': 'getAncestors'
# @todo, use this and add new argument to a method
# Rector\Rector\Dynamic\ArgumentRector:
# $argumentByMethod:
# # Add state argument to the getLastPayment($state), where $state is target last payment state. getLastPayment(PaymentInterface::STATE_NEW)
# -
# type: 'add'
# class: 'Sylius\Component\Core\Model\OrderInterface'
# method: 'getLastPayment'
# position: 0 # as append | default value is 0, config argument need not be listed
# typehint: false # default value is false, config argument need not be listed
# property: '$state'
# default_value: PaymentInterface::STATE_NEW
Rector\Rector\Dynamic\ParentTypehintedArgumentRector:
$typehintForArgumentByMethodAndClass:
# Constructor of Sylius\Bundle\CoreBundle\Context\SessionAndChannelBasedCartContext has been changed to use Sylius\Component\Core\Storage\CartStorageInterface
'Sylius\Bundle\CoreBundle\Context\SessionAndChannelBasedCartContext':
'__construct':
'$cartStorage': 'Sylius\Component\Core\Storage\CartStorageInterface'
# @todo, use this and change typehint of return value
# Rector\Rector\Dynamic\ReturnTypehintRector:
# $returnTypehintByMethod:
# # OrderInterface::getAdjustmentsRecursively and OrderItemInterface::getAdjustmentsRecursively return types changed from array to Collection
# 'Sylius\Component\Order\Model\OrderInterface':
# 'getAdjustmentsRecursively':
# 'array': 'Doctrine\Common\Collections\Collection'
#
# 'Sylius\Component\Order\Model\OrderItemInterface':
# 'getAdjustmentsRecursively':
# 'array': 'Doctrine\Common\Collections\Collection'
#
# # PrioritizedServiceRegistryInterface::all method return type changed from Zend's PriorityQueue to iterable
# 'Sylius\Component\Registry\PrioritizedServiceRegistryInterface':
# 'all':
# 'Zend\Stdlib\PriorityQueue': 'iterable'
Rector\Rector\Dynamic\ClassReplacerRector:
$oldToNewClasses:
'DateTime': 'DateTimeInterface'
'Sylius\Bundle\CoreBundle\Context\SessionAndChannelBasedCartContext': 'Sylius\Component\Core\Storage\CartStorageInterface'
'Sylius\Bundle\CoreBundle\EmailManager\ShipmentEmailManager': 'Sylius\Bundle\AdminBundle\EmailManager\ShipmentEmailManager'
'Sylius\Bundle\CoreBundle\EmailManager\ShipmentEmailManagerInterface': 'Sylius\Bundle\AdminBundle\EmailManager\ShipmentEmailManagerInterface'
'Sylius\Bundle\CoreBundle\EmailManager\ContactEmailManager': 'Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManager'
'Sylius\Bundle\CoreBundle\EmailManager\ContactEmailManagerInterface': 'Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface'
'Sylius\Bundle\CoreBundle\EmailManager\OrderEmailManager': 'Sylius\Bundle\ShopBundle\EmailManager\OrderEmailManager'
'Sylius\Bundle\CoreBundle\EmailManager\OrderEmailManagerInterface': 'Sylius\Bundle\ShopBundle\EmailManager\OrderEmailManagerInterface'
'Sylius\Bundle\CoreBundle\EventListener\UserMailerListener': 'Sylius\Bundle\ShopBundle\EventListener\UserMailerListener'
'Sylius\Bundle\CoreBundle\Form\Type\ProductTaxonChoiceType': 'Sylius\Bundle\CoreBundle\Form\Type\Taxon\ProductTaxonAutocompleteChoiceType'
'Sylius\Component\Order\Factory\AddToCartCommandFactoryInterface': 'Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactoryInterface'
'Sylius\Bundle\ResourceBundle\Model\ResourceLogEntry': 'Sylius\Component\Resource\Model\ResourceLogEntry'

View File

@ -0,0 +1,11 @@
# source: https://github.com/Sylius/Sylius/blob/master/UPGRADE-1.0.md#upgrade-from-101-to-102
services:
# @todo, use this and return typehint
# https://github.com/rectorphp/rector/blob/master/docs/DynamicRectors.md#change-parameters-type-hinting-according-to-the-parent-type
Rector\Rector\Dynamic\ReturnTypehintRector:
$returnTypehintClassAndMethod:
# class
'Sylius\Bundle\AdminApiBundle\Model\ClientManager':
# method
'findClientByPublicId': '?Sylius\Bundle\AdminApiBundle\Model\ClientInterface'

View File

@ -0,0 +1,11 @@
# source: https://github.com/Sylius/Sylius/blob/master/UPGRADE-1.0.md#upgrade-from-108-to-109
services:
# @todo, use this and return typehint
# https://github.com/rectorphp/rector/blob/master/docs/DynamicRectors.md#change-parameters-type-hinting-according-to-the-parent-type
Rector\Rector\Dynamic\ReturnTypehintRector:
$returnTypehintClassAndMethod:
# class
'Sylius\Bundle\CoreBundle\Templating\Helper\VariantResolverHelper':
# method
'resolveVariant': '?Sylius\Component\Product\Model\ProductVariantInterface'

View File

@ -0,0 +1,14 @@
<?php declare (strict_types=1);
class SomeClass
{
/**
* @var Sylius\Component\Review\Factory\ReviewFactoryInterface
*/
private $reviewFactory;
public function createReview()
{
$this->reviewFactory->createForSubject($subject);
}
}

View File

@ -0,0 +1,35 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\Sylius\Review;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @covers \Rector\Rector\Contrib\Sylius\Review\ReplaceCreateMethodWithoutReviewerRector
*/
final class ReplaceCreateMethodWithoutReviewerRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideWrongToFixedFiles()
*/
public function test(string $wrong, string $fixed): void
{
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
}
/**
* @return string[][]
*/
public function provideWrongToFixedFiles(): array
{
return [
[__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'],
[__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct.php.inc'],
];
}
protected function provideConfig(): string
{
return __DIR__ . '/config.yml';
}
}

View File

@ -0,0 +1,14 @@
<?php declare (strict_types=1);
class SomeClass
{
/**
* @var Sylius\Component\Review\Factory\ReviewFactoryInterface
*/
private $reviewFactory;
public function createReview()
{
$this->reviewFactory->createForSubjectWithReviewer($subject);
}
}

View File

@ -0,0 +1,14 @@
<?php declare (strict_types=1);
class SomeClass
{
/**
* @var Sylius\Component\Review\Factory\ReviewFactoryInterface
*/
private $reviewFactory;
public function createReview()
{
$this->reviewFactory->createForSubjectWithReviewer($subject, NULL);
}
}

View File

@ -0,0 +1,2 @@
services:
Rector\Rector\Contrib\Sylius\Review\ReplaceCreateMethodWithoutReviewerRector: ~