From 7993f6fa5be9c0c27aad546cc039273e7e3dbe2b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 10 Apr 2018 09:19:34 +0200 Subject: [PATCH] tests - use real objects, to allow real-project type comparison --- .../src/NodeAnalyzer/DocBlockAnalyzer.php | 35 ++++++++++++++++++- .../Correct/correct.php.inc | 8 +---- .../Source/SomeChildOfValueObject.php | 8 +++++ .../Source/SomeValueObject.php | 8 +++++ .../ValueObjectRemoverRectorTest.php | 4 +-- .../Wrong/wrong.php.inc | 8 +---- .../ValueObjectRemoverRector/config.yml | 4 +-- 7 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 tests/Rector/Dynamic/ValueObjectRemoverRector/Source/SomeChildOfValueObject.php create mode 100644 tests/Rector/Dynamic/ValueObjectRemoverRector/Source/SomeValueObject.php diff --git a/packages/ReflectionDocBlock/src/NodeAnalyzer/DocBlockAnalyzer.php b/packages/ReflectionDocBlock/src/NodeAnalyzer/DocBlockAnalyzer.php index c7289d42c0c..5571462dcee 100644 --- a/packages/ReflectionDocBlock/src/NodeAnalyzer/DocBlockAnalyzer.php +++ b/packages/ReflectionDocBlock/src/NodeAnalyzer/DocBlockAnalyzer.php @@ -8,7 +8,9 @@ use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Tags\Param; use phpDocumentor\Reflection\Type; use phpDocumentor\Reflection\Types\Boolean; +use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Integer; +use phpDocumentor\Reflection\Types\Null_; use phpDocumentor\Reflection\Types\Object_; use phpDocumentor\Reflection\Types\String_; use PhpParser\Comment\Doc; @@ -66,10 +68,41 @@ final class DocBlockAnalyzer $docBlock = $this->docBlockFactory->createFromNode($node); foreach ($docBlock->getTags() as $tag) { - if ($tag instanceof TolerantVar) { // @todo: use own writeable Var + if ($tag instanceof TolerantVar) { + $oldTagType = $tag->getType(); + + // this could be abstracted to replace values + if ($oldTagType instanceof Compound) { + $newCompoundTagTypes = []; + + foreach ($oldTagType->getIterator() as $i => $oldTagSubType) { + if ($oldTagSubType instanceof Object_) { + $oldTagValue = (string) $oldTagSubType->getFqsen(); + + // is this value object to be replaced? + if (is_a($oldTagValue, $oldType, true)) { + $newCompoundTagTypes[] = $this->resolveNewTypeObjectFromString($newType); + continue; + } + } + + $newCompoundTagTypes[] = $oldTagSubType; + } + + // nothing to replace + if (! count($newCompoundTagTypes)) { + continue; + } + + // use this as new type + $newCompoundTag = new Compound($newCompoundTagTypes); + $this->setPrivatePropertyValue($tag, 'type', $newCompoundTag); + $this->saveNewDocBlockToNode($node, $docBlock); + } } } + // is this still needed? $this->replaceInNode($node, sprintf('%s|null', $oldType), sprintf('%s|null', $newType)); $this->replaceInNode($node, sprintf('null|%s', $oldType), sprintf('null|%s', $newType)); } diff --git a/tests/Rector/Dynamic/ValueObjectRemoverRector/Correct/correct.php.inc b/tests/Rector/Dynamic/ValueObjectRemoverRector/Correct/correct.php.inc index b01675c2959..562c8280a2d 100644 --- a/tests/Rector/Dynamic/ValueObjectRemoverRector/Correct/correct.php.inc +++ b/tests/Rector/Dynamic/ValueObjectRemoverRector/Correct/correct.php.inc @@ -2,13 +2,7 @@ namespace SomeNamespace; -class SomeValueObject -{ -} - -class SomeChildOfValueObject extends SomeValueObject -{ -} +use Rector\Tests\Rector\Dynamic\ValueObjectRemoverRector\Source\SomeChildOfValueObject; class ActionClass { diff --git a/tests/Rector/Dynamic/ValueObjectRemoverRector/Source/SomeChildOfValueObject.php b/tests/Rector/Dynamic/ValueObjectRemoverRector/Source/SomeChildOfValueObject.php new file mode 100644 index 00000000000..15b8d5e303f --- /dev/null +++ b/tests/Rector/Dynamic/ValueObjectRemoverRector/Source/SomeChildOfValueObject.php @@ -0,0 +1,8 @@ +