From 09a7d5e82a3a664f4aed5c392d3b7b857db11f1a Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Sun, 7 Mar 2021 08:59:55 +0100 Subject: [PATCH] Guides: Remove createSpanNode from Parser The Parser had a method createSpanNode that did nothing more than instantiate a SpanNode; this code could easily be moved to the invoking locations as this does not fall within the parser's responsibilities and only incurs dependencies. Although the dependencies are not directly solved with this change, it does clean up the interface and paves the way for more improvements. --- src/Guides/Nodes/SpanNode.php | 12 +----------- src/Guides/Nodes/TableNode.php | 2 +- .../RestructuredText/Directives/Replace.php | 3 ++- src/Guides/RestructuredText/Parser.php | 9 --------- .../RestructuredText/Parser/DocumentParser.php | 9 +++++---- .../RestructuredText/Parser/LineDataParser.php | 15 +++++++++++---- 6 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/Guides/Nodes/SpanNode.php b/src/Guides/Nodes/SpanNode.php index 6cfdd54b24..98b4204a2c 100644 --- a/src/Guides/Nodes/SpanNode.php +++ b/src/Guides/Nodes/SpanNode.php @@ -22,9 +22,6 @@ class SpanNode extends Node { - /** @var string */ - protected $value; - /** @var SpanToken[] */ protected $tokens; @@ -33,8 +30,6 @@ class SpanNode extends Node */ public function __construct(Environment $environment, $span) { - parent::__construct(); - if (is_array($span)) { $span = implode("\n", $span); } @@ -45,15 +40,10 @@ public function __construct(Environment $environment, $span) $spanProcessor = new SpanProcessor($environment, $span); - $this->value = $spanProcessor->process(); + parent::__construct($spanProcessor->process()); $this->tokens = $spanProcessor->getTokens(); } - public function getValue() : string - { - return $this->value; - } - /** * @return SpanToken[] */ diff --git a/src/Guides/Nodes/TableNode.php b/src/Guides/Nodes/TableNode.php index 9328b14fc2..d4f58e56b6 100644 --- a/src/Guides/Nodes/TableNode.php +++ b/src/Guides/Nodes/TableNode.php @@ -175,7 +175,7 @@ public function finalize(Parser $parser) : void if ($this->lineChecker->isListLine($lines[0], false)) { $node = $parser->parseFragment($col->getContent())->getNodes()[0]; } else { - $node = $parser->createSpanNode($col->getContent()); + $node = new SpanNode($parser->getEnvironment(), $col->getContent()); } $col->setNode($node); diff --git a/src/Guides/RestructuredText/Directives/Replace.php b/src/Guides/RestructuredText/Directives/Replace.php index cc0ed63a4c..6d8c32ee34 100644 --- a/src/Guides/RestructuredText/Directives/Replace.php +++ b/src/Guides/RestructuredText/Directives/Replace.php @@ -5,6 +5,7 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; use phpDocumentor\Guides\Nodes\Node; +use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\RestructuredText\Parser; /** @@ -28,6 +29,6 @@ public function processNode( string $data, array $options ) : ?Node { - return $parser->createSpanNode($data); + return new SpanNode($parser->getEnvironment(), $data); } } diff --git a/src/Guides/RestructuredText/Parser.php b/src/Guides/RestructuredText/Parser.php index bf8a9d252b..bb7bf11bd1 100644 --- a/src/Guides/RestructuredText/Parser.php +++ b/src/Guides/RestructuredText/Parser.php @@ -8,7 +8,6 @@ use phpDocumentor\Guides\Configuration; use phpDocumentor\Guides\Environment; use phpDocumentor\Guides\Nodes\DocumentNode; -use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\Parser as ParserInterface; use phpDocumentor\Guides\References\Doc; use phpDocumentor\Guides\References\Reference; @@ -143,14 +142,6 @@ public function getFilename() : string return $this->filename ?: '(unknown)'; } - /** - * @param string|string[]|SpanNode $span - */ - public function createSpanNode($span) : SpanNode - { - return new SpanNode($this->environment, $span); - } - public function parse(string $contents) : DocumentNode { $this->getEnvironment()->reset(); diff --git a/src/Guides/RestructuredText/Parser/DocumentParser.php b/src/Guides/RestructuredText/Parser/DocumentParser.php index 23b0232e44..30ac091b63 100644 --- a/src/Guides/RestructuredText/Parser/DocumentParser.php +++ b/src/Guides/RestructuredText/Parser/DocumentParser.php @@ -19,6 +19,7 @@ use phpDocumentor\Guides\Nodes\SectionBeginNode; use phpDocumentor\Guides\Nodes\SectionEndNode; use phpDocumentor\Guides\Nodes\SeparatorNode; +use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\Nodes\TableNode; use phpDocumentor\Guides\Nodes\TitleNode; use phpDocumentor\Guides\RestructuredText\Directives\Directive; @@ -412,7 +413,7 @@ private function flush() : void $token = $this->environment->createTitle($level); $node = new TitleNode( - $this->parser->createSpanNode($data), + new SpanNode($this->environment, $data), $level, $token ); @@ -495,7 +496,7 @@ private function flush() : void $buffer = $this->buffer->getLinesString(); - $node = new ParagraphNode($this->parser->createSpanNode($buffer)); + $node = new ParagraphNode(new SpanNode($this->environment, $buffer)); break; } @@ -653,7 +654,7 @@ private function parseListLine(?string $line, bool $flush = false) : bool if ($listLine !== null) { if ($this->listLine instanceof ListLine) { - $this->listLine->setText($this->parser->createSpanNode($this->listLine->getText())); + $this->listLine->setText(new SpanNode($this->environment, $this->listLine->getText())); /** @var ListNode $listNode */ $listNode = $this->nodeBuffer; @@ -677,7 +678,7 @@ private function parseListLine(?string $line, bool $flush = false) : bool if ($flush) { if ($this->listLine instanceof ListLine) { - $this->listLine->setText($this->parser->createSpanNode($this->listLine->getText())); + $this->listLine->setText(new SpanNode($this->environment, $this->listLine->getText())); /** @var ListNode $listNode */ $listNode = $this->nodeBuffer; diff --git a/src/Guides/RestructuredText/Parser/LineDataParser.php b/src/Guides/RestructuredText/Parser/LineDataParser.php index 6d93d6a9fa..8873953675 100644 --- a/src/Guides/RestructuredText/Parser/LineDataParser.php +++ b/src/Guides/RestructuredText/Parser/LineDataParser.php @@ -5,6 +5,7 @@ namespace phpDocumentor\Guides\RestructuredText\Parser; use Doctrine\Common\EventManager; +use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\RestructuredText\Event\OnLinkParsedEvent; use phpDocumentor\Guides\RestructuredText\Parser; use function array_map; @@ -172,13 +173,13 @@ public function parseDefinitionList(array $lines) : DefinitionList $classifiers = array_map( function (string $classifier) { - return $this->parser->createSpanNode($classifier); + return new SpanNode($this->parser->getEnvironment(), $classifier); }, array_map('trim', $parts) ); $definitionListTerm = [ - 'term' => $this->parser->createSpanNode($term), + 'term' => new SpanNode($this->parser->getEnvironment(), $term), 'classifiers' => $classifiers, 'definitions' => [], ]; @@ -186,7 +187,10 @@ function (string $classifier) { // last line } elseif ($definitionListTerm !== null && trim($line) === '' && count($lines) - 1 === $key) { if ($currentDefinition !== null) { - $definitionListTerm['definitions'][] = $this->parser->createSpanNode($currentDefinition); + $definitionListTerm['definitions'][] = new SpanNode( + $this->parser->getEnvironment(), + $currentDefinition + ); $currentDefinition = null; } @@ -199,7 +203,10 @@ function (string $classifier) { // empty line, start of a new definition for the current term } elseif ($currentDefinition !== null && $definitionListTerm !== null && trim($line) === '') { - $definitionListTerm['definitions'][] = $this->parser->createSpanNode($currentDefinition); + $definitionListTerm['definitions'][] = new SpanNode( + $this->parser->getEnvironment(), + $currentDefinition + ); $currentDefinition = null; }