add RemovedAndAddedFilesTrait, improve EventClassNaming

This commit is contained in:
TomasVotruba 2020-05-29 11:31:31 +02:00
parent 0b59ea3035
commit 9e4558e97d
7 changed files with 92 additions and 17 deletions

View File

@ -6,12 +6,12 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use Rector\Core\Exception\NotImplementedException;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
@ -38,7 +38,7 @@ final class StaticTypeMapper implements TypeMapperInterface
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
throw new NotImplementedException();
return new ThisTypeNode();
}
/**

View File

@ -44,7 +44,7 @@ final class EventClassNaming
return $this->prependShortClassEventWithNamespace($shortEventClassName, $className);
}
public function resolveEventFileLocation(MethodCall $methodCall): string
public function resolveEventFileLocationFromMethodCall(MethodCall $methodCall): string
{
$shortEventClassName = $this->getShortEventClassName($methodCall);
@ -54,6 +54,15 @@ final class EventClassNaming
return $fileInfo->getPath() . DIRECTORY_SEPARATOR . 'Event' . DIRECTORY_SEPARATOR . $shortEventClassName . '.php';
}
public function resolveEventFileLocationFromClassNameAndFileInfo(
string $className,
SmartFileInfo $smartFileInfo
): string {
$shortClassName = $this->classNaming->getShortName($className);
return $smartFileInfo->getPath() . DIRECTORY_SEPARATOR . 'Event' . DIRECTORY_SEPARATOR . $shortClassName . '.php';
}
public function createEventClassNameFromClassAndProperty(string $className, string $methodName): string
{
$shortEventClass = $this->createShortEventClassNameFromClassAndProperty($className, $methodName);

View File

@ -11,10 +11,14 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NetteKdyby\Naming\EventClassNaming;
use Rector\NetteKdyby\NodeFactory\CustomEventFactory;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @see \Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceEventManagerWithEventSubscriberRector\ReplaceEventManagerWithEventSubscriberRectorTest
@ -26,9 +30,15 @@ final class ReplaceEventManagerWithEventSubscriberRector extends AbstractRector
*/
private $eventClassNaming;
public function __construct(EventClassNaming $eventClassNaming)
/**
* @var CustomEventFactory
*/
private $customEventFactory;
public function __construct(EventClassNaming $eventClassNaming, CustomEventFactory $customEventFactory)
{
$this->eventClassNaming = $eventClassNaming;
$this->customEventFactory = $customEventFactory;
}
public function getDefinition(): RectorDefinition
@ -114,7 +124,9 @@ PHP
$eventReference = $oldArgs[0]->value;
$classAndStaticProperty = $this->getValue($eventReference, true);
$eventCass = $this->eventClassNaming->createEventClassNameFromClassPropertyReference($classAndStaticProperty);
$eventClassName = $this->eventClassNaming->createEventClassNameFromClassPropertyReference(
$classAndStaticProperty
);
$args = [];
if ($oldArgs[1]->value instanceof New_) {
@ -129,9 +141,25 @@ PHP
}
}
$class = new New_(new FullyQualified($eventCass), $args);
$class = new New_(new FullyQualified($eventClassName), $args);
$node->args[] = new Arg($class);
// 3. create new event class with args
$eventClassInNamespace = $this->customEventFactory->create($eventClassName, $args);
/** @var SmartFileInfo|null $fileInfo */
$fileInfo = $node->getAttribute(AttributeKey::FILE_INFO);
if ($fileInfo === null) {
throw new ShouldNotHappenException();
}
$eventFileLocation = $this->eventClassNaming->resolveEventFileLocationFromClassNameAndFileInfo(
$eventClassName,
$fileInfo
);
$this->printNodesToFilePath($eventClassInNamespace, $eventFileLocation);
return $node;
}
}

View File

@ -120,7 +120,7 @@ PHP
// 2. guess event name
$eventClassName = $this->eventClassNaming->createEventClassNameFromMethodCall($node);
$eventFileLocation = $this->eventClassNaming->resolveEventFileLocation($node);
$eventFileLocation = $this->eventClassNaming->resolveEventFileLocationFromMethodCall($node);
// 3. create new event class with args
$eventClass = $this->customEventFactory->create($eventClassName, (array) $node->args);

View File

@ -4,23 +4,19 @@ declare(strict_types=1);
namespace Rector\NetteKdyby\Tests\Rector\MethodCall\ReplaceEventManagerWithEventSubscriberRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\NetteKdyby\Rector\MethodCall\ReplaceEventManagerWithEventSubscriberRector;
final class ReplaceEventManagerWithEventSubscriberRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
public function test(): void
{
$this->doTestFile($file);
}
$this->doTestFile(__DIR__ . '/Fixture/fixture.php.inc');
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
$expectedEventFilePath = dirname($this->originalTempFile) . '/Event/SomeClassCopyEvent.php';
$this->assertFileExists($expectedEventFilePath);
$this->assertFileEquals(__DIR__ . '/Source/ExpectedSomeClassCopyEvent.php', $expectedEventFilePath);
}
protected function getRectorClass(): string

View File

@ -14,6 +14,7 @@ use Rector\Reporting\Rector\AbstractRector\NodeReportCollectorTrait;
trait AbstractRectorTrait
{
use RemovedAndAddedFilesTrait;
use DoctrineTrait;
use NodeTypeResolverTrait;
use NameResolverTrait;

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Rector\AbstractRector;
use PhpParser\Node;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
/**
* This could be part of @see AbstractRector, but decopuling to trait
* makes clear what code has 1 purpose.
*
* @property BetterStandardPrinter $betterStandardPrinter
*/
trait RemovedAndAddedFilesTrait
{
/**
* @var RemovedAndAddedFilesCollector
*/
private $removedAndAddedFilesCollector;
/**
* @required
*/
public function autowireRemovedAndAddedFilesTrait(
RemovedAndAddedFilesCollector $removedAndAddedFilesCollector
): void {
$this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector;
}
/**
* @param Node|Node[]|null $node
*/
protected function printNodesToFilePath($node, string $fileLocation): void
{
$eventContent = $this->betterStandardPrinter->prettyPrintFile($node);
$this->removedAndAddedFilesCollector->addFileWithContent($fileLocation, $eventContent);
}
}