-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
207 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Barryvdh\LaravelIdeHelper\DocBlock; | ||
|
||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\DocBlock\DescriptionFactory; | ||
use phpDocumentor\Reflection\DocBlock\StandardTagFactory; | ||
use phpDocumentor\Reflection\DocBlock\Tag; | ||
use phpDocumentor\Reflection\DocBlock\TagFactory; | ||
use phpDocumentor\Reflection\DocBlockFactory; | ||
use phpDocumentor\Reflection\TypeResolver; | ||
use phpDocumentor\Reflection\Types\Context; | ||
|
||
class DocBlockBuilder | ||
{ | ||
/** @var $tagFactory TagFactory */ | ||
private $tagFactory = null; | ||
|
||
private function __construct( | ||
protected string $summary = '', | ||
protected ?DocBlock\Description $description = null, | ||
protected array $tags = [], | ||
protected ?Context $context = null | ||
) { | ||
} | ||
|
||
public static function createFromReflector(\Reflector $reflector, ?Context $context = null): static | ||
{ | ||
if ($doc = $reflector->getDocComment()) { | ||
$docblock = DocBlockFactory::createInstance()->create($doc, $context); | ||
|
||
return new static( | ||
$docblock->getSummary(), | ||
$docblock->getDescription(), | ||
$docblock->getTags(), | ||
$docblock->getContext() | ||
); | ||
} | ||
|
||
return static::create('', null, [], $context); | ||
} | ||
|
||
public static function create( | ||
string $summary = '', | ||
?DocBlock\Description $description = null, | ||
array $tags = null, | ||
?Context $context = null | ||
): static { | ||
return new static($summary, $description, $tags, $context); | ||
} | ||
|
||
public function getDocBlock(): DocBlock | ||
{ | ||
$tags = collect($this->tags)->unique(function (Tag $tag) { | ||
if (method_exists($tag, 'getVariableName')) { | ||
return $tag->getName() . ' ' . $tag->getVariableName(); | ||
} | ||
return (string) $tag; | ||
})->toArray(); | ||
|
||
return new DocBlock($this->summary, $this->description, $tags, $this->context); | ||
} | ||
|
||
public function getSummary(): ?string | ||
{ | ||
return $this->summary; | ||
} | ||
|
||
public function setSummary(string $summary) | ||
{ | ||
$this->summary = $summary; | ||
} | ||
|
||
public function getDescription(): ?DocBlock\Description | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function setDescription(?DocBlock\Description $description) | ||
{ | ||
$this->description = $description; | ||
} | ||
|
||
public function getTags() | ||
{ | ||
return $this->tags; | ||
} | ||
|
||
public function appendTagline(string $tagline) | ||
{ | ||
$tag = $this->getTagFactory()->create($tagline, $this->context); | ||
|
||
$this->appendTag($tag); | ||
} | ||
|
||
public function hasTag(string $name): bool | ||
{ | ||
foreach ($this->tags as $tag) { | ||
if ($tag->getName() === $name) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function appendTag(Tag $tag) | ||
{ | ||
$this->tags[] = $tag; | ||
} | ||
|
||
public function removeTag(Tag $tagToRemove) | ||
{ | ||
foreach ($this->tags as $i => $tag) { | ||
if ($tag === $tagToRemove) { | ||
unset($this->tags[$i]); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public function clearTags() | ||
{ | ||
$this->tags = []; | ||
} | ||
|
||
private function getTagFactory(): TagFactory | ||
{ | ||
if ($this->tagFactory === null) { | ||
$fqsenResolver = new LocalFsqenResolver(); | ||
$tagFactory = new StandardTagFactory($fqsenResolver); | ||
$descriptionFactory = new DescriptionFactory($tagFactory); | ||
|
||
$tagFactory->addService($descriptionFactory); | ||
$tagFactory->addService(new TypeResolver($fqsenResolver)); | ||
|
||
$this->tagFactory = $tagFactory; | ||
} | ||
return $this->tagFactory; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/TypeResolver/LocalFsqenResolver.php → src/DocBlock/LocalFsqenResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.