From 2ab961e7340c7711078968aace9efb1d81e25fd4 Mon Sep 17 00:00:00 2001 From: Jeppe Knockaert Date: Tue, 23 Oct 2018 15:45:59 +0200 Subject: [PATCH] Prevent duplicate function declarations in docblocks When a method is declared both in the phpdoc and in the class itself, PhpStorm (2018.3) complains about duplicate function declarations, preventing correct code hinting. To solve this, I remove the already declared functions from the generated dockblocks. --- composer.json | 2 +- src/Alias.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6102959..7cb9e15 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "illuminate/support": "^5.5,<5.8", "illuminate/console": "^5.5,<5.8", "illuminate/filesystem": "^5.5,<5.8", - "barryvdh/reflection-docblock": "^2.0.4", + "barryvdh/reflection-docblock": "^2.0.5", "composer/composer": "^1.6" }, "require-dev": { diff --git a/src/Alias.php b/src/Alias.php index 80f8d2f..f14bb12 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -13,6 +13,7 @@ namespace Barryvdh\LaravelIdeHelper; use Barryvdh\Reflection\DocBlock; use Barryvdh\Reflection\DocBlock\Context; use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer; +use Barryvdh\Reflection\DocBlock\Tag\MethodTag; use ReflectionClass; class Alias @@ -395,12 +396,33 @@ class Alias $serializer = new DocBlockSerializer(1, $prefix); if ($this->phpdoc) { + $this->removeDuplicateMethodsFromPhpDoc(); return $serializer->getDocComment($this->phpdoc); } return ''; } + /** + * Removes method tags from the doc comment that already appear as functions inside the class. + * This prevents duplicate function errors in the IDE. + * + * @return void + */ + protected function removeDuplicateMethodsFromPhpDoc() + { + $methods = count($this->methods) > 0 ? $this->methods : $this->getMethods(); + $methodNames = array_map(function (Method $method) { + return $method->getName(); + }, $methods); + + foreach ($this->phpdoc->getTags() as $tag) { + if ($tag instanceof MethodTag && in_array($tag->getMethodName(), $methodNames)) { + $this->phpdoc->deleteTag($tag); + } + } + } + /** * Output an error. *