This commit is contained in:
TomasVotruba 2017-09-30 16:45:30 +02:00
parent 1adea347b3
commit 74bb999f49
2 changed files with 59 additions and 31 deletions

View File

@ -17,15 +17,15 @@ final class DocBlockAnalyzer
return (bool) $docBlock->getAnnotationsOfType($annotation);
}
public function removeAnnotationFromNode(Node $node, string $annotationName, string $annotatoinContent = ''): void
public function removeAnnotationFromNode(Node $node, string $annotationName, string $annotationContent = ''): void
{
$docBlock = $this->createDocBlockFromNode($node);
$annotations = $docBlock->getAnnotationsOfType($annotationName);
foreach ($annotations as $annotation) {
if ($annotatoinContent) {
if (Strings::contains($annotation->getContent(), $annotatoinContent)) {
if ($annotationContent) {
if (Strings::contains($annotation->getContent(), $annotationContent)) {
$annotation->remove();
}
} else {
@ -33,23 +33,9 @@ final class DocBlockAnalyzer
}
}
$docContent = $docBlock->getContent();
if (strlen($docBlock->getContent()) <= 7) {
$docContent = '';
}
$this->saveNewDocBlockToNode($node, $docBlock);
$doc = new Doc($docContent);
$node->setDocComment($doc);
/** @var Node $parentNode */
$parentNode = $node->getAttribute('parentNode');
if ($parentNode) {
$parentNode->setAttribute('origNode', null);
}
// ref: https://github.com/nikic/PHP-Parser/issues/420#issuecomment-333301992
$node->setAttribute('parentNode', null);
$node->setAttribute('origNode', null);
$this->nullNode($node);
}
public function getAnnotationFromNode(Node $node, string $annotation): string
@ -98,4 +84,28 @@ final class DocBlockAnalyzer
{
return new DocBlock($node->getDocComment());
}
private function nullNode(Node $node): void
{
/** @var Node $parentNode */
$parentNode = $node->getAttribute('parentNode');
if ($parentNode) {
$parentNode->setAttribute('origNode', null);
}
$node->setAttribute('parentNode', null);
$node->setAttribute('origNode', null);
}
private function saveNewDocBlockToNode(Node $node, DocBlock $docBlock): void
{
$docContent = $docBlock->getContent();
if (strlen($docBlock->getContent()) <= 7) {
$docContent = '';
}
$doc = new Doc($docContent);
$node->setDocComment($doc);
}
}

View File

@ -5,6 +5,7 @@ namespace Rector\Rector\Contrib\Nette\Utils;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\PropertyReflection;
use Rector\BetterReflection\Reflector\CurrentFileAwareClassReflector;
use Rector\Builder\MethodBuilder;
use Rector\Node\Attribute;
@ -157,18 +158,7 @@ final class MagicMethodRector extends AbstractRector
$propertyReflection = $this->classReflection->getProperty($prop);
if ($propertyReflection && ! $propertyReflection->isStatic()) {
if ($op === 'get' || $op === 'is') {
$type = null;
$op = 'get';
} elseif (! $type
&& preg_match('#@var[ \t]+(\S+)' . ($op === 'add' ? '\[\]#' : '#'), $propertyReflection->getDocComment(), $match)
) {
$type = $match[1];
}
if ($type && $currentNamespace && preg_match('#^[A-Z]\w+(\[|\||\z)#', $type)) {
$type = $currentNamespace . '\\' . $type;
}
$type = $this->resolveType($currentNamespace, $op, $type, $propertyReflection, $match);
$methods[$name] = [
'propertyType' => $type,
@ -179,4 +169,32 @@ final class MagicMethodRector extends AbstractRector
return $methods;
}
/**
* @param mixed[] $match
*/
private function resolveType(
string $currentNamespace,
string $op,
string $type,
PropertyReflection $propertyReflection,
array $match
): string {
if ($op === 'get' || $op === 'is') {
$type = null;
$op = 'get';
} elseif (! $type && preg_match(
'#@var[ \t]+(\S+)' . ($op === 'add' ? '\[\]#' : '#'),
$propertyReflection->getDocComment(),
$match
)) {
$type = $match[1];
}
if ($type && $currentNamespace && preg_match('#^[A-Z]\w+(\[|\||\z)#', $type)) {
$type = $currentNamespace . '\\' . $type;
}
return $type;
}
}